If you wish to run a command at system startup, adding these commands to /etc/rc.local is a good option. However, you may experience that the file does not exist, so let's set it up. (If the file exists but it is not working, these steps can also be followed for troubleshooting).

1. Creating rc.local

Type the following to create rc.local

sudo nano /etc/rc.local

1.1. Contents of rc.local

Insert the contents below into the file.

#!/bin/sh

# Insert your commands below

Close nano and save the file.

1.2. Setting permissions

Make sure the script is executable.

sudo chmod +x /etc/rc.local

To enable the rc.local service to run at boot, you also need to create a system service unit for it. Create a file for the service unit.

2. Create a service for rc.local


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

2.1. Contents for rc-local.service

Add the following contents to the file you just created:

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

[Service]
Type=oneshot
ExecStart=/etc/rc.local
TimeoutSec=0
StandardOutput=tty
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target

Save the file and exit the text editor.

2.2. Handling the service

We need to reload the daemons, enable our newly created daemon, start it and then we will check the status. All commands can be pasted in one go:

sudo systemctl daemon-reload
sudo systemctl enable rc-local.service
sudo systemctl start rc-local.service
sudo systemctl status rc-local.service