I am trying to set up an autossh service to forward ports to a remote server when my monitoring server boots. After some finagling, I got a pretty barebones system script working, with one catch. The service fails to properly start when it is run at boot, and yet works fine when started manually, or restarted immediately after reboot.
Description=AutoSSH tunnel service for Grafana to access mariadb on remote; local port 33060
After=network-online.target
[Service]
Environment="AUTOSSH_GATETIME=0"
ExecStart=/usr/bin/autossh -M 0 -N [email protected] -i /home/myuser/.ssh/id_rsa -o \"ServerAliveInterval 30\" -o \"ServerAliveCountMax 3\" -L 172.17.0.1:33307:127.0.0.1:3306
[Install]
WantedBy=multi-user.target
On boot, no connection is possible. The output from journalctl is something like:
--
-- The job identifier is 152.
Apr 07 15:25:31 monitoring autossh[752]: port set to 0, monitoring disabled
Apr 07 15:25:31 monitoring autossh[752]: starting ssh (count 1)
Apr 07 15:25:31 monitoring autossh[752]: ssh child pid is 759
Apr 07 15:25:31 monitoring autossh[759]: bind [172.17.0.1]:33307: Cannot assign requested address
Apr 07 15:25:31 monitoring autossh[759]: channel_setup_fwd_listener_tcpip: cannot listen to port: 33307
Apr 07 15:25:31 monitoring autossh[759]: Could not request local forwarding.
If I immediately sudo systemctl restart autossh-mariadb-tunnel.service, the connection works fine.