3

I am trying to start a persistent ssh connection at boot with autossh in systemd, following guides such as this one. When I do so, it connects and then immediately disconnects, with very little information in the logs. My system is (uname -a):

Linux local_machine_name 3.16.0-4-amd64 #1 SMP Debian 3.16.43-2+deb8u2 (2017-06-26) x86_64 GNU/Linux

When running:

autossh -M 0 dbase1

The above command results in a connection that is stable for several days. dbase1 is defined in my config file, which looks like (anonymised):

Host dbase1
HostName x.x.x.x
User serverusername
LocalForward 54320 localhost:5432
ServerAliveInterval 30
ServerAliveCountMax 3
ExitOnForwardFailure yes
ProxyCommand ssh -q [email protected] nc %h %p 2> /dev/null

I then created the following service in /etc/systemd/system/

[Unit]
Description=AutoSSH tunnel service
After=network.target
[Service]
Type=simple
User=*username with cert and config file in home/username/.ssh/ folder*
Environment="AUTOSSH_GATETIME=0"
ExecStart=/usr/bin/autossh -vvv -M 0 dbase1
[Install]
WantedBy=multi-user.target

After reloading the systemctl daemon and starting the service, the connection is made and then disconnects. Here is the output of journalctl -u myautossh.service (starting from the welcome message from the remote server, confirming a connection):

Jul 21 14:38:30 local_machine_name autossh[555]: *** Connection successful, welcome to the remote server! ***
Jul 21 14:38:30 local_machine_name autossh[555]: debug1: client_input_channel_req: channel 2 rtype exit-status reply 
Jul 21 14:38:30 local_machine_name autossh[555]: debug2: channel 2: rcvd eof
Jul 21 14:38:30 local_machine_name autossh[555]: debug2: channel 2: output open -> drain
Jul 21 14:38:30 local_machine_name autossh[555]: debug2: channel 2: obuf empty
Jul 21 14:38:30 local_machine_name autossh[555]: debug2: channel 2: close_write
Jul 21 14:38:30 local_machine_name autossh[555]: debug2: channel 2: output drain -> closed
Jul 21 14:38:30 local_machine_name autossh[555]: debug2: channel 2: rcvd close
Jul 21 14:38:30 local_machine_name autossh[555]: debug3: channel 2: will not send data after close
Jul 21 14:38:30 local_machine_name autossh[555]: debug2: channel 2: almost dead
Jul 21 14:38:30 local_machine_name autossh[555]: debug2: channel 2: gc: notify user
Jul 21 14:38:30 local_machine_name autossh[555]: debug2: channel 2: gc: user detached
Jul 21 14:38:30 local_machine_name autossh[555]: debug2: channel 2: send close
Jul 21 14:38:30 local_machine_name autossh[555]: debug2: channel 2: is dead
Jul 21 14:38:30 local_machine_name autossh[555]: debug2: channel 2: garbage collecting
Jul 21 14:38:30 local_machine_name autossh[555]: debug1: channel 2: free: client-session, nchannels 3
Jul 21 14:38:30 local_machine_name autossh[555]: debug3: channel 2: status: The following connections are open:
Jul 21 14:38:30 local_machine_name autossh[555]: #2 client-session (t4 r0 i3/0 o3/0 fd -1/-1 cc -1)
Jul 21 14:38:30 local_machine_name autossh[555]: debug1: channel 0: free: port listener, nchannels 2
Jul 21 14:38:30 local_machine_name autossh[555]: debug3: channel 0: status: The following connections are open:
Jul 21 14:38:30 local_machine_name autossh[555]: debug1: channel 1: free: port listener, nchannels 1
Jul 21 14:38:30 local_machine_name autossh[555]: debug3: channel 1: status: The following connections are open:
Jul 21 14:38:30 local_machine_name autossh[555]: debug1: fd 0 clearing O_NONBLOCK
Jul 21 14:38:30 local_machine_name autossh[555]: debug1: fd 1 clearing O_NONBLOCK
Jul 21 14:38:30 local_machine_name autossh[555]: debug3: fd 2 is not O_NONBLOCK
Jul 21 14:38:30 local_machine_name autossh[555]: Transferred: sent 3372, received 3384 bytes, in 0.7 seconds
Jul 21 14:38:30 local_machine_name autossh[555]: Bytes per second: sent 4633.6, received 4650.1
Jul 21 14:38:30 local_machine_name autossh[555]: debug1: Exit status 0

Looking at /var/log/syslog only adds this additional line (after successful server connection and the error messages above):

Jul 21 14:38:30 local_machine_name autossh[555]: ssh exited with status 0; autossh exiting

Anyone know why it disconnects?

4
  • Exiting does not mean disconnecting.
    – Jakuje
    Commented Jul 21, 2017 at 15:24
  • So...? The connection disconnects immediately - running "w" in the terminal shows no ssh connection with the systemd method. Running systemctl status myautossh.service shows that the service is inactive (dead).
    – fifthace
    Commented Jul 21, 2017 at 15:40
  • A comment at the end of your linked-to article suggests using Type=forking and adding -f.
    – meuh
    Commented Jul 21, 2017 at 18:02
  • I've tried the -f flag and Type=forking. That gives a wide range of error messages. According to the article, the -f flag isn't supported by systemd (but I tried it anyway, because I'm desperate).
    – fifthace
    Commented Jul 21, 2017 at 18:28

3 Answers 3

4

The problem seems to be that within systemd you will have no stdin, so the ssh command connects then tries to read input and stops on eof. The missing option is -N:

ExecStart=/usr/bin/autossh -vvv -N -M 0 dbase1

If you have OpenSSH 5.4 or later you can replace the use of netcat nc by the ssh built-in equivalent -W:

ProxyCommand ssh -q -W %h:%p [email protected]
2

The service is likely attempting to start before the network is all the way up. Try replacing After=network.target with After=network-online.target.

See the NetworkTarget page on the systemd wiki for more information on the distinction.

1
  • 1
    This doesn't change anything (but thanks for the repsonse!)
    – fifthace
    Commented Jul 21, 2017 at 18:33
0

Run your autossh command in terminal as root (sudo) and approve authenticity (i.e. type 'yes'). This also checks that the path to identity file is correct.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .