Skip to content

Linux Foundations / Manage a Linux host ยท Step 9 of 50

Lesson: Ownership PermissionsSwitch
โ† Linux Foundations

System administration

Ownership Permissions

Change the owner and group of files with chown and chgrp.

Permissions only make sense together with ownership. Each file has one user owner and one group owner, and the permission bits refer to them.

Seeing who owns a file

$ ls -l report.csv
-rw-r----- 1 maya devs 412 Mar  4 09:00 report.csv
$ stat -c '%U %G' report.csv
maya devs

Here maya is the user owner and devs is the group owner. The stat -c format shown is from GNU coreutils, which most Linux distributions use.

Changing the owner with chown

Giving a file to another user needs root, so you normally use sudo:

$ sudo chown liam report.csv
$ ls -l report.csv
-rw-r----- 1 liam devs 412 Mar  4 09:00 report.csv

An ordinary user cannot give away a file, even one they own.

You can set the owner and group together with a colon:

$ sudo chown maya:finance report.csv
$ ls -l report.csv
-rw-r----- 1 maya finance 412 Mar  4 09:00 report.csv

Writing :finance with nothing before the colon changes only the group.

Changing the group with chgrp

chgrp changes just the group:

$ chgrp devs report.csv

You do not need root for this if you own the file and belong to the new group. Check your groups with id or groups.

Recursive changes

Add -R to change a directory and everything inside it:

$ sudo chown -R www-data:www-data /srv/site

The web server account is often www-data on Debian and Ubuntu. Other distributions use different names, so check which account your server runs as. Check the path carefully before you press Enter, because a recursive chown on a system directory can break your machine and there is no undo.

Changing ownership of a program can also clear its special setuid and setgid bits, so check them afterwards.

Try it

$ id
$ touch mine.txt
$ ls -l mine.txt

Pick another group from the id output, run chgrp with it on mine.txt, and list the file again. Then try chown root mine.txt without sudo and read the error. For a guided exercise, see the permissions in depth lab.

Check your understanding

Ownership Permissions

All 4 sections ยท no timer

Unlimited retries

Questions from every section are combined into one assessment. Answer them all correctly to complete the article.

Your completion saves in this browser. Sign in to sync it to your account.