How to Use The Chown Command On Linux

Easily change the owner of Linux files

Files and File Folders on the Ubuntu Linux desktop

With Linux, every file is owned by a specific user. Although files can also be given group permissions, the purpose of ownership is to ensure that only those who actually have access to the file have the ability to read, write, or execute (run it, like an application). Without ownership, those files are, generally speaking, off limits.

If you open a terminal window on your Linux desktop and issue the command ls -l, you will see the third column from the left displaying who the owner of the files and folders are.

Screenshot of the ls -l command showing ownership.

That’s all fine and good, if you are the only user on your Linux system. But if you have others that log into your system, or you’re installing software that requires a file be owned by a different user, what do you do?

You change the owner of the file or folder with the help of the chown command. Chown stands for Change Owner and it’s quite easy to use. Let’s find out how.

A Word Of Warning

In order to change the ownership of files and/or directories, you will have to make use of sudo. By using sudo (which stands for Super User Do), you are able to run administrative tasks, which means you can act on files and/or directories you wouldn’t normally be able to. To that end, use sudo with care.

Changing Ownership of a Single File

Let’s say you have two users on your Linux machine:

  • bethany
  • jacob

The user bethany has a file named test that jacob needs to access. If bethany issues the command ls -l, she notes that she is the owner of the file. 

Screenshot of ownership of the test file.

In order for jacob to access the file, bethany needs to change the ownership.

Another Word Of Warning

In this example, there is a much better method of dealing with this issue, with groups. However, as we are talking about ownership (not groups), we’ll continue on with this example, as it still works just fine.

For bethany to change the ownership of the file test, such that it is then owned by jacob, she would issue the command sudo chown jacob ~/test. After running this command, bethany then checks the ownership with the command ls -l to see that jacob is now the owner of the file.

Screenshot of jacob now having ownership of the file.

However, just because jacob is the owner of the file, doesn’t mean he has both read and write access. Why? Because, as it stands, the file is in a directory (/home/bethany) owned by bethany, and jacob doesn’t have write access to the directory. In order for jacob to have both read and write access, the file would have to be moved into a directory giving jacob both read and write access. Bethany can move that folder into jacob’s home directory with the command sudo mv ~/test /home/jacob. Or, even better, bethany can create a new directory and give jacob read and write access to that folder by way of ownership. This would be done (by user bethany) with the following steps:

  1. Open a terminal.

  2. Create a new directory with the command mkdir ~/jacob.

  3. Change the ownership of the directory with the command sudo chown jacob ~/jacob.

  4. Move the test file into the new directory with the command mv test ~/jacob.

Here’s where groups come into play. Although the new directory is owned by jacob, because the directory /home/bethany/jacob still belongs to the group bethany (as noted by the fourth column from the left), the user bethany is still able to access the file (with both read and write privileges).

Screenshot of jacob having ownership with bethany still in the group.

Changing Ownership of an Entire Directory

Say bethany has an entire directory (filled with files) that jacob needs access to. She can either change the ownership of those files, one at a time, or she can simply change it all in one fell swoop. How would she do that? Say the directory in question is /home/bethany/jacob. To give jacob ownership of that directory, bethany would run the command sudo chown -R jacob ~/jacob. The -R option stands for recursive, which means it will change the ownership of the directory and then dive into the directory and change the ownership of all the files contained within.

And those are the basics of using the chown command.