There are plenty of times when you may want to add a new service to start when your Linux machine boots. Or you may want to stop a service from starting upon boot. And, like nearly every aspect of Linux, there are many ways to deal with this scenario. And different distributions handle this in different ways. So what is the best way for you to manage this task?
Because different distributions handle this task differently, we will examine how Fedora (and friends) handle the task and how Ubuntu (and friends) handle the task. As well we will also examine a neutral method that can always work in a pinch. All three methods will be command line, so stretch out those fingers and get ready to type.
Fedora (and friends)
The Fedora distribution uses the chkconfig command to update and query system run-level information for system services. The usage of this command is:
chkconfig OPTIONS SERVICE ON/OF
- OPTIONS are the various options the command offers.
- SERVICE is the service you want to add at startup.
- ON/OFF is either on or off - depending on if you want the service to start or not.
The confusion with the chkconfig command generally boils down to runlevel. The typical Linux runlevels are:
- 0 - Halt
- 1 - Single user mode
- 2 - Multi user mode
- 3 - Multi user mode with networking
- 4 - Not used
- 5 - X11
- 6 - Reboot
So with chkconfig you can also define at which point the service starts. So let's say you want Apache to start at boot and you want it to start for levels 3, 4, and 5. For this you would issue the command (as the root user):
chkconfig --level 345 httpd on
Now, if you don't want Apache to run at boot you could issue the command:
chkconfig httpd off
If you want to know what services are running at boot you can issue the command:
The above command will list out all services that are starting at boot time.
Ubuntu (and friends)
Ubuntu (and friends) takes a totally different route to the same destination. Instead of using chkconfig Ubuntu uses the update-rc.d command. This command makes things pretty simple. The command structure is:
update-rc.d SERVICE OPTIONS
Where OPTIONS are available options and SERVICE is the service you want to start.
With update-rc.d there is an option that makes it simple: defaults. So to add sshd to the start up process, you would issue the command:
sudo update-rc.d sshd defaults
To remove the same service from start up you would issue the following command:
sudo update-rc.d sshd remove
Now let's take a look at a fail safe, nearly-universal method
There is another means of getting a service to start. I recommend using either of the two above before you try this means. The rc.local file is a file that is executed at the end of the multiuser runlevel. By default, this script does nothing, but you can add to it so that it does.
Say you want Apache to start at boot up, and you want to do so from rc.local. You can do this by adding one of the following lines at the end of your /etc/rc.local file.
Save that file and you should be good to go. If you change your mind and do not want that service to start at boot, just remove the line you added.
The above should allow you get that service that needs to start at boot working correctly. Make sure, however, you use the distribution-prescribed method before you use the rc.local method.