作为IT工程师,掌握以下Linux命令可以有效提升服务器安全性:
# 查看当前登录用户
who
w
# 查看用户登录历史
last
# 创建新用户并设置密码
sudo useradd -m username
sudo passwd username
# 修改用户密码策略
sudo chage -l username # 查看策略
sudo chage -M 90 username # 设置密码90天过期
# 检查特权用户
awk -F: '($3 == "0") {print}' /etc/passwd
# 查看sudo权限用户
sudo grep -Po '^sudo.+:\K.*$' /etc/group
# 查找SUID/SGID文件
find / -type f \( -perm -4000 -o -perm -2000 \) -exec ls -la {} \;
# 查找世界可写文件
find / -type f -perm -o+w -exec ls -la {} \;
# 查找无主文件
find / -nouser -o -nogroup
# 检查文件完整性
sha256sum /path/to/file # 生成校验和
# 查看开放端口
ss -tulnp
netstat -tulnp
# 检查防火墙规则
sudo iptables -L -n -v
sudo ufw status # 如果使用UFW
# 检查SSH配置
sudo grep -i "^PermitRootLogin" /etc/ssh/sshd_config
sudo grep -i "^PasswordAuthentication" /etc/ssh/sshd_config
# 查看失败的登录尝试
sudo grep "Failed password" /var/log/auth.log
# 查看系统资源使用情况
top
htop
free -h
# 查看磁盘使用情况
df -h
du -sh /path/to/directory
# 查看系统日志
journalctl -xe # systemd系统
tail -f /var/log/syslog
tail -f /var/log/auth.log
# 检查计划任务
crontab -l # 当前用户
sudo crontab -l -u root # root用户
ls -la /etc/cron.*
# 检查可用更新
sudo apt update && sudo apt list --upgradable # Debian/Ubuntu
sudo yum check-update # CentOS/RHEL
# 执行安全更新
sudo apt upgrade --only-upgrade security # Debian/Ubuntu
sudo yum update --security # CentOS/RHEL
# 检查已安装软件包
dpkg -l | grep ^ii # Debian/Ubuntu
rpm -qa # CentOS/RHEL
# 查看运行中的进程
ps aux
pstree
# 查找可疑进程
ps aux | grep -i "crypt\|miner\|backdoor\|shell"
# 管理系统服务
sudo systemctl list-units --type=service
sudo systemctl status servicename
通过熟练掌握这些命令并定期执行安全检查,可以显著提升Linux服务器的安全性。