Enabling and disabling services during start up in GNU/Linux

April 01, 2006
This guide shows you how to enable and disable services in Linux. This is the command line method of enabling and disabling services. Some Linux distributions like Fedora, Ubuntu and so on do provide a GUI front-end as well.

In any Linux distribution, some services are enabled to start at boot up by default.

On my machine, I have PCMCIA, Cron, Postfix Mail Transport Agent ... just to name a few, which start during boot up.

You should run only those services that are absolutely necessary. Disable the rest, as they can be potential security risks and also waste hardware resources.


For example, my machine does not have any PCMCIA cards so I can safely disable it. Same is the case with Postfix which is also not used.

So how do you disable these services so that they are not started at boot time?

The answer to that depends on the type of Linux distribution you are using.

True, many Linux distributions including Ubuntu bundle with them a GUI front end to accomplish the task which makes it easier to enable and disable the system services.

But there is no standard GUI utility common across all Linux distributions. And this makes it worth while to learn how to enable and disable the services via the command line.

Most Linux distributions have one thing in common. That being, all the start-up scripts are stored in the /etc/init.d/ directory.

Update: As pointed out in the comments below, some Linux distributions such as Slackware use the BSD style of init scripts which are in /etc/rc.d


Lets say, you want to enable Apache web server in different run levels. Then you should have a script related to the Apache web server in the /etc/init.d/ directory.

The script is usually created at the time of installing the software. And in my machine (which runs Ubuntu), it is named apache2. Where as in Red Hat, it is named httpd. Usually, the script will have the same name as the process or daemon.

Here I will explain different ways of enabling and disabling the system services.

How to enable and disable services in Red Hat


Red Hat, Fedora, and Red Hat based Linux distributions such as CentOS make use of the script called chkconfig to enable and disable the system services running in Linux.

How to enable a service


As an example, lets enable the Apache web server to start in run levels 2, 3, and 5. This is how it is done.

We first add the service using chkconfig script. Then turn on the service at the desired run levels.

# chkconfig httpd --add
# chkconfig  httpd  on --level 2,3,5

This will enable the Apache web server to automatically start in the run levels 2, 3 and 5. You can check this by running the command -

# chkconfig --list httpd

How to disable a service


# chkconfig httpd off
# chkconfig httpd --del

Red Hat/Fedora also have a useful script called service which can be used to start or stop any service. For example, to start Apache web server using the service script, the command is as follows -

# service httpd start

and to stop the service...

# service httpd stop

The options being start, stop and restart which are self explanatory.

How to enable or disable services in Debian / Ubuntu


To Enable and disable services across runlevels in Debian, Ubuntu, and other Debian based Linux distributions we use a script called update-rc.d.

How to enable a service


As an example, to enable Apache web server in Debian, do the following -

# update-rc.d apache2 defaults

... this will enable the Apache web server to start in the default run levels of 2,3,4 and 5. Of course, you can do it explicitly by giving the run levels instead of the defaults keyword as follows:

# update-rc.d apache2 start 20 2 3 4 5 . stop 80 0 1 6 .

The above command modifies the sym-links in the respective /etc/rcX.d directories to start or stop the service in the destined runlevels. Here X stands for a value of 0 to 6 depending on the runlevel. One thing to note here is the dot (.) which is used to terminate the set which is important. Also 20 and 80 are the sequence codes which decides in what order of precedence the scripts in the /etc/init.d/ directory should be started or stopped.

To enable the service only in runlevel 5, you do this instead -

# update-rc.d apache2  start 20 5 . stop 80 0 1 2 3 4 6 .

How to disable a service


To disable the service in all the run levels, you execute the command:

# update-rc.d -f apache2 remove

Here -f option which stands for force is mandatory.

How to enable or disable services in Gentoo


Gentoo Linux also uses a script to enable or disable services during boot-up. The name of the script is rc-update. Gentoo has three default run levels. They are -

  1. boot,
  2. default and
  3. nonetwork.

How to enable a service


Lets set Apache web server to start in the default runlevel.

# rc-update add apache2 default

How to disable a service


To remove the web server, use the del option as follows.

# rc-update del apache2

List all running services


To list all the running services at your run level and check their status, you use the rc-status command.

# rc-status --all

How to enable and disable services manually


I remember the first time I started using Linux, there were no such scripts to aid the user in enabling or disabling the services during start-up.

You did it the old fashioned way which was creating or deleting symbolic links in the respective /etc/rcX.d/ directories. Here X in rcX.d is a number which stands for the runlevel.

