简体   繁体   中英

Connect ssh after reboot with port forwarding

I am trying to automatically connect server -> server on startup using ssh with port forwading. I need this so that the 1st server can connect to the 2nd sever's postgres DB.

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..

But the problem is, that the ssh connection is NOT made by itself on startup. 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..

Please can someone help me establish the ssh connection on startup?

Thanks in advance

I think I have solved it by adding "-N" to the ssh connection parameters. 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. 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..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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