CentOS 7 筆記

常用初始配置

  • 禁用 firewalld
    systemctl stop firewalld
    systemctl disable firewalld
    
  • 禁用 NetworkManager
    systemctl stop NetworkManager
    systemctl disable NetworkManager
    
  • 禁用 postfix
    systemctl stop postfix
    systemctl disable postfix
    
  • 如果不用 NFS,可以禁用 rpcbind
    systemctl stop rpcbind
    systemctl disable rpcbind
    
  • 禁用 selinux,可能需要重啓操作系統
    sed -i '/^SELINUX=/cSELINUX=disabled' /etc/selinux/config
    setenforce 0
    # 可能需要重啓
    
  • 配置網卡靜態地址
    cd /etc/sysconfig/network-scripts
    sed -i -e '/^BOOTPROTO/d' -e '/^ONBOOT/d' \
        -e '/^IPADDR/d' -e '/^NETMASK/d' -e '/^PREFIX/d' \
        -e '/^GATEWAY/d' -e '/^DNS/d' ${ifcfg}
    cat >> ${ifcfg} <<-END
    ONBOOT=yes
    BOOTPROTO=static
    IPADDR=${ip}
    PREFIX=${mask}
    GATEWAY=${gw}
    DNS1=${dns}
    END
    systemctl restart network
    
  • 修改 sysctl.conf
    sed -i -e '/^net.ipv4.tcp_syncookies/d' \
        -e '/^net.ipv4.tcp_tw_reuse/d' \
        -e '/^net.ipv4.tcp_tw_recycle/d' \
        -e '/^net.ipv4.tcp_fin_timeout/d' /etc/sysctl.conf
    cat >> /etc/sysctl.conf <<-END
    net.ipv4.tcp_syncookies = 1
    net.ipv4.tcp_tw_reuse = 1
    net.ipv4.tcp_tw_recycle = 1
    net.ipv4.tcp_fin_timeout = 8
    END
    sysctl -p
    
  • 修改主機名
    hostnamectl set-hostname ${hostname}
    sed -i "/[ \t]\+${hostname}[ \t]*$/d" /etc/hosts
    echo "${ip} ${hostname}" >> /etc/hosts
    
  • 禁用 sshd 域名解析
    sed -i '/UseDNS/d' /etc/ssh/sshd_config
    echo 'UseDNS no' >> /etc/ssh/sshd_config
    
  • 刪除可能存在的 TMOUT 環境變量
    sed -i '/^export[ \t]\+TMOUT=/d' /etc/profile
    
  • 配置 history 命令數量和執行時間
    echo 'export HISTSIZE=10000' > /etc/profile.d/history.sh
    echo 'export HISTTIMEFORMAT="[%F %T] "' >> /etc/profile.d/history.sh
    
  • 修改時間同步服務器地址
    sed -i '/^server /d' /etc/chrony.conf
    echo "server ${ip|domain} iburst" >> /etc/chrony.conf
    

安全設置

  • /etc/pam.d/sshd
    • 用戶 ssh 登陸密碼錯誤 3 次後鎖住用戶 10 分鐘
      auth required pam_tally2.so deny=3 unlock_time=600 even_deny_root
      
  • /etc/login.defs
    • 密碼過期天數
      PASS_MAX_DAYS 60
      
    • 過期前警告天數
      PASS_WARN_AGE 7
      
    • 最短使用天數
      PASS_MIN_DAYS 1
      
    • 最短長度
      PASS_MIN_LEN 8
      
  • /etc/pam.d/system-auth
    • 密碼與前 5 次不同
      password    sufficient    pam_unix.so sha512 shadow nullok try_first_pass use_authtok remember=5
      
  • /etc/security/pwquality.conf
    • 密碼最小長度 8 位
      authconfig --passminlen=8 --update
      
    • 密碼最少 2 種字符
      authconfig --passminclass=2 --update
      
    • 最多 2 個連續相同字符
      authconfig --passmaxrepeat=2 --update
      
    • 最多 4 個連續同類字符
      authconfig --passmaxclassrepeat=4 --update
      
    • 至少 1 個小寫字符
      authconfig --enablereqlower --update
      
    • 至少 1 個大寫字符
      authconfig --enablerequpper --update
      
    • 至少 1 個數字
      authconfig --enablereqdigit --update
      
    • 至少 1 個特殊字符
      authconfig --enablereqother --update
      
  • /etc/ssh/sshd_config
    • 禁用 root 遠程登陸
      PermitRootLogin no
      
    • 只允許私鑰登陸
      PubkeyAuthentication yes
      PasswordAuthentication no
      
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章