1

利用autossh和中间主机为内网主机建立稳定ssh 连接

 3 years ago
source link: http://blog.foool.net/2021/09/%e5%88%a9%e7%94%a8autossh%e5%92%8c%e4%b8%ad%e9%97%b4%e4%b8%bb%e6%9c%ba%e4%b8%ba%e5%86%85%e7%bd%91%e4%b8%bb%e6%9c%ba%e5%bb%ba%e7%ab%8b%e7%a8%b3%e5%ae%9assh-%e8%bf%9e%e6%8e%a5/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
← 上一篇

利用autossh和中间主机为内网主机建立稳定ssh 连接

Posted on 2021/09/14

通常会遇到一些内网主机没有独立IP 地址,隐藏在NAT 之后,用户无法直接建立ssh 连接。

这时候就需要一个中间人机器(具有独立IP)做为跳板,内网机器反向连接至中间机器。用户登陆时,首先连接至中间机器,再反向连接至内网主机。

其步骤如下:

  1. 在内网主机,运行 ssh -R 7777:middleman_machine:22
  2. 在中间主机,运行 ssh -p 7777 user@localhost

ssh -R 参数中7777 是远端映射的端口,连接该端口将建立起和内网22 号端口的链接;

但这样存在两个问题:1)ssh 连接超过固定时间会自动释放;2)每次连接中间机器都需要用户手动输入密码。

第一个问题通过autossh 解决

autossh 通过将ssh 命令包裹至一个循环中,并在ssh 命令断开时自动建立连接,这样就保证了即使内网机器无法访问,也会自动建立和中间主机的逆向连接。autossh 命令格式如下

autossh [autossh options] [ssh options]

即autossh 除了自身参数,其他参数直接用ssh 的即可。

第二个问题通过公钥免密码登录解决:1)内网主机执行ssh-keygen;2)ssh-copy-id -i ~/.ssh/id_rsa.pub user@middleman_machine

结合起autossh 和免密码登录,autossh 命令如下:

autossh -o "PasswordAuthentication=no" -o "PubkeyAuthentication=yes" -i ~/.ssh/id_rsa -R 7777:middleman:22 user@middleman

将该命令添加至开机启动模块中实现开机启动。

此条目由qing发表在其他分类目录,并贴了计算机标签。将固定链接加入收藏夹。

发表评论 取消回复

您的电子邮箱地址不会被公开。 必填项已用*标注

评论

显示名称 *

电子邮箱地址 *

网站地址

在此浏览器中保存我的显示名称、邮箱地址和网站地址,以便下次评论时使用。

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK