Chown Command: A Beginner’s Guide to Linux

In Linux, every file and directory has an owner and a group assigned to it. The owner of a file has complete control over the file, including the ability to read, write, and execute the file. The group, on the other hand, is a collection of users who have similar permissions for the file.

By default, the owner of a file is the user who created it. However, there may be instances where you need to change the ownership of a file or a directory. This is where the Linux chown command comes into play.

What is the Linux Chown Command?

The Linux chown command stands for “change owner” and is used to modify the ownership of files and directories in a Linux system. With the chown command, you can change the owner of a file or directory to a different user or group.

How to Use the Linux Chown Command?

Before diving into how to use the chown command, let’s review its basic syntax. The chown command expression takes the following form:

chown [OPTIONS] OWNER[:GROUP] FILE

USER is the username 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, and 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 the user’s login group.
  • USER:GROUP: If both the user and the group are specified (with no space between 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.

To find out who owns a file or what group the file belongs to, use the ls -l command:

ls -l filename.txt

Example output:

-rw-r--r-- 12 joshua users 12.0K Feb 11 2023 12:00 filename.txt
|[-][-][-]-          [------] [---]
                          |       |
                          |       +-----------> Group
                          +-------------------> Owner

Normal users can change the group of a file only if they own the file and only to a group of which they are a member. Administrative users can change the group ownership of all files.

Changing the Owner of a File

To change the owner of a file, use the chown command followed by the username of the new owner and the target file as an argument:

chown USER FILE

For example, the following command will change the ownership of a file named file1 to a new owner named Joshua:

chown joshua file1

To change the ownership of multiple files or directories, specify them as a space-separated list. The following command changes the ownership of a file named file1 and directory dir1 to a new owner named Joshua:

chown joshua file1 dir1

The numeric user ID (UID) can also be used instead of the username. The following example changes the ownership of a file named file2 to a new owner with UID 1000:

chown 1000 file2

If a numeric owner exists as a username, then the ownership will be transferred to the username. To avoid this, prefix the ID with a + symbol:

chown +1000 file2

Changing the Owner and Group of a File

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

The following command will change the ownership of a file named file1 to a new owner named joshua and group users:

chown joshua:users file1

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

chown joshua: file1

Changing the Group of a File

To change only the group of a file, use the chown command followed by a colon (:) and the new group name (with no space between them) and the target file as an argument:

chown :GROUP FILE

The following command will change the owning group of a file named file1 to www-data:

chown :www-data file1

Another command you can use to change the group ownership of files is chgrp.

Changing Symbolic Link Ownership

When the –recursive option is not used, the chown command changes the group ownership of the files to which the symlinks point, not the symbolic links themselves.

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

chown www-data: symlink1

You might receive a “cannot dereference ‘symlink1’: Permission denied” error because, by default, most Linux distributions protect symlinks, and you cannot operate on target files. This option is specified in /proc/sys/fs/protected_symlinks. A value of 1 means enabled, and 0 is disabled, and it’s recommended not to disable the symlink protection.

To change the group ownership of the symlink itself, use the -h option:

chown -h www-data symlink1

Recursively Changing the File Ownership

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

chown -R USER:GROUP DIRECTORY

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 the chown command is a symbolic link that points to a directory, the -H option will cause the command to traverse it. -L tells the chown to traverse each symbolic link to a directory that is encountered. Usually, it would be best if you did not use these options because they 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 the 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 file1 to file2:

chown --reference=file1 file2

Changing the Owner of Multiple Files with Wildcards

You can use wildcards to change the owner of multiple files that match a certain pattern. For example, if you want to change the owner of all .txt files in a directory, you can use the following command:

chown joshua *.txt

This command will change the ownership of all .txt files in the current directory to joshua.

Conclusion

The chown command is an essential tool for managing file ownership in Linux. It allows you to change the owner, group, or both for a file or directory. Understanding the basic syntax and options of the chown command can help you effectively manage your files and directories. Whether you’re a beginner or an experienced user, the chown command is useful in your toolbox.

Your Mastodon Instance
Share to...