常用Linux命令总结 作为后端开发者,Linux服务器是日常工作中打交道最多的环境。熟练掌握常用命令能提升工作效率,在排查问题时更加得心应手。
文件操作基础 创建、复制、移动、删除 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 touch app.logcp config.example.json config.jsoncp -r /data/backup /data/backup_newmv old_name.txt new_name.txtrm app.logrm -f temp.txtrm -rf old_project/
注意:rm -rf 加上根目录时很危险,操作前确认好路径。
软链接和硬链接 1 2 3 4 5 ln -s /usr/local/python3.9/bin/python pythonln file.txt file_link.txt
目录操作 1 2 3 4 5 6 7 8 9 10 11 12 pwd cd /var/log cd .. cd ~ cd - mkdir project mkdir -p /path/to/nested/dir ls ls -l ls -la ls -lh ls -lt
文件内容查看 1 2 3 4 5 6 7 8 9 10 11 12 13 14 cat app.log head -n 20 app.log tail app.log tail -n 50 app.log tail -f app.log grep "ERROR" app.log grep -n -i "error" app.log grep -rn "password" ./config/ less app.log wc -l app.log
文件搜索 1 2 3 4 5 6 7 8 find / -name "nginx.conf" find . -name "*.log" find . -type d -name "node_modules" find . -size +100M find . -mtime -7 locate nginx.conf which python
权限管理 1 2 3 4 5 6 7 8 9 10 ls -l app.py chmod 755 script.sh chmod 644 app.pychmod +x start.shchmod -R 755 dir /chown user app.py chown user:group app.pychown -R user:group dir /
数值
权限
777
rwxrwxrwx
755
rwxr-xr-x
644
rw-r–r–
600
rw——-
用户和用户组 1 2 3 4 5 6 7 whoami who su - newuser sudo apt update useradd -m newuser passwd newuser usermod -aG docker username
进程管理 1 2 3 4 5 6 7 ps aux ps aux | grep nginx top / htop kill 1234 kill -9 1234 pkill nginx nohup ./script.sh &
网络命令 1 2 3 4 5 6 7 ping google.com ifconfig / ip addr netstat -tulnp / ss -tulnp curl https://api.example.com wget https://example.com/file.zip ssh user@server.com scp file.txt user@server:/path/
系统管理 日期时间 1 2 3 4 date date -s "2026-04-28 10:00:00" sudo ntpdate -u pool.ntp.org sudo hwclock -w
重启关机 1 2 sudo reboot sudo shutdown -h now
安装更新 1 2 3 4 5 6 7 8 9 10 11 sudo apt updatesudo apt install <package>sudo yum updatesudo yum install <package>sudo dnf updatesudo dnf install <package>
防火墙 ufw(Ubuntu) 1 2 3 4 5 6 sudo ufw status sudo ufw allow 80/tcp sudo ufw deny 3306/tcp sudo ufw delete allow 80/tcp sudo ufw enable sudo ufw disable
firewalld(CentOS/RHEL) 1 2 3 4 5 6 7 8 9 10 11 12 systemctl status firewalld.service systemctl list-unit-files|grep firewalld.service firewall-cmd --list-all firewall-cmd --query-port=8080/tcp firewall-cmd --permanent --add-port=8080/tcp firewall-cmd --permanent --remove-port=8080/tcp firewall-cmd --reload sudo systemctl stop firewalld sudo systemctl start firewalld sudo systemctl restart firewalld
压缩和解压 1 2 3 4 5 6 7 8 9 10 11 12 tar -cvf archive.tar dir / tar -xvf archive.tar tar -czvf archive.tar.gz dir / tar -xzvf archive.tar.gz tar -cjvf archive.tar.bz2 dir / tar -xjvf archive.tar.bz2 zip -r archive.zip dir / unzip archive.zip unzip archive.zip -d /target/dir/
磁盘管理 1 2 3 4 5 6 df -h df -h /home du -sh dir / du -h --max-depth=1 mount /dev/sdb1 /mnt umount /mnt
实用技巧 管道和重定向 1 2 3 4 5 6 7 8 9 cat app.log | grep ERROR | tail -20grep ERROR app.log | wc -l echo "hello" > file.txt echo "world" >> file.txt command > output.log 2>&1 command > /dev/null 2>&1
快捷键
快捷键
作用
Ctrl + C
终止当前命令
Ctrl + Z
暂停命令
Ctrl + D
退出shell
Ctrl + L
清屏
Ctrl + R
搜索历史命令
Ctrl + A/E
光标到行首/行尾
Ctrl + U
删除整行
Tab
自动补全
!!
执行上一条命令
!abc
执行最近abc开头的命令
常见场景 端口被占用 1 2 3 4 5 6 7 8 9 10 11 lsof -i:8080 netstat -tulnp | grep 8080 ss -tulnp | grep 8080 kill -9 <PID>pkill -f "java" killall <进程名>
进程运行状况 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 top htop ps aux | grep nginx pstree -p <PID> lsof -p <PID> netstat -antp | grep <PID> tail -f app.logtail -f app.log | grep ERROR
服务管理 1 2 3 4 5 6 7 8 9 10 11 12 13 14 sudo systemctl start nginx sudo systemctl stop nginx sudo systemctl restart nginx sudo systemctl reload nginx sudo systemctl status nginx sudo systemctl enable nginx sudo systemctl disable nginx sudo service nginx startsudo service nginx stopsudo service nginx restartsudo service nginx status
排查服务器负载高 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 uptime top -c free -h iostat -x 1 df -hiftop nethogs
日志分析 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 grep -c "ERROR" app.log awk '{print $1}' access.log | sort | uniq -c | sort -nr | head -20 cat access.log | awk '{print $1}' | sort | uniq -c | sort -nr | head -10grep "api/" access.log | awk '{sum+=$NF; count++} END {print sum/count}' awk -v start=$(date -d '2 hours ago' +%s) '$0 ~ /^\w+ \d+ \d+:\d+:\d+/ { gsub(/\[|\]/,"",$4); cmd="date -d \""$4"\" +%s"; cmd | getline ts; close(cmd); if (ts > start) print }' app.log
数据库相关 1 2 3 4 5 6 7 8 9 10 11 12 13 mysql -u root -p mysql -h host -P 3306 -u user -p database redis-cli redis-cli -h host -p 6379 psql -U user -d database netstat -an | grep 3306 | wc -l
文件传输 1 2 3 4 5 6 7 8 9 10 11 12 13 scp file.txt user@server:/path/ scp -r dir / user@server:/path/ scp user@server:/path/file.txt ./ scp -l 1000 file.txt user@server:/path/ rsync -avz dir / user@server:/path/ rsync -avz --delete dir / user@server:/path/
查看大文件 1 2 3 4 5 6 7 8 9 10 11 find . -type f -size +100M -exec ls -lh {} \; du -sh */ | sort -hr | head -10ls -lh file.txtdu -sh dir /