systemd Cookbook

Network Proxy (like socat)

Sometimes you want to create a simple proxy. For example, you want to forward the local port 3389/tcp to the remote host 192.0.2.100 port 3389/tcp to enable RDP access for other hosts. You maybe stumble upon socat and put together a working command like this:

socat TCP4-LISTEN:3389,fork,reuseaddr TCP4:192.0.2.100:3389

Now you want to start this proxy in the background in case of a reboot of the system. systemd offers its own way of creating a proxy like this with systemd-socket-proxyd.

You have to create a socket and a service file. The socket file defines the listening socket and will trigger the service file on start and stop.

/etc/systemd/system/proxy-to-rdphost.socket
[Socket]
ListenStream=3389
 
[Install]
WantedBy=sockets.target
/etc/systemd/system/proxy-to-rdphost.service
[Unit]
Requires=proxy-to-rdphost.socket
After=proxy-to-rdphost.socket
 
[Service]
ExecStart=/usr/lib/systemd/systemd-socket-proxyd 192.0.2.100:3389
PrivateTmp=yes
PrivateNetwork=no

After creating/editing the files, systemd has to be reloaded.

systemctl daemon-reload

Enable the socket to activate it at boot and immediately start it with “–now”.

systemctl enable --now proxy-to-rdphost.socket