gongxiangp2/scripts/fix-frp-ssh-pubkey.sh

41 lines
1.1 KiB
Bash
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
# 修复FRP服务器SSH公钥认证
# 在FRP服务器上执行
echo "=== 备份SSH配置文件 ==="
cp /etc/ssh/sshd_config /etc/ssh/sshd_config.backup.$(date +%Y%m%d_%H%M%S)
echo "备份完成"
echo ""
echo "=== 修改PubkeyAuthentication配置 ==="
sed -i 's/^PubkeyAuthentication no/PubkeyAuthentication yes/' /etc/ssh/sshd_config
sed -i 's/^#PubkeyAuthentication yes/PubkeyAuthentication yes/' /etc/ssh/sshd_config
echo "配置已修改"
echo ""
echo "=== 验证配置 ==="
grep -E '^PubkeyAuthentication' /etc/ssh/sshd_config
echo ""
echo "=== 清理重复的公钥 ==="
sort -u ~/.ssh/authorized_keys > ~/.ssh/authorized_keys.tmp
mv ~/.ssh/authorized_keys.tmp ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys
echo "重复公钥已清理"
echo ""
echo "=== 测试SSH配置 ==="
sshd -t
if [ $? -eq 0 ]; then
echo "SSH配置测试通过"
echo ""
echo "=== 重启SSH服务 ==="
systemctl restart sshd
echo "SSH服务已重启"
echo ""
echo "配置完成!现在可以使用SSH密钥免密登录了。"
else
echo "SSH配置测试失败,请检查配置"
exit 1
fi