OpenVPN – auto-start on Linux

By | 03/08/2019
 

I have an OpenVPN AS server running on EC2 in AWS and the openvnp client on my Arch Linux (см.OpenVPN: настройка OpenVPN Access Server и AWS VPC peering).

Need to have it started and connection established after OS start.

Will use the systemd, documentation is here>>>, will work with any other Linux-system.

Client’s configs have to be stored in the /etc/openvpn/client/ (check the WorkingDirectory in the systemd unit file below), now here is nothing:

[simterm]

$ sudo ls -l /etc/openvpn/client/
total 0

[/simterm]

Copy your file and save it with the *.conf(because of the --config %i.conf below) extension:

[simterm]

$ sudo cp Dropbox/OpenVPN-AS/setevoy-client.ovpn /etc/openvpn/client/setevoy-ovnas.conf

[/simterm]

Check client’s status now – use the openvpn-client@ + filename without extension:

[simterm]

$ systemctl status [email protected][email protected] - OpenVPN tunnel for setevoy/ovnas
   Loaded: loaded (/usr/lib/systemd/system/[email protected]; disabled; vendor preset: disabled)
   Active: inactive (dead)
     Docs: man:openvpn(8)
           https://community.openvpn.net/openvpn/wiki/Openvpn24ManPage
           https://community.openvpn.net/openvpn/wiki/HOWTO

[/simterm]

Unit-file’s config:

[simterm]

$ systemctl cat [email protected]
# /usr/lib/systemd/system/[email protected]
[Unit]
Description=OpenVPN tunnel for %I
After=syslog.target network-online.target
Wants=network-online.target
Documentation=man:openvpn(8)
Documentation=https://community.openvpn.net/openvpn/wiki/Openvpn24ManPage
Documentation=https://community.openvpn.net/openvpn/wiki/HOWTO

[Service]
Type=notify
PrivateTmp=true
WorkingDirectory=/etc/openvpn/client
ExecStart=/usr/bin/openvpn --suppress-timestamps --nobind --config %i.conf
CapabilityBoundingSet=CAP_IPC_LOCK CAP_NET_ADMIN CAP_NET_RAW CAP_SETGID CAP_SETUID CAP_SYS_CHROOT CAP_DAC_OVERRIDE
LimitNPROC=10
DeviceAllow=/dev/null rw
DeviceAllow=/dev/net/tun rw
ProtectSystem=true
ProtectHome=true
KillMode=process

[Install]
WantedBy=multi-user.target

[/simterm]

Start its service:

[simterm]

$ systemctl start [email protected]
==== AUTHENTICATING FOR org.freedesktop.systemd1.manage-units ====
Authentication is required to start '[email protected]'.
Authenticating as: setevoy
Password: 
==== AUTHENTICATION COMPLETE ====

[/simterm]

Check:

[simterm]

20:23:12 [setevoy@setevoy-arch-pc ~]   
$ ps aux | grep vpn
root     26477  0.1  0.0  24676 10732 ?        Ss   20:23   0:00 /usr/bin/openvpn --suppress-timestamps --nobind --config setevoy-ovnas.conf

[/simterm]

Enable on boot:

[simterm]

$ systemctl enable [email protected]
==== AUTHENTICATING FOR org.freedesktop.systemd1.manage-unit-files ====
Authentication is required to manage system service or unit files.
Authenticating as: setevoy
Password: 
==== AUTHENTICATION COMPLETE ====
Created symlink /etc/systemd/system/multi-user.target.wants/[email protected] → /usr/lib/systemd/system/[email protected].

[/simterm]

Check DNS:

[simterm]

$ cat /etc/resolv.conf
# Generated by resolvconf
nameserver 172.31.26.29
nameserver 1.1.1.1

[/simterm]

Check Yandex.ru (it’s blocked in Ukraine and available via VPN only):

[simterm]

$ curl -LI ya.ru
...
HTTP/1.1 200 Ok
...

[/simterm]

Done.