Linux for Backend Developers
From process and file model to systemd, networking and troubleshooting — the Linux foundation every backend engineer needs.
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 重新加载配置,不中断连接
| Signal | Meaning | Typical 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/2 | user-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 # 原地批量替换端口
# 找出「谁把磁盘吃光了」
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
| Tool | Answers |
|---|---|
| ping / mtr | 网络通不通、丢包在哪一跳 |
| ss / netstat | 谁在监听、哪个进程、监听在哪个地址 |
| iptables / ufw | 包是被防火墙拦了还是放行了 |
| curl -v | HTTP 层通不通、证书、重定向 |
| tcpdump / strace | 终极手段:抓包 / 系统调用级追踪 |
为什么后端必须懂 Linux
无论服务跑在物理机、云服务器还是容器里,底层都是 Linux。理解内核如何暴露进程、文件与网络,你就能在几分钟内定位一个假死的服务,而不是靠猜。
- 一切皆文件——配置、设备、套接字都在文件系统树里
- 进程是树状的,有父子关系、信号与退出码
- 用户、组和权限位是最基本的隔离手段
# 最常看的三张「身份证」
uname -a # 内核版本 / 发行版架构
free -h # 内存(-h 人类可读)
df -h / # 磁盘占用
nproc # CPU 核数
进程、信号与作业控制
一个运行中的程序就是一个进程:有 PID、父进程 PPID、属主和退出状态。用 ps / top / htop 查看,用信号与之通信。
# 查找与观察
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 (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/2 | user-defined | 自定义触发,如日志切割 |
文件、权限与文本处理
权限位是属主/属组/其他的 读/写/执行 三组九位。对目录而言,「执行」代表「能否进入」。日常文本三件套是 grep、awk、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 # 原地批量替换端口
# 找出「谁把磁盘吃光了」
du -sh /var/* | sort -rh | head # 按大小排序各目录
# 找出被删除但仍被进程占用的文件(空间没释放)
lsof +L1 2>/dev/null | awk '$7 > 100000'
systemd:让服务开机自启、挂了自动拉起
主流发行版几乎全用 systemd 做 init。写一个 unit 文件描述如何启动服务,它就负责启动顺序、重启策略、日志与资源限制。
; /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:进程常驻前台,这是现代推荐做法
- Restart=always + RestartSec=3:崩溃 → 等 3 秒 → 重启,频率上限由 StartLimitBurst 控制
- After= 只管启动顺序、不等服务就绪——真要等就绪用 ExecStartPost 或健康检查
网络基础与排障循环
服务不通时,自底 upward 排查:网卡 → 路由 → 防火墙 → 监听套接字 → 应用。两件兵器最常用:ss 看套接字、curl 看 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
| 工具 | 回答什么 |
|---|---|
| ping / mtr | 网络通不通、丢包在哪一跳 |
| ss / netstat | 谁在监听、哪个进程、监听在哪个地址 |
| iptables / ufw | 包是被防火墙拦了还是放行了 |
| curl -v | HTTP 层通不通、证书、重定向 |
| tcpdump / strace | 终极手段:抓包 / 系统调用级追踪 |