How to Change File/Group Owner with chown Command in Linux

Short for change ownership, Chown command is a command-line utility that is used to change the user or group ownership of a file or directory and even links. The Linux philosophy is such that every file or directory is owned by a specific user or group with certain access rights.

Using different examples, we will try and see the various use cases of the chown command. Chown command employs quite a simple and straight forward syntax.

$ chown OPTIONS USER: GROUP file(s)

Let’s briefly flesh out the parameters:

The attribute USER refers to the username of the user that will own the file. You can specify either the username or the UID ( User ID). Meanwhile, the GROUP option indicates the name of the new group that the file will acquire after running the command. The file option represents a regular file or a directory or even a symbolic link. These are the three entities whose permissions can be altered.

A few points to note:

1) When the  USER option is specified alone, ownership of the file/directory changes to that of the specified user while the group ownership remains unchanged. Here’s an example:

$ chown john file1.txt

In the above command, user ownership of the file file1.txt changes from the current user to the user john.

2) If the  USER option is proceeded by a full colon i.e. USER: and the group name is not provided, then the user takes ownership of the file but the file’s group ownership switches to the user’s login group. For example:

$ chown john: file1.txt

In this example, the user john takes ownership of the file file1.txt, but the group ownership of the file changes to john’s login group.

3) When both the user and group options are specified separated by a colon i.e USER:GROUP  – without any spaces therein – the file takes ownership of the new user and the group as specified

$ chown john:john file1.txt

In the above example, the file takes the user and group ownership of user john.

4)  When the USER option is left out and instead the group option is preceded by the full colon :GROUP then, only the group ownership of the file changes.

How to view file permissions

To view file permissions, simply use the ls -l command followed by the file name

$ ls -l filename

For example:

$ ls -l file1.txt

list-file-permissions-linux

From the output, we can see that the file is owned by user linuxtechi which and belongs to the group linuxtechi in the 3rd and 4th columns respectively.

How to change file owner with chown command

Before changing permissions, always invoke sudo if you are not working as the root user. This gives you elevated privileges to change user and group ownership of a file.

To change file ownership, use the syntax:

$ sudo chown user filename

For example,

$ sudo chown james file1.txt

Change-file-owner-linux-chown-command

From the output, you can clearly see that the ownership of the file has changed from linuxtechi to user james.

Alternatively, instead of using the username, you can pass the UID of the user instead. To get the UID, view the /etc/passwd file.

$ cat /etc/passwd | grep username

From the example below, we can see that the UID of user linuxtechi is 1002

uid-file-linux-command

To change the file ownership back to linuxtechi user, we shall execute the command:

$ sudo chown 1002 file1.txt

chown-uid-linux-file-command

How to change the group owner with chown command

As earlier discussed, to change the group owner of a file, omit the user and simply prefix the group name with a full colon.

$ sudo chown :group file

For example, to change the group owner of file1.txt from linuxtechi to docker, we executed the command:

$ sudo chown :docker file1.txt

Changing-group-owner-chown-linux-command

How to change both file owner and group owner of a file

If you want to change both the owner and group that a file belongs to, specify both the user and group options separated by a full colon as shown in the syntax below. Be sure that there are no spaces between the options and the colon.

$ sudo chown user:group filename

For example, the following command changes the ownership of the file file1.txt  to user james and group redis as verified using the ls command.

$ sudo chown james:redis file1.txt

change-file-group-owner-chown-command

How to recursively change file ownership

When applying permissions to directories, you might want to apply changes recursively i.e make the ownership changes to descend and apply to files and sub-directories. To achieve this, user the recursive option -R or –recursive directive.

$ sudo chown -R user:group directory

For example, the command below assigns all files and folders in the /var/www directory ownership to the www-data group.

$ sudo chown -R :www-data /var/www

The example below assigns ownership of the directory reports alongside all the files and folders in the directory to the user linuxtechi.

$ sudo chown -R linuxtechi reports

How to change ownership using a reference file

Lastly, there’s a nifty way that you can use to change ownership of a file, and that is by using a reference file. Using the chown command, you can change the user and group ownership of a file using another file as the point of reference.

The syntax is shown below:

$ chown –reference=ref_file file

Suppose you want to assign user and group ownership of file1.txt to another file file2.txt. How would you go about it? This is illustrated in the command below.

$ chown --reference=file1.txt file2.txt

linux-chown-reference-command-example

The output above confirms that file2.txt inherits the user and group ownership of file1.txt.In the command, file1.txt is the reference file.

Conclusion

Chown command is a powerful tool that is used for managing file and directory ownership.  For additional information, check out the chown man pages.

$ man chown

Also Read : Linux Zip and Unzip Command with Examples

The post How to Change File/Group Owner with chown Command in Linux first appeared on Linuxtechi.



from Linuxtechi https://ift.tt/31Jg8gg

Post a Comment

0 Comments