通过代理连接SSH

ben*_*r78 45 ssh terminal proxy

我不知道我在这里做什么,所以如果你能帮助我,请记住这一点!

我试图通过代理连接到我的虚拟服务器,但我无法连接,它只是挂起.我假设这是因为它没有通过我们的代理.

我在家尝试过完全相同的东西,它完美无缺.我正在运行使用终端进行连接的OSX.

任何人都可以告诉我如何通过SSH获取代理?

rog*_*ack 58

以下是理查德·克里斯滕森(Richard Christensen)作为单行代码的答案,无需编辑文件(用您自己的设置替换大写,PROXYPORT通常为80):

 ssh USER@FINAL_DEST -o "ProxyCommand=nc -X connect -x PROXYHOST:PROXYPORT %h %p"
Run Code Online (Sandbox Code Playgroud)

您也可以-o ...对scp 使用相同的选项,请参阅https://superuser.com/a/752621/39364

如果你在OS X中得到这个:

 nc: invalid option -- X
 Try `nc --help' for more information.
Run Code Online (Sandbox Code Playgroud)

可能是你不小心使用自制的netcat版本(你可以通过执行which -a nc命令看到 - /usr/bin/nc应该首先列出).如果有两个,那么一个解决方法是指定所需的nc的完整路径,例如ProxyCommand=/usr/bin/nc......

对于CentOS nc也有同样的问题invalid option --X.connect-proxy是一种替代方案,易于安装使用yum和工作 -

ssh -o ProxyCommand="connect-proxy -S PROXYHOST:PROXYPORT %h %p" USER@FINAL_DEST
Run Code Online (Sandbox Code Playgroud)

  • 我使用带有gnu-netcat的arch linux和`nc:invalid选项 - X`仍然存在.这个问题的解决方案是用openbsd-netcat替换gnu-netcat.有关详细信息,请参阅https://pagekite.net/wiki/Howto/SshOverPageKite/#wrongnetcat.这两个版本可能相互冲突. (9认同)
  • @Petr,--proxy命令可能对您有用。编辑:向下滚动并注意到一个示例在shoaly的答案中。 (2认同)
  • 我得到:“nc:代理错误:“HTTP/1.0 403 Forbidden”ssh_exchange_identification:连接被远程主机关闭”我该怎么办? (2认同)
  • 对于 CentOS 7,我没有 ncat 的“-X”选项,我使用了以下结构: ssh REMOTEUSER@REMOTEHOST -o "ProxyCommand=ncat REMOTEHOST 22 --proxy PROXYHOST:PROXYPORT --proxy-type http"。例如: ssh user123@ssh.othercomany.com -o "ProxyCommand=ncat ssh.mycomany.com 22 --proxy proxy.mycomany.com:80 --proxy-type http" (2认同)

Ric*_*sen 26

如果要经常使用SSH代理连接,则不必每次都将它们作为参数传递.您可以添加以下行~/.ssh/config

Host foobar.example.com
    ProxyCommand          nc -X connect -x proxyhost:proxyport %h %p
    ServerAliveInterval   10
Run Code Online (Sandbox Code Playgroud)

然后连接使用

ssh foobar.example.com
Run Code Online (Sandbox Code Playgroud)

资源:

http://www.perkin.org.uk/posts/ssh-via-http-proxy-in-osx.html


小智 22

我使用-o "ProxyCommand=nc -X 5 -x proxyhost:proxyport %h %p"ssh选项通过OSX上的socks5代理连接.

  • 您的答案是唯一在** macOS 1.12 **上运行的答案。我在ssh配置中添加了它:`ProxyCommand nc -X 5 -x proxyhost:proxyport%h%p` (2认同)
  • 在Ubuntu 16.04上完美运行谢谢! (2认同)

sho*_*aly 10

@rogerdpack for windows平台真的很难找到带-X(http_proxy)的nc.exe,但是,我发现nc可以被ncat取代,完整示例如下:

Host github.com
     HostName github.com
         #ProxyCommand nc -X connect -x 127.0.0.1:1080 %h %p
         ProxyCommand ncat --proxy 127.0.0.1:1080 %h %p
     User git
     Port 22
     IdentityFile D:\Users\Administrator\.ssh\github_key
Run Code Online (Sandbox Code Playgroud)

和--proxy与--proxy可以做一个完美的工作


Ham*_*ani 7

对于 Windows,@shoaly 参数并不完全适合我。我收到此错误:

NCAT DEBUG: Proxy returned status code 501.
Ncat: Proxy returned status code 501.
ssh_exchange_identification: Connection closed by remote host
Run Code Online (Sandbox Code Playgroud)

我想通过 ssh 连接到远程服务器,但我的网络中的 SSH 端口已关闭。我找到了两个解决方案,但第二个更好。

  • 使用 Ncat 解决问题:

    1. 我下载了Tor Browser,运行并等待连接。
    2. 我从Nmap 发行版中得到Ncat并解压到当前目录中。ncat.exe
    3. SSH 在Git Bash 中使用 Ncat 作为 ProxyCommand 并带有附加--proxy-type socks4参数:

      ssh -o "ProxyCommand=./ncat --proxy-type socks4 --proxy 127.0.0.1:9150 %h %p" USERNAME@REMOTESERVER
      
      Run Code Online (Sandbox Code Playgroud)

      注意 Ncat 的这个实现不支持socks5。

  • 更好的解决方案

    1. 执行前面的步骤 1。
    2. 在 Git Bash 中使用 connect.c 作为 ProxyCommand 的 SSH:

      ssh -o "ProxyCommand=connect -a none -S 127.0.0.1:9150 %h %p"
      
      Run Code Online (Sandbox Code Playgroud)

      注意connect.c 支持socks 版本4/4a/5。

要在git使用 ssh 的命令中使用代理(例如在使用 GitHub 时)——假设你安装了 Git Bash——C:\Program Files\Git\打开~/.ssh/config并添加以下条目:

host github.com
    user git
    hostname github.com
    port 22
    proxycommand "/c/Program Files/Git/mingw64/bin/connect.exe" -a none -S 127.0.0.1:9150 %h %p
Run Code Online (Sandbox Code Playgroud)