I'm about to upgrade from 14.04 LTS to 16.04 LTS, but first I need to sort out the systemd services for most important software I use, like OpenVPN client. I have a perfectly working upstart script for 14.04.

I read a lot about systemd, but have no too much experience. From another post I also understand the following:

OpenVPN is a templatized service under systemd. The services are named openvpn@config.service. So you should be starting your /etc/openvpn/myvpn.conf instance with

systemctl start openvpn@myvpn.service

I compile OpenVPN Client. I just don't get this, is openvpn@myvpn.service automatically generated, or how? Further to this, how can I add Restart settings (or any other) to this service, in this case openvpn@myvpn.service? I would like to add


As recommended on freedesktop.org:

Setting this to on-failure is the recommended choice for long-running services, in order to increase reliability by attempting automatic recovery from errors. For services that shall be able to terminate on their own choice (and avoid immediate restarting), on-abnormal is an alternative choice.

My aim is to OpenVPN client to restart always if not stopped by me.


Regarding the "openvpn@myvpn.service" part of your question, another example: I have an OpenVPN configuration file named /etc/openvpn/Germany.conf so I start the openvpn daemon with systemctl start openvpn@Germany.service. This is my default config, so it goes into /etc/default/openvpn.

In case my german gateway is down, I have another configuration file /etc/openvpn/Netherlands.conf and to use that I call systemctl start openvpn@Netherlands.service

Regarding the Restart setting, this probably goes into the [Service] section of /etc/systemd/system/multi-user.target.wants/openvpn.service though I haven't tested that myself.

Note: The above assumes that you use the Ubuntu package via apt-get install openvpn. I'm not sure what happens when you compile the stuff yourself? Also: Why would you do that?

  • Thanks for your answer! I was confused with the openvpn@name.service part, now I get it. The restart settings should be Restart=on-failure, and I need to edit the systemd service of the given openvpn@name.service that I want to modify. I used to compile OpenVPN from source, but now I just use the official OpenVPN repository. It is up to date, something that Ubuntu is not most of the time. – gurabli Jun 14 '16 at 10:47

Further to this, how can I add Restart settings (or any other) to this service, in this case openvpn@myvpn.service?

From https://www.freedesktop.org/software/systemd/man/systemd.unit.html (emphasis mine):

Along with a unit file foo.service, a "drop-in" directory foo.service.d/ may exist. All files with the suffix ".conf" from this directory will be parsed after the file itself is parsed. This is useful to alter or add configuration settings for a unit, without having to modify unit files. Each drop-in file must have appropriate section headers. Note that for instantiated units, this logic will first look for the instance ".d/" subdirectory and read its ".conf" files, followed by the template ".d/" subdirectory and the ".conf" files there. Also note that settings from the "[Install]" section are not honored in drop-in unit files, and have no effect.

In addition to /etc/systemd/system, the drop-in ".d" directories for system services can be placed in /usr/lib/systemd/system or /run/systemd/system directories. Drop-in files in /etc take precedence over those in /run which in turn take precedence over those in /usr/lib. Drop-in files under any of these directories take precedence over unit files wherever located.

Based on above, if you had more than one openvpn configuration and you would like to alter some aspect of configuration for all openvpn services based on these configurations you would need to create /etc/systemd/openvpn@.service.d directory and place there appropriate .conf files.

If you want to alter specific openvpn service like openvpn@myvpn.service in your case then you have to create /etc/systemd/openvpn@myvpn.service.d directory and place there appropriate .conf files.

  • 3
    You can use systemctl edit openvpn@.service or systemctl edit openvpn@myvpn.service, and systemd will automatically create that directory for you – muru Jan 13 '17 at 0:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.