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.txtIn 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.txtIn 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
| Command | Changes |
|---|---|
chown alice file | The owner |
chown alice:crew file | Owner and group at once |
chown :crew file or chgrp crew file | Only the group |
chown -R alice:crew folder | A folder and everything inside it |
sudo chgrp crew report.txt
ls -l report.txt
sudo chown -R root:crew project
ls -lR projectIn 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 filechanges owner and group,chgrponly the group,-Rwhole folders.