قالب وردپرس درنا توس
Home / Tips and Tricks / Using the chown command on Linux

Using the chown command on Linux



  A terminal window on a Linux system
Fatmawati Achmad Zaenuri / Shutterstock.com

All files and directories in Linux systems belong to someone. You can change their ownership with the command chown . We show you how.

Each file belongs to a user and a group.

Linux is a multi-user system. The operating system allows you to define multiple user accounts and to log in a valid user on the computer. In addition, multiple users can simultaneously use a computer.

Linux uses the concept of ownership to log which files belong to which user and to increase security. Each file belongs to one owner ̵

1; a user – and a group.

When a file is created, its owner is the user who created it. The group to which the file belongs – the "owning" group – is the user's current group. Users and groups have names as well as numeric identities called user (or unique) ID (UID) and group ID (GID).

When you create a file, it belongs to you and belongs to your current group. Usually this is the group you signed up with. By default, this is a group that has the same name as your username and was created when you were created as a user in the system.

With the command chown you can change the owner values ​​to something else. You can specify a new owner, a new group or a new owner and a new group at the same time. The owner of a file can change group ownership, but only root can change user ownership because it affects another user. Without root privileges, you can not make another user on the system unintentionally "take over" a file.

Why should you change the ownership?

Here are some examples of situations in which you might want to do this:

  • When transferring files between different Linux or Unix-like operating systems, you must transfer the user and group owners to the new user and group owners of the account change under which you want to use the files on the new Linux computer.
  • A user can leave your organization and all his files are the responsibility of another employee. You must change the owner and the group owner to the employee who is now responsible for these files.
  • You can create a script that is used by a specific user.
  • You can create a file or a logged directory You must be logged in as root, but you want a specific user to have access to it.

Viewing Your Groups, UID, and GID

You can use the groups command to list the groups in which you are located.

  Groups 

  Groups in a Terminal Window

For a list of groups, their numeric IDs, and their UID and GID, use the command id : [19659018] ID

  ID in a terminal window

You can use some options with ID to refine the output.

  • -u : Enter your UID.
  • -g : Enter your effective (current) GID.
  • -nu : Enter your username.
  • -ng : Enter your current group name. [19659028] id -u
      id -g 
      id -nu 
      id -ng 

      Issuing the id Command in a Terminal Window

    Viewing the User and Group Ownership of a File [19659005] To view the owner of a file or directory, use the option -l (long listing) with ls .

      ls -l 
       ls -l in a terminal window 

    We can see that name dave appears twice in the collection. The leftmost appearance indicates that the file owner is a user named dave . The rightmost Dave tells us that the file belongs to a group also known as Dave .

    By default, they are added to a private user when a Linux user is created. Group named after their user name. You are the only member of this group.

    This executable belongs to the user mary and the group to which the file belongs is the private group of mary.

      ls - l 

      ls -l in a terminal window

    This file belongs to user oscar but the group to which the file belongs is called researchlab . This means that other members of the researchlab group can access this file according to the file permissions set for members of this group.

      <img class = "alignnone size-full wp-image-438644" src = "https://www.howtogeek.com/wp-content/uploads/2019/08/x6-3.png.pagespeed.gp + jp + jw + pj + ws + js + rj + rp + rw + ri + cp + md.ic.PjqginMs8C.png "alt =" ls -l in a terminal window [19659044] Changing user ownership 

    Let us Here are a few examples: This command changes the user ownership of the while.c file to the user mary .

      sudo chown mary while.c 

      sudo chown mary while.c in a terminal window [19659045] <code> ls </code> lets you see the changes to the file properties. </p>
<pre>  ls -l while.c </pre>
<p><img class=

    You can Use chown to change ownership of multiple files simultaneously.

      sudo chown mary getval.c global.c goto.c 

      sudo chown mary getval.c global.c goto.c in a terminal window

    This changes the user ownership of all three files.

      ls -l getval.c global.c goto.c 

      ls -l getval.c global.c goto.c in a terminal window

    You can use wildcards to select file groups. This command changes the user ownership of all files beginning with the letter "c".

      sudo chown mary c *. * 

      sudo chown mary c *. * In a terminal window

    All of the files will now have Mary as the owner. Note that none of the group owners have changed.

      ls -l mary c *. * 

      ls -l mary c *. * In a terminal window

    Let's change the owner of a directory. We simply pass the directory name to chown instead of a filename.

      sudo chown mary ./archive/[19659065<sudochownmary/archive/initeminalwindow" width="646" height="57"/> 

    To check the owner properties of the directory we used ls but also use the option -d (directory). It lists the properties of the directory, not the files it contains.

      ls -l -d ./archive/[19659068)ls-ld/archive/include_terminalwindow" width="646" height="122"/> 

    To change To own all files in a directory, you can use the option -R (recursive) use. This option changes the user ownership of all files in the folder archive .

      sudo chown -R mary ./archive/[19659071<sudochown-rmary/archive/initeminalwindow" width="646" height="57"/> 

    Now let's look at the files in the archive directory.

      ls -l ./archive/[19659074)ls-l/archive/inclusive_interface" width="646" height="292"/> 

    As expected, all of the files now belong to Mary .

    Changing Group Ownership

    There are several ways to change group ownership.

    To change the group ownership concurrently with the user Pass the new owner name and the new group name with a colon ":". The group must already exist.

      sudo chown mary: researchlab charm.c 

      sudo chown mary: researchlab charm.c in a terminal window

    The user owner and the group to which the file belongs have both been changed.

      ls -l charm.c 

      ls -l charm.c in a terminal window

    A short way to change the group owner into the current owner's group, just type in the colon and omit the group name.

      sudo chown mary: caps.c 

      sudo chown mary: caps.c in a terminal window

      ls -l caps.c 

      ls -l caps.c in a terminal window

    Both user and group ownership rights have been changed to Mary .

    To change only the group ownership, you must precede a colon and omit the user name. The owner of the user is not changed.

      sudo chown: researchlab at.c 

      sudo chown: researchlab at.c in a terminal window

      ls -l at.c 

      ls -l at.c 


<p><img class=

    Group ownership has been changed, but user ownership remains unchanged.

    Using Chown with UID and GID Values ​​

    You can use the numeric UID and GID values ​​with the command chown . This command sets the user and the group ownership to mary .

      sudo chown 1001: 1001 at.c 

      sudo chown 1001: 1001 at.c in a terminal window [19659018] ls -l at.c

      ls -l at.c in a terminal window [19659010] Possession is nine-tenths of the law

    Or so it is said. However, under Linux, ownership is an integral part of file security, with the rest covered by file permissions. Use the commands chmod and chmod to secure file access to your system.




Source link