I would like to make it so a terminal command runs every time I start up my computer, but the command I want to run requires root permissions. I enter in the command in the start up applications part of settings with sudo before it, but since it requires my password I don't think it is running.


Add a cron job to root's cron table. Cron jobs are tasks that run on a schedule. You can schedule jobs to run every time the system starts up.

As root, enter crontab -e to edit the cron table, which is just the list of scheduled tasks. Then use the @reboot nickname (instead of the normal timing syntax) to make a job that runs on every startup:

@reboot your_command

You could configure sudo to allow you to run that command without entering your password. I'd normally recommend against this, but then again I'd recommend not running root applications automatically at all (unless they're setuid root and properly permissions/security-managed), and hey it's your machine. This technique is especially useful if you need a sudo command to run, say, on every login. (A boot cronjob makes sense if it's only needed on startup, I agree).

You can do this in one of two ways:

  1. Add an appropriate entry in /etc/sudoers, the sudo configuration file, which is edited by running visudo as root (or through sudo).
  2. (RECOMMENDED) Create a file in /etc/sudoers.d with your configuration changes. The /etc/sudoers file should contain the directive #includedir /etc/sudoers.d at the bottom, which will cause it to read files in this directory as additional configs. (If it doesn't, this option #2 won't work.)

Either way, the existing /etc/sudoers has an explanation of the config syntax, but it's kind of long and you don't need to worry too much about any of the existing config. (There's also a man page, man 5 sudoers.)

What you need to do is write a user specification line allowing you to run a particular command without entering your password.

As the file comments note, user specifications have this basic format:

## Syntax:
##      user    MACHINE=COMMANDS

And there are some examples of the syntax for certain types of configs. To accomplish what you need, the user specification is:

username ALL = NOPASSWD: /path/to/command

(Replace "username" and "/path/to/command" with the appropriate values; "ALL" and "NOPASSWD" are literal.)

Note that you should always use the full path to the command, both in the sudoers configuration, and when launching it automatically. This helps ensure that the program run is the one you expect, not some other (potentially malicious) program taking its place due to $PATH hijacking. That advice also applies to root crontab entries or any other method of automatically granting root permissions.

Your Answer

