F
F
Fyodor2015-04-07 16:04:42
SSH
Fyodor, 2015-04-07 16:04:42

How to make ssh tunnels create and reconnect automatically?

I don't know much about linux. the task such - that from the server to be connected to databases which are located on other servers. I sort of figured out how to manually create tunnels and everything works. but as far as I understand, they work exactly until I close Putty (that is, I connect to the server via ssh too).
So here's how to make these connections run when the main server boots up and try to auto-recover in case of breaks there, let's say. I'm looking for, I read, but I don't have enough knowledge to somehow apply it. I found that there is an autossh package that seems to solve this problem, but I can’t find explanations on how to use it to solve my problem.
thanks for any help =)

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Armenian Radio, 2015-04-07
@Richard_Ferlow

autossh needs to be started along with the system, for example by pushing it into systemd .

[Unit]
Description=AutoSSH service for port 2222
After=network.target

[Service]
Environment="AUTOSSH_GATETIME=0"
ExecStart=/usr/bin/autossh -M 0 -NL 2221:localhost:2222 -o TCPKeepAlive=yes [email protected]

[Install]
WantedBy=multi-user.target

Here
Well, for it all to work, you need to create a pair of crypto keys (without a passphrase) and transfer the public key to a remote server:
Creating and sending a key pair ( in a hurry )
Answer all questions in the affirmative - Enter, do not set a passphrase
Enter the password from the remote host.
After that, it
will work without a password
. Please note that you need to do this under the same user under which the script is running, most likely - root

A
AVKor, 2015-04-07
@AVKor

man screen

V
Vlad Zhivotnev, 2015-04-07
@inkvizitor68sl

flock -n /tmp/tun-lock -c "autossh -R 8080:127.0.0.1:8080 [email protected]"
In cron for once every 5 minutes, for example.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question