Jump to content

Ubuntu 20.04 LTS Auto Restart Code Assistance

Dravinian

Hi all,

 

I have an OpenVPN running on a virtual machine with a kill switch enabled.  It would be great if on reboot the virtual machine, which starts automatically, would just start OpenVPN and not need me to shell in and run the 'restart.sh' script that I wrote for it - I don't have any internet til I do this and it is a pain.

 

Now I used a guide to get me through the process of getting the virtual machine up and while there were some issues, I got everything to work...except the last bit of code which has the OpenVPN run automagically.


The code was written for Ubuntu 18.04 LTS and I am running 20.04 LTS - the guide writer spoke about changes to RC-Local being made between 16.05 and 18.04 and I am wondering whether further changes were made. [apologies missed a part of the guide - not from the process but from this post]

 

Create an rc.local service /etc/systemd/system/rc-local.service

[Unit]
Description=/etc/rc.local Compatibility
ConditionPathExists=/etc/rc.local


[Service]
Type=forking
ExecStart=/etc/rc.local start
TimeoutSec=0
StandardOutput=tty
RemainAfterExit=yes
SysVStartPriority=99


[Install]
WantedBy=multi-user.target

Create rc.local script, /etc/rc.local

#!/bin/sh -e
sudo bash /etc/openvpn/iptables.sh &
sleep 10
sudo sh -c "echo 1 > /proc/sys/net/ipv4/ip_forward"
sudo bash /etc/openvpn/connect.sh &

exit 0

Enable/Start rc.local service

sudo systemctl enable rc-local
sudo systemctl start rc-local.service

enable works fine.

 

start throws up this error:

 

Job for rc-local.service failed because the control process exited with error code see systemctl status rc-local.service

 

I look at systemctl status rc-local.service and I get:

 

953706413_ErrorMessage.thumb.PNG.a00b9ca124144c9a252466ad883986a6.PNG

 

I am running as root, with the root password and I shouldn't be denied permission and I am using sudo, so I am a bit lost on what is happening, and I am wondering whether rc.local has changed in 20.04 and if I want to run this thing I need a different piece of code.

 

So if anyone happens to have that code and wouldn't mind throwing it my way I would appreciate it.

Link to comment
Share on other sites

Link to post
Share on other sites

I did create my own .service in /etc/systemd/system/  as I wanted to try and keep things as simple as possible to see if it would work:

 

startvpn.service

 

[Unit]
After=network.service

[Service]
ExecStart=/usr/local/bin/restart.sh

[Install]
WantedBy=default.target

Within /user/local/bin/ I put the following restart script that I run on start up each time:

#!/bin/sh
sudo bash /etc/openvpn/iptables.sh &
sleep 10
sudo sh -c "echo 1 > /proc/sys/net/ipv4/ip_forward"
sudo bash /etc/openvpn/connect.sh &

exit 0

 

Which is ripped entirely from rc.local - as you can see.

 

The problem is, since the service is inactive on start up...it doesn't run.

 

I clearly need some code to run something on start-up, on a virtual machine, which appears to be a bit different?

Link to comment
Share on other sites

Link to post
Share on other sites

Ok, I followed this process and it now works:

 

Ubuntu 20.04 LTS does not ship with /etc/rc.local so rather than creating it manually yourself I ran this code:

 

printf '%s\n' '#!/bin/bash' 'exit 0' | sudo tee -a /etc/rc.local

Then, I created the service as I did in the previous posts:

 

sudo nano /etc/systemd/system/rc-local.service

Then I gave the execute permission to /etc/rc.local (something I believe I had been missing)

 

sudo chmod +x /etc/rc.local

I was then able to 'enable' and then 'start' rc-local.service.

 

I did 

 

sudo systemctl status rc-local.service

Got a positive result and rebooted the virtual machine.


And bang! VPN start automagically.

Link to comment
Share on other sites

Link to post
Share on other sites

Can I say, I am still rubbish at this, and my understanding is extremely limited.

 

However, getting that VPN running and getting Pi-Hole up and running actually took me in total about 10 hours of tinkering around with code.

 

This time....only took an hour...I am getting faster!

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

rc.local is the "old" way Ubuntu used to handle system services. Ubuntu has moved to systemd years ago and rc.local is no longer supported. You most likely followed an outdated guide originally.

 

What you did works but it has nothing to do with what rc.local used to be - it's just a shell script that you named rc.local. Personally I would remove all references to rc.local and just call it "vpn" or something. Also yes, your scripts need to be granted execution permissions in order to be executed... alternatively you can run it through bash.

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×