Linux File Permissions (chmod, octal)
Reading rwx permission strings, setting them with chmod in numeric (octal) and symbolic form, and checking them with ls -l and getfacl.
1Authorization on a Linux file
Authorization is when an entity is granted permission to have a certain type of access to a resource. On Linux, every file carries its own access controls that say which users get which types of access. Each file has an owner (one user account) and a group (a named set of accounts), and everyone else counts as others.
2Reading a permission string
There are three types of access, always in this order:
| Letter | Permission | On a file, it allows |
|---|---|---|
r | Read | Viewing the file's contents |
w | Write | Making changes to the file |
x | Execute | Running the file as a program |
A - means that permission is absent, so read and execute without write shows as r-x. The three sets are written for the owner, then the group, then all other users, with no spaces: rwxrwxrwx.
3Viewing permissions: ls -l and getfacl
The command ls -l shows the current settings for owner, group, and others. A + at the end of the permissions means extra permissions have been set for that file (for specific users or groups), and they can be viewed with getfacl.
$ ls -l -rw-r--r-- 1 drlee nurses 2048 Nov 3 09:12 patients.csv -rwxr-x--- 1 drlee nurses 512 Nov 3 09:12 report.sh -rw-r-----+ 1 drlee nurses 880 Nov 3 09:15 schedule.txt
patients.csvisrw-r--r--: the owner (drlee) can read and change it, the nurses group can read it, and every other user can read it too. For patient data, that lastris a problem.report.shisrwxr-x---: drlee can read, edit, and run it; nurses can read and run it; others have no access.schedule.txtends in+: rungetfacl schedule.txtto see the extra entries.
4chmod, numeric method
To change permissions, use chmod. The numeric method is chmod ### filename: the first digit is the owner, the second the group, the third all others. Each digit is the sum of the permissions granted:
| Digit | Read (4) | Write (2) | Execute (1) | Shown as |
|---|---|---|---|---|
| 0 | --- | |||
| 1 | yes | --x | ||
| 2 | yes | -w- | ||
| 3 | yes | yes | -wx | |
| 4 | yes | r-- | ||
| 5 | yes | yes | r-x | |
| 6 | yes | yes | rw- | |
| 7 | yes | yes | yes | rwx |
| Command | Result | Meaning |
|---|---|---|
chmod 750 test | rwxr-x--- | Owner read, write, execute; group read and execute; others nothing |
chmod 543 test | r-xr---wx | Owner read and execute; group read only; others write and execute |
chmod 777 test | rwxrwxrwx | Everyone can read, write, and execute (almost never a good idea) |
chmod 640 test | rw-r----- | Owner read and write; group read; others nothing |
5chmod, symbolic method
The symbolic method is chmod entity + or - permission filename. It adds or removes permissions without retyping the rest.
| Entities | Operators | Permissions |
|---|---|---|
u user owner · g group · o others · a all | + add · - remove | r read · w write · x execute |
Entities and permissions can be combined in one command. Starting from rw-r-----:
| Command | Before | After |
|---|---|---|
chmod ug+rx testfile | rw-r----- | rwxr-x--- |
chmod o+r testfile | rw-r----- | rw-r--r-- |
chmod go-w testfile | rwxrwxrwx | rwxr-xr-x |
Permissions read owner, group, others, each as r, w, x. In numeric chmod, add r = 4, w = 2, x = 1 for each digit. In symbolic chmod, pick who (u, g, o, a), add or remove (+ or -), and say which permission.
6Worked examples
What can the group do?
A file shows -rw-r-----. What can each entity do, and what is the numeric mode?
- 1Owner
rw-: read and write; 4 + 2 = 6. - 2Group
r--: read only; 4. - 3Others
---: nothing; 0.
Patient data readable by everyone
In the listing above, patients.csv is rw-r--r--. Write a command that keeps drlee's and the nurses' access but removes everyone else's, both ways.
- 1Owner rw = 6, group r = 4, others none = 0.
- 2Numeric:
chmod 640 patients.csv. - 3Symbolic: only others' read must go, so
chmod o-r patients.csv, which turnsrw-r--r--intorw-r-----.
chmod 640 patients.csv or chmod o-r patients.csv; both give rw-r-----.A script for the team
A script backup.sh should be readable, editable, and runnable by its owner; readable and runnable by its group; and readable and runnable by everyone else. What numeric command sets this?
- 1Owner rwx = 4 + 2 + 1 = 7.
- 2Group r-x = 4 + 1 = 5.
- 3Others r-x = 5.
chmod 755 backup.sh, shown as rwxr-xr-x.The private key
A file holding a server's private key is set to chmod 777. Explain the risk and give a better setting.
- 1777 is
rwxrwxrwx: every account on the system can read, change, or run it. - 2Anyone who reads the private key can impersonate the server or decrypt its traffic.
- 3Only the owner needs to read and write it; no one else needs any access.
chmod 600, which gives rw-------.7Common mistakes
It is always owner, group, others, and always read, write, execute. In 750 the 7 is the owner, not the others.
Read is 4, write is 2, execute is 1. Read and write is 6, not 3; write and execute is 3.
It gives every user full control. Grant the least access each entity needs.
A + after the permissions means extra entries exist. Check them with getfacl.
“The file is currently rw-r--r--, so every user on the system can read patient data. Running chmod 640 patients.csv keeps read and write for the owner and read for the nurses group, and removes all access for others.”
Read the current setting, name who has too much access, and give the command with the result it produces.