Skip to content
0xjakobPublic

About

systemd service for creating reverse SSH connection

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

1 Commit

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 

Repository files navigation

Reverse SSH Tunnel

This systemd service allows you to create a reverse SSH tunnel from a remote device to a remote server. This is often useful when having a device behind a firewall you don't have access to. You can then reach the remote device via the remote server ("jump host").

In contrast to many other solutions found on the Internet, this service also focuses on locking itself down via systemd's sandboxing options as much as possible. The goal is to only allow the SSH tunnel to build up and nothing more. On the device as well as on the host, two specialized system users are used to build the SSH tunnel. If that user gets compromised, an attacker is left with the usual barriers of a system user on the Linux system without a login shell. If the SSH-key used to connect to the remote server is leaked accidentally, an attacker is left with the locked down system user without shell and a very restricted set of SSH options.

Systemd's security rating at the time of writing is 1.1 OK 🙂 out of 10 (lower is better), which is basically as good as you can get for a network-based service.

Prerequisite

The assumption is that the remote device is a Linux system with systemd as init system and openssh-server and client installed. The systemd service has been tested on Debian 11 (bullseye). You will also need a server to connect to.

Remote Server

  • Create the user r_ssh_user. Make sure that the user has a home directory for the authorized_keys file, but does not have a login shell (/usr/sbin/nologin), since it won't need a login shell.

Installation on the Remote Device

  • Create /etc/reverse-ssh/, the future home directory of the new r_ssh_user.
  • Create the user r_ssh_user (might be changed in accordance with the repository name) with following properties:
    • home directory set to /etc/reverse-ssh
    • no login shell (/usr/sbin/nologin)
  • Create the directory /etc/reverse-ssh/.ssh/, and change its ownership to r_ssh_user.
  • Create an ssh key pair that r_ssh_user uses later for authentication against the remote server. Name the private key id_rsa_automatic.
  • Copy that private key to /etc/reverse-ssh/.ssh/ and give it exclusive read/write (600) access for r_ssh_user. If you ls -la into /etc/reverse-ssh, it should look like this:
total 12
drwxr-xr-x 2 r_ssh_user root 4096 Jun 28 16:30 .
drwxr-xr-x 3 root       root 4096 Jun 28 16:24 ..
-rw------- 1 r_ssh_user root  411 Jun 28 16:24 id_rsa_automatic
  • On the remote server, create a file <r_ssh_user_home_dir>/.ssh/authorized_keys, with exactly one line, assembled as follows:
permitlisten="localhost:2222",command="/bin/false",no-agent-forwarding,no-X11-forwarding <content of the public key file>

If you want to use another port on the server (for accessing by localhost only), change the number from 2222 to something else, and also change it in the environment file below.

  • Now you should test the connection by issueing the command from the service file:
sudo -u r_ssh_user ssh -i /etc/reverse-ssh/.ssh/id_rsa_automatic -N -R localhost:2222:localhost:22 r_ssh_user@<remote_server_name> -o ServerAliveInterval=10 -o ExitOnForwardFailure=yes

Check the ssh fingerprint from your remote server. If they match, confirm with 'yes'. Then try to login to the remote device from the remote server:

ssh -p 2222 <user_on_remote_device>@localhost

If this works, you are half-way there. Now you just need to automate the setup using the following steps.

  • Copy the reverssh systemd unit file to /etc/systemd/system/ of your remote device
  • Create a directory /etc/systemd/system/reverssh.service.d alongside the file reverssh.service.
  • Now the important part: the service is configured with systemd environment variables. You need to create a systemd drop-in file that sets at least the following environment variable:
    • SERVERNAME: the name of the remote server to use as jump host (Note: you could also set the additional environment variables that have a default in reverssh, but this is not a must.) E.g.:
[Service]
Environment="SERVERNAME=<servername>"

Name the file 20variables.conf and copy it to /etc/systemd/system/reverssh.service.d.

  • reload systemd, enable and start the service
sudo systemctl daemon-reload
sudo systemctl enable reverssh
sudo systemctl start reverssh
  • Check that the service is 'active (running)':
<regular_user_remote_machine>@debian:~$ systemctl status reverssh.service 
● reverssh.service - Reverse SSH Tunnel Service
     Loaded: loaded (/etc/systemd/system/reverssh.service; enabled; preset: enabled)
    Drop-In: /etc/systemd/system/reverssh.service.d
             └─20variables.conf
     Active: active (running) since Sun 2026-06-28 16:59:22 CEST; 2s ago
...
  • On the remote server, you should now be able to connect to the remote device, using the "normal" user's credentials, i.e., not the r_ssh_user user:
ssh -p 2222 localhost
<regular_user_remote_machine>@localhost's password: 
Linux debian 6.12.74+deb13+1-amd64 #1 SMP PREEMPT_DYNAMIC Debian 6.12.74-2 (2026-03-08) x86_64


The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
Last login: Sun Jun 28 16:54:30 2026 from 10.0.5.1
<regular_user_remote_machine>@debian: $

Congrats!

About

systemd service for creating reverse SSH connection

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors