How To Use Chown Command in Linux: Examples and Quick Tips

Chown Command in Linux

On Linux, every file has an owner and a group. They are given access rights accordingly. 

With the chown command, you can change the owner and group of a file or directory. Configuring file and folder permissions is critical to the security of the files.  

In this brief guide, we will cover all you need to know to use chown to change permissions. We use chown version 8.28 in Ubuntu 18.04.2. We also provide some examples. 

You will need a Linux machine with access to a terminal. You need to have superuser privileges (so you can use the sudo command) to change ownership of any file.  

Syntax of the Linux Chown Command

Like many other commands, the chown command’s syntax is split into a few sections. Its help file makes the command’s syntax clear:

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

As you can see, you can use the command with options, but this is not always necessary, depending on what you’re trying to do.

The [USER] part must indicate the numeric user ID or the username of the new owner you’re trying to make.

The colon is used in the syntax when you need to change a file’s group. You must then write which new group you’re trying to provide ownership to.

Finally, you must write the target file in the FILE part of the syntax.

Checking the Ownership of a File 

Before using the chown command, you might need to determine the file’s original group or owner. 

Doing this is as simple as navigating to the directory or file and running:

ls -l

 

Changing a File’s Owner

You might be able to guess that you will need to specify the new owner and the file in the chown command, like so:

chown NewUser FILE

Let’s say we have a user “jane” and a file “example.” To give jane ownership, you would:

chown jane example

You can use the same format if you want to change the owner of a directory.

Remember that you don’t necessarily need to specify the username; you can also use the user ID to change ownership. Here’s what that would look like: 

chown 1005 example

Before you use the UID, ensure that no user has a UID set as their username. The command gives precedence to usernames. So, if one user has a username set as another user’s UID, the first user will become the new owner of the file.

You can run id -u USERNAME to find the UID of any user. 

Changing Ownership of Multiple Files

When you need to change the ownership of multiple files, you can simply list the filenames in the final part of the command. Note that the names need to have a space between them. 

Let’s say we need to give Jane access to the files example2 and example3. You would write the following command:

chown jane example2 example3

You can also list file names and directory names in the same command and change their ownership in one go:

chown jane example2 directory1

 

Changing a File’s Group 

The chown command allows you to change a file or directory’s group without changing the owner. So, it supplies the same result as the chgrp command. 

As you’d expect, the chown command for changing groups involves the colon, the name of the group, and the file name:

chown :GroupName FILE

Let’s say you want to change the group for the example2 file. You would write:

chown :group5 example2

You can list multiple directories or files to change the group in one go. Also, using a group ID instead of the group name is possible. The colon must still be there in the command.

Changing Both Group and Owner

To change the group and owner of a file, you would run the chown command in the following format:

chown User:Group FILE

Let’s say you want to change the owner of example3 to “lara” and give ownership to group5. You would write:

chown lara:group5 example3

 

Changing Group to a Specific User’s Login Group

If no group is specified when using the chown command, it automatically assigns the owner’s login group to the directory/file.

To change a file or directory’s group to a specific user’s login group, you can use:

chown User: FILE

This command would change the group to jane’s login group:

chown jane: example3

 

Transferring Group and Ownership from One File to Another

Besides allowing you to assign new owners and groups, the chown command allows you to use a reference file’s owner and group.

Pulling this off is as straightforward as using the –reference option. It will copy the settings of the file and assign it to another. The syntax looks like this:

chown –reference=ReferenceFile FILE

 

Verifying the Current Owner and Group of a File or Directory

You can use the –from option with the command to find the current group and owner of a file before changing them. The syntax for checking and changing is:

chown –from=CurrentUser:CurrentGroup NewUser:NewGroup FILE

Continuing with the examples shown above, here’s how you would change the group and owner of the example2 file:

chown –from=jane:group5 lara:group6 example2 

 

Verifying Only the Owner

The –from option also allows you to validate just the present owner. The syntax involved is:

chown –from=CurrentUser NewUser FILE

An example of this is:

chown –from=lara jane example2

 

Verifying Only the Group

As you’d expect, you can use the chown command to verify only the group before changing it. The –from option still needs to be used.

The syntax is:

chown –from:CurrentGroup :NewGroup FILE

Picking up from our previous example, you could verify that group6 is the new group and change it back to group5, like so:

chown –from=:group6 :group5 FILE

 

Changing File Ownership Recursively

You can use the chown command to assign new ownership to all files and subdirectories inside a directory. All you need to do is use the -R option, like in the following syntax:

chown -R User:Group PathOrDirectoryName

We can change the user and group of directory1 and its contents from our previous example like this:

chown -R lara:group6 Dir1

 

Changing the Owner of a Symbolic Link

You can change the owner of a symbolic link with the -h option. If you don’t use the option, the owner of the linked file will change, and the owner and group of the symbolic link will remain the same. 

The syntax for changing the owner and group of a symbolic link looks like this:

chown -h User:Group SymbolicLink

 

Displaying the Process Details of the Chown Command

The Linux terminal doesn’t display the chown process’s details by default. However, you can take a look under the hood with two options:

The -v option will show the process’s details without changing the owner. On the other hand, the -c option shows the details only when the target files’ owner or group is changed. 

So, if you run:

chown -v jane example2

The terminal will give the output:

ownership of example2 retained as jane

But if you used the -c option in the above command, no message would appear since the group or owner is the same. 

The -v option is particularly useful in conjunction with the recursive option -R. It will show all the changes made inside a directory.

Suppressing Chown Errors 

Error messages can pop up sometimes when you use the chown command. But you can use the -f option to prevent these messages from appearing.

You must note that while the -f option suppresses most errors, an error message will appear if you specify an invalid username. 

Conclusion

Remember that the terminal is case-sensitive, so if you accidentally type a capital in a file or directory name, your command will not work as intended. 

With this guide handy, you will have an easier time changing groups and owners of files and directories.

Learn more