File System & Permissions — DevOps

On the server web-1, the app's code belongs to the user deploy (files 644, folders 755). The app runs as the user www-data and may only write in storage/…

Uploading an attachment to notes-api still fails with EACCES. While you investigate, you find something worse: the file holding the database password can be read by every user on the server. Neither is a bug in the code. Both are about a file's owner and permissions.

On a server, every file has an owner and permissions, which decide who may read, change and run that file. Picture a restaurant kitchen. The owner writes the recipe book. The cook may read the recipes and use the kitchen, but may not write in the recipe book. Only the manager opens the safe. On web-1, the recipe book is notes-api's code, the kitchen is the storage/ folder, and the safe is /etc/notes-api.env, the file of secrets (private values, such as a database password).

Three players on web-1

User Who it is What it needs
deploy you, and the deploy script (a file of commands) to write the code in /srv/notes-api
www-data the user that runs the app (and later Nginx, a web server) to read the code, to write in storage/
root the server's admin; systemd (the program that starts the app) runs as root too to read the secrets when it starts the app

In the kitchen picture, deploy is the restaurant owner, the app (www-data) the cook, and root the manager. Besides them, a server can have other logins, for example sari, a teammate. The principle: each user gets only the permissions it needs. The app only has to read its code. If the app could write to app.js, an attacker (someone who breaks in on purpose) who got in through the app could plant their own code there.

Reading permissions: three trios, three digits

The basics are in the Permission & Ownership lesson of the Terminal track. In short: a file's permissions are written as 9 letters in three trios (sets of three letters). The order is owner, group, others: the file's owner, the members of the file's group (a named set of users), then every other user. In ls -l, these 9 letters come after the type letter in front (d folder, - file). Each letter's value: r = 4 (read), w = 2 (write), x = 1 (run, or enter for a folder), and - means no permission. The values in one trio add up to one digit:

Number Sum Letters Usually for
755 7 = 4+2+1, 5 = 4+1, 5 = 4+1 rwxr-xr-x folders and scripts
644 6 = 4+2, 4, 4 rw-r--r-- code files
640 6 = 4+2, 4, 0 rw-r----- a config file a group reads
600 6 = 4+2, 0, 0 rw------- secrets

Why new files are 644 and new folders 755

New files and folders always belong to the user who made them. Their permissions come from the umask: a number for the permissions taken away from new files and folders. A new file starts from 666 (no x), a new folder from 777. A umask of 022 takes w away from group and others, so a new file becomes 644 and a new folder 755. On Ubuntu, 022 is root's umask. A regular user with a group of the same name, such as deploy, gets 002: new files are 664 and new folders 775, so the group may write too. The umask command shows yours. On web-1, the ~/.profile of deploy (a file the shell reads when deploy logs in) holds umask 022, so deploy also gets 644 and 755. Yesterday's result, seen with ls -ld (-d: show the folder itself, not what is inside):

deploy@web-1:/srv/notes-api$ ls -ld /etc/notes-api.env app.js deploy.sh storage/attachments
-rw-r--r-- 1 root   root    152 Oct  8 17:20 /etc/notes-api.env
-rw-r--r-- 1 deploy deploy  299 Oct  8 17:35 app.js
-rw-r--r-- 1 deploy deploy  291 Oct  8 17:35 deploy.sh
drwxr-xr-x 2 deploy deploy 4096 Oct  8 17:40 storage/attachments

Fixing it: chown and chmod

