本文目录导读:

在Shell脚本中禁用SSH密码登录,主要需要修改SSH服务的配置文件/etc/ssh/sshd_config,以下是几种实现方法:
方法1:使用sed命令直接修改配置文件
#!/bin/bash # 禁用SSH密码登录脚本 # 备份原配置文件 cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak.$(date +%Y%m%d_%H%M%S) # 修改PasswordAuthentication为no sed -i 's/^#*PasswordAuthentication.*/PasswordAuthentication no/' /etc/ssh/sshd_config # 确保PubkeyAuthentication已启用 sed -i 's/^#*PubkeyAuthentication.*/PubkeyAuthentication yes/' /etc/ssh/sshd_config # 重启SSH服务 systemctl restart sshd 2>/dev/null || systemctl restart ssh 2>/dev/null || service sshd restart 2>/dev/null echo "SSH密码登录已禁用!"
方法2:更安全的实现(包含验证和错误处理)
#!/bin/bash
set -e # 遇到错误立即退出
# 颜色输出
RED='\033[0;31m'
GREEN='\033[0;32m'
NC='\033[0m' # No Color
disable_ssh_password() {
local config_file="/etc/ssh/sshd_config"
local backup_file="${config_file}.bak.$(date +%Y%m%d_%H%M%S)"
# 检查是否root用户
if [[ $EUID -ne 0 ]]; then
echo -e "${RED}错误:需要root权限运行此脚本${NC}"
exit 1
fi
# 检查配置文件是否存在
if [[ ! -f "$config_file" ]]; then
echo -e "${RED}错误:找不到SSH配置文件 $config_file${NC}"
exit 1
fi
# 备份配置文件
echo "备份原配置文件到 $backup_file"
cp "$config_file" "$backup_file"
# 修改配置
echo "正在修改SSH配置..."
# 禁用密码登录
if grep -q "^PasswordAuthentication" "$config_file"; then
sed -i 's/^PasswordAuthentication.*/PasswordAuthentication no/' "$config_file"
else
echo "PasswordAuthentication no" >> "$config_file"
fi
# 启用密钥登录
if grep -q "^PubkeyAuthentication" "$config_file"; then
sed -i 's/^PubkeyAuthentication.*/PubkeyAuthentication yes/' "$config_file"
else
echo "PubkeyAuthentication yes" >> "$config_file"
fi
# 测试配置语法
echo "测试SSH配置语法..."
if command -v sshd &>/dev/null; then
sshd -t || {
echo -e "${RED}错误:SSH配置语法错误,恢复备份${NC}"
cp "$backup_file" "$config_file"
exit 1
}
fi
# 重启SSH服务
echo "重启SSH服务..."
local services=("sshd" "ssh" "ssh.service")
local restarted=false
for service in "${services[@]}"; do
if systemctl is-active --quiet "$service" 2>/dev/null; then
systemctl restart "$service" && restarted=true && break
fi
done
if [[ "$restarted" == false ]]; then
service sshd restart 2>/dev/null || {
echo -e "${RED}警告:无法自动重启SSH服务,请手动重启${NC}"
echo "可以使用: systemctl restart sshd"
}
fi
echo -e "${GREEN}✓ SSH密码登录已禁用!${NC}"
echo -e "${GREEN}✓ 请确保已经配置好SSH密钥登录,以免被锁在系统外!${NC}"
}
# 添加安全提示
echo "⚠ 重要提示:在禁用密码登录前,请确保:"
echo "1. 已经配置好SSH密钥认证"
echo "2. 有另一个打开的SSH会话作为备份"
echo "3. 知道如何恢复配置(备份文件在 /etc/ssh/sshd_config.bak.*)"
echo ""
echo -n "确认继续吗?(yes/no): "
read -r confirm
if [[ "$confirm" == "yes" ]]; then
disable_ssh_password
else
echo "操作已取消"
exit 0
fi
方法3:一行命令快速禁用(用于自动化脚本)
# 单行命令禁用SSH密码登录 sudo sed -i 's/^#*PasswordAuthentication.*/PasswordAuthentication no/' /etc/ssh/sshd_config && sudo systemctl restart sshd
方法4:通过脚本参数控制
#!/bin/bash
# 用法: ./disable_ssh_password.sh [enable|disable]
ACTION=${1:-disable} # 默认为禁用
case $ACTION in
disable)
sed -i 's/^#*PasswordAuthentication.*/PasswordAuthentication no/' /etc/ssh/sshd_config
echo "SSH密码登录已禁用"
;;
enable)
sed -i 's/^#*PasswordAuthentication.*/PasswordAuthentication yes/' /etc/ssh/sshd_config
echo "SSH密码登录已启用"
;;
*)
echo "用法: $0 [enable|disable]"
exit 1
;;
esac
systemctl restart sshd
重要安全注意事项
-
先测试再应用:在修改配置前,最好先测试语法:
sshd -t # 测试SSH配置语法
-
保留备份:始终保留配置文件的备份
-
确保有备用会话:在禁用密码登录前,确保:
- 已经配置好SSH密钥认证
- 有另一个SSH会话保持连接
- 或者有物理访问权限
-
检查当前登录方式:可以先检查是否已经有SSH密钥登录成功:
# 查看当前SSH会话使用的认证方式 echo $SSH_AUTH_SOCK
-
验证密钥配置:确保公钥已添加到
~/.ssh/authorized_keys
使用这些脚本前,请务必备份和验证,以免被锁在系统外。