Linux File Permissions Explained
Ownership with chown and chgrp
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
chown alice notes.txt # make alice the owner
chown alice:developers notes.txt # owner alice, group developers
Changing the group with chgrp
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
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:
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:
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
ls -l /etc/hostname— who owns it?chown $(whoami) /tmp/copy.txtafter copying a system file — observe what changedgroups— which groups are you a member of?
Advertisement