我有一个私人子网中的服务器,我想通过面向互联网的机器连接到该服务器。 有一些这方面的教程。 我用这个: https : //heipei.github.io/2015/02/26/SSH-Agent-Forwarding-considered-harmful/
问题在于它假设我可以编辑~/.ssh/config文件。 如果我在CI上运行代码,我宁愿使用我的仓库中的configuration文件,并使用-F开关。 在这种情况下,上述策略停止工作,因为用作ProxyCommand的ssh命令不会加载相同的configuration文件,也不知道别名。 我做的是:
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
这工作,但有点肮脏,因为我需要将文件的名称放入文件本身,如果有人更改文件名将会中断。 所以我的问题是:是否有一个更清洁的方式来创build一个configuration文件的别名和ProxyCommand可以使用-F ?
根据这个超级用户条目 ,从版本7.3p1,有一个Include指令,所以你可以创build一个configuration文件,其中包括您的“常规”configuration,但具有所有的ProxyCommand条目。 这样,如果你指定了这个文件,代理连接就可以工作,如果你省略-F开关,默认configuration将被读取,如下所示:
~/.ssh/config :
Host ansible User ubuntu Hostname xxx.compute.amazonaws.com
~/.ssh/proxyconfig :
Include config Host app User ubuntu Hostname 10.0.2.40 ProxyCommand ssh -W %h:%p ansible
如果你有如上configuration,你可以使用
ssh -F proxyconfig app
到达“应用”服务器。
如果您无法将上述版本安装到客户端计算机上,则可以在命令行中指定ProxyCommand ,而不需要单独的configuration文件,如下所示:
ssh -o ProxyCommand='ssh -W %h:%p ansible' app
由于每次编写整个命令都有点不舒服,因此可能需要为该命令创build一个别名,或者如果要通过代理访问更多的计算机,则可以使用如下函数:
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 }
并使用它
proxyssh ansible app
对于代理主机,您不再需要ProxyCommand 。 有一个新的选项ProxyJump ,而不需要另一个ssh的configuration。 它将在内部发出相同的命令,但是如果提供的话它也会传递-F参数 :
Host ansible User ubuntu Hostname xxx.compute.amazonaws.com Host app User ubuntu Hostname 10.0.2.40 ProxyJump ansible
从OpenSSH 7.3开始可以使用此function。
你可以通过使用ProxyCommand作为参数来编辑~/.ssh/config 。
从OpenSSH 5.4 (2010-03-08)已经有了“netcat模式”:
- 为ssh(1)添加了“netcat模式”:
ssh -W host:port ...这将客户端上的stdio连接到服务器上的单个端口。 例如,这允许使用ssh作为ProxyCommand来通过中间服务器路由连接。 BZ#1618
所以有可能:
ssh -o ProxyCommand="ssh -W %h:%p firewall" [email protected]
对于历史版本,您可以使用外部nc (来自Vivek Gite的SSH ProxyCommand文章 ):
ssh -o ProxyCommand='ssh user@firewall nc 10.0.2.40 22' [email protected]
哪里
firewall是面向Internet的服务器。 10.0.2.40是本地networking上的服务器。 nc )用于在本地networking上的服务器之间设置和build立TCPpipe道。