如何使用本地 ssh 密钥访问第三台计算机?

Fra*_*ogo 2 git ssh key roaming

我想知道是否有办法使用我的本地 ssh 密钥从第三台远程机器访问 git 服务器。例如:假设我们有三台计算机:PC1、PC2 和 GITSERVER。

我将我的 ssh 密钥存储在 PC1 上,这些密钥在 GITSERVER 上获得授权,从 PC1 i ssh 到 PC2,我想从中克隆存储在 GITSERVER 上的存储库。

有没有一种方法可以在不复制我的私钥或创建新私钥的情况下做到这一点?

Saj*_*han 5

一种方法是转发 SSH 代理。首先,打开ssh_config文件并添加以下两行:

$ sudo nano /etc/ssh/ssh_config 

Host <PC2-server-ip.com>
ForwardAgent yes
Run Code Online (Sandbox Code Playgroud)

将 ssh 密钥添加到代理列表中:

$ eval "$(ssh-agent)"
$ ssh-add ~/.ssh/id_rsa          # add ssh key to list of agent 
identites
$ ssh-add -l                     # you can see the agent just added
Run Code Online (Sandbox Code Playgroud)

现在,登录到 PC2 并从 GITSERVER 克隆:

$ ssh <user>@<pc2-server-ip>
$ git clone ...
Run Code Online (Sandbox Code Playgroud)


mue*_*hsi 4

正如 Sajib Khan 所说,ssh_config 文件可以帮助您。但是,您始终随身携带私钥。在某些用例中,您可能不想在远程计算机上使用您的私钥。为此,如果您想将密钥带到远程计算机,则可以为每个主机定义:

$ vim ~/.ssh/config
Host remote-pc2 # local name of the host (You can use this on your local terminal to connect to the remote host)
HostName 127.0.0.1 # enter correct IP here
User francesco # your login name
ForwardAgent yes
Run Code Online (Sandbox Code Playgroud)

实现此目的的另一种方法:您可以启动ssh-agent并向其添加密钥,这可以在您的.bashrc

$ vim ~/.bashrc
eval `ssh-agent -s`
ssh-add ~/.ssh/id_rsa
# check if key is there
$ ssh-add -l
Run Code Online (Sandbox Code Playgroud)

这对您很有帮助,这样您就不必在每次启动 ssh 会话时输入密码。您可以使用以下命令将 ssh 密钥从本地计算机获取到另一台计算机以进行一次连接:

$ ssh -A username@remote-host
Run Code Online (Sandbox Code Playgroud)