1130 字
6 分钟
SSH 免密配置

1. SSH 认证方式#

SSH 支持两种认证方式:

方式说明安全性
密码认证输入用户名和密码登录较低,易受暴力破解
密钥认证使用公钥/私钥对进行认证较高,推荐使用

密钥认证原理:

  1. A 节点生成密钥对(公钥 + 私钥)
  2. A 节点将公钥发送到 B 节点
  3. A 节点发起连接时,B 节点用公钥加密随机数发回
  4. A 节点用私钥解密后发回,B 节点验证通过即允许登录

2. 密钥对生成#

2.1 基本用法#

Terminal window
ssh-keygen -t rsa

执行后提示:

Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): # 直接回车使用默认路径
Enter passphrase (empty for no passphrase): # 直接回车免密钥密码
Enter same passphrase again: # 直接回车确认

生成的文件:

文件说明
~/.ssh/id_rsa私钥(严格保密,不可外传)
~/.ssh/id_rsa.pub公钥(需分发到目标节点)

2.2 常用参数#

参数说明
-t rsa指定密钥类型为 RSA
-t ed25519使用 Ed25519 算法(更短更安全)
-b 4096指定密钥长度为 4096 位(默认 2048)
-C "注释"添加注释,通常为邮箱或用途说明
-f /path/file指定密钥文件保存路径

2.3 生成示例#

Terminal window
# 默认配置(推荐)
ssh-keygen -t rsa
# 指定密钥长度
ssh-keygen -t rsa -b 4096
# 使用 Ed25519 算法
ssh-keygen -t ed25519 -C "admin@server"
# 指定保存路径
ssh-keygen -t rsa -f /root/.ssh/mykey

3. 免密登录配置#

3.1 方式一:ssh-copy-id(推荐)#

A 节点(发起方)执行:

Terminal window
# 生成密钥对
ssh-keygen -t rsa
# 发送公钥到 B 节点(首次需输入 B 节点密码)
ssh-copy-id -i ~/.ssh/id_rsa.pub root@B节点IP

验证免密登录:

Terminal window
ssh root@B节点IP

3.2 方式二:手动复制公钥#

ssh-copy-id 不可用时(如某些精简系统),手动操作:

A 节点(发起方):

Terminal window
# 生成密钥对
ssh-keygen -t rsa
# 查看公钥内容
cat ~/.ssh/id_rsa.pub

B 节点(接收方):

Terminal window
# 创建 .ssh 目录(如果不存在)
mkdir -p ~/.ssh
chmod 700 ~/.ssh
# 将 A 的公钥追加到授权文件
echo "ssh-rsa AAAAB3... root@A节点" >> ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys

3.3 验证配置#

Terminal window
# 从 A 节点测试连接 B 节点(不应提示输入密码)
ssh root@B节点IP
# 执行远程命令测试
ssh root@B节点IP hostname

4. SSH 配置文件#

4.1 服务端配置 /etc/ssh/sshd_config#

关键参数:

# 允许 root 用户通过 SSH 登录
PermitRootLogin yes
# 启用公钥认证
PubkeyAuthentication yes
# 启用密码认证(免密配置后可设为 no 禁用密码登录)
PasswordAuthentication yes
# 指定授权密钥文件路径
AuthorizedKeysFile .ssh/authorized_keys
# SSH 端口(默认 22)
Port 22

修改后重启服务:

Terminal window
systemctl restart sshd

4.2 客户端配置 ~/.ssh/config#

为常用连接配置别名,简化登录命令:

~/.ssh/config
Host node1
HostName 192.168.1.10
User root
Port 22
IdentityFile ~/.ssh/id_rsa
Host node2
HostName 192.168.1.11
User root
Port 22
IdentityFile ~/.ssh/id_rsa
Host *
ServerAliveInterval 60
ServerAliveCountMax 3

配置后可直接使用别名登录:

Terminal window
ssh node1 # 等同于 ssh -i ~/.ssh/id_rsa root@192.168.1.10
scp file node1:/ # 等同于 scp -i ~/.ssh/id_rsa file root@192.168.1.10:/

5. 多节点批量配置#

5.1 循环分发公钥#

需要对多个节点配置免密时:

Terminal window
# 定义节点列表
NODES="192.168.1.11 192.168.1.12 192.168.1.13"
# 循环分发公钥
for node in $NODES; do
ssh-copy-id -i ~/.ssh/id_rsa.pub root@$node
done

5.2 结合 expect 实现自动化#

当需要批量输入密码时:

auto_ssh_copy_id.sh
#!/bin/bash
NODES="192.168.1.11 192.168.1.12 192.168.1.13"
PASSWORD="your_password"
for node in $NODES; do
expect -c "
spawn ssh-copy-id -i ~/.ssh/id_rsa.pub root@$node
expect {
\"password:\" { send \"$PASSWORD\r\"; exp_continue }
\"yes/no\" { send \"yes\r\"; exp_continue }
eof
}
"
done

5.3 使用 Ansible 批量配置#

Terminal window
# 使用 ping 模块验证免密连接
ansible all -m ping -o

6. 常见问题排查#

6.1 权限问题#

SSH 对目录和文件权限要求严格:

Terminal window
# 正确权限设置
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
chmod 600 ~/.ssh/id_rsa
chmod 644 ~/.ssh/id_rsa.pub
# 用户主目录权限不能为 777
chmod 755 ~

6.2 SELinux 阻止#

Terminal window
# 临时关闭 SELinux
setenforce 0
# 永久关闭
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config

6.3 防火墙阻止#

Terminal window
# 检查防火墙规则
iptables -L -n | grep 22
# 开放 SSH 端口
firewall-cmd --permanent --add-service=ssh
firewall-cmd --reload

6.4 调试连接#

Terminal window
# 详细模式查看连接过程
ssh -vvv root@目标IP
# 检查服务端日志
tail -f /var/log/secure

6.5 公钥未生效#

Terminal window
# 检查 authorized_keys 内容
cat ~/.ssh/authorized_keys
# 检查 sshd 配置
grep -E "PubkeyAuthentication|AuthorizedKeysFile" /etc/ssh/sshd_config
# 重启 sshd
systemctl restart sshd

7. 安全加固建议#

7.1 禁用密码登录#

配置免密登录后,建议禁用密码认证:

Terminal window
vi /etc/ssh/sshd_config
PasswordAuthentication no
Terminal window
systemctl restart sshd

7.2 禁用 root 远程登录#

PermitRootLogin no

7.3 修改默认端口#

Port 2222
Terminal window
# 防火墙放行新端口
firewall-cmd --permanent --add-port=2222/tcp
firewall-cmd --reload
systemctl restart sshd

7.4 限制登录用户#

AllowUsers user1 user2

7.5 使用 Fail2Ban 防暴力破解#

Terminal window
yum install -y fail2ban
systemctl enable fail2ban && systemctl start fail2ban
SSH 免密配置
https://blog.xeu.asia/posts/ssh-config/
作者
Xeu
发布于
2024-06-18
许可协议
CC BY-NC-SA 4.0