Lesson 2 of 12 - Foundations: the server, the network and Git

Users, permissions and services on a Linux server

By Branko Petric - Updated - About 8 min - Free

The short answer

Every file has an owner, a group and permissions for read, write and execute. Every program runs as a user, and that decides what it can touch. When a service fails, check it with systemctl status, read its logs with journalctl, and fix permissions for one user, never with chmod 777.

Already know this? Skip to the 3 questions

Users and why they matter

Linux is a multi-user system. Every file belongs to a user, and every running program runs as a user. That second part is the one beginners miss.

When nginx serves a page, it usually runs as a user like www-data. When your app writes a file, it writes as whatever user started it. So "Permission denied" almost always means: this user is not allowed to do that to this file.

  • whoami shows your current user.
  • id app shows the user app and its groups.
  • sudo runs one command as root, the user that can do anything. Use it for that command, not as a way of life.

Reading permissions

Run ls -l and look at the first column:

ls -l /etc/app/config.yml
# -rw-r----- 1 app app 512 Nov 28 10:02 /etc/app/config.yml

After the first character (- for a file, d for a directory) come three groups of three: owner, group, others. Each group says read (r), write (w) and execute (x), or - for no.

So rw-r----- means the owner app can read and write, the group app can read, and everyone else gets nothing. In numbers, r=4, w=2, x=1, added up per group. That is 640.

sudo chown app:app /var/app/uploads   # owner and group become app
sudo chmod 750 /var/app/uploads       # owner rwx, group r-x, others none

Why chmod 777 is the wrong fix

777 means read, write and execute for everyone. It makes "Permission denied" disappear, which is why it is tempting at 2am.

The cost: any user or process on the server can now change those files. If your web app is compromised, the attacker can replace scripts that other services run. If a config file holds a database password, every account can read it. And the real question, which user needed access, is never answered.

The fix is two steps. Find the user the program runs as (ps -o user= -p <pid> or the User= line in its service file). Then give that user, and only that user, the access it needs with chown and a tight chmod.

Processes and systemd services

A process is a running program. Each one has a process ID (PID).

ps aux | grep nginx      # which nginx processes run, as which user
top                      # live CPU and memory per process, q to quit
kill 1234                # ask process 1234 to stop

On most servers, long-running programs are managed by systemd as services. systemd starts them at boot, restarts them if they crash, and collects their logs.

systemctl status nginx
# Active: failed (Result: exit-code)
journalctl -u nginx -n 50 --no-pager
# nginx: [emerg] open() "/etc/nginx/ssl/site.key" failed (13: Permission denied)

systemctl status tells you the state and shows the last few log lines. journalctl -u nginx shows the full log for that unit. Add -f to follow it live, like tail -f.

In that example the log says exactly what is wrong: nginx cannot read its key file. You check ls -l on the key, fix the owner or group, and run sudo systemctl restart nginx. Then systemctl status nginx again to confirm it says active (running).

Common beginner mistakes

  • Using chmod 777 to make an error go away. Find the user that needs access and give access to that user only.
  • Restarting before reading the logs. journalctl -u <service> usually tells you the cause in one line. A restart without reading it just fails again.
  • Doing everything as root. Running apps as root turns any small bug into full control of the server. Give each service its own user.

Check your understanding

3 questions. Retry as often as you like - nothing counts against you.

Question 1 of 3

A file shows -rw-r----- with owner app and group app. Who can read it?

Select all that apply - one or more answers can be right.

A file shows -rw-r----- with owner app and group app. Who can read it?

Want the bigger picture? This lesson maps to this step of the DevOps roadmap.