← All lessons
Files and permissions · Lesson 5 of 7

Owners, groups and sudo

Every file has one owner and one group. Every user belongs to one or more groups, which is how a team shares files without opening them to everyone: put the files in the team's group and give the group the permissions.

id shows who you are and which groups you're in. groups lists just the groups.

$ id
uid=1000(sam) gid=1000(sam) groups=1000(sam),27(sudo),1001(staff)

root is the administrator. It can read and change anything, which is why people don't log in as root day to day: one typo could wreck the system. Instead you run single commands as root with sudo, which asks for your own password and checks that you're allowed (usually by being in the sudo or wheel group).

$ cat /etc/shadow
cat: /etc/shadow: Permission denied
$ sudo cat /etc/shadow
[sudo] password for sam:

chown changes the owner, and only root can give a file away, so it's usually run with sudo: sudo chown dana report.txt. chown dana:staff report.txt sets the owner and group together, chgrp staff report.txt sets just the group, and -R does a whole folder.

A good habit: when something says Permission denied, don't reach for sudo straight away. Check with ls -l who owns it and what's allowed. Using sudo where it isn't needed leaves root-owned files in your home folder that you then can't change.

Practice

Which command lists the groups you belong to?

More info

Quick reference

id, groupsWho you are and your groups.
rootThe administrator: can do anything.
sudo cmdRun one command as root.
chown user fileChange the owner (needs root).
chown user:group fileOwner and group together.
chgrp group fileChange the group.

The other Linux courses