1. 程式人生 > 其它 >運維大鏢客:Linux 合規配置指令碼第一版

運維大鏢客:Linux 合規配置指令碼第一版

技術標籤:運維devops落地運維linux安全centos

還在為Linux合規配置檢查不過關而發愁嗎?
最新的配置合規指令碼來啦!

#!/bin/bash

#################################################################
## Linux 裝置合規配置指令碼
## 基於 Centos7
## 在執行之前請務必先瀏覽一遍 有一些重要的資訊需要留意
## 注意,此指令碼有些部分只能執行一遍,如果執行多次會產生錯誤
## Update at: 20201208

#################################################################
USER_NAME=demo USER_PASSWD=demopass!@ NTP_SERVER= ################################################################# ## 設定螢幕保護 echo "=== 正在設定螢幕保護" gconftool-2 --direct \ --config-source xml:readwrite:/etc/gconf/gconf.xml.mandatory \ --type bool \ --set /apps/gnome-screensaver/idle_activation_enabled true
gconftool-2 --direct \ --config-source xml:readwrite:/etc/gconf/gconf.xml.mandatory \ --type bool \ --set /apps/gnome-screensaver/lock_enabled true gconftool-2 --direct \ --config-source xml:readwrite:/etc/gconf/gconf.xml.mandatory \ --type string \ --set /apps/gnome-screensaver/mode blank-only gconftool-2 --direct \ --config-source xml:readwrite:/etc/gconf/gconf.xml.mandatory \ --type int \ --set /apps/gnome-screensaver/idle_delay 15 #################################################################
## 設定ssh警告banner touch /etc/sshbanner chown bin:bin /etc/sshbanner chmod 644 /etc/sshbanner echo " Authorized users only. All activity may be monitored and reported " >/etc/sshbanner echo "Banner /etc/sshbanner" >> /etc/ssh/sshd_config echo "=== 正在重啟 sshd 服務" systemctl restart sshd ################################################################# ## 設定口令生存週期 echo "=== 正在設定口令生存週期" cp -p /etc/login.defs /etc/login.defs_bak sed -i "s/^PASS_MIN_LEN.*/PASS_MIN_LEN 8 /g" /etc/login.defs sed -i "s/^PASS_MAX_DAYS.*/PASS_MAX_DAYS 90 /g" /etc/login.defs ################################################################# ## 設定ssh登入成功後的警告banner echo "=== 正在設定ssh登入成功後的警告banner" echo " Authorized users only. All activity may be monitored and reported " > /etc/motd ################################################################# ## 新建使用者賬戶 echo "=== 正在建立自用賬戶" useradd $USER_NAME echo $USER_PASSWD | passwd $USER_NAME --stdin &>/dev/null echo "$USER_NAME ALL=(ALL) NOPASSWD: ALL">> /etc/sudoers ################################################################# ## 刪除無用賬戶 echo "=== 正在刪除無用賬戶" userdel gdm userdel listen userdel webservd userdel nobody4 userdel noaccess ################################################################# ## 設定使用者口令密碼複雜度策略 echo "=== 正在修改使用者口令密碼複雜度策略" cp -p /etc/pam.d/system-auth /etc/pam.d/system-auth_bak sed -i "s/^password requisite.*/password requisite pam_cracklib.so dcredit=-1 ucredit=-1 lcredit=-1 ocredit=-1 minclass=2 minlen=8 /g" /etc/pam.d/system-auth sed -i "s/^password sufficient.*/password sufficient pam_unix.so md5 shadow nullok try_first_pass use_authtok /g" /etc/pam.d/system-auth ################################################################# ## 設定使用者預設MASK echo "=== 正在設定使用者預設MASK" cp -p /etc/profile /etc/profile_bak cp -p /etc/csh.login /etc/csh.login_bak cp -p /etc/csh.cshrc /etc/csh.cshrc_bak cp -p /etc/bashrc /etc/bashrc_bak cp -p /root/.bashrc /root/.bashrc_bak cp -p /root/.cshrc /root/.cshrc_bak echo "umask 027" >> /etc/profile echo "umask 027" >> /etc/csh.login echo "umask 027" >> /etc/csh.cshrc echo "umask 027" >> /etc/bashrc echo "umask 027" >> /root/.bashrc echo "umask 027" >> /root/.cshrc ################################################################# ## 設定登入超時 echo "=== 正在設定登入超時" cp -p /etc/profile /etc/profile_bak cp -p /etc/csh.cshrc /etc/csh.cshrc_bak echo "TMOUT=180" >> /etc/profile echo "export TMOUT" >> /etc/profile echo "set autologout=30" >> /etc/csh.cshrc ################################################################# ## 新增使用者組 echo "=== 正在新增使用者組 ${USER_NAME}" groupadd ${USER_NAME} usermod -g ${USER_NAME} ${USER_NAME} ################################################################# ## 防syn攻擊 增加主機訪問控制 ## 這裡需要增加特定的IP地址段 echo "=== 正在設定主機IP地址限制" cp -p /etc/hosts.allow /etc/hosts.allow_bak cp -p /etc/hosts.deny /etc/hosts.deny_bak ## 注意該地址範圍 別把自己牆到外面 ## 這裡sshd允許10段網路訪問 ## 我們加入的這個限制會影響4A登入,一定注意 echo "sshd:all:allow" >> /etc/host.allow echo "sshd:10.:allow" >> /etc/host.allow echo "telnet:10.:allow" >> /etc/host.allow echo "sshd:234.234.234.234:deny" >> /etc/hosts.deny echo "telnet:234.234.234.234:deny" >> /etc/hosts.deny ################################################################# ## 更改telnet警告banner echo "=== 正在更改telnet警告banner" echo " Authorized users only. All activity may be monitored and reported " > /etc/issue echo " Authorized users only. All activity may be monitored and reported " > /etc/issue.net systemctl restart xinetd ################################################################# ## 設定口令重複次數限制 echo "=== 設定口令重複次數限制" touch /etc/security/opasswd chown root:root /etc/security/opasswd chmod 600 /etc/security/opasswd sed -i "s/^password required.*/password required pam_unix.so remember=5 /g" /etc/pam.d/system-auth ################################################################# ## 設定口令鎖定策略 echo "=== 正在設定口令鎖定策略" sed -i "2 s/^/auth required pam_tally2.so deny=6 onerr=fail no_magic_root unlock_time=120\n/" /etc/pam.d/system-auth ################################################################# ## 設定禁止imcp重定向 echo "=== 正在禁止ICMP重定向" cp -p /etc/sysctl.conf /etc/sysctl.conf_bak grep "net.ipv4.conf.all.accept_redirects=0" /etc/sysctl.conf if [ $? == "1" ]; then echo "net.ipv4.conf.all.accept_redirects=0" >> /etc/sysctl.conf fi sysctl -p ################################################################# ## 鎖定無用賬戶 echo "=== 正在鎖定無用賬戶" user_arr=("lp" "nobody" "uucp" "games" "rpm" "smmsp" "nfsnobody") for user in ${user_arr[@]} do res=`egrep -w "$user" /etc/shadow | awk -F: '( $2 !~ "!" ) {print $1":"$2}'` if [ -n "$res" ]; then echo "**正在鎖定使用者 $user" sed -i "s/$user:/$user:\!/g" /etc/shadow fi done