如何使用 autossh 获取持久的反向 SSH 隧道



我设法在Raspberry Pi 2和我的服务器(具有静态IP的服务器)之间创建了一个反向SSH隧道,并且工作正常。我在服务器上使用的用户帐户称为"ksproxy"(它不是真正的"代理",而是其他什么)。

现在我正在尝试使autossh(来自Debian/Raspbian软件包autossh)也可以工作,但我没有成功。我可能很接近。

(我在这个问题中更改了真实 IP,37.xxx.yyy.zzz不发布服务器的实际 IP)

这是工作正常的方法:(没有自动)

在 RPI 上:

rspi@antlia:~ $ ssh -N -R 20000:localhost:22 ksproxy@37.xxx.yyy.zzz

在服务器(具有静态 IP 的服务器)上:

ksproxy@37.xxx.yyy.zzz:~$ ssh rspi@localhost -t -p 20000
rspi@localhost's password:
rspi@antlia:~ $

所以一切正常:我输入密码,然后得到一个终端/提示。

我甚至可以从我的桌面访问树莓派(首先通过服务器),执行以下操作:

ssh -t ksproxy@37.xxx.yyy.zzz "ssh rspi@localhost -p 20000"
ksproxy@37.xxx.yyy.zzz password:
rspi@localhost's password:
...
rspi@antlia:~

它首先询问服务器的密码,然后询问Pi的密码,一切都很好。

目前为止,一切都好。

现在我尝试相同的方法,但这次使用 autossh:

rspi@antlia:~ $ autossh -M 20000 -N -i /home/rspi/.ssh/id_rsa ksproxy@37.xxx.yyy.zzz
ksproxy@37.xxx.yyy.zzz:~$ ssh rspi@localhost -p 20000

这"有效",但它只是卡在那里,什么都不做。

我试图"-vvv"ssh命令的输出,但它只是表明什么都没有发生。

如果我尝试另一个端口,它会失败:

ksproxy@37.xxx.yyy.zzz:~$ ssh rspi@localhost -p 1234
ssh: connect to host localhost port 1234: Connection refused

如果我尝试正确的端口 (20000),但这次使用 -t 参数,同样的事情:它"有效",但我没有得到没有终端/提示。

这是-vvv输出

ksproxy@37.xxx.yyy.zzz:~$ ssh -vvv rspi@localhost -t -p 20000
OpenSSH_6.7p1 Debian-5+deb8u1, OpenSSL 1.0.1k 8 Jan 2015
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: Applying options for *
debug2: ssh_connect: needpriv 0
debug1: Connecting to localhost [127.0.0.1] port 20000.
debug1: Connection established.
debug1: key_load_public: No such file or directory
debug1: identity file /home/ksproxy/.ssh/id_rsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/ksproxy/.ssh/id_rsa-cert type -1
debug1: key_load_public: No such file or directory
...
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_6.7p1 Debian-5+deb8u1

它不要求输入密码,也不显示任何终端/提示。

我在这里不理解或做错了什么?

请注意,我不认为这是一个防火墙问题,因为"非autossh"方法工作正常(但是我没有得到自动"始终启动"/重新连接功能)。我真的很想让 autossh 工作(我知道我可以找到一种解决方法,比如一些 crontab 自动重新启动我的手动 SSH 隧道,但这可能比让 autossh 工作更脆弱)。

autossh-N-L-R或任何其他转发都不会建立永久的ssh连接,而这种连接确实没有多大作用。 您需要为 Autossh 提供相同(或可比较)的参数。

最新更新