通过中间机器转发 SSH 流量

117 ssh bash git command-line tunnel

SSH 隧道让我很困惑。我想知道我是否可以在 Linux 中做到这一点。

我有3台机器..

A. My local machine at home.
B. Machine at work that I can SSH into (middle man).
C. My desktop at work that I can only SSH into from machine B.
Run Code Online (Sandbox Code Playgroud)

所以我可以从 A -> B 和 B -> C 进行 SSH,但不能从 A -> C 进行 SSH。

有没有办法设置从 A 到 B 的 SSH 隧道,所以当我运行其他 SSH 命令时,它们只能在我的本地机器 A 上工作?我基本上是在尝试将 git repo 从工作克隆到家(并且我无法在机器 B 上安装 git)。

此外,一旦设置.. 我将如何取消设置?

eph*_*ent 137

将其.ssh/config放在 hostA 上的文件中(有关详细信息,请参阅man 5 ssh_config):

# .ssh/config on hostA:
Host hostC
    ProxyCommand ssh hostB -W %h:%p
Run Code Online (Sandbox Code Playgroud)

现在以下命令将自动通过 hostB 隧道

hostA:~$ ssh hostC
Run Code Online (Sandbox Code Playgroud)

您可能希望添加诸如-oCiphers=arcfour-oClearAllForwardings=yes加快速度的选项,因为ssh在内部包装在ssh计算上更昂贵并且额外的努力和包装器在隧道传输已经加密的流量时不需要那么安全。


如果您使用的 OpenSSH 早于 5.3,则该-W选项不可用。在这种情况下,您可以使用 netcat ( nc)实现上述内容:

ProxyCommand ssh hostB nc %h %p  # or netcat or whatever you have on hostB
Run Code Online (Sandbox Code Playgroud)


小智 7

编辑:这是错误的方法。请参阅ephemient 的答案。这个答案会起作用,但可能不太安全,而且绝对不那么棒。

听起来您想要如下解决方案:

ssh -L localhost:22:machinec:22 machineb
Run Code Online (Sandbox Code Playgroud)

这将为您提供一个 shell machineb。别管这个;最小化终端窗口。

现在,只要你做的SSH连接localhost,你实际上将连接到machinec通过machineb。完成隧道后,只需关闭运行上述命令的终端即可。

请注意,您需要超级用户权限才能运行该命令。

  • 总的来说,这很邪恶……但我不得不用旧版本的 OpenSSH 或其他 ssh 客户端来做这件事。你可以选择一些像 8022 这样的高端口:这样它就不会干扰本地主机上的任何 ssh 服务,并且不需要以 root 身份运行。只需将 `-p 8022` 添加到您的 ssh 命令中。使用 git 很容易:使用 URI `ssh://localhost:8022/path/to/repo.git`。 (3认同)

小智 6

对于交互式 shell,您可以使用这个简单的命令:

ssh -J <user>@<hostB> <user>@<hostC>
Run Code Online (Sandbox Code Playgroud)

-J 选项用于jump


Peq*_*que 5

或者,由于OpenSSH 7.3引入了一个ProxyJump指令:

Host final
  ProxyJump intermediate
Run Code Online (Sandbox Code Playgroud)

在示例中:

Host intermediate
  Hostname 10.0.0.42
  User root
Host final
  Hostname final.destination.com
  User foo
  ProxyJump intermediate
Run Code Online (Sandbox Code Playgroud)

然后ssh final会跳过中间机。


Lar*_*y K 3

听起来您想要 A 上的 shell 别名导致 C 上发生 ssh

  1. 我假设在 A 上,您可以输入 ssh me@b "ssh me@c hostname" 并返回“C”
  2. 创建一个别名 sshc,将 sshc foo 扩展为 ssh me@b "ssh me@c foo"
  3. 有关创建别名的确切语法,请参阅 superuser.com