🔐 Ubuntu SSH密钥登录配置实践 - 涵盖不同ubuntu版本
本指南详细介绍如何在Ubuntu系统上配置SSH密钥登录,涵盖不同版本(16.04/18.04/20.04/22.04/24.04/26.04)的差异和最佳实践。SSH密钥登录比密码登录更安全、更高效。
📋 准备工作
步骤1:检查系统版本
首先确认您的Ubuntu版本,因为不同版本的SSH配置可能有所不同:
# 查看Ubuntu版本
lsb_release -a
cat /etc/os-release步骤2:更新系统
确保系统已更新到最新状态:
sudo apt update
sudo apt upgrade -y🔑 第一阶段:生成SSH密钥对
步骤1:进入SSH目录
在客户端机器上执行:
cd ~/.ssh
ls -la步骤2:生成SSH密钥对
推荐使用ed25519算法(现代系统),如果系统不支持则使用RSA:
# 使用ed25519算法(推荐,Ubuntu 20.04+)
ssh-keygen -t ed25519 -C "your_email@example.com"
# 如果系统不支持ed25519,使用RSA 4096位
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"⚠️ 注意:
按Enter键接受默认文件名(通常是id_ed25519或id_rsa)
可以设置密码保护私钥,也可以留空(不推荐用于生产环境)
建议为每个服务器使用不同的密钥对
🚪 第二阶段:在服务器上配置SSH密钥
步骤1:登录服务器
使用当前可用的方式登录服务器(可能是密码或之前的密钥):
ssh username@server_ip步骤2:创建.ssh目录
确保.ssh目录存在并设置正确的权限:
mkdir -p ~/.ssh
chmod 700 ~/.ssh步骤3:上传公钥到服务器
有几种方法可以将公钥传输到服务器:
方法1:使用ssh-copy-id(推荐)
# 在客户端执行
ssh-copy-id -i ~/.ssh/id_ed25519.pub username@server_ip方法2:手动复制公钥
# 在服务器上执行
echo "粘贴公钥内容到这里" >> ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys🚨 重要权限设置:
# 设置正确的权限
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
# 如果使用root用户,还需要设置
chmod 700 /root
chown root:root /root⚙️ 第三阶段:配置SSH服务器
步骤1:编辑SSH配置文件
sudo nano /etc/ssh/sshd_config配置文件内容(针对不同版本)
Ubuntu 16.04/18.04:
# RSA认证
RSAAuthentication yes
PubkeyAuthentication yes
# 允许root登录(根据需求选择)
PermitRootLogin yes
PermitRootLogin prohibit-password
# 密码认证(初始设为yes,测试成功后改为no)
PasswordAuthentication yesUbuntu 20.04+(推荐配置):
# 允许密钥认证
PubkeyAuthentication yes
# 禁用密码认证(仅限密钥)
PasswordAuthentication no
# root设置
PermitRootLogin prohibit-password
# 禁用空密码
PermitEmptyPasswords no
# 使用更强的算法
HostKey /etc/ssh/ssh_host_ed25519_key
HostKey /etc/ssh/ssh_host_rsa_key
# 只允许强算法
KexAlgorithms curve25519-sha256,curve25519-sha256@libssh.org,diffie-hellman-group16-sha512
Ciphers chacha20-poly1305@openssh.com,aes256-gcm@openssh.com
MACs hmac-sha2-512-etm@openssh.com步骤2:重启SSH服务
根据Ubuntu版本选择适当的重启命令:
🧪 第四阶段:测试SSH密钥登录
步骤1:测试密钥登录
关闭当前SSH会话,使用密钥重新登录:
# 默认密钥
ssh username@server_ip
# 指定密钥文件
ssh -i ~/.ssh/id_ed25519 username@server_ip✅ 成功标志:
如果能成功登录且不需要输入密码,说明密钥认证配置成功。
步骤2:测试密码登录(如果已禁用)
如果已经禁用密码登录,尝试使用密码登录应该被拒绝:
ssh username@server_ip🔒 第五阶段:安全加固
步骤1:禁用密码登录
确认密钥登录正常工作后,禁用密码登录:
sudo nano /etc/ssh/sshd_config修改以下配置:
# 禁用密码认证
PasswordAuthentication no
# 只允许密钥认证
PubkeyAuthentication yes
# root只允许密钥登录
PermitRootLogin prohibit-password步骤2:限制登录用户
在sshd_config中添加,只允许特定用户使用SSH:
# 只允许特定用户
AllowUsers alice bob
# 或只允许某个组
AllowGroups sshusers步骤3:更改默认SSH端口
将SSH端口从22改为其他端口(如2222):
# 在sshd_config中
Port 2222步骤4:配置Fail2Ban防护
安装并配置Fail2Ban防止暴力破解:
# 安装Fail2Ban
sudo apt install fail2ban -y
# 创建配置文件
sudo nano /etc/fail2ban/jail.localFail2Ban配置示例
[DEFAULT]
bantime = 1h
findtime = 10m
maxretry = 3
[sshd]
enabled = true
port = 22,2222
filter = sshd
logpath = /var/log/auth.log
maxretry = 3
bantime = 1h📊 不同Ubuntu版本的差异
🛠️ 常见问题解决
Q1: "Permission denied (publickey,password)"
解决方案:
# 检查权限
ls -la ~/.ssh/
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
# 检查SSH配置
sudo grep -E "PubkeyAuthentication|PasswordAuthentication" /etc/ssh/sshd_config
# 查看日志
sudo tail -f /var/log/auth.logQ2: Root用户无法使用密钥登录
解决方案:
# 检查root目录权限
sudo chmod 700 /root
sudo chown root:root /root
# 检查root的authorized_keys
sudo ls -la /root/.ssh/authorized_keys
sudo chmod 600 /root/.ssh/authorized_keys
# 确保PermitRootLogin配置正确
# PermitRootLogin prohibit-password 或 PermitRootLogin yesQ3: SSH连接超时
解决方案:
# 检查防火墙
sudo ufw status
sudo ufw allow 22 # 或自定义端口
# 检查SSH服务状态
sudo systemctl status ssh
# 检查网络连接
telnet server_ip 22
或
nc -zv server_ip 22🔧 高级配置
1. 使用SSH Agent Forwarding
# 在客户端配置
echo "ForwardAgent yes" >> ~/.ssh/config
# 使用时添加 -A 参数
ssh -A username@server_ip2. 配置SSH隧道
# 创建本地端口转发
ssh -L 8080:localhost:80 username@server_ip
# 创建远程端口转发
ssh -R 8080:localhost:80 username@server_ip3. 使用SSH配置文件
~/.ssh/config 示例
# 服务器别名
Host myserver
HostName 192.168.1.100
User alice
Port 2222
IdentityFile ~/.ssh/id_ed25519
ForwardAgent yes
ServerAliveInterval 60
# 默认配置
Host *
ServerAliveInterval 300
ServerAliveCountMax 3📝 脚本自动化
一键配置脚本
#!/bin/bash
# Ubuntu SSH密钥配置脚本
# 作者:AI助手
# 日期:2026-09-13
SSH_DIR="$HOME/.ssh"
CONFIG_FILE="/etc/ssh/sshd_config"
echo "开始配置SSH密钥登录..."
# 1. 生成SSH密钥对
echo "生成SSH密钥对..."
cd "$SSH_DIR"
ssh-keygen -t ed25519 -C "ubuntu@$(hostname)" -f "$SSH_DIR/id_ed25519" -N ""
# 2. 设置权限
echo "设置文件权限..."
chmod 700 "$SSH_DIR"
chmod 600 "$SSH_DIR/id_ed25519"
chmod 644 "$SSH_DIR/id_ed25519.pub"
# 3. 备份并编辑SSH配置
echo "配置SSH服务器..."
sudo cp "$CONFIG_FILE" "$CONFIG_FILE.backup"
# 确保配置正确
sudo sed -i 's/^#PubkeyAuthentication yes$/PubkeyAuthentication yes/' "$CONFIG_FILE"
sudo sed -i 's/^#PasswordAuthentication yes$/PasswordAuthentication no/' "$CONFIG_FILE"
sudo sed -i 's/^PermitRootLogin yes$/PermitRootLogin prohibit-password/' "$CONFIG_FILE"
# 4. 重启SSH服务
echo "重启SSH服务..."
if [ -f /bin/systemctl ]; then
sudo systemctl restart ssh
else
sudo service ssh restart
fi
echo "配置完成!"
echo "公钥内容:"
cat "$SSH_DIR/id_ed25519.pub"🎯 最佳实践总结
安全建议
✅ 使用Ed25519或RSA 4096位密钥
✅ 为每个服务器使用不同的密钥
✅ 设置强密码保护私钥(生产环境)
✅ 禁用密码登录,只使用密钥认证
✅ 更改默认SSH端口
✅ 使用Fail2Ban防止暴力破解
✅ 定期轮换SSH密钥
✅ 限制SSH访问的IP地址
注意事项
⚠️ 在禁用密码登录前,确保密钥登录正常工作
⚠️ 保留一个管理员账户的密码访问作为备用
⚠️ 定期备份SSH密钥
⚠️ 在生产环境更改配置前,先在测试环境验证
🔍 参考资料
总结
SSH密钥登录是Ubuntu服务器安全的重要实践。通过本指南的配置,您已经实现了比密码登录更安全、更高效的远程访问方式。记住,安全是一个持续的过程,定期审查和更新您的安全配置。
评论区