Permissions and users
Linux is built for many users sharing one machine, so every file records who owns it and what everyone else may do with it. Most "Permission denied" errors you will hit on a server come down to the rules in this chapter.
Owner, group, others
Every file has an owner (a user) and a group. When someone tries to open a file, Linux checks them in order: if you are the owner, the owner permissions apply; otherwise if you are in the file's group, the group permissions apply; otherwise the "others" permissions apply. Only one set is ever used.
Each set is a mix of three permissions:
- r (read): see the contents. For a folder, list what is inside.
- w (write): change the contents. For a folder, create, rename or delete files inside it.
- x (execute): run the file as a program. For a folder, it means you may enter it (
cdinto it) at all.
Reading ls -l
$ ls -l deploy.sh
-rwxr-x--- 1 demee devs 812 Sep 27 10:14 deploy.sh
The first character is the type (- file, d directory, l link). Then come three groups of three: rwx for the owner (demee can do everything), r-x for the group (members of devs can read and run it), and --- for everyone else (no access at all).
r=4, w=2, x=1, which is where numbers like 750 come from.Changing permissions with chmod
The numeric form is the one you will see in tutorials and exams. Each permission has a value: read = 4, write = 2, execute = 1. Add them up for each of owner, group and others, and you get three digits.
$ chmod 755 deploy.sh # 7=rwx owner, 5=r-x group, 5=r-x others
$ chmod 644 notes.txt # 6=rw- owner, 4=r-- group, 4=r-- others
$ chmod 600 ~/.ssh/id_ed25519 # private key: only you can read or write it
$ chmod +x script.sh # just add execute, leave the rest alone
$ chmod -R g+w shared/ # give the group write access, recursively
755 is the usual setting for scripts and folders, 644 for ordinary files, and 600 for secrets. Play with the calculator to see how the digits and letters line up:
Users, groups and ownership
Accounts are listed in /etc/passwd (name, user ID, home folder, login shell); the password hashes are kept separately in /etc/shadow, which only root can read. Groups let you give several people the same access without listing each one.
$ sudo adduser deploy # new user with a home folder
$ sudo usermod -aG sudo deploy # add to the sudo group (-a = append, don't replace)
$ sudo chown deploy:deploy /srv/app # new owner:group
$ sudo chown -R www-data /var/www # recursively
$ id deploy # which groups is deploy in?
$ passwd # change your own password
-a in usermod -aG replaces all the user's groups instead of adding one, which can lock them out of sudo.root, sudo and su
root is the superuser: permissions don't apply to it, so it can read, change or delete anything. Working as root all the time is dangerous because one typo can destroy the system, which is why servers are normally set up with a regular user that borrows root powers only when needed.
sudo command runs one command as root after asking for your own password, and it logs what you did. su username switches your whole shell to another user and needs their password. In practice you use sudo for everything, and on many cloud servers root login is disabled entirely.