简体   繁体   English

重启后通过端口转发连接ssh

[英]Connect ssh after reboot with port forwarding

I am trying to automatically connect server -> server on startup using ssh with port forwading. 我正在尝试使用带端口转发的ssh在启动时自动连接服务器 - >服务器。 I need this so that the 1st server can connect to the 2nd sever's postgres DB. 我需要这个,以便第一台服务器可以连接到第二台服务器的postgres数据库。

For the connection I am using 对于我正在使用的连接

ssh -i /root/.ssh/id_rsa -L 5434:localhost:5432 user@ipAddress

This works fine when I try it manually and I can connect to my DB with 当我手动尝试它时,这可以正常工作,我可以连接到我的数据库

psql -U postgres -h localhost -p 5434

with having the .pgpass file in the home dir.. 在家庭目录中有.pgpass文件..

But the problem is, that the ssh connection is NOT made by itself on startup. 但问题是,ssh连接不是在启动时自行完成的。 I thought of using the sudo crontab's @reboot, but that did not work.. Then I tried to move the script to /etc/rc.local based on this but also with no luck.. 我想过使用sudo crontab的@reboot,但是没有用。然后我尝试将脚本移动到/etc/rc.local基于但也没有运气..

Please can someone help me establish the ssh connection on startup? 有人可以帮我在启动时建立ssh连接吗?

Thanks in advance 提前致谢

I think I have solved it by adding "-N" to the ssh connection parameters. 我想我已经通过在ssh连接参数中添加“-N”来解决它。 This should keep it in background and it seems to be working.. So now I have 这应该保持在后台,它似乎工作..所以现在我有

ssh -N -i /root/.ssh/id_rsa -L 5434:localhost:5432 user@ipAddress

in the root's crontab and it connects after reboot. 在root的crontab中,它在重启后连接。 This does not solve the "cold start" connection, but since it is a server it will be mostly only restarted and not powered down and started.. 这并不能解决“冷启动”连接问题,但由于它是一台服务器,因此它大部分只会重新启动而不会断电并启动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM