插件窝 干货文章 Linux服务器安全配置:提高系统防御能力

Linux服务器安全配置:提高系统防御能力

bash etc 检查 install 354    来源:    2025-04-11

Linux服务器安全配置指南

一、基础安全配置

  1. 系统更新与补丁管理

    • 定期执行 yum update (RHEL/CentOS) 或 apt update && apt upgrade (Debian/Ubuntu)
    • 配置自动安全更新:

      # RHEL/CentOS 8+
      dnf install dnf-automatic
      systemctl enable --now dnf-automatic.timer
      
      # Ubuntu/Debian
      apt install unattended-upgrades
      dpkg-reconfigure unattended-upgrades
      
  2. 最小化安装原则

    • 安装时只选择必要的软件包
    • 定期检查并移除不需要的软件包:apt autoremovednf autoremove
  3. SSH安全加固

    # 编辑 /etc/ssh/sshd_config
    Port 2222                     # 修改默认端口
    PermitRootLogin no            # 禁止root直接登录
    PasswordAuthentication no     # 禁用密码认证,仅使用密钥
    MaxAuthTries 3                # 最大尝试次数
    ClientAliveInterval 300       # 客户端活动检查间隔
    ClientAliveCountMax 0         # 不活动时断开连接
    AllowUsers your_username      # 只允许特定用户登录
    

    重启SSH服务:systemctl restart sshd

二、用户与权限管理

  1. 用户账户安全

    • 创建专用管理用户: bash useradd -m -s /bin/bash adminuser passwd adminuser usermod -aG sudo adminuser # Debian/Ubuntu usermod -aG wheel adminuser # RHEL/CentOS
    • 设置密码策略:

      # 编辑 /etc/login.defs
      PASS_MAX_DAYS 90
      PASS_MIN_DAYS 7
      PASS_WARN_AGE 14
      
      # 安装密码复杂度模块
      apt install libpam-pwquality   # Debian/Ubuntu
      yum install pam_pwquality      # RHEL/CentOS
      
      # 配置 /etc/security/pwquality.conf
      minlen = 12
      minclass = 3
      
  2. SUDO权限控制

    • 编辑 /etc/sudoers/etc/sudoers.d/ 下的文件: %admin ALL=(ALL) ALL %wheel ALL=(ALL) ALL Defaults logfile="/var/log/sudo.log" Defaults log_input, log_output

三、网络与防火墙配置

  1. 防火墙配置

    • 使用firewalld (RHEL/CentOS): bash systemctl enable --now firewalld firewall-cmd --permanent --add-port=2222/tcp firewall-cmd --permanent --add-service=http firewall-cmd --reload firewall-cmd --list-all
    • 使用ufw (Ubuntu/Debian): bash ufw allow 2222/tcp ufw allow http ufw enable ufw status verbose
  2. 网络服务限制

    • 使用TCP Wrappers (/etc/hosts.allow/etc/hosts.deny)
    • 禁用不必要的服务: bash systemctl list-unit-files --type=service | grep enabled systemctl disable <unnecessary_service>

四、文件系统安全

  1. 文件权限管理

    • 关键目录权限: bash chmod 750 /home/* chmod 700 /etc/ssh/ssh_host*_key chmod 644 /etc/ssh/ssh_host*_key.pub chmod 600 /etc/ssh/sshd_config
    • 使用umask限制新文件权限: bash # 编辑 /etc/profile 或 /etc/bashrc umask 027
  2. 文件完整性检查

    • 安装AIDE (Advanced Intrusion Detection Environment):

      apt install aide   # Debian/Ubuntu
      yum install aide  # RHEL/CentOS
      
      aideinit
      mv /var/lib/aide/aide.db.new /var/lib/aide/aide.db
      
    • 定期检查: bash aide --check

五、日志与监控

  1. 日志配置

    • 配置rsyslog: bash # 编辑 /etc/rsyslog.conf *.emerg :omusrmsg:* auth.* /var/log/auth.log authpriv.* /var/log/secure
    • 日志轮转:配置 /etc/logrotate.conf/etc/logrotate.d/
  2. 入侵检测

    • 安装fail2ban:

      apt install fail2ban  # Debian/Ubuntu
      yum install fail2ban  # RHEL/CentOS
      
      cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local
      
    • 配置SSH防护: [sshd] enabled = true port = 2222 filter = sshd logpath = /var/log/auth.log maxretry = 3 bantime = 86400

六、高级安全措施

  1. SELinux/AppArmor

    • SELinux (RHEL/CentOS): bash getenforce # 检查状态 setenforce 1 # 临时启用
    • AppArmor (Ubuntu/Debian): bash aa-status # 检查状态 systemctl start apparmor
  2. 内核参数加固

    • 编辑 /etc/sysctl.confnet.ipv4.conf.all.rp_filter=1 net.ipv4.conf.default.rp_filter=1 net.ipv4.icmp_echo_ignore_broadcasts=1 net.ipv4.conf.all.accept_redirects=0 net.ipv6.conf.all.accept_redirects=0 kernel.exec-shield=1 kernel.randomize_va_space=2 应用设置:sysctl -p

七、定期维护与审计

  1. 安全审计工具

    • Lynis系统审计: bash apt install lynis # Debian/Ubuntu yum install lynis # RHEL/CentOS lynis audit system
    • OpenSCAP: bash yum install openscap-scanner scap-security-guide oscap xccdf eval --profile xccdf_org.ssgproject.content_profile_stig \ --results scan-results.xml \ --report scan-report.html \ /usr/share/xml/scap/ssg/content/ssg-centos7-ds.xml
  2. 定期检查清单

    • 检查异常用户:cat /etc/passwd
    • 检查SUID文件:find / -perm -4000 -type f -exec ls -la {} \; 2>/dev/null
    • 检查监听端口:ss -tulnpnetstat -tulnp
    • 检查计划任务:ls -la /etc/cron*crontab -l

通过实施以上安全措施,可以显著提高Linux服务器的防御能力。但请记住,安全是一个持续的过程,需要定期审查和更新安全策略。