Linux for Backend Developers

From process and file model to systemd, networking and troubleshooting — the Linux foundation every backend engineer needs.

BeginnerUpdated 2026-10-2848K views

Why Linux knowledge is non-negotiable

Whether your service runs on a bare-metal box, an ECS instance or inside a container, the runtime is Linux. Knowing how the kernel exposes processes, files and network lets you debug a stuck service in minutes instead of guessing.

  • Everything is a file — config, devices, sockets live in the filesystem tree
  • A process tree with parent/child relationships, signals and exit codes
  • Users, groups and permission bits are the first line of isolation
# 最常看的三张「身份证」
uname -a              # 内核版本 / 发行版架构
free -h               # 内存(-h 人类可读)
df -h /               # 磁盘占用
nproc                 # CPU 核数

Processes, signals and jobs

A running program is a process: it has a PID, a parent (PPID), a user, and an exit status. You inspect with ps / top / htop, and you communicate with signals.

# 查找与观察
ps aux | grep nginx          # 找进程(aux = 全部用户 + 详细列)
ps -ef --forest              # 按树形看父子关系
top -p $(pgrep -d, nginx)    # 只看 nginx 的实时占用

# 信号
kill 1234            # SIGTERM:礼貌地请它退出(默认)
kill -9 1234         # SIGKILL:内核直接回收,进程没机会清理
kill -HUP $(pgrep nginx)   # 让 nginx 重新加载配置,不中断连接
SIGTERM vs SIGKILL is the difference between a graceful shutdown (connections drained, buffers flushed) and a hard kill (half-written files, locked mutexes, split-brain clusters). Always prefer TERM and reach for -9 only when the process ignores TERM.
SignalMeaningTypical use
SIGTERM (15) politely ask to exit 默认 kill,应用可做清理
SIGKILL (9)kernel reaps the process卡死进程的最后手段
SIGINT (2)interrupt from terminal (Ctrl+C)前台任务退出
SIGHUP (1)hang up (terminal closed)常被复用为「重载配置」
SIGUSR1/2user-defined自定义触发,如日志切割

Files, permissions and text tools

Permission bits are read/write/execute for owner/group/others. For a directory, execute means "can enter". The daily text-processing trio is grep, awk and sed.

# 权限
chmod 754 app.sh     # rwxr-xr--:属主全权限,组可读+进入,其他只读
chmod +x deploy.sh  # 给所有人加执行权限
chown app:app /var/log/app   # 改属主与属组

# 文本三件套
grep -rn 'KEY' /etc/nginx/            # 递归 + 行号找关键字
awk '{print $1}' access.log | sort | uniq -c | sort -rn | head   # 统计访问 IP Top10
sed -i 's/8080/80/g' app.conf         # 原地批量替换端口
A classic trap: chmod 777 to "make it work". It also makes it the first thing an attacker thanks you for. Fix the real permission (usually group membership + 640/750) instead.
# 找出「谁把磁盘吃光了」
du -sh /var/* | sort -rh | head      # 按大小排序各目录
# 找出被删除但仍被进程占用的文件(空间没释放)
lsof +L1 2>/dev/null | awk '$7 > 100000'

systemd: services that survive reboots

systemd is the init system on essentially every mainstream distro. You write a unit file describing how to start your service, and it handles start order, restart policy, logs and resource limits.

; /etc/systemd/system/app.service
[Unit]
Description=Order API (Go)
After=network-online.target mysql.service
Wants=network-online.target

[Service]
Type=simple
User=app
WorkingDirectory=/opt/app
ExecStart=/opt/app/bin/server -c /opt/app/config.yaml
Restart=always
RestartSec=3
LimitNOFILE=65535
Environment=GIN_MODE=release

[Install]
WantedBy=multi-user.target
systemctl daemon-reload        # 改完 unit 文件必须 reload
systemctl enable --now app    # 开机自启 + 立即启动
systemctl status app          # 状态 / 最近几行日志
journalctl -u app -f          # 实时跟日志(-f = follow)
journalctl -u app --since '1 hour ago'
  • Type=simple: the process stays in the foreground — the modern, recommended style
  • Restart=always + RestartSec=3: crash → wait 3s → relaunch, with backoff handled by StartLimitBurst
  • After= does not wait for readiness, only for start order — use ExecStartPost or healthchecks for real readiness

Network basics and the troubleshooting loop

When a service is unreachable, walk the stack bottom-up: NIC → route → firewall → listening socket → application. Two tools carry most of the load: ss for sockets and curl for HTTP.

# 1. 网卡与路由
ip a                        # 看 IP 地址
ip route                    # 看路由表 / 默认网关

# 2. 端口在不在监听
ss -tlnp | grep 8080        # t=tcp l=监听 n=数字端口 p=进程
# 注意:127.0.0.1:8080 与 0.0.0.0:8080 是两回事!前者外网连不进来

# 3. 防火墙
sudo iptables -L -n | grep 8080
sudo firewall-cmd --list-ports     # CentOS/RHEL/openEuler
sudo ufw status                    # Ubuntu/Debian

# 4. 链路
ping 10.0.0.5
telnet 10.0.0.5 3306        # 或 nc -zv 10.0.0.5 3306
curl -v http://api.example.com/health
If ss shows the port listening on 127.0.0.1 only, no amount of firewall opening will let another machine in. The fix is in the app config (host 0.0.0.0), not in iptables.
ToolAnswers
ping / mtr网络通不通、丢包在哪一跳
ss / netstat谁在监听、哪个进程、监听在哪个地址
iptables / ufw包是被防火墙拦了还是放行了
curl -vHTTP 层通不通、证书、重定向
tcpdump / strace终极手段:抓包 / 系统调用级追踪

Friends & Partners