我的答案其实和这里的其他答案都一样,但是,我想说明一下~/.ssh/config
和ProxyJump的用处,
假设我需要分3次跳转到一个目的地,而且,每一次跳转,我都需要特定的用户名、主机、端口和身份。由于身份条件的限制,这只能通过~/.ssh/config
配置文件来完成:
Host hop1
User user1
HostName host1
Port 22
IdentityFile ~/.ssh/pem/identity1.pem
Host hop2
User user2
HostName host2
Port 22
IdentityFile ~/.ssh/pem/identity2.pem
ProxyJump hop1
Host hop3
User user3
HostName host3
Port 22
IdentityFile ~/.ssh/pem/identity3.pem
ProxyJump hop2
从你的电脑上,你可以单独测试每一次跳转,即
$ ssh hop1 # will go from your PC to the host1 in a single step
$ ssh hop2 # will go from your PC to host1, then from host1 to host2, i.e. in two steps
$ ssh hop3 # will go from your PC to host1, then to host2, then to host3, i.e. in three steps
另一个很酷的地方是,这个文件还可以实现~/.ssh/config
文件传输,比如
$ sftp hop1 # will connect your PC to host1 (i.e. file transfer between your PC and host1)
$ sftp hop2 # will connect your PC to host1 to host2 (i.e. file transfer between your PC and host2)
$ sftp hop3 # will connect your PC to host1 to host2 to host3 (i.e. file transfer between your PC and host3)