vpnc and systemd

1 minute read Published:

Writing a systemd unit file and some other extras to get vpnc running correctly on a Linux laptop.

vpnc is a VPN client. It runs as a daemon on Linux.

I wanted vpnc to run on boot and after wakeup/suspend.

For boot, it’s easy:

$ cat /etc/systemd/system/vpcn.service
[Unit]
Description=vpnc
Documentation=vpnc
Requires=network.target
After=network.target suspend.target
ConditionPathExists=/usr/sbin/vpnc

[Service]
Type=forking
User=root
Group=root
ExecStart=/usr/sbin/vpnc myvpncprofile
SyslogIdentifier=vpnc
Restart=on-failure
RestartSec=3

[Install]
WantedBy=multi-user.target sleep.target

The problem is that after a sleep/wakeup cycle, vpnc doesn’t work properly. This is a pretty common issue and was tricky to solve.

The solution came from the systemd docs:

$ cat /lib/systemd/system-sleep/vpnc
shanssian:sevagh.github.io $ cat /lib/systemd/system-sleep/vpnc
#!/bin/sh
set -e

case "$1" in
        pre) killall vpnc ;;
        post) /usr/sbin/vpnc myvpncprofile ;;
esac

This method passes the arguments pre <suspend|hibernate|hybrid-sleep before your system goes to sleep, and post after wakeup, to all the scripts in system-sleep/.

My goal was to kill vpnc before sleeping and start it back up after the system comes back online.