linux上自動禁止root和口令登入,開啟祕鑰登入(自動更改)
阿新 • • 發佈:2019-01-01
#!/bin/bash #禁止root登陸 A=`cat -n /etc/ssh/sshd_config | grep PermitRootLogin | grep -v of | wc -l` if [ $A == 1 ];then A1=`cat -n /etc/ssh/sshd_config | grep PermitRootLogin | grep -v of | awk '{print $1}'` sed -i ''"$A1"'c PermitRootLogin no' /etc/ssh/sshd_config elif [ $A != 1 ];then A1=`cat -n /etc/ssh/sshd_config | grep PermitRootLogin | grep -v of | grep -v "#" | awk '{print $1}'` sed -i ''"$A1"'c PermitRootLogin no' /etc/ssh/sshd_config fi #禁止口令登陸 B=`cat -n /etc/ssh/sshd_config | grep PasswordAuthentication | grep -v PAM | wc -l` if [ $B == 1 ];then B1=`cat -n /etc/ssh/sshd_config | grep PasswordAuthentication | grep -v PAM | awk '{print $1}'` sed -i ''"$B1"'c PasswordAuthentication no' /etc/ssh/sshd_config elif [ $B != 1 ];then B1=`cat -n /etc/ssh/sshd_config | grep PasswordAuthentication | grep -v PAM | grep -v "#" | awk '{print $1}'` sed -i ''"$B1"'c PasswordAuthentication no' /etc/ssh/sshd_config fi #啟用金鑰登陸 C=`cat -n /etc/ssh/sshd_config | grep PubkeyAuthentication | wc -l` if [ $C == 1 ];then C1=`cat -n /etc/ssh/sshd_config | grep PubkeyAuthentication | awk '{print $1}'` sed -i ''"$C1"'c PubkeyAuthentication yes' /etc/ssh/sshd_config elif [ $C != 1 ];then C1=`cat -n /etc/ssh/sshd_config | grep PubkeyAuthentication | grep -v "#" | awk '{print $1}'` sed -i ''"$C1"'c PubkeyAuthentication yes' /etc/ssh/sshd_config fi systemctl restart sshd clear ##禁止口令登入: AAA=`cat /etc/ssh/sshd_config | grep PasswordAuthentication | grep -v "#" | grep no | wc -l` if [ $AAA == 1 ];then echo "禁止口令登入 ok" else echo "禁止口令登入 失敗!!" fi ##禁止root登入: BBB=`cat /etc/ssh/sshd_config | grep PermitRootLogin | grep -v "#" | grep no | wc -l` if [ $BBB == 1 ];then echo "禁止root登入 ok" else echo "禁止root登入 失敗!!" fi ##開啟祕鑰登入: CCC=`cat /etc/ssh/sshd_config | grep PubkeyAuthentication | grep -v "#" | grep yes | wc -l` if [ $CCC == 1 ];then echo "開啟祕鑰登入 ok" else echo "開啟祕鑰登入 失敗!!" fi