File permissions, ownership and disk quotas
Who may do what to a file: reading an ls -l line, the two ways to express a mode, umask, and the three special bits that change what execution and directory membership mean.
Lesson 4 of 6 in objective 104. Devices, Linux filesystems, filesystem hierarchy standard, part of LPIC-1 Exam 101-500.
Reading and writing a mode
Permissions come in three sets — owner, group, other — each with read, write and execute. In octal, read is 4, write 2 and execute 1, so 640 is rw- r-- ---, and 755 is rwx r-x r-x. In symbolic form you name who and what: chmod u+x adds execute for the owner, chmod go-w removes write from group and other, chmod a=r sets everyone to read-only. Both spellings appear in questions and both must be readable on sight.
On a DIRECTORY the bits mean something different, and this is where marks are lost. Read lets you list the names in it. Write lets you create and delete entries — which means write permission on a directory lets you delete a file you cannot write to. Execute (often called the search bit) lets you traverse it and access things inside by name; a directory with r but not x gives you the names and nothing else.
chown changes the owner and chgrp the group; chown user:group does both at once. Only root may give a file away to another user.
umask, and the special bits
New files are not created with the mode you might expect. The umask REMOVES bits from the maximum, which is 666 for files and 777 for directories, because nothing gets the execute bit merely by being created. With the common umask of 022, a new file is 644 and a new directory 755. A umask of 077 gives 600 and 700 — private by default.
The word usually used for this is "subtracted", and it is worth being careful with, because it only tells the truth when every digit of the umask fits inside the digit above it. The umask clears bits: a mask digit of 2 takes away write if write was there, and takes away nothing if it was not. Where subtraction and bit-clearing disagree, bit-clearing is what happens. Under umask 027 a new file is 640, not the 639 the arithmetic suggests — and 639 is not even a permission, since 9 is not an octal digit. Read each digit as "which of read, write and execute to withhold" and the awkward masks come out right.
Three special bits sit above the usual nine. SUID (4000) on an executable makes it run as its owner rather than as the user who launched it, which is how passwd can write to /etc/shadow. SGID (2000) does the same for the group on an executable, and on a DIRECTORY it means new files inherit the directory's group — the standard trick for a shared project folder. The sticky bit (1000) on a directory means only a file's owner may delete it, which is why /tmp is world-writable without being a free-for-all. In ls -l they appear in place of the x: s for SUID or SGID, t for sticky, and an uppercase S or T when the underlying execute bit is not set.
Worth carrying in
- chmod 640 file
- Octal: read 4, write 2, execute 1, per owner/group/other.
- chmod u+x,go-w
- Symbolic: who (ugoa), operation (+-=), what (rwx).
- chown user:group
- Change owner and group together. Giving a file away needs root.
- umask 022
- Withholds bits from 666 for files and 777 for directories. 022 gives 644 and 755.
- SUID 4000
- Executable runs as its owner. Shows as s in the owner block.
- SGID 2000
- On an executable, runs as its group; on a directory, new files inherit the group.
- sticky 1000
- On a directory, only the owner of a file may remove it.
/tmpis 1777. - ls -l
- Type, then three permission triplets, links, owner, group, size, time, name.
What the exam does with this
- Deleting a file is governed by the DIRECTORY's permissions, not the file's. This is the most-missed idea on the objective.
umaskapplies to 666 for files, not 777 — new files never get execute automatically.- The
umaskCLEARS bits rather than subtracting: 027 gives a file 640, not 639. Only masks whose digits fit inside the digits above them make the arithmetic look right. - SGID on a directory means group inheritance; SGID on a file means run-as-group. Same bit, two meanings.
- Objective
- 104. Devices, Linux filesystems, filesystem hierarchy standard
- Share of the exam
- 25% (the whole objective)
- Questions in this lesson
- 15
- Signed for by a person
- 0
Partly checked. None of the 15 questions here has been read against the cited source by a person. 15 questions have been checked against their cited clause by an automated pass — which is not the same thing, and is not a signature.
Only questions a person has signed for are used in mock exams here. That is the whole difference between the two kinds of checking above.
How these questions are written — where each question comes from, what the verification ledger records, and what happens when one is found wrong.
Drill this lesson
A lesson is one sitting: the trainer draws a short run from these questions alone and spaces the ones you get wrong.
Practise File permissions, ownership and disk quotas
Questions in this lesson
- A script must be readable and executable by everyone but writable only by its owner. Which octal mode does that? machine-checked
- A user's shell has umask 022. With no other mechanism involved, what permissions does a regular file created by that shell's redirection get? machine-checked
- `ls -l /usr/bin/passwd` shows `-rwsr-xr-x 1 root root`. What does the s in the owner's execute position mean? machine-checked
- /tmp is shown by ls -ld as `drwxrwxrwt`. What does the trailing t achieve on this world-writable directory? machine-checked
- Which command sets both the owner and the group of report.txt to alice and staff in a single invocation? machine-checked
- Select the THREE true statements about the special permission bits on Linux. machine-checked
- You want every regular file a shell creates to come out as rw-r----- (640) and every new directory as rwxr-x--- (750). Type the complete command that sets that default. machine-checked
- The file /srv/reports/q3.txt is rw-r----- and owned by alice:staff. Everyone in staff must now be able to edit it, and nothing else about its mode may change. Which command does exactly that? machine-checked
- The directory /srv/incoming is drwxr-xr-x and owned by user bob. Inside it sits report.log, owned by root with mode 644. Bob runs `rm /srv/incoming/report.log` and it succeeds. Why? machine-checked
- A copied web tree under /srv/www has inconsistent modes. Every directory must become traversable and readable by everyone, and every file readable by everyone, but no data file may be made executable. Which single command achieves that? machine-checked
- alice owns notes.txt and wants to hand it over to bob. Her `chown bob notes.txt` fails with 'Operation not permitted'. What is the explanation? machine-checked
- The directory /srv/keys is drwx--x--x and owned by root. It contains deploy.pem with mode 644. User carol has been told the full path of that file. What can carol do? machine-checked
- script.sh must end up as -rwxr-x--- no matter what its current mode is. Select the TWO commands that guarantee that result. machine-checked
- Select the THREE true statements about the file creation mask in a Linux shell. machine-checked
- An application tree unpacked under /srv/app is still owned by the account that extracted it. Type the complete command that makes user deploy and group deploy the owners of /srv/app and of everything beneath it. machine-checked
Practise File permissions, ownership and disk quotas
The rest of objective 104
- Partitions and filesystems
- Keeping filesystems intact
- Mounting and unmounting filesystems
- File permissions, ownership and disk quotas — you are here
- Hard links and symbolic links
- Finding files and where they belong