How to Change Ownership of a File in Ubuntu
Managing file permissions is essential for security in Ubuntu. This guide explains how to use the chown command to transfer file ownership to a specific user or group quickly. You will learn the basic syntax, how to apply changes recursively, and the necessary privileges required to execute these commands successfully.
Prerequisites
To change file ownership, you must have superuser privileges. You
will need to use the sudo command before executing any
ownership changes. Additionally, ensure you know the username of the
target owner and the path to the file or directory you wish to
modify.
Basic Chown Command
The chown command stands for “change owner.” The basic
syntax requires specifying the new owner followed by the file name. Open
your terminal and run the following command:
sudo chown new_username file_nameFor example, to change the owner of document.txt to a
user named alice, type:
sudo chown alice document.txtChanging User and Group
You can also change the user and the group ownership simultaneously.
Separate the user and group with a colon (:). The syntax
looks like this:
sudo chown new_username:new_group file_nameIf you only want to change the group while keeping the existing user, omit the username before the colon:
sudo chown :new_group file_nameRecursive Ownership Change
To change the ownership of a directory and all files contained within
it, use the recursive flag -R. This is useful when
migrating project folders between users.
sudo chown -R new_username /path/to/directoryVerifying the Change
After running the command, confirm that the ownership has been
updated. Use the ls command with the -l flag
to list detailed file information.
ls -l file_nameThe output will display the new owner and group in the third and fourth columns respectively.