Changing owners and rights: chown and chmod
Video 113 of 140 in Virtualization Foundations, in the unit “Users, groups, permissions and sudo”. 2:52, with English and Hindi audio.
Sometimes a file has the wrong owner, or the right people cannot reach it. Two commands fix that. chmod changes the rights of a file, and chown changes its owner and group. Each one can be run only by certain accounts, and each leaves no message when it works, so you need to check the result yourself. This article explains who may use each command, how to hand a file to a team, and how to confirm the change with stat.
What you will learn
- chmod changes the rights of a file, and the owner or root may run it.
- chown changes the owner and the group, and usually only root may run it.
- chgrp changes only the group, and the owner may run it if they belong to the new group.
- chmod prints nothing when it works, so check the result with stat.
- Check the path before you use the recursive option, because it changes a whole folder tree.
Who may change rights, owner and group
- chmod: changes the rights. The owner of the file may do it, and so may root.
- chown: changes the owner, and the group. Usually only root may do it, so ordinary users cannot give files away.
- chgrp: changes only the group. It is handy when a file must belong to a team.
chmod also accepts letters, such as u plus x, which adds execute for the owner only. Numbers are common in scripts.
Changing the rights and checking them
chmod 640 report.csvstat -c '%A %U %G %n' report.csvNote: chmod is quiet when it works. Always check the result with stat, so you see the real mode.
Handing a file to a team
sudo chown bob:devs report.csvThe chown command takes owner colon group, so bob colon devs sets both at once. Ordinary users cannot change owners, so the command starts with sudo.
The recursive option changes a whole folder tree at once. Try it on a copy first, because one wrong path reaches many files.
Two jobs, kept apart
Changing the mode is the owner's job, and changing the owner is root's job. A file that cannot be opened by the right person can be fixed with chmod by the owner. If the owner is wrong, root must run chown. Keeping those two jobs apart helps when a change fails.
Try it yourself
Use a practice file, and an account named bob and a group named devs that exist on the machine. Run the three commands in order, and check the result after each change.
Set the mode of report.csv to 640. The owner reads and writes, the group reads, and others get nothing.
chmod 640 report.csvWhat you should see: chmod prints nothing when it works.
Check the mode, the owner, the group and the name.
stat -c '%A %U %G %n' report.csvWhat you should see: The shell prints -rw-r----- alice alice report.csv, if alice owns the file.
Hand the file to bob and the devs group. Ordinary users cannot change owners, so the command starts with sudo.
sudo chown bob:devs report.csvCheck the owner and the group again.
stat -c '%A %U %G %n' report.csvWhat you should see: The shell prints -rw-r----- bob devs report.csv, so the owner is now bob and the group is devs.
Terms
- Owner
- The account that owns a file. The owner may change the file's mode with chmod.
- Recursive option
- A switch that makes a command apply to a whole folder tree, including every file and folder inside it.
Check yourself
Who may use chown, and why does the command start with sudo for most users?
Usually only root may change a file's owner. Ordinary users run chown through sudo, which grants root's rights for that command.
Why should you check a file after running chmod?
chmod prints nothing when it works, so stat is the only way to see the real mode the file has now.
What does the recursive option do?
It changes a whole folder tree at once, so one wrong path can reach many files. Try it on a copy first.
Previous: File modes: read, write and execute. Next: umask: why new files get their rights.