🔐 Linux SSH密钥创建完整教程
掌握Linux SSH密钥的创建和管理,为你的服务器提供安全、便捷的远程访问方式!
📋 什么是SSH密钥? SSH密钥简介 SSH密钥是一种基于非对称加密的身份验证方式,比传统的用户名密码登录更安全、更方便。它由一对密钥组成:
私钥 :保存在本地,用于身份验证
公钥 :上传到服务器,用于验证身份
为什么使用SSH密钥?
🔒 安全性更高 :无法被暴力破解
🚀 登录更快 :无需输入密码
🛡️ 防中间人攻击 :密钥具有唯一性
📱 支持自动化 :脚本和CI/CD流程
🛠️ 第一步:生成SSH密钥对 基本命令格式 1 ssh-keygen -t rsa -C "your_email@example.com"
参数说明 :
-t rsa:指定密钥类型为RSA(推荐)
-C:添加注释,通常使用邮箱地址
你也可以使用 -t ed25519 生成更现代的Ed25519密钥
完整生成过程 1 2 3 4 5 ssh-keygen -t rsa -b 4096 -C "your_email@example.com" ssh-keygen -t ed25519 -C "your_email@example.com"
执行后的交互过程 :
1 2 3 4 5 6 Enter file in which to save the key (/root/.ssh/id_rsa): Created directory '/root/.ssh' . Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /root/.ssh/id_rsa. Your public key has been saved in /root/.ssh/id_rsa.pub.
密钥文件说明 生成完成后,会在 ~/.ssh/ 目录下创建两个文件:
id_rsa:私钥文件(保密)
id_rsa.pub:公钥文件(可分享)
🔧 第二步:配置SSH密钥 查看生成的密钥 1 2 3 4 5 6 7 8 cd ~/.sshls -lacat id_rsa.pub
设置正确的文件权限 1 2 3 4 5 6 7 8 chmod 600 ~/.ssh/id_rsachmod 644 ~/.ssh/id_rsa.pubchmod 700 ~/.ssh
权限说明 :
私钥:600(所有者读写,组和其他用户无权限)
公钥:644(所有者读写,组和其他用户只读)
.ssh目录:700(只有所有者可访问)
📤 第三步:将公钥上传到服务器 方法一:使用ssh-copy-id(推荐) 1 2 3 4 5 6 7 8 ssh-copy-id username@server_ip ssh-copy-id -p 2222 username@server_ip ssh-copy-id -i ~/.ssh/id_rsa.pub username@server_ip
方法二:手动复制 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 cat ~/.ssh/id_rsa.pubssh username@server_ip mkdir -p ~/.sshecho "你的公钥内容" >> ~/.ssh/authorized_keyschmod 700 ~/.sshchmod 600 ~/.ssh/authorized_keys
方法三:使用scp命令 1 2 3 4 5 6 7 scp ~/.ssh/id_rsa.pub username@server_ip:~/ ssh username@server_ip cat ~/id_rsa.pub >> ~/.ssh/authorized_keysrm ~/id_rsa.pub
🔌 第四步:测试SSH连接 基本连接测试 1 2 3 4 5 6 7 8 ssh username@server_ip ssh -p 2222 username@server_ip ssh -i ~/.ssh/id_rsa username@server_ip
详细连接信息 1 2 3 4 5 6 7 8 ssh -v username@server_ip ssh -vv username@server_ip ssh -vvv username@server_ip
⚙️ 第五步:SSH客户端配置 创建SSH配置文件 创建 ~/.ssh/config 文件来简化连接:
配置文件示例 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 Host * ServerAliveInterval 60 ServerAliveCountMax 3 Compression yes Host myserver HostName 192.168.1.100 User myusername Port 22 IdentityFile ~/.ssh/id_rsa IdentitiesOnly yes Host production HostName production.example.com User deploy Port 2222 IdentityFile ~/.ssh/production_key StrictHostKeyChecking no
使用别名连接 配置完成后,可以直接使用别名连接:
1 2 3 4 5 ssh myserver ssh -i ~/.ssh/id_rsa myusername@192.168.1.100
🔒 安全最佳实践 密钥管理
🔐 设置密码短语 :为私钥添加密码保护
📁 安全存储 :私钥存储在安全位置,不要上传到云端
🗑️ 定期更换 :建议每6-12个月更换一次密钥
📧 有意义注释 :使用邮箱或用途作为密钥注释
服务器安全 1 2 3 4 5 6 7 8 9 10 11 sudo nano /etc/ssh/sshd_configPasswordAuthentication no PubkeyAuthentication yes PermitRootLogin no MaxAuthTries 3 sudo systemctl restart sshd
防火墙配置 1 2 3 sudo ufw allow from 192.168.1.0/24 to any port 22sudo ufw allow from 10.0.0.0/8 to any port 22
🐛 常见问题解决 问题1:权限被拒绝 错误信息 :Permission denied (publickey)
可能原因 :
私钥权限设置错误
公钥未正确添加到服务器
服务器SSH配置问题
解决方法 :
1 2 3 4 5 6 7 8 ls -la ~/.ssh/id_rsachmod 600 ~/.ssh/id_rsassh -v username@server_ip
问题2:密钥不被接受 错误信息 :Server refused our key
解决方法 :
1 2 3 4 5 6 7 8 cat ~/.ssh/authorized_keysls -la ~/.ssh/ssh-copy-id username@server_ip
问题3:连接超时 可能原因 :
解决方法 :
1 2 3 4 5 6 7 8 sudo systemctl status sshsudo ufw statusping server_ip
📚 进阶技巧 多密钥管理 1 2 3 4 5 6 7 8 9 10 ssh-keygen -t ed25519 -f ~/.ssh/github_key -C "github@example.com" ssh-keygen -t ed25519 -f ~/.ssh/server_key -C "server@example.com" Host github.com IdentityFile ~/.ssh/github_key Host myserver IdentityFile ~/.ssh/server_key
密钥代理 1 2 3 4 5 6 7 8 9 10 11 eval "$(ssh-agent -s) " ssh-add ~/.ssh/id_rsa ssh-add -l ssh-add -d ~/.ssh/id_rsa
批量部署密钥 1 2 3 4 5 for server in server1 server2 server3; do ssh-copy-id username@$server done
🎯 总结 通过本教程,你已经掌握了:
✅ SSH密钥对的生成和配置
✅ 公钥的安全部署方法
✅ SSH客户端的配置和优化
✅ 安全最佳实践和故障排除
✅ 进阶技巧和多密钥管理
下一步建议
🔧 学习SSH隧道和端口转发
📁 配置SSH密钥的自动部署
🚀 集成到CI/CD流程中
🔒 深入学习SSH安全配置
💡 提示 :SSH密钥是现代服务器管理的基础,熟练掌握后将为你的运维工作带来极大便利。
如有问题,欢迎在下方评论区留言,我会及时回复!
标签:#Linux #SSH #密钥管理 #远程连接 #安全认证 #服务器管理