Self-compiled OpenVPN won't start from systemd

Made it work by manually creating two files in /lib/systemd/system.

The first one is openvpn.service:

[Unit]
Description=OpenVPN service
After=network.target

[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/bin/true
ExecReload=/bin/true
WorkingDirectory=/etc/openvpn

[Install]
WantedBy=multi-user.target

and second is [email protected]:

[Unit]
Description=OpenVPN connection to %i
PartOf=openvpn.service
ReloadPropagatedFrom=openvpn.service
Before=systemd-user-sessions.service
Documentation=man:openvpn(8)
Documentation=https://community.openvpn.net/openvpn/wiki/Openvpn23ManPage
Documentation=https://community.openvpn.net/openvpn/wiki/HOWTO

[Service]
PrivateTmp=true
KillMode=mixed
Type=forking
ExecStart=/usr/local/sbin/openvpn --daemon ovpn-%i --status /run/openvpn/%i.status 10 --cd /etc/openvpn --script-security 2 --config /etc/openvpn/%i.conf --writepid /run/openvpn/%i.pid
PIDFile=/run/openvpn/%i.pid
ExecReload=/bin/kill -HUP $MAINPID
WorkingDirectory=/etc/openvpn
ProtectSystem=yes
CapabilityBoundingSet=CAP_IPC_LOCK CAP_NET_ADMIN CAP_NET_BIND_SERVICE CAP_NET_RAW CAP_SETGID CAP_SETUID CAP_SYS_CHROOT CAP_DAC_READ_SEARCH CAP_AUDIT_WRITE
LimitNPROC=10
DeviceAllow=/dev/null rw
DeviceAllow=/dev/net/tun rw

[Install]
WantedBy=multi-user.target

After creating them, do sudo systemctl daemon-reload to reload the new changes.

Generally, the files are the same, as if openvpn was installed from official repo, the only difference is ExecStart=/usr/sbin/openvpn should be ExecStart=/usr/local/sbin/openvpn, pointing to compiled local openVPN.

Edit: If you use openvpn 2.4+, remove PIDFile=/run/openvpn/%i.pid and --writepid /run/openvpn/%i.pid from the second file, as it prevents server from starting on boot. Found it here


Updating from answer by Ilya (I don't have reputation to comment)

I have it working on Ubuntu 16.04 by using the provided service as a template (so a single service file), changing two things:

sudo cp ~/Downloads/openvpn-2.4.8/distro/systemd/[email protected] /lib/systemd/system/[email protected]

sudo vi /lib/systemd/system/[email protected]

Change:

  • @sbindir@ to /usr/local/sbin/
  • Type=notify to Type=simple

Reload and Start

sudo systemctl daemon-reload
service [email protected] restart
service [email protected] status

This assumes you've already got your config in /etc/openvpn/client/client.service.conf