OpenVPN STARTUP!?!

I know EXACTLY what command I want to occur at startup of the Pi…but I have very sketchy information on how to put it at startup.

The command I want to start as it starts up is the following

sudo openvpn --daemon --cd /etc/openvpn/OpenVPN-UDP --config MYVPN.ovpn

I shouldn’t have to put this command in every time, and it is driving me a little crazy.

Anything helps! Thanks in advance!

Have you used the forum’s search tool to look up “run command at startup”? Or the wiki page?

I have found a few things.

I first tried crontab, but turns out the version of crontab on OSMC doesnt recognize the @reboot command.

Then I tried running a init.d file, but it didn’t work.

And finally I tried to write my own script to run at startup, and that failed as well.

I also found Running scripts on startup and shutdown - General - OSMC

but that doesnt tell me how to do it…

I am editing rc.local at the moment to try that.

Where do i need to address this in my command?

Note: applications are run as the root user when they are called from rc.local. We recommend you use ‘su osmc -c /path/to/your/program’ to run the application as the default OSMC user.

It has all the info on how to do it.

At the beginning of your command. But as your command starts with sudo, you don’t need this.

1 Like

Okay. This is all pretty new to me and ive been trying to fix it for 2 days now.

so, the command to put in the rc.local is

su osmc -c /path/to/your/program openvpn --daemon --cd /etc/openvpn/OpenVPN-UDP --config MYVPN.ovpn

And…how can I find the path to my program?

Okay. Giving it a try!

