Visual Studio Code 配置ssh远程连接 在 Git Bash 中生成 SSH 密钥对

一、先解读报错本质

/c/Users/i-sandau/.ssh/id_rsa: No such file or directory 明确说明:在 Git Bash 对应的家目录(C:\Users\i-sandau\.ssh\)下,没有找到默认名称的 SSH 私钥文件 id_rsa。因为你之前从未执行过 SSH 密钥生成命令,所以该目录下不存在密钥对,自然无法完成私钥加载。

二、步骤 1:在 Git Bash 中生成 SSH 密钥对

这是创建 id_rsaid_rsa.pub 的核心步骤,命令通用且无需复杂配置,操作如下:
  1. 保持当前 Git Bash 窗口打开,执行以下密钥生成命令:
    bash
    运行
    ssh-keygen -t rsa
    
  2. 执行后会出现三次提示,一路直接按回车即可(使用默认配置,无需额外输入)
    • 提示 1:Enter file in which to save the key (/c/Users/i-sandau/.ssh/id_rsa): → 回车(保存到默认路径,即 C:\Users\i-sandau\.ssh\)。
    • 提示 2:Enter passphrase (empty for no passphrase): → 回车(设置密钥密码,留空则无密码,后续使用无需输入额外密码,更便捷)。
    • 提示 3:Enter same passphrase again: → 回车(确认密钥密码,留空则直接回车)。
  3. 生成成功的提示:会显示密钥的「指纹」和「随机艺术图像」,此时你可以在 C:\Users\i-sandau\.ssh\ 目录下看到两个文件:
    • id_rsa:SSH 私钥文件(核心文件,不可泄露给他人,无需手动编辑)。
    • id_rsa.pub:SSH 公钥文件(用于复制到远程服务器,可公开)。

三、步骤 2:重新加载 SSH 私钥到代理(可以忽略)

密钥生成完成后,回到 Git Bash 窗口,重新执行之前的私钥加载命令,即可成功:
bash
运行
# 1. 若 SSH 代理已启动(之前显示 Agent pid 1790,无需重新启动),直接加载私钥
ssh-add ~/.ssh/id_rsa
  1. 成功提示:Identity added: /c/Users/i-sandau/.ssh/id_rsa (/c/Users/i-sandau/.ssh/id_rsa),说明私钥已成功加载到 SSH 代理中。
  2. 若提示代理已停止,可重新启动代理再加载:
    bash
    运行
    eval $(ssh-agent -s)
    ssh-add ~/.ssh/id_rsa
    

四、步骤 3:将公钥复制到远程服务器(实现无密码 SSH 登录)

私钥加载完成后,需要将公钥 id_rsa.pub 复制到远程服务器 10.126.126.2kanye 用户下,才能实现公钥认证,步骤如下:
  1. 仍在 Git Bash 中执行以下命令,直接复制公钥到远程服务器(最便捷):
    bash
    运行
    ssh-copy-id kanye@10.126.126.2
    
  2. 执行后会提示 kanye@10.126.126.2's password: → 输入 kanye 用户的正确密码(输入无明文显示,输入完成回车即可)。
  3. 成功提示:Number of key(s) added: 1,说明公钥已成功复制到远程服务器的 ~/.ssh/authorized_keys 文件中。

 

示例:

i-sandau@DESKTOP-5L1H632 MINGW64 ~
$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/i-sandau/.ssh/id_rsa):
Enter passphrase for "/c/Users/i-sandau/.ssh/id_rsa" (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /c/Users/i-sandau/.ssh/id_rsa
Your public key has been saved in /c/Users/i-sandau/.ssh/id_rsa.pub
The key fingerprint is:
SHA256:mZ61BxlsML6CEuEw5tJO1lYSOyDQTMJ8RsFyaA0hyBg i-sandau@DESKTOP-5L1H632
The key's randomart image is:
+---[RSA 3072]----+
|EO@o+.. o        |
|*#oX + . +       |
|o.@ =   . +      |
|.+ o o   = o     |
|  o . . S +      |
|   .   o o o     |
|        o . .    |
|           .     |
|                 |
+----[SHA256]-----+

i-sandau@DESKTOP-5L1H632 MINGW64 ~
$ # 1. 若 SSH 代理已启动(之前显示 Agent pid 1790,无需重新启动),直接加载私钥
ssh-add ~/.ssh/id_rsa
Identity added: /c/Users/i-sandau/.ssh/id_rsa (i-sandau@DESKTOP-5L1H632)

i-sandau@DESKTOP-5L1H632 MINGW64 ~
$ ssh-copy-id kanye@10.126.126.2
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: ssh-add -L
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
kanye@10.126.126.2's password:

Number of key(s) added: 1

Now try logging into the machine, with: "ssh 'kanye@10.126.126.2'"
and check to make sure that only the key(s) you wanted were added.


i-sandau@DESKTOP-5L1H632 MINGW64 ~
$

 

 

 

作者: 高志远

高志远,24岁,男生

发表评论

邮箱地址不会被公开。