我在这里做什么其实并不清楚,请您谅解,如果您能帮助我,我会非常感激!
我正在尝试通过代理连接到我的虚拟服务器,但无法连接,连接就一直挂着。我猜测这是因为无法通过我们的代理。
我在家也尝试了完全相同的操作,效果完美。我使用Terminal在OS X上进行连接。
请问有人可以指导我如何通过SSH穿过代理吗?
我在这里做什么其实并不清楚,请您谅解,如果您能帮助我,我会非常感激!
我正在尝试通过代理连接到我的虚拟服务器,但无法连接,连接就一直挂着。我猜测这是因为无法通过我们的代理。
我在家也尝试了完全相同的操作,效果完美。我使用Terminal在OS X上进行连接。
请问有人可以指导我如何通过SSH穿过代理吗?
以下是将Richard Christensen的解答转化为一行代码的方法,不需要编辑文件(将大写字母替换为自己的设置,PROXYPORT通常为80):
ssh USER@FINAL_DEST -o "ProxyCommand=nc -X connect -x PROXYHOST:PROXYPORT %h %p"
您也可以对scp使用相同的-o ...
选项,详情请参见我的superuser答案。
如果您在OS X上遇到此问题:
nc: invalid option -- X
Try `nc --help' for more information.
可能你不小心使用了Homebrew版本的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
如果您经常使用 SSH 代理连接,您不必每次都将它们作为参数传递。您可以将以下行添加到~/.ssh/config
中。
Host foobar.example.com
ProxyCommand nc -X connect -x proxyhost:proxyport %h %p
ServerAliveInterval 10
连接使用以下步骤:
ssh foobar.example.com
proxyhost
,将proxyport
替换为--在这种情况下--8080。 - Pit我使用 -o "ProxyCommand=nc -X 5 -x proxyhost:proxyport %h %p"
ssh 选项在 OSX 上通过 socks5 代理连接。
ProxyCommand nc -X 5 -x proxyhost:proxyport %h %p
- Deniz针对@rogerdpack的回答,只是一些备注:对于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
使用 ncat 和 --proxy
参数可以完美地工作。
对于 Windows,@shoaly 的参数并没有完全适用于我。我一直收到这个错误:
NCAT DEBUG: Proxy returned status code 501.
Ncat: Proxy returned status code 501.
ssh_exchange_identification: Connection closed by remote host
我想ssh到一个远程服务器,但由于我的网络中SSH端口已经关闭。我找到了两种解决方法,但第二种更好。
To solve the problem using Ncat:
ncat.exe
into the current directory.SSH using Ncat as ProxyCommand in Git Bash with addition --proxy-type socks4
parameter:
ssh -o "ProxyCommand=./ncat --proxy-type socks4 --proxy 127.0.0.1:9150 %h %p" USERNAME@REMOTESERVER
Note that this implementation of Ncat does not support socks5.
THE BETTER SOLUTION:
SSH using connect.c as ProxyCommand in Git Bash:
ssh -o "ProxyCommand=connect -a none -S 127.0.0.1:9150 %h %p"
Note that connect.c supports socks version 4/4a/5.
要在使用ssh的git
命令中使用代理(例如在使用GitHub时)— 假设您已经在C:\Program Files\Git\
中安装了Git Bash — 打开~/.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
localhost
代替127.0.0.1
。 - Cadoizssh USERNAME@HOSTNAME -J PROXYHOSTNAME
ssh USERNAME@HOSTNAME -o "ProxyCommand=ssh PROXYHOSTNAME -W %h:%p"
或者在您的ssh配置文件中($HOME/.ssh/config):
Host HOSTNAME
User USERNAME
ProxyCommand ssh PROXYHOSTNAME -W %h:%p
最老的客户端需要使用 netcat。具体取决于 netcat 的版本和支持的选项(请参阅其他答案)。
Squid does not support some access protocols. For example, the SSH protocol is currently not supported
。 - winkmal我使用proxychains-ng中的proxychains ssh user@host
命令。默认情况下,它将使用位于127.0.0.1:9050
的socks4代理,但是您可以在/etc/proxychains.conf
配置文件中更改它,或者像这样指定另一个配置文件:proxychains -f custom.conf
proxychains
在需要身份验证的 socks 代理中非常有用,而 nc
无法处理它。 - Id2ndRProxyCommand nc -proxy xxx.com:8080 %h %p
移除 -X connect
并使用 -proxy
替代。
这对我来说是起作用的。
$ which nc
/bin/nc
$ rpm -qf /bin/nc
nmap-ncat-7.40-7.fc26.x86_64
$ ssh -o "ProxyCommand nc --proxy <addr[:port]> %h %p" USER@HOST
$ ssh -o "ProxyCommand nc --proxy <addr[:port]> --proxy-type <type> --proxy-auth <auth> %h %p" USER@HOST
这是我解决问题的方法,希望能帮助其他人。
我的系统是debian 10,且进行了最小安装。
我也遇到了同样的问题。
git clone git@github.com:nothing/nothing.git
Cloning into 'nothing'...
nc: invalid option -- 'x'
nc -h for help
ssh_exchange_identification: Connection closed by remote host
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
git clone git@github.com:nothing/nothing.git
Cloning into 'nothing'...
/usr/bin/nc: invalid option -- 'X'
nc -h for help
ssh_exchange_identification: Connection closed by remote host
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
我知道nc有不同的版本,例如openbsd-netcat
和GNU-netcat
,你可以在debian中将nc更改为openbsd版本,但我选择更改类似corkscrew
这样的软件,因为系统中这两个版本的nc名称相同,许多人不太理解。我的方法如下。
sudo apt install corkscrew
然后。
vim ~/.ssh/config
将这个文件改为这样。
Host github.com
User git
ProxyCommand corkscrew 192.168.1.22 8118 %h %p
192.168.1.22
和 8118
是我的代理服务器地址和端口,您应该根据自己的服务器地址进行更改。
它工作得很好。
感谢@han。