我在私有子网中有一台服务器,我想通过面向 Internet 的机器连接到该服务器。有一些教程。我用过这个:https : //heipei.github.io/2015/02/26/SSH-Agent-Forwarding-thinked-harmful/
它的问题在于它假设我可以编辑~/.ssh/config
文件。但是,如果我在 CI 上运行代码,我宁愿使用存储库中附带的配置文件并使用-F
switch。在这种情况下,上述策略停止工作,因为所ssh
使用的命令ProxyCommand
不加载相同的配置文件并且不知道别名。我所做的是:
Host ansible
User ubuntu
Hostname xxx.compute.amazonaws.com
Host app
User ubuntu
Hostname 10.0.2.40
ProxyCommand ssh -F test-ssh.cfg -W %h:%p ansible
Run Code Online (Sandbox Code Playgroud)
这有效但有点脏,因为我需要将文件名放入文件本身,如果有人更改文件名,它会中断。所以我的问题是:有没有更简洁的方法来创建带有别名的配置文件,并且ProxyCommand
可以与-F
?
根据这个 SuperUser entry,从 7.3p1 版本开始,有一个Include
指令,因此您可以创建一个配置文件,其中包含您的“常规”配置,但包含所有ProxyCommand
条目。这样,如果您指定该文件,代理连接将起作用,如果您省略-F
开关,则将读取默认配置,如下所示:
~/.ssh/config
:
Host ansible
User ubuntu
Hostname xxx.compute.amazonaws.com
Run Code Online (Sandbox Code Playgroud)
~/.ssh/proxyconfig
:
Include config
Host app
User ubuntu
Hostname 10.0.2.40
ProxyCommand ssh -W %h:%p ansible
Run Code Online (Sandbox Code Playgroud)
如果你有像上面这样的配置,你可以使用
ssh -F proxyconfig app
Run Code Online (Sandbox Code Playgroud)
到达“应用程序”服务器。
如果您无法将上述版本安装到您的客户端计算机,您可以ProxyCommand
在命令行中指定,而不需要单独的配置文件,如下所示:
ssh -o ProxyCommand='ssh -W %h:%p ansible' app
Run Code Online (Sandbox Code Playgroud)
由于每次都编写整个命令有点不舒服,您可能需要为命令创建别名,或者 - 如果您想通过代理访问更多计算机 - 一个函数,如下所示:
function proxyssh {
# The first argument is the 'stepping stone',
# the second is the target to ssh into
ssh -o proxyCommand="ssh -W %h:%p $1" $2
}
Run Code Online (Sandbox Code Playgroud)
并像使用它一样
proxyssh ansible app
Run Code Online (Sandbox Code Playgroud)
对于代理主机,您不再需要ProxyCommand
。有一个新的 option ProxyJump
,它可以在不需要另一个ssh
配置的情况下执行相同的操作。它将在内部发出相同的命令,但如果提供,它也会传递-F
参数:
Host ansible
User ubuntu
Hostname xxx.compute.amazonaws.com
Host app
User ubuntu
Hostname 10.0.2.40
ProxyJump ansible
Run Code Online (Sandbox Code Playgroud)
此功能自OpenSSH 7.3起可用。