Owners and Groups: chown and chgrp

Giving files away needs root

Every file has an owner and a group. You can't give your files to someone else, because then anyone could plant files in other people's names:

ls -l report.txt
chown root report.txt

In the terminal

student@vulntech:~$ ls -l report.txt
-rw-r--r-- 1 student student 10 Oct 17 06:30 report.txt
student@vulntech:~$ chown root report.txt
chown: changing ownership of 'report.txt': Operation not permitted

Changing owners is an administrator's job. sudo runs one command as the administrator, root. It asks for your password, which is student on this machine (nothing shows while you type). The next module covers sudo in detail.

sudo chown root report.txt
ls -l report.txt

In the terminal

student@vulntech:~$ sudo chown root report.txt
[sudo] password for student:
student@vulntech:~$ ls -l report.txt
-rw-r--r-- 1 root student 10 Oct 17 06:31 report.txt

chown and chgrp

CommandChanges
chown alice fileThe owner
chown alice:crew fileOwner and group at once
chown :crew file or chgrp crew fileOnly the group
chown -R alice:crew folderA folder and everything inside it
sudo chgrp crew report.txt
ls -l report.txt
sudo chown -R root:crew project
ls -lR project

In the terminal

student@vulntech:~$ sudo chgrp crew report.txt
[sudo] password for student:
student@vulntech:~$ ls -l report.txt
-rw-r--r-- 1 student crew 10 Oct 17 06:31 report.txt
student@vulntech:~$ sudo chown -R root:crew project
student@vulntech:~$ ls -lR project
project:
total 1
drwxr-xr-x 2 root crew 0 Oct 17 06:31 docs
-rw-r--r-- 1 root crew 0 Oct 17 06:31 plan.txt

project/docs:
total 1
-rw-r--r-- 1 root crew 0 Oct 17 06:31 notes.txt

Common mistake: Be careful with sudo chown -R: run it on the wrong folder, say your whole home directory, and you lose access to your own files.

Remember: sudo chown user:group file changes owner and group, chgrp only the group, -R whole folders.

HOME LEARN COMMUNITY DASHBOARD
Practice terminal Starting…