Permissions and processes
Linux is a multi-user system: many users and programs share one machine. Permissions control who can read, change or run each file, and processes are the programs currently running. Both matter the moment you work on a server.
Reading permissions
-rw-r--r-- 1 you staff 65 Nov 19 09:00 notes.txt
│└┬┘└┬┘└┬┘ │ │
│ │ │ │ │ └── group
│ │ │ │ └─────── owner
│ │ │ └── others (everyone else): r-- read only
│ │ └───── group: r-- read only
│ └──────── owner (user): rw- read + write
└────────── type: - file, d directory, l link| Letter | On a file | On a directory |
|---|---|---|
r read |
View its contents | List what's inside |
w write |
Change it | Create or delete files inside |
x execute |
Run it as a program | Enter it with cd |
Changing permissions: chmod
Symbolic mode
The script couldn't run until it was made executable (+x). This is the most common chmod you'll ever type.
| Example | Meaning |
|---|---|
chmod +x file |
Everyone may execute |
chmod u+x file |
Only the owner (user) may execute |
chmod g-w file |
Remove write permission from the group |
chmod o-r file |
Others may not read |
Numeric (octal) mode
Each permission has a value: r = 4, w = 2, x = 1. Add them up for each of user, group and others:
| Number | Permissions | Typical use |
|---|---|---|
755 |
rwx r-x r-x | Scripts, programs, directories |
644 |
rw- r-- r-- | Normal files |
600 |
rw- --- --- | Private files: SSH keys, secrets |
700 |
rwx --- --- | Private directories |
⚠️ Never
chmod 777(everyone can do everything) to "fix" a permission error. It's a security hole. Find out who needs access and grant just that. SSH refuses to use private keys that aren't600.
Users, root and sudo
- Each file has an owner and a group (change them with
chown user:group file). - root is the all-powerful administrator account.
sudo commandruns a single command as root, after asking for your password. It's needed for installing software (sudo apt install …) and editing system files.
Use sudo sparingly, and never just because "it didn't work". Understand why first.
Processes
Every running program is a process, with a unique PID (process ID).
| Command | Shows |
|---|---|
ps |
Your processes in this terminal |
ps aux |
All processes on the system, with their user, CPU and memory |
top / htop |
A live, updating view (press q to quit) |
pgrep node |
PIDs of processes matching a name |
Stopping processes
- Ctrl + C stops the program running in the foreground of your terminal.
kill PIDpolitely asks a process to stop (the SIGTERM signal).kill -9 PIDforces it to die immediately (SIGKILL). This is a last resort: the process can't clean up.pkill nodekills processes by name.- You can only kill your own processes unless you use
sudo.
Background jobs (real terminals)
python server.py & # & runs it in the background
jobs # list background jobs
fg %1 # bring job 1 back to the foreground
# Ctrl + Z suspends the foreground job; `bg` resumes it in the backgroundFor servers that must keep running after you log out, use a process manager: systemd services, pm2 for Node, Docker containers, or tmux/screen sessions.
"Port already in use"
A classic developer problem: you start a server and get EADDRINUSE: port 3000. Something is already running on that port:
lsof -i :3000 # which process is using port 3000?
kill <PID> # stop itPractice
- Make
projects/scripts/hello.shexecutable and run it with two names as arguments. - Create
~/.ssh-key-demoand give it permissions so only you can read and write it. Check withls -l. - Find the PID of the
python worker.pyprocess and stop it. Confirm it's gone.
1.What permissions does chmod 644 notes.txt give?
2.A script fails with "Permission denied" when you run ./deploy.sh. What's the usual fix?