Operations 12 min read

Mastering chkconfig: Control Linux Services and Runlevels with Ease

This article explains the chkconfig command for managing Linux services across runlevels, detailing its syntax, common usage examples, custom service creation, and includes sample init scripts, illustrating how to add, remove, enable, disable, and query services effectively.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Mastering chkconfig: Control Linux Services and Runlevels with Ease

Command Overview

chkconfig is used to update, query, and modify system services for different runlevels. It acts as a switch to enable or disable services at runlevels 0‑6.

Runlevel Definitions

0 – Halt

1 – Single‑user mode

2 – Multi‑user without NFS

3 – Full multi‑user (standard)

4 – Unused

5 – X11 (graphical)

6 – Reboot

Common Commands

chkconfig --list [name]          # List services
chkconfig --add [name]            # Add service
chkconfig --del [name]            # Delete service
chkconfig --level <level> <name> on|off|reset

The --level option specifies which runlevels (e.g., 235) the service should be on or off, with priority numbers for start (on) and stop (off).

Configuration File Example

# chkconfig: 345 30 70
# description: Saves and restores system entropy pool

This header indicates the service runs at levels 3,4,5 with start priority 30 and stop priority 70.

Typical Usage Examples

# vi check.sh
chkconfig network && echo "Network service is configured"
chkconfig httpd   && echo "httpd service is configured"
# chkconfig --list
abrtd   0:off 1:off 2:off 3:off 4:off 5:off 6:off
...
# chkconfig --list | grep 3:on
# chkconfig --list | grep network
# chkconfig --add iptables
# chkconfig --list | grep iptables
iptables 0:off 1:off 2:on 3:on 4:on 5:on 6:off
# chkconfig --del iptables
# chkconfig --level 5 mysql off
# chkconfig --level 235 mysql on

Custom Service Creation

To create a custom service, place a script in /etc/init.d with a proper chkconfig header.

#!/bin/bash
# chkconfig: 345 30 70
# description: Test Service Running different level
# author: Jerry_1126
# version: v1.01

case "$1" in
  start) echo "The Test service is started!" ;;
  stop)  echo "The Test service is stopped!" ;;
  restart) echo "The Test service is restarted!" ;;
  *) echo "Invalid parameter!" ;;
esac

After making the script executable, register it with:

chmod +x /etc/init.d/test
chkconfig --add test
chkconfig --list | grep test

Appendix: Common Services

httpd – Web server

crond – Scheduled tasks

dhcpd – DHCP server

named – DNS server

nfs – NFS service

... (additional services listed)

Example: Apache httpd Init Script

#!/bin/bash
# chkconfig: 85 15
# description: Apache HTTP Server

# Load configuration
[ -f /etc/sysconfig/httpd ] && . /etc/sysconfig/httpd

apachectl=/usr/sbin/apachectl
httpd=${HTTPD-/usr/sbin/httpd}
pidfile=${PIDFILE-/var/run/httpd.pid}
lockfile=${LOCKFILE-/var/lock/subsys/httpd}

start() {
  echo -n "Starting $prog: "
  LANG=$HTTPD_LANG daemon --pidfile=$pidfile $httpd $OPTIONS
  RETVAL=$?
  [ $RETVAL -eq 0 ] && touch $lockfile
  return $RETVAL
}
# ... (rest of script omitted for brevity)

The script demonstrates typical start, stop, reload, and status functions for managing the Apache service.

LinuxService Managementrunlevelschkconfiginit scripts
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.