How to Use the Chown Command in Linux to Change File Owners

In Linux, every file and directory is owned by a specific user and a specific group. This ownership dictates who has the right to read, write, or execute that file. When you need to transfer ownership of a file from one user to another (for example, when moving web files to the Apache user), you use the chown (change owner) command.

Basic Syntax

The standard structure for the command is:

sudo chown [new_owner]:[new_group] [filename]

Note: Because changing ownership is an administrative action, you almost always need to preface the command with sudo.

Common Examples

1. Change the Owner Only

To change the owner of a file named report.txt to a user named john, without altering the group:

sudo chown john report.txt

2. Change the Owner and the Group

To change both simultaneously, separate the user and group with a colon. For example, to give ownership to the user www-data and the group www-data:

sudo chown www-data:www-data index.html

3. Change the Group Only

If you want to keep the current owner but change the group assignment, leave the owner section blank but keep the colon:

sudo chown :developers project_file.py

4. Change Ownership Recursively

If you want to change the ownership of a directory and everything inside it (all sub-folders and files), you must use the -R (recursive) flag. This is incredibly common when setting permissions for a web server directory:

sudo chown -R john:developers /var/www/html/mywebsite

How to Check Ownership

To verify that your chown command worked, use the list command with the long format flag (ls -l):

ls -l report.txt

The output will show the permissions string, followed by the Owner’s name, and then the Group’s name, confirming your changes were successful.

Leave a Reply

Your email address will not be published. Required fields are marked *

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.

Receive our best articles and tips delivered straight to your inbox.