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

Who may change rights, owner and group

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

Set the mode to 640: the owner reads and writes, the group reads, others get nothing
chmod 640 report.csv
Show the mode, the owner, the group and the name
stat -c '%A %U %G %n' report.csv

Note: chmod is quiet when it works. Always check the result with stat, so you see the real mode.

Handing a file to a team

Make bob the owner and devs the group, in one step
sudo chown bob:devs report.csv

The 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.

  1. Set the mode of report.csv to 640. The owner reads and writes, the group reads, and others get nothing.

    chmod 640 report.csv

    What you should see: chmod prints nothing when it works.

  2. Check the mode, the owner, the group and the name.

    stat -c '%A %U %G %n' report.csv

    What you should see: The shell prints -rw-r----- alice alice report.csv, if alice owns the file.

  3. 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.csv
  4. Check the owner and the group again.

    stat -c '%A %U %G %n' report.csv

    What 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.