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. "Imperator" is a nice appropriate name for it, because an imperator (Roman emperor) issued commands.
Copy/paste the following command line into the terminal, for creating a new shell script file called imperator.sh:
sudo touch /opt/imperator.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/imperator.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/imperator.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/imperator.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/imperator.service
Press Enter.
9. Copy/paste the following blue text block into the empty text file:
[Unit]
Description=Imperator service
After=network.target
[Service]
ExecStart=/opt/imperator.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 imperator
Press Enter.
11. Copy/paste the following command line into the terminal, in order to make it launch automatically upon booting:
sudo systemctl enable imperator
Press Enter.
12. Reboot your computer.
13. Finally, verify whether imperator has actually been issued and done its job. Note that you have to wait a bit: it should have effect 20 seconds after logging in!
This is the terminal command that you can use to check whether it has done anything to the Tx-power (obviously only useful for the example command in this how-to):
iwconfig
Press Enter.
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 imperator
sudo systemctl disable imperator
sudo rm -v /opt/imperator.sh
sudo rm -v /etc/systemd/system/imperator.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