Easily Change Ownership of File in Linux

You'll eventually come across the need to change ownership of file or directory in Linux. The best method to do this is through the use of the chown command which stands for "Change Owner".  This command allows you to easily modify the user and group ownership of a file or entire directory.

Change ownership of file Linux

How the Chown Command Works

The basic syntax of the chown command to change the owner of a file or directory in Linux is as follows:

sudo chown [options] new_owner:group file_or_directory
Term Description
sudo Used to run the command with administrative privileges; changing owners often requires root or superuser permissions.
new_owner Replace with the new owner's username or user ID.
group Optionally set a new group name or group ID. If not specified, the file's group will remain unchanged.
file_or_directory The path to the file or directory whose ownership you want to change.

How to Change ownership of file in Linux | Chown

Note that you will need appropriate superuser or root permissions to change the ownership of a file.

  1. Open a Terminal. In Ubuntu or Debian, simply press Ctrl + Alt + T
  2. Check the current ownership of the file (optional).
    Before changing owners, you might want to verify the current owner. You can do this using the ls -l command. For example:

    ls -l filename.txt

    This will display information about the file, including the current owner and group.

  3. Proceed to change ownership of a file using chown command.
    For example, to change the ownership of filename.txt to user john, you would type the following:

    sudo chown john filename.txt

More examples of Changing Owners In Linux

Change ownership of file to a specific user and group:

sudo chown john:users filename.txt

Change owner of a directory in Linux to a specific user and group:

sudo chown alice:developers directory_path_and_name/

Change the owner of a directory, leaving the group unchanged:

sudo chown michael directory_path_and_name/

Remember to use caution when changing ownership, especially with sudo privileges, as it can create significant changes. Always double check the parameters you are setting from the terminal, before pressing enter.