Introduction to Linux - A Hands on Guide | Linux Bible | Linux From Scratch | A Newbie's Getting Started Guide to Linux | Linux Command Line Cheat Sheet | More Linux eBooks



Sunday, 25 January 2015

Tutorial on "chkconfig" Command in Linux with Examples

    Whenever a new service (like Samba or NFS) is added to a Linux system, it is not configured to start automatically when the system starts up. So, by default, whenever you add a new service, you'll have to start it manually after the system reboots. 'chkconfig' command allows you to configure that newly added service to start after every system start up automatically. Not only this, you can change the configuration so as to add any service at different run-levels. With 'chkconfig' you can display the list of services those are configured for startup at a particular run-level.



This article explains the use of 'chkconfig' command in Linux.

1. Check Service Status

With 'chkconfig', you can determine whether a service is configured for startup, using the 'exit status' of the command. For this, just mention the service name as an argument to 'chkconfig' command. If the service is configured for startup, it's exit status will be '0', if not then it will be '1' (or non-zero).

Note: Exit status of any command can be determined by executing echo $? immediately after that command is executed. Exit status '0' denotes successful command execution, while non-zero exit status indicates that there was some error in executing that command or the command failed to execute.

Syntax:

chkconfig <SERVICE-NAME>
Example:

MyLinuxBox mandar ~ > chkconfig sshd
MyLinuxBox mandar ~ > echo $?
0
MyLinuxBox mandar ~ > chkconfig smbd
MyLinuxBox mandar ~ > echo $?
1
You can make use of this exit status in a bash script as follows (provide service name as an argument to the script):

#!/bin/bash

chkconfig $1

if [ $1 -eq 0 ]
then
    echo "Service $1 is configured for startup."
else
    echo "Service $1 is not configured for startup."
fi
Or simply:

#!/bin/bash
(chkconfig $1 && echo "Configured!") || echo "Not Configured."

2. Display configuration status of all services

When 'chkconfig' is executed with option --list, it will display all the services and their startup configuration status.

MyLinuxBox mandar ~ > chkconfig --list
NetWorkManager   0:off   1:off   2:off   3:on    4:off   5:on    6:off
abrtd            0:off   1:off   2:off   3:on    4:off   5:on    6:off
acpid            0:off   1:off   2:on    3:on    4:on    5:on    6:off
atd              0:off   1:off   2:off   3:on    4:on    5:on    6:off
.
.
.
.
As above command will display all configuration of all the services, it might be very difficult to locate a particular service in that long list. Here, 'grep' would come to the rescue. Let's verify the startup configuration of 'sshd' once again.

MyLinuxBox mandar ~ > chkconfig --list | grep sshd
sshd   0:off   1:off   2:on    3:on    4:on    5:on    6:off
With the combination of chkconfig and grep, you can find all the services which have been configured to start at a particular run-level, as follows:

Syntax:

chkconfig --list | grep 3:on

3. Adding and Deleting Services

'chkconfig' command with the options --add and --del would add and delete, respectively, services would be turned on the run-levels 2 through 5. Let's suppose, we have newly installed 'vsftpd' on our system. So, its startup configuration would look like:

MyLinuxBox mandar ~ > chkconfig --list | grep vsftpd
vsftpd   0:off   1:off   2:off   3:off   4:off   5:off   6:off
Lets add it to the startup configuration.

MyLinuxBox mandar ~ > chkconfig --add vsftpd
MyLinuxBox mandar ~ > chkconfig --list | grep vsftpd
vsftpd   0:off   1:off   2:on    3:on   4:on    5:on    6:off
Lets now remove the 'vsftpd' service from the startup list.

MyLinuxBox mandar ~ > chkconfig --del vsftpd
MyLinuxBox mandar ~ > chkconfig --list | grep vsftpd
vsftpd   0:off   1:off   2:off   3:off   4:off   5:off   6:off

4. Add or Remove a Service at a particular run-level

'chkconfig' command when executed with --add or --del options, it would impact all the run-levels from 2 through 5. In order to turn on or off any particular service at a particular run-level, you can use:

chkconfig --level <LEVEL> <SERVICE> <on/off>
Example:
MyLinuxBox mandar ~ > chkconfig --level 35 vsftpd on
MyLinuxBox mandar ~ > chkconfig --list | grep vsftpd
vsftpd   0:off   1:off   2:off   3:on   4:off   5:off   6:off

MyLinuxBox mandar ~ > chkconfig --level 35 vsftpd off
MyLinuxBox mandar ~ > chkconfig --list | grep vsftpd
vsftpd   0:off   1:off   2:off   3:off   4:off   5:off   6:off

5. How it works?

Just take a look at /etc/rc.d directory and its contents. It has some directories named rc0.d, rc1.d, rc2.d, rc3.d, rc4.d, rc5.d and rc6.d. Each directory is associated with a run-level. Whenever a service is added/removed to/from the startup, a file (or a script file rather) linking to the corresponding service in /etc/init.d is created in /etc/rc.d/rcX.d directory, where X is the run-level.

For example, if service 'vsftpd' is added to the startup at run-level 3, a script (with name somewhat like S24vsftpd) would be created in /etc/rc.d/rc3.d which will be linked to /etc/init.d/vsftpd file. If the service is removed from startup, the filename would still be pointing to the same file, but it's name would be something like 'K24vsftpd'.

Note: If the file name starts with 'S', the service will be started at that particular run-level, whereas if the file name starts with 'K', the service will be killed.

Check:
1. Make sure that 'vsftpd' is turned off at all levels.


2. Check in /etc/rc.d/rc3.d and /etc/rc.d/rc5.d directories for any scripts related to 'vsftpd' service.


Yes, as expected, we could find a file, whose name starts with 'K', in both rc3.d and rc5.d directories.
3. Add the 'vsftpd' service to the startup at run-levels 3 and 5.


4. Now, check again for any entries for 'vsftpd' in rc3.d and rc5.d directories.


Yes, there are !!

That's all for this article. Stay tuned for a lot more articles on various Linux commands.




0 comments:

Post a Comment