In Ubuntu Linux, security is baked into the foundation of the file system. Unlike older operating systems where anyone can modify anything, every single file and folder in Linux is mathematically assigned to an “Owner” and a “Group.”
If you create a text file, you own it. You can read it, edit it, and delete it. If your roommate logs into the same computer with a different account, they will receive a “Permission Denied” error if they try to edit your file. This system prevents rogue software from destroying critical system configurations.
However, what happens if you download a web server configuration file using the powerful root administrator account, but you need the standard www-data web service account to actually read and execute it? You must formally transfer ownership of the file to the new user. You accomplish this using the chown (Change Owner) command.
This guide explains how to use the chown command in the Ubuntu terminal.
The Basic chown Command
The syntax for the chown command is straightforward. You state the command, tell it the name of the new owner, and then point it at the file you want to transfer.
Warning: You cannot give away files you do not own, and you cannot steal files from other people. Changing ownership almost always requires superuser privileges, meaning you must preface the command with sudo.
To transfer ownership of a file named report.txt to a user named sarah, you would type:
sudo chown sarah report.txt
If the command executes successfully, the terminal will simply drop to the next line without any confirmation message. If you want to verify the change, run the ls -l command to view the file’s metadata.
Changing Both the Owner and the Group
In Linux, every file also belongs to a “Group.” (For example, multiple people in an office might belong to the “accounting” group, allowing them all to read the same financial files).
You can use the chown command to change the Owner and the Group simultaneously by separating the two names with a colon (:).
To give the file report.txt to the user sarah and assign it to the accounting group, run:
sudo chown sarah:accounting report.txt
Pro Tip: If you want to change the file to belong to Sarah, and you want to automatically assign the file to whatever Sarah’s primary default group is (without having to look up what her group is named), just leave the group name blank after the colon:
sudo chown sarah: report.txt
Changing Ownership of an Entire Directory (Recursive)
If you move an entire folder full of hundreds of photographs from an external hard drive into your home directory, transferring ownership of each file one by one would take hours.
You can instruct chown to change the ownership of a folder and every single file and sub-folder buried inside of it by using the -R (Recursive) flag. Note that the flag must be a capital ‘R’.
sudo chown -R john /home/john/Pictures/Vacation/
The system will dive deep into the Vacation directory and instantly transfer ownership of every single image inside to the user “john.”