0

I need to run a script upon startup, which I understand needs to be done in the init.d folder. Now this script requires sudo permissions, it prompts the user for the sudo password when being run normally.

Is it possible to have my startup script run with sudo permissions automatically granted? Do I need to give the user password-less sudo permission? Ideally I'd have it run without any user interaction.

1

Normally, the system runs startup scripts with full root access by default, so you don't have to do anything.

Note that the script location depends on which init package is used in your system.

For classic SysVinit, the start-up scripts are usually located in /etc/init.d and symbolic links indicating their place in the startup/shutdown order are created in /etc/rc<number>.d/ directories, where <number> indicates the runlevel your script is supposed to run on. (A runlevel is sort of "overall system state": on RedHat-style systems, the default runlevel is 5 if you have GUI login enabled, and 3 otherwise. On Debian-style systems, it tends to be 3 always.)

Updating the symbolic links in /etc/rc<number>.d/ can be tedious, so there is often a tool that can do it for you. The most common such tool is chkconfig: it requires you to place some specially formatted comments at the beginning of your script (see man chkconfig for details appropriate to your distribution). Then you can just place your script into /etc/init.d and run sudo chkconfig --add <your script name>.

If your system uses a newer replacement for SysVinit, like upstart or systemd, they may include a SysVinit compatibility system so the above instructions may still apply; but you should consider learning the new ways too, since SysVinit is considered aged and less than optimal for modern multi-core systems.

2
  • I've looked into this and the two systems I have are Ubuntu 14.04 using upstart, and Ubuntu 16.04 using systemd. How can I tell if they have Sys V compatibility? Does that mean the OS would run scripts from both init systems? Apr 25 '18 at 18:13
  • Basically, if /etc/init.d/ exists (and is populated by anything other than a README telling not to put scripts there any more), you can expect that the compatibility mechanism exists. Although competing init systems existed, SysVinit was the standard solution in the Unix world for such a long time that compatibility with it is not going to vanish overnight.
    – telcoM
    Apr 25 '18 at 21:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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