How-To manage services with chkconfig and service

GeSHi installation not found. See configuration of the filter.

Red Hat based distribution come with a set of command line utilities to manage services and daemons: chkconfig and service.

chkconfig will allow you to manipulate if weather or not a service should be started for each runlevels.
On the other hand, service will let you start, stop and get a service status at run time.

This tutorial will explain how you can activate/desactivate and change service status using both chkconfig and service

Any time you install a new service under Fedora, RedHat or CentOS, the service won't be started automatically, nor will it be upon reboot.

To start, stop or get a service status, the service command will be used. On the other hand, chkconfig will be used to set a service to be started or not at certain runlevels during boot up.

1. Manipulating service during Run-Time: service

After you installed a new service package, you might want to configure it and once configured, start it. In this example, we will start the apache service, named httpd. To do so, simply type:

# service httpd start

To know which arguments are available, type:

# service httpd

Now, if you want to stop it. Type:

# service httpd stop

You can also get the status of the service, weather it is running or not by typing:

# service httpd status

Unfortunately, upon reboot, those services won't be restarted. To make sure the service will be started, you need to use chkconfig.

2. Setting services to start on boot up

You can set the services to start or not upon boot up in a persistant way by using chkconfig.

2.1. Listing the services

First of all, you might want to check the actual settings by listing the services. This can be done with:

# chkconfig --list

It is a good idea to pipe the result to grep:

# chkconfig --list | grep service

If you know the exact name of the service use:

# chkconfig --list servicename

You should get an output like:

[root@localhost ~]# chkconfig --list sendmail
sendmail 0:off 1:off 2:on 3:on 4:on 5:on 6:off

Services managed by xinetd have a different output:

# chkconfig --list tftp
tftp off

2.2. Changing service settings

Let's carry on with the output of sendmail. We can see that it is started when booting on runlevel 2,3,4,5. We can change this behaviour and set it not to start at all, whichever is the runlevel with:

[root@localhost ~]# chkconfig --level 2345 sendmail off
[root@localhost ~]# chkconfig --list sendmail
sendmail 0:off 1:off 2:off 3:off 4:off 5:off 6:off

Now, lets say you want sendmail to be started on runlevel 3 and 5, this can be achive with:

[root@localhost ~]# chkconfig --level 35 sendmail on
[root@localhost ~]# chkconfig --list sendmail
sendmail 0:off 1:off 2:off 3:on 4:off 5:on 6:off

When enabling or disable a service managed by xinetd, let say tftp the syntax and effect are different, running :

# chkconfig tftp off

will disable tftp on boot and will stop tftp without having to restart xinetd with:

service xinetd restart

2.3. Adding and removing a service from chkconfig manager

You can add a service which supports chkconfig by typing:

# chkconfig --add servicename

Likewise, you can remove a service from chkconfig management by typing:

# chkconfig --del servicename

Mind that this will have the effect of disabling the service upon next boot