How to use chown command for beginners

Photo of author

By rasho

The chown command allows you to change the user and/or group ownership of a given file, directory, or symbolic link.
In Linux, all files are associated with an owner and a group and assigned with permission access rights for the file owner, the group members, and others.
In this tutorial, we will show you how to use the chown command through practical examples.
Before we start with the chown com
mand tutorial, it’s worth mentioning that all examples and instructions mentioned here have been tested on Linux Mint 19.3

Linux chown command explained

As already mentioned in the beginning, the chown command lets you change the file owner and group through the command line. Following is the command’s generic syntax:

chown [OPTIONS] USER[:GROUP] FILE(s)

USER is the user name or the user ID (UID) of the new owner. GROUP is the name of the new group or the group ID (GID). FILE(s) is the name of one or more files, directories or links. Numeric IDs should be prefixed with the + symbol.

  • USER – If only the user is specified, the specified user will become the owner of the given files, the group ownership is not changed.
  • USER: – When the username is followed by a colon :, and the group name is not given, the user will become the owner of the files, and the files group ownership is changed to user’s login group.
  • USER:GROUP – If both the user and the group are specified (with no space betwen them), the user ownership of the files is changed to the given user and the group ownership is changed to the given group.
  • :GROUP – If the User is omitted and the group is prefixed with a colon :, only the group ownership of the files is changed to the given group.
  • : If only a colon : is given, without specifying the user and the group, no change is made.

By default, on success, chown doesn’t produce any output and returns zero.

How to change the owner of a file?

Consider the following example:

$ ls -lart file
-rw-rw-r-- 1 rasho rasho 0 Jan 12 22:07 file

Here, the owner of the file is ‘rasho’ and the group it belongs to is also ‘rasho’. Now, to change the owner to, say ‘root,’ use the following command:

$ chown root file

Type following command to confirms that the owner has now been changed to ‘root’.

$ ls -lart file
-rw-rw-r-- 1 root rasho 0 Jan 12 22:07 file

[ads]

How to change the group of a file

Changing a group is similar to changing the owner. The only difference is in the syntax of the command, which is as follows:

chown :[group-name] [file-name]

So suppose the requirement is to change the group of ‘file’ to ‘root’. Then the command would be:

$ sudo chown :root file

Type following command shows the group was successfully changed from ‘rasho’ to ‘root’.

$ ls -lart file
-rw-rw-r-- 1 root root 0 Jan 12 22:07 file

Change both owner and the group

To change both the owner and the group of a file use the chown command followed by the new owner and group separated by a colon : with no intervening spaces and the target file.

chown USER:GROUP FILE

So in our case, to change the existing owner and group from ‘root’ to ‘rasho’, we’ll use the following command:

$ chown rasho:rasho file

The following command shows the above command in action:

$ ls -lart file
-rw-rw-r-- 1 rasho rasho 0 Jan 12 22:07 file

If you omit the group name after the colon : the group of the file is changed to the specified user’s login group:

$ chown linuxize: file

Using chown command on symbolic link file

For example, if you try to change the owner and the group of the symbolic link symlink1 that points to /var/www/file, chown will change the ownership of the file or directory the symlink points to:

$ chown www-data: symlink1

Chances are that instead of changing the target ownership, you will get a “cannot dereference ‘symlink1’: Permission denied” error.

The error occurs because by default on most Linux distributions symlinks are protected, and you cannot operate on target files. This option is specified in /proc/sys/fs/protected_symlinks. 1 means enabled and 0 disabled. We recommend not to disable the symlink protection.
To change the group ownership of the symlink itself, use the -h option:

$ chown -h www-data symlink1

How to Recursively Change the File Ownership

To recursively operate on all files and directories under the given directory, use the -R (--recursive) option:

$ chown -R USER:GROUP DIRECTORY

[ads]
The following example will change the ownership of all files and subdirectories under the /var/www directory to a new owner and group named www-data:

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

If the directory contains symbolic links pass the -h option:

$ chown -hR www-data: /var/www

Other options that can be used when recursively changing the directory ownership are -H and -L.

If the argument passed to chown command is a symbolic link that points to a directory, the -H option will cause the command to traverse it. -L tells chown to traverse each symbolic link to a directory that is encountered. Usually, you should not use these options because you might mess up your system or create a security risk.

Using a Reference File

The --reference=ref_file option allows you to change the user and group ownership of given files to be same as those of the specified reference file (ref_file). If the reference file is a symbolic link chown will use the user and group of the target file.

$ chown --reference=REF_FILE FILE

For example, the following command will assign the user and group ownership of the file1 to file2

$ chown --reference=file1 file2

Conclusion

The chown command, as most of you’d likely agree, isn’t difficult to use. What’s even better is that the tool’s man page contains a lot of details that might be of help to users (especially newbies). Try out the examples we’ve explained here, and for the rest of the features/options, go through the chown man page. In case of any doubt or query, leave a comment below.

Leave a Comment