如何通过 SSH 隧道为 SSH 连接配置快捷方式

Sim*_*tti 22 ubuntu ssh ssh-tunnel

我公司的生产服务器(FOO、BAR...)位于两个网关服务器(A、B)后面。为了连接到服务器 FOO,我必须使用我的用户名 JOHNDOE 打开与服务器 A 或 B 的 ssh 连接,然后从 A(或 B)我可以访问任何使用标准用户名打开 SSH 连接的生产服务器(我们称之为威比)。

所以,每次我必须做这样的事情:

ssh johndoe@a
...
ssh webby@foo
...
# now I can work on the server
Run Code Online (Sandbox Code Playgroud)

可以想象,当我需要使用scp或需要快速打开多个连接时,这很麻烦。

我已经配置了一个 ssh 密钥,并且我正在使用 .ssh/config 作为一些快捷方式。

我想知道我是否可以创建某种 ssh 配置以便输入

ssh foo
Run Code Online (Sandbox Code Playgroud)

并让 SSH 为我打开/转发所有连接。是否可以?

编辑

womble 的回答正是我要找的,但现在看来我不能使用 netcat,因为它没有安装在网关服务器上。

weppos:~ weppos$ ssh foo -vv
OpenSSH_5.1p1, OpenSSL 0.9.7l 28 Sep 2006
debug1: Reading configuration data /Users/xyz/.ssh/config
debug1: Applying options for foo
debug1: Reading configuration data /etc/ssh_config
debug2: ssh_connect: needpriv 0
debug1: Executing proxy command: exec ssh a nc -w 3 foo 22
debug1: permanently_drop_suid: 501
debug1: identity file /Users/xyz/.ssh/identity type -1
debug2: key_type_from_name: unknown key type '-----BEGIN'
debug2: key_type_from_name: unknown key type 'Proc-Type:'
debug2: key_type_from_name: unknown key type 'DEK-Info:'
debug2: key_type_from_name: unknown key type '-----END'
debug1: identity file /Users/xyz/.ssh/id_rsa type 1
debug2: key_type_from_name: unknown key type '-----BEGIN'
debug2: key_type_from_name: unknown key type 'Proc-Type:'
debug2: key_type_from_name: unknown key type 'DEK-Info:'
debug2: key_type_from_name: unknown key type '-----END'
debug1: identity file /Users/xyz/.ssh/id_dsa type 2
bash: nc: command not found
ssh_exchange_identification: Connection closed by remote host
Run Code Online (Sandbox Code Playgroud)

wom*_*ble 36

作为凯尔答案的更具体版本,您要放入~/.ssh/config文件的内容是:

host foo
  User webby
  ProxyCommand ssh a nc -w 3 %h %p

host a
  User johndoe
Run Code Online (Sandbox Code Playgroud)

然后,当您运行“ssh foo”时,SSH 将尝试 SSH 到johndoe@a,运行netcat( nc),然后webby@foo通过此隧道执行 SSH 到。魔法!

当然,为了做到这一点,需要在网关服务器上安装netcat;该软件包适用于每个主要发行版和操作系统。


Kyl*_*ndt 7

您可以在 ~/.ssh/config 文件中使用 ProxyCommand 指令,例如使用 netcat 作为中继:

host server2
    ProxyCommand ssh server1 nc server2 22
Run Code Online (Sandbox Code Playgroud)

您只需使用'ssh server2'。该指令的手册页信息可在“man ssh_config”中找到


Ins*_*yte 5

我更喜欢一种不同的方法,它维护一个到网关服务器的预先验证的隧道。在~/.ssh/config

Host a
    ControlMaster auto
    ControlPath ~/.ssh/control-master/%r@%h:%p
Run Code Online (Sandbox Code Playgroud)

然后在.bashrc

s () {
        if ( ssh -O check a 2>&1 > /dev/null 2>&1 )
        then
                ssh -t a ssh $1
        else
                if [[ -S ~/.ssh/control-master/insyte@a:22 ]]
                then
                        echo "Deleting stale socket..."
                        rm ~/.ssh/control-master/insyte@a:22
                fi
                echo "Opening master session..."
                if ssh -Nf a
                then
                         ssh -t a ssh $1
                fi
        fi
 }
Run Code Online (Sandbox Code Playgroud)

所以要连接到 foo:

s foo
Run Code Online (Sandbox Code Playgroud)

第一次连接时,它将根据“a”对您进行身份验证,并打开一个持久的、后台的 ssh 隧道。对“s”的后续调用将通过预先验证的隧道几乎立即打开。

效果很好。