A service reachable only from an SSH server can still be used by applications on the SSH client. On Linux, a persistent SSH tunnel managed by systemd opens a local port at boot and brings the connection back if SSH exits.

With local forwarding, an application connects to a port on the client and the SSH server opens the onward connection. Binding the client port to loopback keeps the listener off the network, but other processes on the client can still reach it. Keep the destination application's usual authentication in place.

Set up the unit on the client from a separate administrator account with sudo access. The dedicated local account that runs the tunnel has no sudo privileges and needs a private key it can use without a prompt and the SSH server's verified host key in its known_hosts file. An unattended service starts without an interactive login, so it cannot ask for a key passphrase or approval of an unfamiliar host key.

Steps to create a persistent SSH tunnel with systemd:

  1. Check whether ssh-tunnel.service is already installed.
    $ systemctl list-unit-files ssh-tunnel.service --no-pager
    UNIT FILE STATE PRESET
    
    0 unit files listed.

    If the command lists a unit, stop here to avoid overriding an existing service.

  2. Open /etc/systemd/system/ssh-tunnel.service with your usual editor.
    $ sudoedit /etc/systemd/system/ssh-tunnel.service

    On the client, the unit uses local service account ssh-tunnel with key /home/ssh-tunnel/.ssh/tunnel_ed25519, remote login tunnel@server.example.net, and a free local port above 1023, here 8080. The reports service listens on port 8000 on the SSH server. The service account owns the private key and keeps it readable only by that account.

  3. Enter the unit definition using your local account, private key path, SSH login, and forwarding ports in place of the sample values.
    [Unit]
    Description=SSH tunnel to the reports service
    Wants=network-online.target
    After=network-online.target
     
    [Service]
    Type=exec
    User=ssh-tunnel
    ExecStart=/usr/bin/ssh -F /dev/null -NT \
        -i /home/ssh-tunnel/.ssh/tunnel_ed25519 \
        -o IdentitiesOnly=yes -o BatchMode=yes \
        -o StrictHostKeyChecking=yes -o ExitOnForwardFailure=yes \
        -o ServerAliveInterval=15 -o ServerAliveCountMax=3 \
        -L 127.0.0.1:8080:127.0.0.1:8000 tunnel@server.example.net
    Restart=always
    RestartSec=5s
     
    [Install]
    WantedBy=multi-user.target

    The first 127.0.0.1 in -L binds the port on the client. The second refers to loopback on the SSH server, where the reports service runs. For a destination on another host, that position takes an address reachable from the SSH server.

    -F /dev/null skips client configuration files. The SSH client stays in the foreground for systemd, and -NT requests forwarding without a remote command or terminal. BatchMode=yes disables prompts and StrictHostKeyChecking=yes requires an already trusted host key.

    ExitOnForwardFailure=yes makes SSH exit if the listener cannot start. Server-alive messages detect an unresponsive SSH connection, and Restart=always restarts SSH five seconds after it exits.

  4. Save the unit file.
  5. Close the editor.

    sudoedit writes the saved contents to the system unit path when the editor exits.

  6. Enable the tunnel for automatic startup.
    $ sudo systemctl enable --now ssh-tunnel.service

    Enabling the unit reloads the manager configuration, and --now starts the tunnel immediately. To close the listener and remove the boot setting later, use sudo systemctl disable --now ssh-tunnel.service.

  7. Check that the tunnel starts at boot.
    $ systemctl is-enabled ssh-tunnel.service
    enabled
  8. Check that the tunnel is running.
    $ systemctl status ssh-tunnel.service --no-pager
    * ssh-tunnel.service - SSH tunnel to the reports service
         Loaded: loaded (/etc/systemd/system/ssh-tunnel.service; enabled; preset: enabled)
         Active: active (running) since Mon 2026-11-23 05:06:46 UTC; 1s ago
    ##### snipped #####
       Main PID: 569 (ssh)
    ##### snipped #####

    The Active line should show active (running), and the main process should be ssh.

  9. Check the loopback listener with sudo from your administrator account, using your port in place of 8080.
    $ sudo ss -ltnp 'sport = :8080'
    State  Recv-Q Send-Q Local Address:Port Peer Address:PortProcess                      
    LISTEN 0      128        127.0.0.1:8080      0.0.0.0:*    users:(("ssh",pid=569,fd=4))

    The Local Address:Port column should show the loopback listener, and the SSH PID should match the unit's main PID. Running ss with sudo lets you see the PID of the process that runs under the service account. If SSH is still connecting, repeat this check once it has had a moment to authenticate.

  10. Check the tunnel unit's startup message in the current boot journal.
    $ sudo journalctl -b -u ssh-tunnel.service -n 10 --no-pager
    ##### snipped #####
    Nov 23 05:06:46 client systemd[1]: Started ssh-tunnel.service - SSH tunnel to the reports service.

    A Started entry records the launch. If the unit keeps restarting, the journal can show why, such as an untrusted host key or a local port already in use.
    Related: How to view service logs using journalctl

  11. Request your application's read-only health endpoint through the chosen local port, using 8080 and /health for the reports service.
    $ curl -fsS --noproxy '*' -m 5 \
      http://127.0.0.1:8080/health
    {"service":"reports","status":"ready"}

    The response should match the intended remote application. --noproxy '*' directs the request straight to the listener, and -f makes HTTP errors fail the check. If the listener is up but the request fails, check that the SSH server permits forwarding to the configured destination and can reach its port.

Share this guide