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.

Amie Nel
  • 311
  • 1
  • 2
  • 3
  • why do you believe you would need sudo at boot time? – Rinzwind Jun 14 '19 at 14:39
  • @Rinzwind probably because ethtool needs root to run. SystemD unit as root is the best approach – Thomas Ward Jun 14 '19 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 '19 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 '19 at 02:41

2 Answers2

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.

deimos
  • 721
  • 6
    If @deimos excellent answer was helpful, please accept it: https://askubuntu.com/tour The searchers will appreciate knowing that the answer works as expected. – chili555 Jun 14 '19 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 – Victor Lamoine Dec 02 '19 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.