#!/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
linux上自动禁止root和口令登录,开启秘钥登录(自动更改)
猜你喜欢
转载自blog.csdn.net/zzhlinux911218/article/details/85241469
今日推荐
周排行