2

One of my servers is setup so that it will automatically connect to a VPN after booting up using a systemd service. Now I want to host a systemd socket on this VPN connection. And the socket should also be available after booting up and after the VPN connection is established.

Here is my current application.socket file:

[Unit]
Description=My Socket on VPN

[Socket]
# 192.168.2.2 is the IP from inside the VPN
ListenStream=192.168.2.2:1234
Accept=true

[Install]
WantedBy=sockets.target

The issue is, that the socket starts too early: The network interface is not yet up so systemd decides that it cannot start my socket.

If I then ssh into the server and do a quick systemctl restart application.socket it will start up fine, since the network interface is then up and ready to go. But I don't want to ssh into the server everytime I reboot it. What is the way to make sure, that my socket only starts, when the interface is up, ready and configured? I also don't want to listen to 0.0.0.0 and then block all unwanted devices in iptables.

Things, that I tried so far:

  • After=vpn-application.service -> didn't do anything. Looks like the device is still not available for binding to it
  • After=sys-devices-virtual-net-<VPN Device>.device -> The device does not exist at the time, systemd wants to start my socket. So it fails with x/RESOURCE

How do I make my socket wait for the VPN to be up?

rollstuhlfahrer
  • 288
  • 1
  • 6

1 Answers1

0

You want to start the socket after boot up and also after each time the VPN goes up? Or only once at boot up only if the VPN is also up? With After=sys-devices-virtual-net-<VPN Device>.device the socket will be scheduled to start after the device, but without a requirement this might never happen, you might want to add Wants=vpn-application.service sys-devices-virtual-net-<VPN Device>.device.

If on the other side you want to set the socket at boot but also after starting the VPN, then you can add a Wants=application.socket and Before=application.socket to vpn-application.service.

Also you might want to add network-online.target to Wants and After of vpn-application.service.

https://blog.bjoern-ruberg.de/2015/10/15/start-a-service-after-openvpn-connection-has-been-established-using-systemd/

cabo
  • 1
  • 1