As an example, here is a listing of all the services started in runlevel 5 in my PC running Ubuntu.

ls -l /etc/rc5.d

And the output is ...

lrwxrwxrwx 1 root root  26 2012-03-18 21:10 S20apache2 -> ../init.d/apache2
lrwxrwxrwx 1 root root  20 2011-10-27 12:46 S20kerneloops -> ../init.d/kerneloops
lrwxrwxrwx 1 root root  17 2012-03-19 13:09 S20postfix -> ../init.d/postfix
lrwxrwxrwx 1 root root  27 2011-10-27 12:46 S20speech-dispatcher -> ../init.d/speech-dispatcher
... Shortened for brevity ...
lrwxrwxrwx 1 root root  22 2011-10-27 12:46 S99acpi-support -> ../init.d/acpi-support
lrwxrwxrwx 1 root root  21 2011-10-27 12:46 S99grub-common -> ../init.d/grub-common
lrwxrwxrwx 1 root root  18 2011-10-27 12:46 S99ondemand -> ../init.d/ondemand
lrwxrwxrwx 1 root root  18 2011-10-27 12:46 S99rc.local -> ../init.d/rc.local

As you can see, all the content in the /etc/rc5.d directory are symbolic links pointing to the corresponding script in the /etc/init.d directory.

There can be two kinds of symbolic links in the /etc/rcX.d/ directories.

One starts with the character 'S' followed by a number between 0 and 99 to denote the priority, followed by the name of the service you want to enable.

The second kind of symlink has a name which starts with a 'K' followed by a number and then the name of the service you want to disable.

So in any /etc/rcX.d directory, at any given time, for each service, there should be only one symlink of the 'S' OR 'K' variety but not both.

So coming back to our Apache example, suppose I want to enable Apache web server in the run level 5 but want to disable it in all other run levels, I do the following:

First to enable the service for run level 5, I move into /etc/rc5.d/ directory and create a symlink to the apache service script residing in the /etc/init.d/ directory as follows:

# cd /etc/rc5.d/
# ln -s /etc/init.d/apache2 S20apache2

This creates a symbolic link in the /etc/rc5.d/ directory which the system interprets as - start (S) the apache service before all the services which have a priority number greater than 20.

If you do a long listing of the directory /etc/rc5.d in your system, you can find a lot of symlinks similar to the one below.

# ls -l /etc/rc5.d |grep apache2
lrwxrwxrwx 1 root root  26 2012-03-18 21:10 S20apache2 -> ../init.d/apache2

Now if I start a service, I will want to stop the service while rebooting or while moving to single user mode and so on. So in those run levels I have to create the symlinks starting with character 'K'.

So going back to the apache2 service example, if I want to automatically stop the service when the system goes into run level 0, 1 or 6, I will have to create the symlinks in the /etc/rc0.d, /etc/rc1.d/, /etc/rc6.d/ directories as follows -

# cd /etc/rc0.d
# ln -s /etc/init.d/apache2 K80apache2

One interesting aspect here is the priority. Lower the number, the higher is the priority.

So since the starting priority of apache2 is 20 - that is apache starts way ahead of other services during startup, we give it a stopping priority of 80.

There is no hard and fast rule for this but usually, you follow the formula as follows:

If you have 'N' as the priority number for starting a service, you use the number (100-N) for the stopping priority number and vice versa.

