How to Change Ownership of a File in Linux

Unlike Windows or macOS, Linux is inherently a multi-user operating system built from the ground up for massive enterprise servers. Because dozens of different people might be logged into the same server simultaneously, the operating system uses a strict, military-grade permissions structure. Every single file and folder on the hard drive legally belongs to a specific user and a specific group.

If you create a web server script using your personal user account, and then the automated www-data web user attempts to execute that script to serve a webpage to a customer, Linux will instantly block the transaction and throw a “Permission Denied” error. You must explicitly surrender ownership of the file to the correct user. The tool you use to do this is the chown (change owner) command.

The Syntax of the chown Command

The chown command is incredibly powerful and relatively simple to use, provided you have administrative privileges. A normal user cannot legally steal ownership of a file from someone else, nor can they give their own files away (which prevents users from avoiding storage quotas). You must use sudo to execute the command.

The basic syntax is:

sudo chown [new_owner]:[new_group] [name_of_file]

(Note: It is crucial to include the colon (:) separating the owner and the group without any spaces).

Method 1: Change the Owner and Group Simultaneously

This is the most common usage of the command. For example, if you have a file named config.php that is currently owned by the root administrator, but it needs to be owned by the web server (which operates under the username www-data and the group www-data), you would type:

sudo chown www-data:www-data config.php

The terminal will execute silently. If you want to verify the change was successful, you can use the long-listing command (ls -l) to view the file’s metadata.

ls -l config.php

The output will clearly display the new owner and group names.

Method 2: Change Only the Owner (Leave the Group Intact)

If you have a shared team folder and you only want to change the primary user who owns the file, but leave the overarching group permissions exactly as they are, you simply omit the group declaration from the command.

sudo chown johndoe config.php

John Doe is now the absolute owner of the file, but whatever group previously held rights to the file maintains those rights.

Method 3: Recursively Change Ownership of an Entire Directory

The most infuriating scenario is migrating a massive WordPress website to a new server. You upload a folder containing 10,000 images, and suddenly realize they are all owned by the wrong user. You cannot type the chown command 10,000 times.

You must use the recursive flag (-R). The capital ‘R’ commands the software to grab the master folder, dive inside, and brutally overwrite the ownership of every single sub-folder and file it finds.

To recursively give ownership of the entire /var/www/html/ folder to the www-data user, you would type:

sudo chown -R www-data:www-data /var/www/html/

Massive Warning: The recursive flag is one of the most dangerous commands in Linux. If you accidentally type a space before the slash, or accidentally point it at the root directory (/), you will instantly and permanently destroy the entire operating system, and the server will never boot again.

Get the best tech tips delivered straight to your inbox.

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