$ ls ~yifei/notes/

使用 ssh 反向隧道登录没有 IP 的服务器

Posted on:

Last modified:

假设我们家里的服务器叫做 homeserver,没有公网 IP。然后我们有一台服务器叫做 relayserver, 拥有公网 IP。

在家里执行

homeserver~$ ssh -fN -R 10022:localhost:22 relayserver_user@1.1.1.1

在服务器上就可以登陆啦

relayserver~$ ssh -p 10022 homeserver_user@localhost

然而这样链接还是很不稳定的,我们还是需要一个稳定的链接,这时候就可以使用 autossh 了,它会 保持链接的稳定,自动重新连接。

autossh -M 20000 -f -N your_public_server -R 1234:localhost:22 -C

不过如果你对这些服务器完全有权限,可以直接用 tailscale 等工具,更方便一些。

参考

  1. http://xmodulo.com/access-linux-server-behind-nat-reverse-ssh-tunnel.html
  2. https://superuser.com/questions/37738/how-to-reliably-keep-an-ssh-tunnel-open
WeChat Qr Code

© 2016-2022 Yifei Kong. Powered by ynotes

All contents are under the CC-BY-NC-SA license, if not otherwise specified.

Opinions expressed here are solely my own and do not express the views or opinions of my employer.

友情链接: MySQL 教程站