Based on the Linux Master 2 exam syllabusReport an error
ls -l.chown changes the owner, and chgrp changes the group. You can also change both at once in the form chown user:group. -rw-r--r-- 1 alice dev 1024 Sep 3 09:00 report.txt
└─┬─┘ └┬┘
│ └── owning group (group)
└──────── owner (owner)ls -l just looks them up in /etc/passwd and /etc/group and shows them as names. That is why files left behind after deleting an account show the number as-is in the owner slot.$ id
uid=1001(alice) gid=1001(alice) groups=1001(alice),1500(dev)
$ touch a.txt
$ ls -l a.txt
-rw-r--r--. 1 alice alice 0 Sep 3 09:00 a.txtdev group, the new file's group is her primary group, alice. A secondary group only grants "eligibility to access"; it doesn't decide the group of a new file.| Command | What it does |
|---|---|
chown alice file | Set the owner to alice |
chown alice:dev file | Owner and group together |
chown :dev file | Group only (write only after the colon) |
chgrp dev file | Set the group to dev |
chown -R alice:dev /data | Recursively through subdirectories |
sudo chown alice report.txt # change the owner
sudo chown alice:dev report.txt # owner + group
sudo chgrp dev report.txt # group only
sudo chown -R www-data:www-data /var/www| Usage | Owner | Group |
|---|---|---|
chown alice file | to alice | unchanged |
chown alice:dev file | to alice | to dev |
chown :dev file | unchanged | to dev |
chgrp dev file | unchanged | to dev (same result as above) |
$ ls -l report.txt
-rw-r--r--. 1 root root 1024 Sep 3 09:00 report.txt
$ sudo chown alice report.txt
$ ls -l report.txt
-rw-r--r--. 1 alice root 1024 Sep 3 09:00 report.txt
$ sudo chown :dev report.txt
$ ls -l report.txt
-rw-r--r--. 1 alice dev 1024 Sep 3 09:00 report.txtchown changes only the owner and group. The permissions rw-r--r-- and the modification time stay the same. To adjust permissions too, you have to use chmod separately.chown 1001:1500 file.chmod, recursion is uppercase -R. It is the same for all three: chown, chgrp, and chmod.| Option | Meaning (common to chown and chgrp) |
|---|---|
-R | Apply to everything under the directory |
-v | Print the result for each file processed |
-c | Print only what actually changed |
If a regular user tries it, it gets blocked like this.
$ chown bob a.txt
chown: changing ownership of 'a.txt': Operation not permitted
$ chgrp dev a.txt ← allowed because alice belongs to dev
$ chgrp wheel a.txt ← not allowed for a group she doesn't belong to
chgrp: changing group of 'a.txt': Operation not permittedOnly root can change the owner. If a regular user could hand ownership of their own file to someone else, it would become possible to dodge disk quotas or plant files under someone else's name. On the other hand, a group change is something the owner can also do, but only when switching to a group they belong to.
| Action | Who can do it |
|---|---|
Change owner (chown) | Only root |
Change group (chgrp) | root, or the owner switching to a group they belong to |
Change permissions (chmod) | root, or the file's owner |
Why are new files made with 644 every time? It is because umask trims the permissions from a fixed base value.
umask # 0022 ← check the current value
umask 077 # change the value (applies only to the current shell)
umask -S # u=rwx,g=rx,o=rx ← view in symbolic formSubtract umask from the base value. But the base value differs for files and directories.
| Target | Base value | Reason |
|---|---|---|
| File | 666 (rw-rw-rw-) | To not give execute permission to new files |
| Directory | 777 (rwxrwxrwx) | Because a directory needs x to be entered |
With umask 022:
file 666 - 022 = 644 (rw-r--r--)
directory 777 - 022 = 755 (rwxr-xr-x)With umask 077:
file 666 - 077 = 600 (rw-------)
directory 777 - 077 = 700 (rwx------)| umask | File | Directory | Character |
|---|---|---|---|
| 022 | 644 | 755 | Regular user default |
| 002 | 664 | 775 | For group collaboration |
| 077 | 600 | 700 | Personal use only, strictest |
| 000 | 666 | 777 | No restriction — dangerous |
No matter how you set umask to 0, files never get execute permission. That is because the base value is 666, so the x slot is empty from the start. This comes up as a trick question asking "if umask is 000, does the file become 777?" The answer is 666.
"Subtract from the base value" works out for values like 022 and 077, but what actually happens is the permissions turned on in umask are switched off in the base value. Permissions that weren't there to begin with don't change when switched off. This difference shows up when odd digits are in the umask.
With umask 033, simply subtracting for a file gives 666 − 033 = 633 (rw--wx-wx), but the actual result is different.
base (file) 6 6 6 rw- rw- rw-
umask 0 3 3 --- -wx -wx ← switch off what's on here (w, x)
─────────────────────────────────
result 6 4 4 rw- r-- r-- ← there was no x originally, so only w is switched off| umask | File | Directory | What to note |
|---|---|---|---|
| 033 | 644 | 744 | The file is not 633 |
| 027 | 640 | 750 | Blocks only write for the group, and blocks everything for others |
| 007 | 660 | 770 | Blocks everything for others only |
| 011 | 666 | 766 | A file has no x to switch off, so it stays 666 |
x from that result. For 027, the directory is 750 (rwxr-x---), and the file is 640 (rw-r-----) with the x taken off.$ umask
0022
$ umask -S
u=rwx,g=rx,o=rx
$ umask 027
$ touch new.txt; mkdir newdir
$ ls -ld new.txt newdir
-rw-r-----. 1 user user 0 Sep 3 09:00 new.txt
drwxr-x---. 2 user user 6 Sep 3 09:00 newdirumask shows four digits (0022), the leading 0 is the slot for special permissions. 022 and 0022 are the same value.umask shows what to "block," and umask -S shows what "remains." 022 and u=rwx,g=rx,o=rx are the same setting written from opposite directions. The result of -S has the same shape as the directory permission (755).chmod.chmod 777 after creating, it becomes 777 regardless of umask.| File | Scope |
|---|---|
/etc/profile, /etc/bashrc | System-wide |
~/.bashrc, ~/.bash_profile | Per user |
| Where it's set | Who it applies to | How long it lasts |
|---|---|---|
umask 027 on the command line | That shell and programs run from it | Gone when the shell is closed |
umask 027 in ~/.bashrc | That user | Applied every time a new shell opens |
/etc/profile · /etc/bashrc | All users | Persists (only root can edit) |
For several people to use the same directory, put them in the same group and open up the group permissions.
sudo groupadd dev # create the group
sudo usermod -aG dev alice # add a user (with -aG!)
sudo usermod -aG dev bob
sudo chgrp dev /project # set the directory's group
sudo chmod 770 /project # allow everything up to the group, block otherschmod 770 is rwxrwx---. The owner and group can do everything, and other users are blocked from access altogether.Let's follow what actually happens.
$ ls -ld /project
drwxrwx---. 2 root dev 6 Sep 3 09:00 /project
$ id -gn ← alice's primary group
alice
$ touch /project/plan.txt
$ ls -l /project/plan.txt
-rw-r--r--. 1 alice alice 0 Sep 3 09:05 /project/plan.txt| Stumbling block | Cause | How to solve it |
|---|---|---|
The file's group is alice, not dev | A new file follows the creator's primary group | chgrp dev file — you have to do it every time (tomorrow's SetGID automates it) |
The group permission is r-- so bob can't edit | umask 022 masked the group's w | chmod g+w file, or set umask to 002 |
| A user just added to the group still can't access | The group list is decided at login | Log out and log in again |
| Tool | What it decides | When it acts |
|---|---|---|
chown / chgrp | Whose file it is | On files that already exist |
chmod | What they can do | On files that already exist |
umask | With what permissions a new file is born | On files you create from now on |
Tomorrow we cover the special permissions that sit outside the three regular slots — SetUID, SetGID, and the sticky bit.
ls -l.ls -l.chown : The command that changes the owner. Only root can run it.chgrp : The command that changes the owning group./etc/passwd.Click a choice to reveal the answer and explanation.
Question 1
Which of the following commands changes a file's owner and owning group at once?
Question 2
When the umask value is 022, which of the following is the permission of a newly created regular file?
Question 3
Which of the following has the authority to change a file's owner to another user?
Question 4
When the umask value is 077, which of the following is the permission of a newly created directory?
Question 5
Which of the following commands checks the umask value set in the current shell?