一、系統預設帳号及組管理
删除系統預設不使用的帳号,包括:lp、mail、games、ftp、nobody、postfix等。删除系統預設不使用的組,包括:mail、games、ftp、nobody、postfix等。
二、啟用密碼政策
1 .密碼60天過期,修改密碼最小間隔為1天,最短密碼要求8位,在密碼過期前7天内通知使用者。通過修改/etc/login.defs來實作,修改以下幾行:
PASS_MAX_DAYS 60
PASS_MIN_DAYS 1
PASS_MIN_LEN 8
PASS_WARN_AGE 7
2 .啟用帳号鎖定政策,連續輸錯3次密碼,鎖定使用者5分鐘。修改配置檔案/etc/pam.d/system-auth,修改以下内容:
auth required pam_env.so
auth required pam_tally2.so deny=3 unlock_time=300
三、SSH安全配置
隻使用協定版本2,禁止root登入,禁止空密碼登入。修改配置檔案/etc/ssh/sshd_config,具體配置為:
#default is 2,1
Protocol 2
#default is yes
PermitRootLogin no
#default is no
PermitEmptyPasswords no
四、不活動使用者5分鐘誤操作自動登出。
修改配置檔案/etc/profile,在末尾加入以下内容:
TMOUT=300
五、清除系統别名
cp /etc/aliases /etc/aliases_$( date "+%Y%m%d%H%M%S")
cat /dev/null>&/etc/aliases
六、全部腳本
#!/bin/bash
#===============================================================================
# FILE: NeobyPay.sh
# USAGE: ./NeobyPay.sh
# DESCRIPTION: 此腳本請使用source執行,帶空格的.執行也是可以的。
# OPTIONS: ---
# REQUIREMENTS: ---
# BUGS: ---
# NOTES: ---
# AUTHOR: GeekDevOps (IVAN DU), [email protected]
# ORGANIZATION: Neoby
# CREATED: 2018年01月30日 16時51分16秒
# REVISION: V1.1.1
#===============================================================================
set -o nounset # Treat unset variables as an error
#清除賬号别名
cp /etc/aliases /etc/aliases_$( date "+%Y%m%d%H%M%S")
cat /dev/null>&/etc/aliases
#關于使用者或組需要備份的系統配置檔案
cp /etc/passwd /etc/passwd.$(date +"%Y%m%d%H%M%S")
cp /etc/shadow /etc/shadow.$(date +"%Y%m%d%H%M%S")
cp /etc/group /etc/group.$(date +"%Y%m%d%H%M%S")
#删除不必要賬戶
UnusefulAccounts=("lp" "mail" "games" "ftp" "nobody" "postfix" )
for Username in ${UnusefulAccounts[@]} ;
do
userdel -f $Username >& /dev/null
if [ $? -eq 0 ] ; then
echo "The account $Username has been deleted!"
else
echo "Deleting the account $Username ERROR! Please try again!"
fi
done
#删除不必要的組
UnusefulGroups=("mail" "games" "ftp" "nobody" "postfix")
for Groups in ${UnusefulGroups[@]} ;
do
groupdel $Groups >& /dev/null
if [ $? -eq 0 ] ; then
echo "The group $Groups has been deleted!"
else
echo "Deleting the group $Groups ERROR! Please try again!"
fi
done
#密碼政策
echo "TMOUT=300">>/etc/profile #登入後不活動則300秒逾時
cp /etc/login.defs /etc/login.defs.$(date +"%Y%m%d%H%M%S") #備份配置檔案
sed -i '/^#PermitRootLogin/a\PermitRootLogin no' /etc/ssh/sshd_config #禁止root使用者ssh登入
sed -i '/^#Port/a\Protocol 2' /etc/ssh/sshd_config #使用ssh2協定登入
sed -i "/^PASS_MAX_DAYS/c\PASS_MAX_DAYS 60" /etc/login.defs #密碼有效時間最大值為60天
sed -i "/^PASS_MIN_DAYS/c\PASS_MIN_DAYS 1" /etc/login.defs #密碼修改間隔最小值為1天
sed -i "/^PASS_MIN_LEN/c\PASS_MIN_LEN 8" /etc/login.defs #密碼最短長度為8個字元
sed -i "/^PASS_WARN_AGE/c\PASS_WARN_AGE 7" /etc/login.defs #密碼過期提前7天提醒使用者
cp /etc/pam.d/system-auth /etc/pam.d/system-auth.$(date +"%Y%m%d%H%M%S") #備份配置檔案
sed -i "/^auth required pam_env.so/a\auth required pam_tally2.so deny=3 unlock_time=300" /etc/pam.d/system-auth #密碼輸入錯誤三次之後鎖定使用者,五分鐘之後自動解鎖
source /etc/profile>&/dev/null
systemctl restart sshd #重新開機ssh服務
若有不妥之處還望諸位多多指教。
本文同步分享在 部落格“點亮夢想那束光”(CSDN)。
如有侵權,請聯系 [email protected] 删除。
本文參與“OSC源創計劃”,歡迎正在閱讀的你也加入,一起分享。