Linux chkconfig Command: Managing System Services Across Runlevels
Overview
The chkconfig command provides a way to query and modify system services across different runlevels. Developed by Red Hat under GPL, this tool manages the services that execute during various system boot stages, including persistent daemons. It is important to note that chkconfig does not immediately enable or disable a service—rather, it modifies symbolic links to control service behavior at the next boot or runlevel change.
Syntax
chkconfig [options]
Options
--add <service> Adds the specified service to chkconfig management and updates system startup files
--del <service> Removes the specified service from chkconfig management and deletes related entries
--level <levels> Specifies which runlevels the service should be started or stopped in
Runlevel Reference
Red Hat-based distributions use the following runlevel designations:
| Level | Purpose |
|---|---|
| 0 | Halt/Shutdown |
| 1 | Single-user mode |
| 2 | Multi-user without NFS |
| 3 | Full multi-user mode (text interface) |
| 4 | Reserved for custom use |
| 5 | Full multi-user with X11 (graphical interface) |
| 6 | Reboot |
The --level option accepts a combination of runlevel numbers. The tool operates independently of the current runlevel—it can query or modify service status for any runlevel configuration. When changing runlevels, init does not restart serviecs already running nor stop services already halted.
Service Script Requirements
Each service managed by chkconfig requires a script in /etc/rc.d/ (or /etc/init.d/) with a header containing at least two comment lines:
# chkconfig: 2345 20 80
# description: Manages the example service daemon \
# for handling background tasks
The first line specifies the default runlevels (where the service starts) and its start/stop priority. A hyphen indicates the service should not start by default. The description can span multiple lines using backslash continuation.
Usage Examples
chkconfig --add nginx # Register nginx service with chkconfig
chkconfig --del nginx # Remove nginx service from chkconfig
chkconfig --level 235 nginx on # Enable nginx for runlevels 2, 3, and 5
chkconfig --list # Display all services and their runlevel states
chkconfig --list postfix # Show postfix service configuration
chkconfig --level 35 postfix off # Disable postfix at runlevels 3 and 5
chkconfig postfix on # Enable postfix at all applicable levels (2,3,4,5)
chkconfig --level 2345 redis on # Activate redis across runlevels 2, 3, 4, 5
Adding a New Service
To register a new service with chkconfig:
- Place the service init script in
/etc/init.d/ - Ensure the script conatins the proper chkconfig header comment
- Execute
chkconfig --add servicenameto register it—this creates the appropriate K/S symlinks in/etc/rc.d/rcN.d/ - Configure default runlevel behavior with
chkconfig --level 35 servicename on