Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I had a script that automatically enables my wifi without using networkmanager, but I don't know how to run the script as root while the system is booting. How do I make the script run automatically during boot?

share|improve this question
up vote 28 down vote accepted

Place the script you want to run in the /etc/init.d directory and make the script executable.

chmod 755 myscript

Once that is done create a symbolic link in the run level directory you would like to use, for example if you wanted to run a program in the graphical runlevel 2, the default runlevel for Ubuntu, you would place it in the /etc/rc2.d directory. You just cannot place it the directory, you must signify when it will run by indicating the startup with an “S” and the execution order is important. Place it after everything else that is in the directory by giving it a higher number.

If the last script to be run is rc.local and it is named S99rc.local then you need to add your script as S99myscript.

ln -s /etc/init.d/myscript /etc/rc3.d/S99myscript

Each backward compatible /etc/rc*.d directory has symbolic links to the /etc/init.d/ directory.

share|improve this answer
Note that under Ubuntu Server 14.04, I needed to use /etc/rc2.d, instead of rc3.d as indicated in the example. It seems rc2.d isn't just the graphical run level. – Brad Dec 24 '14 at 3:52

Include the command in /etc/rc.local. It will be run whenever the user's runlevel changes.

Note: You have to put the command before the last line in /etc/rc.local that contains: "exit 0".

share|improve this answer
Note: you have to put the command before the last line in /etc/rc.local that contains: exit 0 – rubo77 Oct 14 '14 at 20:02
/etc/rc.local in ubuntu 15.10 has in comment that "This script is executed at the end of each multiuser runlevel." So the script could be run several times during boot, right? – jarno Dec 27 '15 at 13:33

Include your Script file to /etc/init.d/ with Executable permission then set different run level

$ update-rc.d script-name default

It will put your script on boot startup.

share|improve this answer
Does not work for me on Kali Linux v2.0. Did you mean update-rc.d script-name enable? – Sopalajo de Arrierez Oct 5 '15 at 3:38
update-rc.d <script-name> default shell script which you have copy at /etc/init.d/<script-name> – M S Parmar Oct 5 '15 at 6:32

Use a crontab option to make your script run after reboot,

you can do it by adding @reboot code in cron

@reboot yourScriptPath 

will do it

share|improve this answer
Did you mean using command crontab -e, which does not need superuser privileges? I wonder, if it can run scripts that need to be run as root. – jarno Dec 27 '15 at 12:26
I tested this. It does not run the script as root. – jarno Jan 11 at 13:43
You have to add the line while running command sudo crontab -e to make it run the script as root during startup. – jarno Apr 14 at 14:12
don't forget to remove /var/run/crond.reboot, if not, won't execute the task – Albert Català May 20 at 10:46

Create a text file like this in /etc/cron.d/:


@reboot   root    yourScriptPath

(You should replace yourScriptPath with the path to the script you want to run.)

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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