Featured image of post Windows 创建 SSH 密钥对及 Debian 13 配置指南 🔑

Windows 创建 SSH 密钥对及 Debian 13 配置指南 🔑

Windows 创建 SSH 密钥对及 Debian 13 配置指南 🔑 📋 目录导航 🌟 ౙ

Windows 创建 SSH 密钥对及 Debian 13 配置指南 🔑

SSH Key


📋 目录导航


🌟 简介

本指南将帮助您在 Windows 系统上创建 SSH 密钥对,并将其配置到 Debian 13 服务器,实现安全的无密码登录。

✨ 主要优势:

  • ✅ 提高服务器安全性
  • ✅ 免去每次登录输入密码的麻烦
  • ✅ 支持自动化脚本和工具
  • ✅ 符合安全最佳实践

🚀 一、在 Windows 上创建 SSH 密钥对

1. 📂 打开 Git Bash 终端

在 Windows 上安装 Git 后,右键点击任意位置,选择 “Git Bash Here” 打开终端

2. 🔑 生成 SSH 密钥对

使用以下命令生成名为 Debian13_SSH 的 RSA 密钥对:

1
ssh-keygen -t rsa -b 4096 -C "your_email@example.com" -f ~/.ssh/Debian13_SSH

参数说明:

  • -t rsa: 指定密钥类型为 RSA
  • -b 4096: 指定密钥长度为 4096 位(更安全)
  • -C "your_email@example.com": 添加注释(替换为您的邮箱)
  • -f ~/.ssh/Debian13_SSH: 指定密钥文件名称和路径

🔧 二、在 Debian 13 服务器上配置 SSH 密钥

方法 1:🤖 自动配置(推荐)

使用 ssh-copy-id 命令自动复制公钥到服务器:

1
ssh-copy-id -i ~/.ssh/Debian13_SSH.pub root@10.10.10.246

系统会提示您输入服务器密码,然后自动完成配置。

方法 2:🛠️ 手动配置

1. 👀 查看公钥内容

1
cat ~/.ssh/Debian13_SSH.pub

复制输出的全部内容(以 ssh-rsa 开头的长字符串)

2. 🔐 登录 Debian 13 服务器

1
ssh root@10.10.10.246

3. ⚙️ 在服务器上配置 SSH 密钥

1
2
3
4
5
6
# 创建 .ssh 目录(如果不存在)
mkdir -p ~/.ssh
chmod 700 ~/.ssh

# 编辑 authorized_keys 文件
nano ~/.ssh/authorized_keys

将复制的公钥内容粘贴到文件中,然后保存退出:

  • Ctrl+X
  • Y 确认保存
  • Enter 确认文件名

4. 📝 设置正确的文件权限

1
chmod 600 ~/.ssh/authorized_keys

5. 🚪 退出服务器

1
exit

✅ 三、验证 SSH 密钥配置

1. 🔐 使用 SSH 密钥登录 Debian 服务器

1
ssh -i ~/.ssh/Debian13_SSH root@10.10.10.246

如果无需输入密码直接登录,则配置成功。

使用 Ctrl+D 退出 Debian 终端。

2. ⚡ 简化 SSH 登录配置

创建或编辑 SSH 配置文件:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
cat >> ~/.ssh/config << 'EOF'

# Debian13_SSH 配置
Host debian13
    HostName 10.10.10.246
    User root
    IdentityFile ~/.ssh/Debian13_SSH
    ServerAliveInterval 60
    ServerAliveCountMax 5
EOF

# 设置正确的文件权限
chmod 600 ~/.ssh/config

3. 🎯 使用简化命令登录

配置完成后,可以直接使用以下命令登录:

1
ssh debian13

🔒 四、SSH 服务安全配置(可选但推荐)

1. 📝 编辑 SSH 服务器配置

1
2
ssh root@10.10.10.246
nano /etc/ssh/sshd_config

2. 🛡️ 推荐的安全配置

找到并修改以下配置项:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 禁用 root 密码登录(确保密钥登录正常工作后再启用)
# PermitRootLogin prohibit-password

# 禁用密码认证(确保密钥登录正常工作后再启用)
PasswordAuthentication no

# 使用更安全的密钥算法
PubkeyAuthentication yes

