Create reverse tunnel with autossh and systemd

By | December 6, 2019
  1. Install autossh with sudo apt install autossh
  2. Create autossh.service in /etc/systemd/system/ folder
  3. The content of autossh.service:
    1. [Unit]
      Description=AutoSSH service for a reverse tunnel from SERVER to localhost
      After=network-online.target
      
      # start after DNS ready, but seems no difference with network-online.target
      #After=nss-lookup.target
      
      [Service]
      ExecStart=autossh -M [LOCAL_PORT] -N -D [YOUR_LOCAL_IP]:[LOCAL_PORT] -i [YOUR_PRIVATE_KEY] [REMOTE_USER]@[REMOTE_USERVER] 
      RestartSec=5
      Restart=always
      
      [Install]
      WantedBy=multi-user.target
  4. Execute sudo systemctl enable autossh.service
  5. Execute sudo systemctl start autossh.service

Hope it helps. 🙂