10

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.

  • why do you believe you would need sudo at boot time? – Rinzwind Jun 14 at 14:39
  • @Rinzwind probably because ethtool needs root to run. SystemD unit as root is the best approach – Thomas Ward Jun 14 at 14:52
  • 1
    "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 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. – Peter Cordes Jun 15 at 2:41
12

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.

  • 1
    Thanks, this worked for me – Amie Nel Jun 14 at 15:01
  • 1
    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 at 20:40
  • sudo systemctl start ethtool to start the service immediately. systemctl status ehttool to display status (running, enabled and log). Learn more with man systemctl – Victor Lamoine Dec 2 at 10:18
2

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.

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