2024-09-24 17:59:47 +08:00
|
|
|
|
#!/bin/bash
|
2024-11-18 23:12:52 +08:00
|
|
|
|
declare user_choice
|
2024-09-25 00:51:25 +08:00
|
|
|
|
echo "========$(basename $0 .sh)========"
|
2024-11-18 23:12:52 +08:00
|
|
|
|
echo "1. 更换 SSH 端口"
|
|
|
|
|
echo "2. 修改 SSH 登录方式"
|
2024-09-24 17:59:47 +08:00
|
|
|
|
echo "任意输入返回主菜单"
|
2024-11-18 23:12:52 +08:00
|
|
|
|
read -p "请输入要使用的功能:" user_choice
|
2024-09-24 17:59:47 +08:00
|
|
|
|
|
2024-11-18 23:12:52 +08:00
|
|
|
|
case $user_choice in
|
2024-09-24 17:59:47 +08:00
|
|
|
|
'1')
|
2024-11-18 23:12:52 +08:00
|
|
|
|
read -p "请输入需要修改的端口号(默认22): " new_port
|
2024-09-24 17:59:47 +08:00
|
|
|
|
|
2024-11-18 23:12:52 +08:00
|
|
|
|
if [[ -z $new_port ]];then
|
|
|
|
|
new_port=22
|
2024-09-24 17:59:47 +08:00
|
|
|
|
fi
|
|
|
|
|
|
2024-11-18 23:12:52 +08:00
|
|
|
|
if ! [[ $new_port =~ ^[0-9]+$ ]] || ! ((new_port > 0 && new_port < 65535)); then
|
|
|
|
|
echo "端口号不合法"
|
2024-09-24 17:59:47 +08:00
|
|
|
|
exit
|
|
|
|
|
fi
|
|
|
|
|
|
2024-11-18 23:12:52 +08:00
|
|
|
|
if lsof -i :$new_port -t >/dev/null; then
|
|
|
|
|
echo "$new_port 端口已被占用"
|
2024-09-24 17:59:47 +08:00
|
|
|
|
exit
|
|
|
|
|
fi
|
|
|
|
|
|
2024-11-18 23:12:52 +08:00
|
|
|
|
sed -i "s/^#\?Port.*/Port $new_port/g" /etc/ssh/sshd_config
|
2024-09-24 17:59:47 +08:00
|
|
|
|
|
|
|
|
|
systemctl restart sshd.service
|
|
|
|
|
|
2024-11-18 23:12:52 +08:00
|
|
|
|
echo "端口已修改为$new_port,请确保防火墙放行该端口"
|
2024-09-24 17:59:47 +08:00
|
|
|
|
;;
|
|
|
|
|
'2')
|
2024-11-18 23:12:52 +08:00
|
|
|
|
declare root_login_choice
|
|
|
|
|
declare password_auth_choice
|
|
|
|
|
declare key_auth_choice
|
|
|
|
|
echo "是否关闭 root 用户登录"
|
|
|
|
|
read -p "输入 n 关闭:" root_login_choice
|
2024-09-24 17:59:47 +08:00
|
|
|
|
echo "是否关闭密码登录"
|
2024-11-18 23:12:52 +08:00
|
|
|
|
read -p "输入 n 关闭:" password_auth_choice
|
2024-09-24 17:59:47 +08:00
|
|
|
|
echo "是否关闭密钥登录"
|
2024-11-18 23:12:52 +08:00
|
|
|
|
read -p "输入 n 关闭:" key_auth_choice
|
2024-09-24 17:59:47 +08:00
|
|
|
|
|
2024-11-18 23:12:52 +08:00
|
|
|
|
if [[ ! $root_login_choice =~ [Nn] ]];then
|
2024-09-24 17:59:47 +08:00
|
|
|
|
sed -i 's/^#\?PermitRootLogin.*/PermitRootLogin yes/g' /etc/ssh/sshd_config
|
2024-11-18 23:12:52 +08:00
|
|
|
|
echo "root 用户登录:已开启"
|
2024-09-24 17:59:47 +08:00
|
|
|
|
else
|
|
|
|
|
sed -i 's/^#\?PermitRootLogin.*/PermitRootLogin no/g' /etc/ssh/sshd_config
|
2024-11-18 23:12:52 +08:00
|
|
|
|
echo "root 用户登录:已关闭"
|
2024-09-24 17:59:47 +08:00
|
|
|
|
fi
|
|
|
|
|
|
2024-11-18 23:12:52 +08:00
|
|
|
|
if [[ ! $password_auth_choice =~ [Nn] ]];then
|
2024-09-24 17:59:47 +08:00
|
|
|
|
sed -i 's/^#\?PasswordAuthentication.*/PasswordAuthentication yes/g' /etc/ssh/sshd_config
|
2024-11-18 23:12:52 +08:00
|
|
|
|
echo "密码登录:已开启"
|
2024-09-24 17:59:47 +08:00
|
|
|
|
else
|
|
|
|
|
sed -i 's/^#\?PasswordAuthentication.*/PasswordAuthentication no/g' /etc/ssh/sshd_config
|
2024-11-18 23:12:52 +08:00
|
|
|
|
echo "密码登录:已关闭"
|
2024-09-24 17:59:47 +08:00
|
|
|
|
fi
|
|
|
|
|
|
2024-11-18 23:12:52 +08:00
|
|
|
|
if [[ ! $key_auth_choice =~ [Nn] ]];then
|
2024-09-24 17:59:47 +08:00
|
|
|
|
sed -i 's/^#\?PubkeyAuthentication.*/PubkeyAuthentication yes/g' /etc/ssh/sshd_config
|
2024-11-18 23:12:52 +08:00
|
|
|
|
echo "密钥登录:已开启"
|
2024-09-24 17:59:47 +08:00
|
|
|
|
else
|
|
|
|
|
sed -i 's/^#\?PubkeyAuthentication.*/PubkeyAuthentication no/g' /etc/ssh/sshd_config
|
2024-11-18 23:12:52 +08:00
|
|
|
|
echo "密钥登录:已关闭"
|
2024-09-24 17:59:47 +08:00
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
systemctl restart sshd.service
|
|
|
|
|
;;
|
|
|
|
|
esac
|