Linux 运维命令大全:从系统配置到性能监控

共计 3024 个字符,预计需要花费 8 分钟才能阅读完成。

本文整理 Linux 运维高频命令,涵盖系统管理、网络配置、故障排查等场景,适用于 CentOS/RHEL 等主流发行版

一、系统基础配置

# 1.1 修改主机名(永久生效)  
hostnamectl set-hostname server-prod-01  

# 1.2 关闭防火墙(生产环境慎用)  
systemctl stop firewalld && systemctl disable firewalld  

# 1.3 修改用户密码  
echo "NewPass123!" | passwd --stdin deploy_user  

# 1.4 设置静态IP  
nmcli con mod eth0 ipv4.addresses 192.168.1.100/24 \  
ipv4.gateway 192.168.1.1 \  
ipv4.dns "8.8.8.8 114.114.114.114" \  
ipv4.method manual  
nmcli con up eth0  

二、系统信息查询

# 2.1 系统架构与内核  
uname -a  # 显示系统架构  
lsb_release -a  # 查看发行版信息  

# 2.2 CPU 详细信息  
lscpu  # 完整CPU报告  
grep "model name" /proc/cpuinfo | uniq  # 处理器型号  

# 2.3 内存使用分析  
free -h  # 人性化显示内存  
cat /proc/meminfo | grep MemAvailable  # 可用内存  

三、网络管理

# 3.1 网络工具安装  
yum install -y net-tools bind-utils  # netstat/nslookup等  

# 3.2 网络状态检查  
ss -tulnp  # 比netstat更高效的端口查看  
ip route show  # 显示路由表  

# 3.3 网络诊断  
mtr google.com  # 实时路由跟踪  
nslookup github.com  # DNS解析测试  
tcping -d 80 192.168.1.1  # TCP端口探测  

四、进程与服务管理

# 4.1 进程操作  
pgrep -l nginx  # 查找进程ID  
pkill -9 -f "python.*app"  # 强制结束匹配进程  

# 4.2 后台任务管理  
nohup ./start.sh > app.log 2>&1 &  # 后台运行并记录日志  
disown -h %1  # 剥离任务与终端关联  

# 4.3 进程环境变量  
xargs -0 -L1 -a /proc/
<PID>/environ  # 查看进程环境变量  

五、文件与文本处理

# 5.1 高级文本处理  
grep -vE "^#|^$" config.conf  # 排除注释和空行  
awk '{print $1,$7}' access.log | sort | uniq -c  # 统计访问源  

# 5.2 文件格式转换  
dos2unix script.sh  # Windows转Linux格式  
sed -i 's/\r//g' winfile.txt  # 去除^M字符  

# 5.3 文件查找  
find /var/log -name "*.log" -mtime -7 -size +10M  # 7天内>10MB日志  

六、性能监控

# 6.1 实时监控  
top -p $(pgrep nginx)  # 监控指定进程  
iftop -i eth0  # 实时网络流量  

# 6.2 I/O 性能分析  
iostat -dx 1  # 磁盘I/O详情  
iotop -o  # 高I/O进程排行  

# 6.3 内存泄漏检测  
watch -n1 "grep -e MemFree -e Buffers /proc/meminfo"  

七、用户与权限

# 7.1 用户管理  
useradd -m -s /bin/bash deploy_user  # 创建用户  
echo "deploy_user ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers  

# 7.2 登录审计  
last -5 -i  # 最近5次登录(显示IP)  
grep "Failed password" /var/log/secure  # 登录失败记录  

实用技巧集锦

1. 网络防火墙

# 允许特定IP访问SSH  
iptables -A INPUT -p tcp --dport 22 -s 192.168.1.0/24 -j ACCEPT  

# 端口转发(将80转到8080)  
iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 8080  

# 保存规则(CentOS 7+)  
yum install iptables-services  
systemctl enable iptables && iptables-save > /etc/sysconfig/iptables  

2. 日志分析神器

# 统计HTTP状态码  
awk '{print $9}' access.log | sort | uniq -c | sort -rn  

# 实时错误日志监控  
tail -f /var/log/nginx/error.log | grep -E '50x|timeout'  

# 连接数TOP10 IP  
netstat -ntu | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -n  

3. 安全加固

# 禁用root SSH登录  
sed -i 's/#PermitRootLogin yes/PermitRootLogin no/' /etc/ssh/sshd_config  

# 关键文件锁定  
chattr +i /etc/passwd /etc/shadow /etc/sudoers  

# 历史命令保护  
echo 'export HISTTIMEFORMAT="%F %T "' >> /etc/profile  
echo 'export PROMPT_COMMAND="history -a"' >> /etc/profile  

4. 故障排查组合拳

# 当服务异常时:  
dmesg | tail -20  # 检查内核日志  
journalctl -u nginx --since "10 min ago"  # 服务日志  
strace -p $(pgrep nginx)  # 跟踪系统调用  
lsof -i :80  # 查看端口占用  

性能指标解读

命令输出 关键指标 健康范围
free -h Available > 总内存20%
iostat -dx %util < 70%
top wa(%IO等待) < 5%
ss -s TCP Timewait < 1000
避坑指南:

    禁止直接 rm -rf /:使用 alias rm='rm -i'

    慎用 kill -9:先尝试 kill -15 优雅终止

    关键操作前创建快照:lvcreate -s -L 5G -n db_snap /dev/vg00/db

扩展:Linux 诊断工具箱

# 系统性能综合报告  
sudo yum install sysstat -y && sar -A  

# 硬件信息集合  
lshw -short  # 需要yum install lshw  

# 进程资源使用  
pidstat 1 5 -p $(pgrep java)  # 监控Java进程  
建议将高频命令存入 ~/.bashrc 别名:

alias ports='ss -tulnp'  
alias meminfo='free -h && grep -i commit /proc/meminfo'  
alias errortail='tail -f /var/log/messages | grep -iE "error|fail"'  

通过系统化掌握这些命令,Linux 运维效率可提升 60% 以上。建议配合 Ansible 等自动化工具实现批量管理。

正文完
 0