# 限制最大认证尝试次数
MaxAuthTries 3

# 设置空闲超时时间
ClientAliveInterval 300
ClientAliveCountMax 2

# 只允许特定用户登录(可选)
# AllowUsers root your_username

# 使用更安全的加密算法
Ciphers chacha20-poly1305@openssh.com,aes256-gcm@openssh.com,aes128-gcm@openssh.com,aes256-ctr,aes192-ctr,aes128-ctr
MACs hmac-sha2-512-etm@openssh.com,hmac-sha2-256-etm@openssh.com,umac-128-etm@openssh.com
KexAlgorithms curve25519-sha256,curve25519-sha256@libssh.org,diffie-hellman-group-exchange-sha256

3. 🔄 重启 SSH 服务

1
systemctl restart ssh

4. 🧪 测试新配置

在另一个终端中测试连接,确保配置正确:

1
ssh -v debian13

确认连接成功后,再关闭当前会话。


🛡️ 五、安全最佳实践

1. 🔐 保护私钥文件

1
2
# 设置正确的文件权限
chmod 600 ~/.ssh/Debian13_SSH

2. 🎫 使用密码保护私钥(可选)

如果您在生成密钥时没有设置密码,可以使用以下命令添加:

1
ssh-keygen -p -f ~/.ssh/Debian13_SSH

3. 💾 备份密钥对

~/.ssh/Debian13_SSH~/.ssh/Debian13_SSH.pub 文件备份到安全的位置。

4. 🌐 多服务器配置

如果您有多个服务器,可以为每个服务器创建不同的密钥对:

1
ssh-keygen -t rsa -b 4096 -C "your_email@example.com" -f ~/.ssh/Server2_SSH

🔍 六、故障排除

1. 🚫 权限问题

如果遇到权限错误,请检查文件权限:

1
2
3
4
# 在 Windows Git Bash 中
chmod 700 ~/.ssh
chmod 600 ~/.ssh/Debian13_SSH
chmod 644 ~/.ssh/Debian13_SSH.pub

2. ❌ 连接被拒绝

确保 Debian 13 服务器上的 SSH 服务正在运行:

1
2
# 在 Debian 13 服务器上
systemctl status ssh

3. 🔐 认证失败

检查公钥是否正确添加到服务器的 authorized_keys 文件中:

1
2
# 在 Debian 13 服务器上
cat ~/.ssh/authorized_keys

4. 🐛 调试连接问题

使用详细模式连接以诊断问题:

1
ssh -v -i ~/.ssh/Debian13_SSH root@10.10.10.246

💡 七、高级用法

1. 🤵 使用 SSH 代理管理密钥

1
2
3
4
5
# 启动 SSH 代理
eval "$(ssh-agent -s)"

# 添加私钥到代理
ssh-add ~/.ssh/Debian13_SSH

2. 🔄 端口转发

通过 SSH 建立安全隧道:

1
2
3
4
5
# 本地端口转发
ssh -L 8080:localhost:80 debian13

# 远程端口转发
ssh -R 9090:localhost:3000 debian13

3. 📁 SCP 文件传输

使用 SSH 密钥进行安全文件传输:

1
2
3
4
5
# 从本地复制到远程
scp -i ~/.ssh/Debian13_SSH localfile.txt root@10.10.10.246:/path/to/destination/

# 从远程复制到本地
scp -i ~/.ssh/Debian13_SSH root@10.10.10.246:/path/to/file.txt ./

4. 🔄 RSYNC 同步文件

使用 RSYNC 通过 SSH 同步文件:

1
rsync -avz -e "ssh -i ~/.ssh/Debian13_SSH" /local/path/ root@10.10.10.246:/remote/path/

📚 八、常用 SSH 命令参考

命令 说明
ssh-keygen -t rsa -b 4096 🔑 生成 RSA 密钥对
ssh-copy-id user@host 📤 自动复制公钥到远程主机
ssh -i key.pem user@host 🔐 使用特定密钥连接
ssh -v user@host 🐛 详细模式连接(调试用)
ssh -p 2222 user@host 🚪 指定端口连接

🚀 通过以上步骤,您已经成功创建并配置了 SSH 密钥对,可以实现到 Debian 13 服务器的安全无密码登录。这不仅提高了安全性,还大大简化了登录过程!