Solution: No /etc/rc.local file on Ubuntu 18

I once noticed that by default there is no /etc/rc.local file in Ubuntu 18, but it can be created.

To do this, switch to the root user:

sudo -i

Create a file /etc/rc.local for example with the nano editor (Ctrl+X to exit the editor, y/n to save or cancel changes):

nano /etc/rc.local

Add content to it like in Ubuntu 16:

#!/bin/sh -e
# rc.local
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
# In order to enable or disable this script just change the execution
# bits.
# By default this script does nothing.

exit 0

Let’s make the file executable:

chmod +x /etc/rc.local

Done, now you can write your own commands and they will be executed when the operating system starts, the last line must remain “exit 0”.

I note that with any error, that is, if any command in the rc.local file reports an error when executed, the script will stop running and all other commands will not be executed. Therefore, after restarting Linux, make sure that rc.local has executed all commands (if successful, the status will be active (exited)):

systemctl status rc-local.service

If the status is different, then we will fix the problem and try to start it manually (during a test run manually, you may need to cancel some of the executed commands, I will give an example, let’s say the script contains the “ipset create …” commands that have already been executed when Linux was started, then if not delete the created ipset “ipset destroy …”, then when you run ipset create from rc.local again, an error will occur, since ipset already exists and script execution will stop at this point, or you can not bother with it and just restart Linux, and then check status):

systemctl start rc-local.service

See also my articles:
Run application at startup in Ubuntu
Using and configuring CRON

Leave a comment

Leave a Reply