Linux File Permissions Explained Simply
You type ./backup.sh to run a script, a text file of commands a colleague sent you, and the terminal replies "Permission denied", though the file is right there. This lesson explains Linux file permissions: who they apply to, how to read them, what read, write and execute mean, and how to change them safely.
Owner, group and others
Every file and folder records two names. Its owner is one user account, usually whoever created it. Its group is a named set of users who share access, such as a team; lesson 7 shows how both are made. Everyone else counts as others.
A permission is an on-off switch for one kind of access: read, write or execute. Each of the three audiences has its own three switches, so every file carries nine.
Reading the permission string in ls -l
In the long listing, ls -l (lesson 4), each line starts with a permission string. The diagram splits one line into seven labelled parts.
The first character is the type: - for an ordinary file, d for a directory and l for a symbolic link (lesson 5). Then come three sets of three letters, for the owner, the group and others. Each slot shows its letter when on and a dash when off, always in the order r, w, x.
So anita, the owner, may read, change and run the script (rwx). Members of the group team may read and run it (r-x). Everyone else may only read it (r--).
The grey parts are the link count (for a file, usually 1), the size in bytes and the date of the last change. Some systems add a character after the nine letters, such as +, . or @ on a Mac, to flag extra settings; ignore it for now.
Read, write and execute
On a folder the letters shift meaning, and execute becomes the right to go in.
| Letter | On a file | On a folder |
|---|---|---|
r read | open it and read its contents | list the names inside |
w write | change its contents | create, delete or rename files inside (needs x too) |
x execute | run it as a program | enter it with cd and reach the files inside |
Two surprises follow. Deleting a file changes its folder's list of names, so it needs the folder's w, not the file's. And with r but no x on a folder, you can list the names but not open the files.
Which set applies to you
When you use a file, the kernel, the core of the operating system (lesson 2), picks one set of letters and checks only that. In the flowchart, notice that the questions stop at the first yes.
So on a file marked -r--rw-rw-, everyone may change it except its owner. The owner can fix that, since an owner may always change permissions. One account skips most of these checks: root, the administrator (lesson 7).
Changing permissions with chmod
chmod (change mode; the mode is the set of nine switches) changes permissions. Only the file's owner or root may use it on a file, in one of two styles.
Symbolic: who, plus or minus, which letters
Name who, then + to add, - to remove or = to set exactly, then the letters. Who is u (user, meaning the owner), g (group), o (others) or a (all three). So chmod u+x script.sh lets the owner run it, and chmod go-w notes.txt stops the group and others changing it.
Numeric: add up 4, 2 and 1
Each letter has a value: r is 4, w is 2 and x is 1. Add the values that are on in one set to get a digit from 0 to 7; three digits, for owner, group and others, set all nine switches. The table builds the common digits.
So chmod 644 notes.txt gives rw-r--r--: the owner reads and writes, everyone else only reads, the usual setting for documents. chmod 755 gives rwxr-xr-x, usual for programs and folders. Unlike the symbolic style, a number replaces all nine switches at once.
Changing the owner: chown and chgrp
chown (change owner) hands a file to another user and can set its group too: chown anita:team report.txt. Only root may change an owner, so you type sudo in front, which runs one command as root (lesson 7).
chgrp (change group) changes just the group, as in chgrp team report.txt. You may do that without root on a file you own, but only to a group you belong to. Both take -R for a whole folder, so check the path first.
Two common problems
"Permission denied" when you run a script
New files normally start without any x, so ./script.sh (run script.sh from this folder) is refused. Fix it with chmod u+x script.sh; lesson 16 comes back to this. bash script.sh also works, since bash then only needs to read the file.
Why chmod 777 is a bad idea
Forum answers sometimes suggest chmod 777, which is rwxrwxrwx: everything on for everyone. Then any account, including a program an attacker has taken over, may change the file; in a script, they could add commands that run as you.
Instead, grant only the missing letter, only to those who need it. This is called least privilege.
Check yourself
A file shows -rw-r----- with owner anita and group team. Ben is in team and Chen is not. What may each do?
Anita reads and changes it, Ben only reads it, and Chen can do nothing. Each gets exactly one set: the owner's rw-, the group's r-- or the others' ---.
You own plan.txt, shown as -r--rw-rw-. The group and others can edit it. Why can't you?
As the owner you get only the owner's set, r--; the kernel never falls back to the group's. As owner, you can fix it with chmod u+w plan.txt.
./deploy.sh says "Permission denied". Why fix it with chmod u+x rather than chmod 777?
It adds only the missing switch, execute for you. 777 would also let every account change the script, so anyone could add commands that then run as you.