Chapter 04
Permissions, processes and packages
Who can read, write and run what, what is running and how to stop it, and how to install tools and language versions cleanly.
Permission bits
Also called rwx, ls -l
- What
- Each file has read, write and execute permissions for its owner, its group and everyone else, shown by
ls -las, for example,rwxr-xr--. On a folder, execute means you can enter it. - Why
- They decide who can change your files, whether a script can run and whether SSH accepts your private key.
- Example
-rw-r--r--means the owner can read and write, and the group and others can only read.
chmod
Also called 644, 755, 600
- What
- Changes permissions, either symbolically (
u+x,go-w) or numerically, where read is 4, write 2 and execute 1 for each of owner, group and others. - Why
- Makes scripts runnable and keeps private files, such as SSH keys, private.
chmod +x deploy.sh
chmod 644 index.html # rw-r--r--
chmod 755 bin/tool # rwxr-xr-x
chmod 600 ~/.ssh/id_ed25519
chmod -R go-w project/Watch out
Using chmod 777 to make an error go away, which lets anyone change the file.
Ownership and chown
Also called chown, owner, group
- What
- Every file belongs to a user and a group.
chown user:group filechanges them, usually with sudo. - Why
- Files created with sudo end up owned by root, which later causes "permission denied" in your own home.
ls -l
sudo chown -R "$USER":"$USER" ~/code/app
idWatch out
Changing ownership of system folders such as /usr to fix a single tool.
root, sudo, users and groups
Also called root user, superuser, sudo, id, usermod
- What
- root can do anything.
sudoruns one command as root after asking for your password. Users belong to groups, and group membership grants shared access, such as thesudoordockergroup. - Why
- Working as a normal user and using sudo only when needed limits the damage of mistakes and malware.
sudo apt install nginx
id
groups
sudo usermod -aG docker "$USER" # log out and in afterWatch out
Using sudo for npm, pip or Git inside your projects, which leaves root-owned files behind.
Processes
Also called PID, ps, top, htop
- What
- A running program, with a process ID, an owner and a parent.
ps auxlists them andtoporhtopshow live CPU and memory use. - Why
- Stuck programs, high CPU and "port already in use" usually come down to a process you need to find.
ps aux | grep -i node
pgrep -fl python
top # q to quit
htop # install it firstWatch out
Assuming closing a terminal window stopped the server you started in it.
Signals and kill
Also called signal, SIGTERM, SIGKILL, SIGINT, kill -9
- What
- Messages sent to a process. Ctrl+C sends SIGINT,
kill PIDsends SIGTERM (please stop cleanly),kill -9 PIDsends SIGKILL (stop now, no clean-up). - Why
- Programs can catch SIGTERM to save state and close connections; SIGKILL gives them no chance.
kill 4821
kill -9 4821 # only if it ignores SIGTERM
pkill -f "node server.js"Watch out
Reaching for kill -9 first and leaving lock files or half-written data behind.
Jobs: foreground and background
Also called &, fg, bg, Ctrl+Z, nohup
- What
cmd &starts a job in the background, Ctrl+Z pauses the foreground job,bgresumes it in the background,fgbrings it back andjobslists them.- Why
- You can keep using the terminal while something long runs.
Watch out
Background jobs still end when the terminal closes; use tmux or nohup for anything that must survive.
Installing developer tools
Also called virtual environment, pip, npm, venv
- What
- System tools come from the system package manager; project libraries come from language managers such as pip and npm, installed per project.
- Why
- Keeping the layers separate avoids breaking system tools and makes projects reproducible.
sudo apt install python3-venv build-essential
python3 -m venv .venv && source .venv/bin/activate
pip install requests
npm install express # local to the projectWatch out
sudo pip install or sudo npm install -g, which can break the system and leave root-owned files.
Version managers
Also called nvm, pyenv, mise, .nvmrc
- What
- Tools that install several versions of a language in your home folder and switch between them by changing PATH, such as nvm for Node and pyenv for Python.
- Why
- Different projects need different versions, and a
.nvmrcor.python-versionfile records the one a project expects.
nvm install 22
nvm use 22
node -v > .nvmrc
nvm use # reads .nvmrc
pyenv install 3.12
pyenv local 3.12Watch out
Installing the same language through the system package manager and a version manager, then not knowing which runs.
Disk and system checks
Also called df, du, free, uname
- What
df -hshows free space per file system,du -shshows how big folders are,free -hshows memory on Linux anduname -ashows the kernel.- Why
- A full disk or exhausted memory causes baffling failures in builds, databases and Git.
df -h
du -sh * | sort -h
du -sh ~/.cache ~/code/*/node_modules
free -h # Linux and WSL
uptimeWatch out
Deleting files at random instead of measuring what is actually taking the space.