How can I make /etc/rc.local run on startup?

Can you run your script manually; if not, it's a problem with that script, otherwise look more at rc.local. If that script needs to run as root, sudo must be used to manually run it.

  • Ensure /etc/rc.local, and the script it call, is executable:
    ls -l /etc/rc.local
      -rwxr-xr-x 1 root root 419 2010-08-27 11:26 /etc/rc.local
    
  • Ensure rc.local has a shebang line, which is the default:
    head -n1 /etc/rc.local
      #!/bin/sh -e
    

In my case, none of the instructions were a perfect solution, so try this detailed one:

  1. Save all executing code in a separate text file with an arbitrary name, such as foo.sh
  2. Add #!/bin/sh as the first line in foo.sh, executing it via sudo foo.sh to check for errors
  3. In /etc/rc.local, place the full pathname to foo.sh, prefaced with sh, before exit 0:
    sh '/path/to/your/script/foo.sh'
    
  4. Verify the first line in /etc/rc.local is #!/bin/sh -e
  5. Ensure /etc/rc.local is executable:
    sudo chown root /etc/rc.local
    sudo chmod 755 /etc/rc.local
    
  6. Verify everything works fine:
    sudo /etc/init.d/rc.local start
    
  7. Reboot to test

On newer Ubuntu versions systemd is used and /etc/rc.local is not loaded always by default.

Check if the Compatibility service is loaded with

systemctl status rc-local.service

screenshot

If it contains active (exited) your setting seems fine and you could have another error in your /etc/rc.local file (this could be a command that fails for example).