Home / Linux/Unix/BSD / Chown Command

Chown Command

The chown command is useful for making changes to the user or group ownership of a certain file, directory, or symbolic link.

Every file is associated with a file owner or a group. This grants the file or group owner permission access rights to edit, modify, and make changes to the file.

If you aren’t the file owner and want to change the ownership or change the group assigned with the permission rights, the chown command is exactly the command to do this for you.

Getting Started

Learning how to change the ownership of a file with the chown command is a straightforward process. Before we dive in, here’s a few requisites to note:

  • Linux system
  • Access to the command line (this is where we’ll be using the chown command)
  • Sudo privileges (only the sudo or owner of a file has permission access to change the owner. If you don’t have ownership of a file, you won’t be able to execute the chown command properly).

Chown Command Basic Syntax

Learning how to use the chown command starts by understanding the basic syntax behind it:

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

where,

USER – Refers to the user ID or user name of the new owner of a file

GROUP – Refers to the new group user name or user ID who has ownership of a file

[OPTIONS] – Allows you to use the command with or without additional options

[:] – A colon is used when you want to change the group of a file

FILE(s) – Refers to the name of the directory, file, or symbolic link

Some notes to remember:

  • If you specify only the USER, then this will change the owner of the files to the specified user; however, because only the USER was specified, group ownership will not change.
  • If both the USER and GROUP were specified, this would change the ownership to the specified user and the group ownership.
  • If the username specified follows a colon (USER:) without specifying the group, then this will change the owner of the file to the specified user, while the files group ownership will be changed to the user’s login group.

Checking the Chown Command version

If you want to check the version of your chown in Linux, simply type:

chown --version

How to Check File Ownership Using Chown

Before you can change the owner or the group ownership with the chown command, you have to find out who the original owner or group name.

This will allow you to specify the login group or owner name and change the user and group ownership of the files and directories.

To check for the user or group ownership of the files and directories, we’ll use the ls -l command:

ls -l sample.txt

You’ll be presented with the following output:

drwxr-xr-x 2 Dave users 12.0K Apr 02 18:34 sample.txt

where,

Dave – Refers to the owner

users – Refers to the group

We can also use the ls -l command for directories:

ls -l directory1

Changing the Owner of a File With Chown

If you want to change the owner of a file, the command will be as follows:

chown USER FILE

where,

USER – Refers to the new user name or ID you want to change ownership to

FILE – the name of the file

For example, we want to change ownership for sample.txt to user Mark17. To do this, simply type:

chown Mark17 sample.txt

Similarly, you can also use the user ID instead of the username. Let’s say Mark17’s user ID is 1001. The command will now be as follows:

chown 1001 sample.txt

To change a file and a directory with the chown command, simply type:

chown Mark17 sample.txt directory1

Changing the Owner and Group of a File

Changing the owner and group of a file will require the (:) along with the chown command.

Note that there shouldn’t be any space between the new owner and group specified:

chown USER:GROUP FILE

For example, changing the owner and group ownership for the file sample.txt, type the command:

chown Mark17:users sample.txt

If you don’t specify the group after the (:), the group file will be changed to the user’s login group:

chown Mark17: sample.txt

Changing the Group of a file

To change the group of a file, we use the (:) and the GROUP name specified after that:

chown :GROUP FILE

For example, you want to change ownership to the group name users1. To do this, type the command:

chown :users1 sample.txt

Changing the Symbolic Link Ownership

When changing the symbolic link ownership, it’s important to remember that without using a recursive chown command, chown will change the ownership of the file to where the symlink points to and not the symbolic link itself.

When you run chown with the recursive option, you’ll most likely experience an error message:

"cannot dereference 'symlink1': Permission denied" error

To change group ownership of the symlink, you’ll have to use the -h option:

chown -h users1 symlink1

Using Chown Recursive Command to Change File Ownership

If you want to recursively operate all directories and file under a certain directory, this is where the -r option becomes useful:

chown -r USER:GROUP directory

The command, as shown above, will change ownership of all subdirectories and files under/var/xxx directory to a new user and group with the name “xxx-users.”

chown -r xxx-users: /var/xxx

Now, what happens if the directory contains symbolic links? Using the chown command with the recursive and -h option should allow you to chown recursively the symbolic links:

chown -hR xxx-users: /var/xxx

How to Display Chown Process Details

When you execute the chown command, the terminal will not reveal the details of the chown process. If you want to view the details, you can choose from two options: -v and -c.

-v – This will reveal the chown process details even if the ownership stays the same

-c – This will revel the chown process details only when the owner or group of certain file changes

For example, if we want to specify the current owner as the new owner of a specific file “sample.txt”

chown -v Mark17 sample.txt

Through this command, you’ll be presented with the output:

ownership of 'sample.txt' retained as Mark17

Using both the -v and -c options are especially beneficial when combined with the recursive option:

chown -v -R Mark17 sample.txt

Output

Ownership of 'sample.txt' retained as Mark17
Changed ownership of 'sample2.txt' from user1 to Mark17
Ownership of 'sample3.txt' retained as Mark17

By using both the -v and -R option, this will allow you to see all variables affected once you execute the command.

Final Thoughts

Using the chown command is useful, especially for team-based settings and for managing a wide group of members.

This gives you the advantage of specifying certain users and a specific group to have permission access and ownership for files and directories conveniently.

Convenience aside, you should also take precautions when changing ownership of a file to a new owner or group.

Granting access to files or directories that contain sensitive information you might have overlooked poses potential risks to your Linux system.

Make sure to use the ls -l command to check who the owner of a file is first.

We hope this article has helped you understand more about the chown command and how to use it!

If you’re still confused about how to use chown, you can check out the chown man page or type “man chown” in your command line/terminal.