How to Change File Owner in Linux: Simple chown Command Guide
To change a file owner in Linux, use the
chown command followed by the new owner's username and the file name, like chown newowner filename. You need superuser (root) privileges to change ownership of files you don't own.Syntax
The basic syntax of the chown command is:
chown [options] new_owner[:new_group] file
Here, new_owner is the username of the new owner, new_group is optional and sets the group ownership, and file is the path to the file or directory.
You can change just the owner, or both owner and group separated by a colon.
bash
chown new_owner filename chown new_owner:new_group filename
Example
This example changes the owner of example.txt to user alice. It shows how to run the command and the expected output when checking ownership.
bash
sudo chown alice example.txt ls -l example.txt
Output
-rw-r--r-- 1 alice alice 0 Apr 27 12:00 example.txt
Common Pitfalls
Common mistakes include:
- Trying to change ownership without
sudo, which causes permission denied errors. - Using incorrect usernames or groups that do not exist.
- Forgetting to specify the file or directory name.
Always verify the new owner exists and use sudo if needed.
bash
chown alice example.txt # Error: Operation not permitted sudo chown alice example.txt # Success
Quick Reference
| Command | Description |
|---|---|
| chown alice file.txt | Change owner of file.txt to alice |
| chown alice:staff file.txt | Change owner to alice and group to staff |
| sudo chown -R alice /folder | Recursively change owner of /folder and contents to alice |
| chown --help | Show help and options for chown command |
Key Takeaways
Use the chown command with the new owner's username and the file name to change ownership.
You need superuser privileges (sudo) to change ownership of files you do not own.
You can change both owner and group by separating them with a colon.
Always verify the username and group exist before changing ownership.
Use the -R option to change ownership recursively for directories.