These three commands fix the three problems (the text after # is only a note):

cd /srv/notes-api
sudo chown www-data:www-data storage/attachments # the upload folder belongs to the app user
sudo chmod 600 /etc/notes-api.env                # secret: root only
chmod u+x deploy.sh                              # the owner gets x: 644 becomes 744

The result:

deploy@web-1:/srv/notes-api$ ls -ld /etc/notes-api.env app.js deploy.sh storage/attachments
-rw------- 1 root     root      152 Oct  8 17:20 /etc/notes-api.env
-rw-r--r-- 1 deploy   deploy    299 Oct  8 17:35 app.js
-rwxr--r-- 1 deploy   deploy    291 Oct  8 17:35 deploy.sh
drwxr-xr-x 2 www-data www-data 4096 Oct  8 17:40 storage/attachments

Secrets: 600 owned by root

600 owned by root means only root may read the file. The app still gets DATABASE_URL (the database's address and password), because the one that reads the file is systemd, not the app. systemd runs as root, reads the secret file, then starts the app as www-data and gives it the file's contents as environment variables: settings a program receives when it starts. How (EnvironmentFile=) is in the Systemd & Service Management lesson.

Other users are refused now, deploy without sudo included:

sari@web-1:~$ cat /etc/notes-api.env
cat: /etc/notes-api.env: Permission denied

755 becomes rwxr-xr-x

In the exercise, parsePermission(755) must return "rwxr-xr-x". Turn the number into text with String(octal), split it per digit with split(""), then replace each digit with its trio. The example below uses 644:

const TRIO = { 0: "---", 1: "--x", 2: "-w-", 3: "-wx", 4: "r--", 5: "r-x", 6: "rw-", 7: "rwx" };
console.log(String(644).split("")); // ["6", "4", "4"]
console.log(TRIO[6] + TRIO[4] + TRIO[4]); // rw-r--r--

⚠️ Too much permission, or too little

TL;DR: On the server web-1, the app's code belongs to the user deploy (files 644, folders 755). The app runs as the user www-data and may only write in storage/ (sudo chown www-data:www-data storage/attachments). The secret file /etc/notes-api.env (it holds passwords) is 600 and owned by root, the server's admin, and systemd reads it when systemd starts the app. Permissions are written as three trios (sets of three letters): owner, group, others. r = 4 (read), w = 2 (write), x = 1 (run), added up per trio: 755 = rwxr-xr-x (group and others may read and run, not write), 644 = rw-r--r--, 600 = rw-------. The kernel (the core of Linux) uses one trio only, and root skips the check. New files are 644 and new folders 755 because of the umask 022 (the permissions taken away). chown changes the owner, a numeric chmod sets all the permissions at once, chmod u+x changes one part. Never 777.

⚡ Three digits, three trios

Write:

755 = rwxr-xr-x
644 = rw-r--r--
600 = rw-------

Meaning: A file's permissions are 9 letters in three trios (sets of three letters): owner, group (a named set of users), then others (every other user). r = 4 (read), w = 2 (write), x = 1 (run), - = no permission, added up per trio. In 755, group and others may read and run, but not write.

⚡ Upload refused? Check the owner

Write:

sudo chown www-data:www-data storage/attachments
Guess first: the upload folder storage/attachments belongs to the user deploy with 755. Why can't the app, running as the user www-data, save an upload there?

www-data is not the owner and not in its group, so the kernel (the core of Linux) uses the others trio: r-x, no w. What is wrong is the owner, not the number.

Meaning: chown user:group path changes the owner and the group, and always needs sudo (run a command as root, the server's admin). Now www-data uses the owner trio rwx on that folder.

⚡ Secrets: 600 owned by root

Write:

sudo chmod 600 /etc/notes-api.env

Meaning: This file holds secrets (private values), such as DATABASE_URL: the database's address and password. With 600 owned by root, only root may read it. The app still gets DATABASE_URL: systemd (the program that starts the app) runs as root, reads this file, then gives its contents to the app (the user www-data).

⚡ One part only: chmod u+x

Write:

chmod u+x deploy.sh

Meaning: The script deploy.sh (a file of commands) was 644, with no x, so running it was refused. u is the owner, and +x adds the run permission. The owner digit 6 + 1 = 7, so 644 becomes 744, and ./deploy.sh is no longer refused.

What you will learn