后端开发者的 Linux
能力决定了排查问题的速度和运维效率 。
常用命令
文件与目录操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 # 导航 pwd cd - cd ~ # 查看 ls -lh ls -lhrt ll # 创建/删除 mkdir -p a/b/c rm -rf dir touch file.txt # 复制/移动 cp -r src/ dst/ cp -a src dst mv old new # 链接 ln -s /real/path link
文件内容查看
1 2 3 4 5 6 7 8 9 cat file less file head -n 20 file tail -n 100 file tail -f app.log tail -F app.log # 统计 wc -l file
查找
1 2 3 4 5 6 7 8 9 10 # 按名字找文件 find . -name "*.go" find . -name "*.log" -mtime +7 -delete find . -type f -size +100M # 按内容找(快) grep -rn "TODO" grep -rn "error" --include="*.go" . # 现代替代(更快更好用) fd "name" rg "pattern"
网络相关
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 # 端口/连接 netstat -tlnp ss -tlnp ss -tnp | grep :8080 # 请求测试 curl -v https://example.com curl -X POST -H "Content-Type: application/json" -d '{"k": "v"}' url curl -o file.zip url wget url # DNS dig example.com nslookup example.com # 连通性 ping host telnet host 8080 nc -zv host 8080
系统信息
1 2 3 4 5 6 7 8 9 10 11 top htop free -h df -hdu -sh *du -sh * | sort -huname -auptime who
文件权限
权限模型
1 2 3 4 5 -rwxr-xr-- 1 user group 1024 Jan 1 12:00 file │└┬┘└┬┘└┬┘ │ 用户 组 其他 │ 文件类型(- 文件,d 目录,l 软链接)
每组三位:r(4)w(2)x(1)
1 2 3 rwx = 4 + 2 + 1 = 7 r-x = 4 + 0 + 1 = 5 r-- = 4 + 0 + 0 = 4
权限命令
1 2 3 4 5 6 7 8 chmod 755 filechmod 644 filechmod +x script.shchmod +R 755 dir /chown user:group filechown -R www:www /var/www
常见场景
1 2 3 4 5 6 7 chmod 600 ~/.ssh/id_rsachmod +x deploy.shchmod 755 /var/wwwchmod 644 /var/www/*
特殊权限
sudo:以 root 执行
umask:新建文件默认权限(022 表示新文件 644、新目录 755)
setuid/setgid:例如 passed 命令
进程管理
查看进程
1 2 3 4 5 6 7 8 9 10 11 12 ps aux ps -ef ps aux | grep nginx pstree -p top htop pidof nginx pgrep -fl "go run"