How to automatically execute a root command at startup in Linux Mint 21.x and Ubuntu 22.04.x


Back to the home page


It's not very straightforward to automatically run a root command at startup (with sudo permissions) in Linux Mint and Ubuntu. Unfortunately, systemd has complicated this a lot.... Nevertheless, this how-to will teach you how to achieve this easily.

Note: only for root commands! Do not use this how-to for commands that don't need root permissions.

This how-to doesn't make use of the backwards compatibility that systemd currently still has with the ancient discarded /etc/rc.local. Because it's unclear for how long systemd will continue to support that obsolete method.... Instead, this how-to utilizes the officially recommended modern features of systemd.

Proceed like this:

1. Launch Timeshift and create a manual snapshot of your system, so that you can always roll back your system if things go south.

2. Launch a terminal window.
(You can launch a terminal window like this: *Click*)

An example makes it easier, so I'll use an example in which I want to implement a root command to automatically lower the Tx-power of a WiFi chipset upon startup. Of course you should modify that command according to your wishes and purposes.

In the example I've italicized and boldened what you need to modify, namely the command line contained in the script.

3. First you're going to create a shell script file, that systemd needs to execute upon booting. "Captain" is a nice appropriate name for it, because a captain issues commands.
Copy/paste the following command line into the terminal, for creating a new shell script file called captain.sh:

sudo touch /opt/captain.sh

(if you type: don't omit the space after touch!)

Press Enter. Type your password when prompted. In Ubuntu this remains entirely invisible, not even dots will show when you type it, that's normal. In Mint this has changed: you'll see asterisks when you type. Press Enter again.

4. Copy/paste the following command line into the terminal, in order to edit the new file:

xed admin:///opt/captain.sh

(Note: the three consecutive slashes aren't a typo, but intentional! For Ubuntu: type gedit instead of xed.)

Press Enter.

5. Copy/paste the blue text block below, into the empty file. This text block contains an example command (modify at will), that reduces the Tx-power of a WiFi chipset called wlp2s0.

With a delay ("sleep") of 20 seconds, so that the system is fully operational when it happens (this delay reduces the risk of problems, so it's generally useful for almost all potential commands):

#!/bin/sh
sleep 20
iwconfig wlp2s0 txpower 5
exit 0

Note that the actual command does not (and must not) contain sudo! That's because in systemd it already has root permissions.

Save the modified file and close it.

6. Copy/paste the following command line into the terminal, in order to make the shell script executable:

sudo chmod u+x /opt/captain.sh

Press Enter.

7. Copy/paste the following command line into the terminal, in order to create a new systemd service file:

sudo touch /etc/systemd/system/captain.service

Press Enter.

8. Copy/paste the following command line into the terminal, in order to edit the new service file:

xed admin:///etc/systemd/system/captain.service

Press Enter.

9. Copy/paste the following blue text block into the empty text file:

[Unit]
Description=Captain service
After=network.target

[Service]
ExecStart=/opt/captain.sh

[Install]
WantedBy=multi-user.target

Save the modified file and close it.

10. Copy/paste the following command line into the terminal, in order to start the new service:

sudo systemctl start captain

Press Enter.

11. Copy/paste the following command line into the terminal, in order to make it launch automatically upon booting:

sudo systemctl enable captain

Press Enter.

12. Reboot your computer.

13. Verify whether the command has actually been issued. Note that you have to wait a bit: it should have effect 20 seconds after logging in!

That's it. So many hoops to jump through, for such a simple thing.... Don't we all just love how easy systemd is making our lives for us.


How to undo

Want to undo? This is how:

Execute the following four commands, in this order:

sudo systemctl stop captain

sudo systemctl disable captain

sudo rm -v /opt/captain.sh

sudo rm -v /etc/systemd/system/captain.service

Reboot, and all should be, as it was before....


Want more tips?

Do you want more tips and tweaks? There's a lot more of them on this website!

For example:

Speed up your Linux Mint!

Clean your Linux Mint safely

Avoid 10 fatal mistakes


To the content of this website applies a Creative Commons license.

Back to the home page

Disclaimer