2

I want to set up a tunnel to my home server which will be established right after my machine boots. Using this answer, I've got my autossh command set up inside a service, which has been set to run at boot. I want the tunnel to work over the internet, and have a dynamic DNS service to accommodate this.

If I run the service manually, the tunnel will connect successfully. The issue occurs when the service is launched automatically after boot. The service starts okay, however the connection to my server is not established since my hostname can't be resolved.

ssh child pid is 1413
ssh: Could not resolve hostname my.host.name: Temporary failure in name resolution
ssh exited with error status 255; restarting ssh

My service file is as follows:

[Unit]
Description=Creates tunnel to server
After=network.target

[Service]
User=james
Environment="AUTOSSH_GATETIME=0"
ExecStart=/usr/bin/autossh -M 0 -o "StrictHostKeyChecking=no" -o "ServerAliveInterval 30" -o "ServerAliveCountMax 3" -NR 3030:localhost:22 james@my.host.name

[Install]
WantedBy=multi-user.target

Is there any way to get around this?

0

Replace

After=network.target

with

After=network-online.target
  • The autossh session would still not connect after making this change. Turns out this was actually caused by another error which would prevent autossh from re-connecting after a certain error was thrown. – Top Cat Feb 3 '18 at 19:53
0

Apologies, but it turns out there was an error which I wasn't paying attention to:

Error: remote port forwarding failed for listen port 3030

This error would be displayed after the could not resolve hostname errors.

Although I'm not sure what could be causing the error, I've managed to work around it by adding ExitOnForwardFailure yes to my /etc/ssh/ssh_config. This will exit the ssh session when that error is thrown.

Seems like the underlying ssh session was hanging due to this error. This prevented autossh from attempting to re-connect to my server. With this change to my config, autossh will now attempt to retry the ssh connection upon that error being thrown.

systemctl status is now flooded with these errors as autossh attempts to connect multiple times:

starting ssh (count 16)
ssh child pid is 2463
Error: remote port forwarding failed for listen port 3030
ssh exited with error status 255; restarting ssh
starting ssh (count 17)
ssh child pid is 2473
Error: remote port forwarding failed for listen port 3030
ssh exited with error status 255; restarting ssh
starting ssh (count 18)
ssh child pid is 2481

Whereas previously only one of these errors would be displayed, after which autossh would hang.

The errors which I'd included in my question still occur, however I don't think this is anything to worry about as it's possible that a network connection hasn't been properly established when the service first starts running.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.