The chown command stands for "change owner." While chmod changes what can be done (permissions), chown changes who owns the file or directory.
In Linux, every file and directory is owned by a specific User and belongs to a specific Group.
1. Basic Syntax
The basic syntax for chown is:
sudo chown [user]:[group] [file_or_directory]
(Note: Since changing ownership is a sensitive tasks, you almost always need to use sudo.)
2. Common Use Cases
- Change the owner only:
sudo chown newuser file.txt - Change both the owner and the group:
sudo chown labex:developers project_folder - Change the group only (though the command
chgrpalso exists,chownis often used like this):sudo chown :newgroup file.txt
3. Recursive Change (The -R flag)
If you have a folder full of files and you want to change the owner of everything inside at once, use the -R (recursive) flag:
sudo chown -R labex:labex /home/labex/project/my_work
4. Why would you need chown?
In this lab environment, you'll mainly use it if:
- You used
sudoto create a file: Sometimes, if you create a file usingsudo, it becomes owned by therootuser. You might find you can't edit it normally later. You would usechownto give it back to thelabexuser. - Moving files: If you copy files from another user's directory, ownership labels might need updating so you can work on them.
Summary: The Difference between chmod and chown
chmod: "I want to make this file readable/writable." (Changing Mode)chown: "I want this file to belong to a different person or group." (Changing Owner)
Would you like to try changing the owner of a test file to see how it affects your ability to edit it?