作为IT工程师,掌握Linux系统维护技能至关重要。以下是一份全面的Linux系统维护指南,涵盖日常维护、性能优化、故障排查和安全加固等方面。
# Debian/Ubuntu系统
sudo apt update && sudo apt upgrade -y
sudo apt autoremove
# RHEL/CentOS系统
sudo yum update -y
sudo yum autoremove
# 查看系统日志
journalctl -xe
tail -f /var/log/syslog # 或/var/log/messages
# 日志轮转配置
vim /etc/logrotate.conf
# 检查磁盘使用情况
df -h
du -sh /path/to/directory
# 查找大文件
find / -type f -size +100M -exec ls -lh {} \;
# 清理旧内核(仅保留最新2-3个)
sudo apt autoremove --purge # Debian/Ubuntu
sudo package-cleanup --oldkernels --count=2 # RHEL/CentOS
# 实时监控工具
top
htop
glances
# 内存使用情况
free -m
# IO监控
iostat -x 1
iotop
# 网络连接状态
ss -tulnp
netstat -tulnp
# 带宽监控
iftop
nload
# 网络延迟测试
mtr destination_host
# 查找高CPU进程
ps -eo pid,ppid,cmd,%mem,%cpu --sort=-%cpu | head
# 杀死进程
kill -9 PID
pkill process_name
# 查看启动日志
journalctl -b
# 修复GRUB引导(适用于启动失败)
# 从LiveCD启动后:
mount /dev/sdXY /mnt
mount --bind /dev /mnt/dev
mount --bind /proc /mnt/proc
mount --bind /sys /mnt/sys
chroot /mnt
grub-install /dev/sdX
update-grub
# 检查文件系统
fsck /dev/sdXY
# 强制检查(当系统提示需要检查时)
touch /forcefsck
reboot
# 基本网络诊断
ping google.com
traceroute google.com
dig google.com
# 检查防火墙规则
iptables -L -n
ufw status # Ubuntu
firewall-cmd --list-all # RHEL/CentOS
# 检查特权用户
grep '^sudo' /etc/group
grep ':0:' /etc/passwd
# 密码策略设置
vim /etc/login.defs
vim /etc/pam.d/common-password # Debian/Ubuntu
vim /etc/pam.d/system-auth # RHEL/CentOS
vim /etc/ssh/sshd_config
# 推荐修改:
PermitRootLogin no
PasswordAuthentication no # 使用密钥认证
Port 2222 # 修改默认端口
AllowUsers your_username
# UFW (Ubuntu)
sudo ufw allow 22/tcp
sudo ufw enable
# firewalld (RHEL/CentOS)
sudo firewall-cmd --permanent --add-service=ssh
sudo firewall-cmd --reload
# 安装和运行Lynis安全审计工具
sudo apt install lynis # Debian/Ubuntu
sudo yum install lynis # RHEL/CentOS
sudo lynis audit system
# 使用rsync进行增量备份
rsync -avz --delete /source/directory /backup/location
# 定时备份(crontab示例)
0 2 * * * rsync -avz --delete /important/data /backup/server
# 创建完整系统备份(需要足够磁盘空间)
sudo tar -cvpzf backup.tar.gz --exclude=/backup.tar.gz --one-file-system /
# MySQL备份
mysqldump -u root -p --all-databases > full_backup.sql
# PostgreSQL备份
pg_dumpall > full_backup.sql
# 示例playbook (update_system.yml)
- hosts: all
become: yes
tasks:
- name: Update all packages
apt:
update_cache: yes
upgrade: dist
when: ansible_os_family == "Debian"
- name: Update all packages (RHEL)
yum:
name: '*'
state: latest
when: ansible_os_family == "RedHat"
# 编辑当前用户的crontab
crontab -e
# 示例: 每周日凌晨3点进行系统更新
0 3 * * 0 /usr/bin/apt update && /usr/bin/apt upgrade -y
# 安装和配置Prometheus + Grafana
# 或使用现成方案如Netdata
curl -fsSL https://my-netdata.io/kickstart.sh | bash
任务 | 命令 |
---|---|
查看系统信息 | uname -a , lsb_release -a , cat /etc/*release |
查看硬件信息 | lshw , lscpu , lsblk , lspci |
服务管理 | systemctl status <service> , systemctl restart <service> |
查看内核消息 | dmesg |
检查开放端口 | ss -tulnp , netstat -tulnp |
查找文件 | find / -name "filename" , locate filename |
查看环境变量 | printenv |
检查磁盘健康 | smartctl -a /dev/sdX |
通过遵循这些维护实践,您可以确保Linux系统保持高效、安全和稳定运行。根据具体环境和需求调整这些建议,并建立适合您组织的维护流程。