21

How do I run a script as sudo at boot time?

I need to run ethtool --offload <net> rx off to disable the annoying jme udp checksum error message.

4
  • why do you believe you would need sudo at boot time?
    – Rinzwind
    Jun 14, 2019 at 14:39
  • @Rinzwind probably because ethtool needs root to run. SystemD unit as root is the best approach
    – Thomas Ward
    Jun 14, 2019 at 14:52
  • 2
    "as sudo" - you mean "as root (the all-powerful user with $UID=0)". sudo is a tool for allowing a regular user to run a command as root. "at boot" - everything involved with system startup runs as root
    – waltinator
    Jun 14, 2019 at 17:45
  • @waltinator: except things that drop privileges; e.g. you can have your system start up an X server + user session for a certain user. But running ethtool via sudo from something that had dropped privileges (like the wording of this question suggest) would be a really bad idea vs. sticking it in /etc/rc.local or any more "modern" way to get things run as root during boot. Jun 15, 2019 at 2:41

2 Answers 2

35

You can create a systemd service.

Create a file /etc/systemd/system/ethtool.service:

[Unit]
Description=ethtool script

[Service]
ExecStart=/path/to/yourscript.sh

[Install]
WantedBy=multi-user.target

And script /path/to/yourscript.sh (don't forget to chmod +x it)

#!/bin/bash
ethtool --offload <net> rx off

Enable your service

systemctl enable ethtool

It will run on boot as root.

2
  • 6
    If @deimos excellent answer was helpful, please accept it: askubuntu.com/tour The searchers will appreciate knowing that the answer works as expected.
    – chili555
    Jun 14, 2019 at 20:40
  • 2
    sudo systemctl start ethtool to start the service immediately. systemctl status ehttool to display status (running, enabled and log). Learn more with man systemctl Dec 2, 2019 at 10:18
5

Put your commands in /etc/rc.local or create that file if it does not exist:

 touch /etc/rc.local
 chmod +x /etc/rc.local

All these actions have to be done as root.

2
  • Can we put non-root commands in rc.local? Jun 12, 2020 at 15:47
  • 2
    By now I read almost everywhere in recent articles that systemd is the way to go, and /etc/rc.local is to be seen as legacy measure.
    – steffres
    Jul 29, 2020 at 8:03

Not the answer you're looking for? Browse other questions tagged or ask your own question.