TutsFx logo

Linux File Permissions Explained

Ownership with chown and chgrp

2 min read Last updated 46 minutes ago

Permissions apply to an owner and a group — so to truly control access you also need to control who owns a file. That is the job of chown and chgrp.

Changing the owner with chown

bash
chown alice notes.txt             # make alice the owner
chown alice:developers notes.txt  # owner alice, group developers

Changing the group with chgrp

bash
chgrp developers notes.txt        # group becomes developers

chown user:group file can do the same thing, so chgrp is optional — use whichever you find clearer.

Changing whole trees

bash
chown -R alice:developers /srv/www   # apply to a directory and everything inside
!

You usually need `sudo`

Only the current owner (or root) can change ownership of a file. If you see Operation not permitted, prefix the command with sudo — provided you are allowed to use sudo:

bash
sudo chown root:root config.yml

Why does this matter?

Consider a web server. The web user needs to read your site files, but you don't want to hand out write permission to the world. The clean solution is a group:

bash
sudo chown -R alice:www-data /var/www/mysite
sudo chmod 750 /var/www/mysite

Now alice owns everything, the www-data group can read and execute, and everyone else gets nothing.

Try it

  1. ls -l /etc/hostname — who owns it?
  2. chown $(whoami) /tmp/copy.txt after copying a system file — observe what changed
  3. groups — which groups are you a member of?
Share:

We value your privacy

We use cookies to enhance your browsing experience, serve personalized ads, and analyze traffic. By clicking "Accept", you consent to our use of cookies. Read our Privacy Policy to learn more.