大网数据广告活动
服务器运维问答

SSH jumping with aliases and "-F"

· 52CE 我爱测

I have a server in a private subnet to which I want to connect via a machine that faces the Internet. There are some tutorials for this. I used this one: https://heipei.github.io/2015/02/26/SSH-Agent-Forwarding-considered-harmful…

问题原文

原帖链接 · zefciu · CC BY-SA 3.0

I have a server in a private subnet to which I want to connect via a machine that faces the Internet. There are some tutorials for this. I used this one: https://heipei.github.io/2015/02/26/SSH-Agent-Forwarding-considered-harmful/

The problem with it is that it assumes I can edit the ~/.ssh/config file. If I am however running code on CI, I would rather use the config file shipped in my repository and use -F switch. In this case the above strategy stops working as the ssh command used as ProxyCommand doesn't load the the same configuration file and doesn't know the aliases. What I did was:

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

This works but is a little dirty, as I need to put the name of the file into the file itself and it would break if someone changes the filename. So my question is: is there a cleaner way to create a configuration file with aliases and ProxyCommand that could be used with -F?

已采纳解答

原帖链接 · Lacek · CC BY-SA 3.0

According to this SuperUser entry, from version 7.3p1, there is an Include directive, so you can create a config file which includes your "regular" config, but has all the ProxyCommand entries. This way, if you specify that file, the proxying connections will work, if you omit the -F switch, the default config will be read, like this:

~/.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

If you have configs like above, you can use

ssh -F proxyconfig app

to reach the "app" server.

If you can't install the aformentioned version to your client computer, you can specify the ProxyCommand in the command line, without needing a separate config file, like this:

ssh -o ProxyCommand='ssh -W %h:%p ansible' app

Since it is a bit uncomfortable to write the whole command every time, you might want to make an alias to the command, or - if you want to access more computers by proxy - a function, like this:

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
}

and use it like

proxyssh ansible app

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2017-04-07)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。