26 comments:

  • thank you, this is useful
    on SuSE 10.0, chkconfig method works

  • You missed CRUX/Archlinux's way of doing this:
    startup scripts are not stored in /etc/init.d, but rather in /etc/rc.d. A list of the services you want started is put into /etc/rc.conf and each is started in the order listed (and shutdown in the reverse order in which they were started); it looks like "SERVICES=(net inetd crond named sshd apache)" on my server running an old copy of CRUX for example. It will also run the script /etc/rc.local which can manually start services if you don't want to bother writing a special script; this is much more BSD-like than the SYSV-like init systems you've described. There are no runlevels (or rather, only one runlevel).

    Also, I'm not really sure what the startup scripts of various distributions have to do with either GNU or Linux (as referenced in the title of the post), since I don't think either provides or even recommends one sort of startup script or another (well, I suppose Hurd must have something, but I don't know if it is packaged seperately).

  • Soumyadip Modak

    Well, re: Debian, you could perhaps consider the use of rcconf. While not as granular as the method that you listed here, it is nevertheless easier for newcomers.

    apt-get install rcconf

  • HackMan

    There is one other type of system initialization which is also used. For example in Slackware all startup scripts are located at /etc/rc.d/ and there is no /etc/init.d directory. For most of the services there are separate rc files such as /etc/rc.d/rc.httpd or /etc/rc.d/rc.inetd. There are 2 tings that you can do to disable a service.
    1. You can chmod 644 /etc/rc.d/rc.servicename
    2. You can remove or comment the lines which call this file. Depending when the things are called you can find the lines calling the applications in:
    rc.S - system initialization
    rc.M - multy user mode(including grafics mode)
    rc.K - single user mode

    In Slackware you can start pkgtool and use the Setup option to reset the default configurations which also include what should be started at boot time.

    Regards
    Marian Marinov

  • You're generalization about all distros using /etc/init.d is false. Slackware and it's derivatives use the BSD style /etc/rc.d for it's init scripts.

  • tommahu

    slackware method :

    chmod a-x /etc/rc.d/rc.yourservicename

  • Komakino

    It is not true to say that "all linux distributions have startup scripts in /etc/init.d as Slackware (the oldest distro still going) uses BSD style init scripts which are in /etc/rc.d and are FAR easier to work with than the sys-v style used by the other distros, as disabling them only requires making them non-executable.

  • > But one thing is common for all Linux distributions which is that all the start-up scripts are stored in the '/etc/init.d/' directory.

    One word: Slackware.

  • ashterix

    good explanation!
    just to add, for RH you can list out all services by typing:
    # chkconfig --list

    by default if you type:
    # chkconfig httpd on
    it will enable httpd in runlevel 2,3,5

  • Slackware doesn't conform with what you are saying ( /etc/rc.d )

    Cheers !

  • Stealth

    You know, you could take an easier route by installing a program called: sysv-rc-conf

    Its a slightly more graphical command-line utility (ncurses? idk) that allows you see the list of possible start up scripts, and disable what you please. :)

  • Slackware way:

    all the scripts are in /etc/rc.d/ directory.
    The ones you wanted started at boot just make them executable ie.

    code:
    chmod +x /etc/rc.d/rc.httpd
    /code

    You can also start/stop/restart services manualy:
    ie.

    code:
    sh /etc/rc.d/rc.httpd start
    /code

    If you check you will find this are wery simple shellscripts, that you can easily create/modify for your own purposes.

  • Slackware (and derivatives) don't use /etc/init.d. Instead, they use /etc/rc.d and there reside every init script (BSD style). To enable/desable you just set/reset the executable bit for the script.

  • For SuSE use the insserv(8) function.

    -d, --default Use default runlevels a defined in the scripts. This may restore an edited runlevel link scheme.

    -r, --remove Remove the listed scripts from all runlevels.

  • Jeff

    A quick note. Some distributions, notably Slackware and derivatives still use the older /etc/rc.d/ directory as the container for their startup scripts. It's really simple to enable / disable services on these distributions.
    The files in this directory are ususally named "rc.SERVICE". ie. "rc.sendmail" for the sendmail service.
    To enable / disable the service, simply add / remove the execute bit on the script.
    For apache: "chmod -x /etc/rc.d/rc.httpd" to disable Apache from loading at startup or "chmod +x /etc/rc.d/rc.httpd" to enable it. Simple no?

  • Great! Thanks for the info. Just a small detail between "3) Gentoo Method" and "5) The Old .." there was no "4)". :)

  • I prefer running ntsysv under Red Hat-style systems (or run 'setup' and choose Services) and just selecting there which services should and shouldn't start automatically. If you don't want to differentiate between runlevels I think this is the easiest way.

  • Excellent...thanks Ravi

  • It would be nice to also know how to run these services as a different user

  • It would be nice indeed. I use to do that in KDE using SysV-Init Editor but that application won't start anymore for some reason. So I need a way to set the user runnning a service from the command line :)

  • rajat swarup

    BootUpManager (a.k.a bum) is also a good tool to do this configuration.

    sudo apt-get install bum
    sudo bum

    Then simply check or uncheck the services you want to remove.

  • Free File Hosting

    Hi,

    In Centos there is RunLevels for single user mode and Multi User Mode, what are difference and in which time do I need to use Single User Mode?

    P.S: Cool website.

  • Kevin

    In response to what Free File Hosting said...

    Since centos is just a free distribuation of RHEL.

    Just follow what ashterix said...

    by default if you type:
    # chkconfig httpd on
    it will enable httpd in runlevel 2,3,5

  • Hey Thanks man, very useful !

  • Thanks for the info. Keep Up the good work.

  • For CentOs5, you have to change
    chkconfig httpd on --level 2,3,5

    to

    chkconfig --level 235 httpd on