Linux › Section B · Basic commands and the directory hierarchy › Topic B.11
File modes and permissions
Every file on Linux records who owns it and who may read, change or run it. Reading and setting these permissions, in letters or in octal, is one of the first real administration skills, and it explains a large share of the errors you will meet.
Reading a file mode
Run ls -l and each line begins with a ten-character mode such as -rw-r--r--, followed by the owner and the group. The first character is the file type: - for a regular file, d for a directory, l for a symbolic link, and c, b, p or s for the special files that Device files describes. The other nine characters are three sets of three permissions: for the owning user (u), the file's group (g) and all others (o). A dash means that permission is not granted.
Read, write and execute
For a regular file, r lets you read its contents, w lets you change them, and x lets you run it as a program. For a directory the meanings shift. r lets you list the names inside; w lets you create, delete and rename entries; x, often called search permission, lets you enter the directory and reach the files in it by name. Directories normally need r and x together. Deleting a file depends on write permission on its directory, not on the file. The kernel checks the classes in order and uses only the first that matches: if you own the file, the user bits apply even if the group bits are more generous.
Octal notation
Each set of three bits can be written as one octal digit by adding 4 for r, 2 for w and 1 for x. So rwx is 7, rw- is 6, r-x is 5, r-- is 4 and --- is 0, and a whole mode becomes three digits. 644 (rw-r--r--) suits ordinary documents. 600 (rw-------) suits private files such as SSH keys. 755 (rwxr-xr-x) suits programs and shared directories. 700 (rwx------) keeps a directory private. 640 (rw-r-----) lets the group read but hides the file from everyone else. An octal mode replaces all nine bits in one go.
Changing modes with chmod
chmod takes either an octal mode, as in chmod 640 report.txt, or a symbolic change. A symbolic change names who (u, g, o, or a for all three), an operator (+ adds, - removes, = sets exactly) and the permissions. chmod u+x deploy.sh makes a script runnable by its owner, and chmod go-w notes.txt removes write access for group and others. Separate several changes with commas, as in chmod u+x,g-w file. chmod -R applies a change to a whole directory tree, so check the command before pressing Enter. Honu Learning's chmod calculator converts between the letter and octal forms.
Worked example: two ways to set a mode
A script build.sh is currently 644 (rw-r--r--). You want the owner to read, write and run it, and nobody else to have any access. 1. Work out the target: owner rwx is 4 + 2 + 1 = 7, group none is 0, others none is 0, so the target is 700. 2. Absolute form: chmod 700 build.sh. 3. Symbolic form from the current mode: chmod u+x,go= build.sh. Adding x to rw- gives rwx, and go= clears both of the other sets. 4. Both give -rwx------, which ls -l confirms. 5. The symbolic form changes only what you name, which is safer when the starting mode is unknown.
Owners and groups
chown changes a file's owner and, optionally, its group: chown ana report.txt, or chown ana:developers report.txt to set both. chgrp developers report.txt changes only the group. Both accept -R for directory trees. Only root can give a file to a different owner. An ordinary owner can change the group, but only to a group they belong to. The groups command lists your groups. A new group membership takes effect only at your next login.
The umask
New files do not start with arbitrary permissions. A program asks for a mode, usually 666 for files and 777 for directories, and the kernel removes every bit that is set in your umask. With the common umask 022, which removes write for group and others, new files get 644 and directories 755. With 077 they get 600 and 700, private to you. With 002, often used for shared group work, files get 664 and directories 775. umask alone shows the current value; umask 077 changes it for that shell, and a startup file makes it permanent. The umask removes bits; it never adds them.
Setuid, setgid and the sticky bit
Three special bits form a leading fourth octal digit: 4 for setuid, 2 for setgid and 1 for sticky. A setuid program runs with its owner's privileges: /usr/bin/passwd is -rwsr-xr-x and owned by root, so it can update the root-only password files. Setgid on a program runs it with the file's group; on a directory, it makes new files inherit the directory's group. The sticky bit on a directory lets only a file's owner, the directory's owner or root delete or rename files there. That is why /tmp is drwxrwxrwt (1777): anyone can create files, but not delete other people's. A capital S or T means the bit is set without execute.
Worked example: a shared project directory
The developers group needs a directory, /srv/web, where every member can edit every file and outsiders see nothing. 1. Give it to the group: sudo chgrp developers /srv/web. 2. Set the mode: sudo chmod 2770 /srv/web. The leading 2 is setgid, so new files belong to developers rather than to each creator's own group; 770 gives owner and group full access and others none. 3. ls -ld /srv/web now shows drwxrws---. 4. Each member sets umask 002, so a new file is 666 minus 002, which is 664, rw-rw-r--, group-writable. 5. Others still cannot reach that file, because the directory denies them x.
ACLs and symbolic links
Standard modes allow one owner and one group. Access control lists (ACLs) add entries for extra users and groups: setfacl -m u:ben:rw plan.txt lets Ben read and write the file, getfacl plan.txt prints every entry, and setfacl -x u:ben plan.txt removes his entry. A + after the mode in ls -l, as in -rw-rw-r--+, signals an ACL. A symbolic link is a small file that points to another path, made with ln -s target linkname; ls -l shows it as lrwxrwxrwx linkname -> target. On Linux the link's own permissions are ignored and the target's apply. If the target is moved or deleted, the link dangles and accessing it reports No such file or directory.
Common mistakes to avoid
First, removing x from a directory with an absolute mode such as 644, which stops anyone entering it; directories normally need 755 or 700. Second, using chmod -R 777 to make an error go away, which lets every user change or replace the files: find the real cause instead. Third, forgetting that a file owner gets only the user bits, even when the group bits are broader. Fourth, expecting a new group membership to work before logging in again. Fifth, treating the umask as permissions to add rather than bits to remove. Sixth, reversing the arguments of ln -s: the target comes first.
Key takeaways
ls -l shows the type and three permission sets; only the first matching class applies. For directories, r lists, w changes entries and x enters. Octal digits add 4, 2 and 1: 644, 600, 755 and 700 are common. chmod sets modes in octal or symbolically; chown and chgrp change ownership. The umask removes bits from 666 or 777: 022 gives 644 and 755. Setuid (4), setgid (2) and sticky (1) form a leading digit, as in 2770 and /tmp's 1777. setfacl and getfacl manage ACLs; ln -s makes symbolic links.
Flashcards
Flashcard
Regular file: owner reads and writes, group and others read
Test your understanding
Question 1 of 5
What permissions does chmod 750 give a directory?
Question 2 of 5
With umask 027, what mode does a new regular file get when the program requests 666?
Question 3 of 5
You own notes.txt, its mode is ---rw-r--, and you belong to its group. Can you read it?
Question 4 of 5
Why is /tmp normally mode 1777 (drwxrwxrwt)?
Question 5 of 5
A team directory should give new files the team's group automatically. Which command sets this up?
