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 support 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 it. Without ownership, those files are, generally speaking, off-limits.

When 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.

These assignments aren't fixed, however. You're free to change the owner of the file or folder with the help of the chown command.

A Word Of Warning

To change the ownership of files or directories, use sudo. By using sudo, you are able to run sensitive tasks that require elevated privileges, which means you can act on files or directories you wouldn’t normally be able to.

Changing Ownership of a Single File

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

  • bethany
  • jacob

The user bethany owns 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.

For bethany to change the ownership of the file test such that it is then owned by jacob, she 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. Create a new directory with the command

    mkdir ~/jacob
  2. Change the ownership of the directory with the command:

    sudo chown jacob ~/jacob
  3. Move the test file into the new directory with the command

    mv test ~/jacob

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.

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.