IT WORKED!!!(#$&$!

Thank you!

whereis or which

I used

type openvpn

The guide is just a general primer. Openvpn is already set up to run at startup and just needs a few helpful details to send it on its way.

Check out this thread and the tl;dr is at post #28.

1 Like

Enjoy.

The Openvpn daemon will start automatically but since the systemd change (if you know the old one) you have to enable each VPN profile with systemctl command. So if openvpn is installed and enabled at startup, you have to:

  • copy the ovpn file to /etc/openvpn with a .conf extension (E.g. : /etc/openvpn/MYVPN.conf)
  • enable this profile to start automatically with: > sudo systemctl enable openvpn@MYVPN.service
    This will create a symlink in /etc/systemd and you VPN profile is now manageable as a service (status, disable/enable, etc.)

PS: this is valid for last compiled Openvpn using systemd

You can enable it that way but it’s not a “must-do”. Using this technique runs the risk that you’ll have more than one openvpn profile enabled at the same time. IMO, it’s better to have the “master switch” in /etc/default/openvpn and only enable openvpn.service to run.

osmc@osmc:~$ sudo systemctl disable openvpn
Synchronizing state for openvpn.service with sysvinit using update-rc.d...
Executing /usr/sbin/update-rc.d openvpn defaults
Executing /usr/sbin/update-rc.d openvpn disable
insserv: warning: current start runlevel(s) (empty) of script `openvpn' overrides LSB defaults (2 3 4 5).
insserv: warning: current stop runlevel(s) (0 1 2 3 4 5 6) of script `openvpn' overrides LSB defaults (0 1 6).
osmc@osmc:~$ sudo systemctl status openvpn@test
● openvpn@test.service - OpenVPN connection to test
   Loaded: loaded (/lib/systemd/system/openvpn@.service; disabled)
   Active: inactive (dead)

Apr 18 12:21:47 osmc ovpn-test[1330]: IFCONFIG POOL: base=10.8.0.2 size=252, ipv6=0
Apr 18 12:21:47 osmc ovpn-test[1330]: Initialization Sequence Completed
Apr 18 12:21:47 osmc systemd[1]: Started OpenVPN connection to test.
Apr 18 12:23:17 osmc systemd[1]: Stopping OpenVPN connection to test...
Apr 18 12:23:17 osmc ovpn-test[1330]: event_wait : Interrupted system call (code=4)
Apr 18 12:23:17 osmc ovpn-test[1330]: Closing TUN/TAP interface
Apr 18 12:23:17 osmc ovpn-test[1330]: /sbin/ifconfig tun0 0.0.0.0
Apr 18 12:23:17 osmc ovpn-test[1330]: Linux ip addr del failed: external program exited with error status: 255
Apr 18 12:23:17 osmc ovpn-test[1330]: SIGTERM[hard,] received, process exiting
Apr 18 12:23:17 osmc systemd[1]: Stopped OpenVPN connection to test.
osmc@osmc:~$ sudo systemctl start openvpn@test
osmc@osmc:~$ sudo systemctl status openvpn@test
● openvpn@test.service - OpenVPN connection to test
   Loaded: loaded (/lib/systemd/system/openvpn@.service; disabled)
   Active: active (running) since Tue 2017-04-18 12:28:49 CEST; 9s ago
  Process: 1504 ExecStart=/usr/sbin/openvpn --daemon ovpn-%i --status /run/openvpn/%i.status 10 --cd /etc/openvpn --config /etc/openvpn/%i.conf (code=exited, status=0/SUCCESS)
 Main PID: 1506 (openvpn)
   CGroup: /system.slice/system-openvpn.slice/openvpn@test.service
           └─1506 /usr/sbin/openvpn --daemon ovpn-test --status /run/openvpn/test.status 10 --cd /etc/openvpn --config /etc/openvpn/test.conf

Apr 18 12:28:49 osmc ovpn-test[1506]: /sbin/ifconfig tun0 10.8.0.1 netmask 255.255.255.0 mtu 1500 broadcast 10.8.0.255
Apr 18 12:28:49 osmc ovpn-test[1506]: Socket Buffers: R=[163840->327680] S=[163840->327680]
Apr 18 12:28:49 osmc ovpn-test[1506]: UDPv4 link local (bound): [AF_INET]192.168.11.51:8689
Apr 18 12:28:49 osmc ovpn-test[1506]: UDPv4 link remote: [AF_UNSPEC]
Apr 18 12:28:49 osmc ovpn-test[1506]: GID set to nogroup
Apr 18 12:28:49 osmc ovpn-test[1506]: UID set to nobody
Apr 18 12:28:49 osmc ovpn-test[1506]: MULTI: multi_init called, r=256 v=256
Apr 18 12:28:49 osmc ovpn-test[1506]: IFCONFIG POOL: base=10.8.0.2 size=252, ipv6=0
Apr 18 12:28:49 osmc ovpn-test[1506]: Initialization Sequence Completed
Apr 18 12:28:49 osmc systemd[1]: Started OpenVPN connection to test.
osmc@osmc:~$ sudo systemctl status openvpn
● openvpn.service - OpenVPN service
   Loaded: loaded (/lib/systemd/system/openvpn.service; disabled)
   Active: inactive (dead)

Apr 15 22:08:24 osmc systemd[1]: Starting OpenVPN service...
Apr 15 22:08:24 osmc systemd[1]: Started OpenVPN service.
Apr 18 12:18:43 osmc systemd[1]: Stopping OpenVPN service...

As you can see, I disabled openvpn.service, then started openvpn@test.service. The status of openvpn@test once running was:

osmc@osmc:~$ sudo systemctl status openvpn@test
● openvpn@test.service - OpenVPN connection to test
   Loaded: loaded (/lib/systemd/system/openvpn@.service; disabled)
   Active: active (running) since Tue 2017-04-18 12:28:49 CEST; 9s ago

but openvpn.service and openvpn@.service are both still disabled. So while you can enable openvpn@test.service, AFAIK it’s not a necessary requirement for running the service.

It’s not mandatory you’re right. Since the usage of systemd all .conf file are no more processed and have to be manually enabled so having multiple profiles running at the same time is possible but have to be done by enabling each profile individually. It may be intended too for the usage of namespace of to ease the usage of multiple VPN profile. Both will work and depending of the audience one may be more advisable than the other :slight_smile:

Just install Vpn manager and then no messing around…

Where would be the best place to store the openvpn.log file when using this method?

Hi Sentut,

This is down to personnel preference, but i would place it either:

/etc/openvpn/ - this is probably the easiest, just update your .conf file with log-append openvpn.log

or

/var/log/ - .conf log-append /var/log/openvpn.log

Thanks Tom.

Thanks Tom.

Probably overkill and stupid question…

I’ve previously been using the OpenVPN kodi plugin by Brian Hornsby. This is great because it allows me to choose a VPN location through the Kodi GUI. However using the systemd method creates a new instance. So any VPNs I select in the Kodi Plugin wont affect the openvpn config being run by systemd. Is there any way of making the systemd openvpn instance and the openvpn instance from the Kodi plugin, work together?