Lesson 247 of 296
Linux: processes, permissions, logs and SSH
What you will learn
- Why Linux matters for developers
- Navigating files and reading output
- Processes, signals and ports
- Users, groups and file permissions
- Services and logs with systemd
- SSH and keys
- Diagnosing a server problem
On this page
Why Linux?
Almost every server, container and CI runner runs Linux. You need to be comfortable in a terminal: deploying, reading logs, finding what is using a port, fixing permissions.
Files and navigation
| Command | Does |
|---|---|
pwd, ls -la, cd /var/log | Where am I / list including hidden files with details / change directory |
cat, less, head -n 20, tail -n 50 | Show a file / page through it / first / last lines |
tail -f app.log | Follow a file as it grows (live logs) |
cp, mv, rm, mkdir -p, touch | Copy, move or rename, remove, create directories, create empty files |
find /var/www -name "*.log" -mtime +7 | Find files by name and age |
grep -rn "ERROR" /var/log/app/ | Search text in files recursively, with line numbers |
du -sh *, df -h | Size of directories / free disk space |
wc -l file | Count lines |
cmd1 | cmd2 | Pipe: the output of one becomes the input of the next |
>, >>, 2>&1 | Write output to a file / append / send errors to the same place |
A real example (a pipeline counting the most common HTTP status codes in an nginx access log, where the status is the 9th field):
awk '{print $9}' access.log | sort | uniq -c | sort -rn | headProcesses and signals
A process is a running program with a PID (process ID).
| Command | Does |
|---|---|
ps aux, ps aux | grep node | List processes |
top, htop | Live CPU and memory use |
kill PID | Send SIGTERM: ask the process to stop (graceful shutdown, Lesson 193) |
kill -9 PID | Send SIGKILL: force stop immediately; last resort, no clean-up |
ss -tlnp (or lsof -i :3000) | Which process is listening on which port |
free -h, uptime | Memory; load averages |
nohup cmd & | Run in the background (but use a service manager in production) |
"Port 3000 is already in use": ss -tlnp | grep 3000 (or lsof -i :3000) shows the PID; stop that process.
Users, groups and permissions
Every file has an owner, a group and permissions for three classes: user (owner), group, others. Each can have read, write, execute.
-rwxr-x--- 1 deploy www-data 2048 Nov 23 10:00 deploy.sh
│└┬┘└┬┘└┬┘
│ │ │ └── others: no access
│ │ └───── group (www-data): read and execute
│ └──────── owner (deploy): read, write, execute
└────────── "-" file ("d" would be a directory)Numeric form: r = 4, w = 2, x = 1, added per class:
| Mode | Meaning | Typical use |
|---|---|---|
755 | rwx r-x r-x | Scripts and directories everyone may read |
644 | rw- r-- r-- | Normal files |
600 | rw- --- --- | Private keys, .env files |
700 | rwx --- --- | Private directories such as ~/.ssh |
777 | Everyone can do everything | Never in production |
Commands: chmod 600 .env, chmod +x deploy.sh, chown deploy:www-data file, sudo (run as root). Principle of least privilege (Lesson 265): run applications as a dedicated non-root user; never run your Node app as root.
Services and logs (systemd)
Most Linux servers use systemd to run services: start at boot, restart on crash, collect logs.
[Unit]
Description=JobPrep API
After=network.target
[Service]
User=jobprep
WorkingDirectory=/srv/jobprep
EnvironmentFile=/srv/jobprep/.env
ExecStart=/usr/bin/node dist/main.js
Restart=on-failure
KillSignal=SIGTERM
TimeoutStopSec=30
[Install]
WantedBy=multi-user.target| Command | Does |
|---|---|
sudo systemctl start / stop / restart jobprep | Control the service |
sudo systemctl enable jobprep | Start at boot |
systemctl status jobprep | State and recent log lines |
journalctl -u jobprep -f | Follow the service's logs |
journalctl -u jobprep --since "1 hour ago" | Recent logs |
Classic log files live in /var/log (/var/log/nginx/error.log, /var/log/syslog). PM2 is a common Node-specific alternative to systemd; in containers, the platform does this job.
SSH
SSH (Secure Shell) gives you an encrypted terminal on a remote server.
ssh-keygen -t ed25519 -C "you@example.com" creates ~/.ssh/id_ed25519 (private) and .pub (public)
ssh-copy-id deploy@203.0.113.10 installs your public key on the server
ssh deploy@203.0.113.10 log in
scp file.txt deploy@203.0.113.10:/srv/ copy a file- The private key never leaves your machine (
chmod 600); the public key goes in the server's~/.ssh/authorized_keys. - Harden servers: disable password login and root login over SSH (
PasswordAuthentication no,PermitRootLogin noin/etc/ssh/sshd_config), allow SSH only from known IPs or through a bastion or a session manager (AWS Systems Manager Session Manager needs no open SSH port at all), keep the system updated, and use a firewall (ufw allow 22/tcp). ~/.ssh/configstores shortcuts (Host prod,HostName,User,IdentityFile).
Diagnosing "the site is down"
- Is the process running?
systemctl status appordocker ps. - Is it listening?
ss -tlnp | grep 3000;curl -i localhost:3000/healthzfrom the server. - What do the logs say?
journalctl -u app -n 100ordocker logs --tail 100 app. - Resources?
df -h(full disk is a classic),free -h,top. - Network path? nginx error log, firewall and security group rules, DNS (
dig jobprep.example), TLS certificate expiry.
How to say it in an interview
"I am comfortable on Linux servers: navigating and searching files with grep, find and pipes, following logs with tail and journalctl, finding processes and ports with ps and ss, and stopping them with SIGTERM before resorting to SIGKILL. I understand owner, group and other permissions, using 644 for files, 755 for executables and 600 for secrets, and run apps as a non-root user. I run services with systemd or containers so they restart and log properly, and use SSH keys with password and root login disabled. When something breaks I check the process, the port, the logs, disk and memory, then the network path."
Key words
| Word | Meaning |
|---|---|
| Process / PID | A running program / its ID |
| Signal | SIGTERM (polite stop), SIGKILL (force stop) |
| Pipe | Sends one command's output into another |
| Permissions (rwx) | Read, write, execute for owner, group, others |
| chmod / chown | Change permissions / ownership |
| systemd | Linux service manager |
| journalctl | Reads systemd logs |
| SSH key pair | A private key you keep and a public key on servers |
Now prove it
This lesson is complete when you pass the quiz and all three practice levels.