← Linux and the Command Line Lesson 35 of 51 · 5 XP

chown and chgrp — the owner and the group

Theory

Machine translation, not yet reviewed by a native speaker.

Every file has an owner (user) and a group. The rwx permissions in ls -l refer to them.

ls -l app.log
# -rw-r----- 1 www-data adm 12345 Nov 4 app.log
#              ^owner   ^group

Only the administrator (root, through sudo) can change the owner and the group:

sudo chown kai file.txt              # a new owner
sudo chown kai:dev file.txt          # the owner and the group at once
sudo chown -R www-data:www-data /var/www/site   # recursively — typical for a web server
sudo chgrp dev project/              # only the group

Groups let you give access to several people at once:

groups                     # which groups I'm in
id kai                     # a user's uid, gid and groups
sudo usermod -aG docker kai   # add kai to the docker group (-a means ADD; without -a the groups get replaced!)

After being added to a group, you need to log in again for the change to take effect.

A typical situation: the web server (the user www-data) can't read the website files you uploaded under your own name. The solution is to give the files to it with chown -R www-data:www-data, or to add a shared group. Not to set chmod 777.

In our built-in Linux you're root anyway, so chown works there without sudo.

Sign up for free to mark finished lessons and track your progress.