开发者-导航 猿导航

CentOS 生成 SSH 秘钥文件

发布时间:

目录

一、CentOS 生成 SSH 密钥 #

1. 生成 RSA 密钥对(最常用) #

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

参数说明:

2. 自定义密钥保存路径(可选) #

执行命令后,系统会提示你选择保存路径,默认是 ~/.ssh/id_rsa

Enter file in which to save the key (/root/.ssh/id_rsa): 

3. 设置密码保护(可选) #

接下来,系统会提示你设置私钥的密码(passphrase):

Enter passphrase (empty for no passphrase): 

设置私钥密码就不能免密登录了

生成后会出现两个文件:


二、将公钥添加到目标服务器(或本机)上 #

将公钥拷贝到目标机器的 ~/.ssh/authorized_keys

cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys
chmod 700 ~/.ssh

三、将私钥拷贝到 Windows 本地 #

假设你使用的是 Windows 的 scp 工具或者 WinSCP / PowerShell / Git Bash,将私钥下载到 Windows(例如放在 C:\Users\Tom\.ssh\id_rsa)。

从 CentOS 拷贝:

# 在 Windows 上执行,假设你用的是 Git Bash 或 PowerShell 且 CentOS IP 是 192.168.1.10
scp user@192.168.1.10:~/.ssh/id_rsa C:\Users\Tom\.ssh\id_rsa

四、Windows 通过 SCP 使用私钥上传文件到 CentOS #

使用 Git Bash / PowerShell:

scp -i C:\Users\Tom\.ssh\id_rsa C:\path\to\local\file.txt user@192.168.1.10:/home/user/

或者使用 pscp.exe(来自 PuTTY 工具包):

pscp -i C:\path\to\private_key.ppk C:\path\to\file.txt user@192.168.1.10:/home/user/

注意:PuTTY 的私钥是 .ppk 格式,需用 PuTTYgen 将 id_rsa 转换为 .ppk


五、注意事项 #