Linux, like all operating systems, is nothing more than a collection of daemons. Each daemon can be configured as a service to startup at boot time and to properly shutdown when the system is halted. In CentOS, Red Hat Linux and Fedora, Linux system services are managed using the chkconfig command, scripts are stored in the /etc/init.d directory and services can be stopped and started using the services command. This article will serve as a primer in booting, changing runlevels and configuring linux system services.
Linux Runlevels Defined
Linux (and most unix systems) share these same runlevels listed below. Different run levels exist to allow administrators to fix, repair, modify, enhance security, reboot or turn off the system. The runlevel chosen determines which services will startup at boot or at change into a runlevel. While runlevels can be modified to do whatever you as the administrator want, you will
Runlevel | Description |
---|---|
0 – Halt | Shutdown the system |
1 – Single User mode | No networking, or multiuser services. Only root user can login. Allows administrator to repair the system or perform system maintenance. |
2 – Mutli-user mode, no networking | Console logins only, no networking. Text-based console logins only. |
3 – Multi-user mode, with networking | All features of runlevel 2, but also has networking. |
4 – Unused State – User Definable | Undefined user environment. Can be used to create a custom runlevel. |
5 – Multi-User mode, with network, with graphical environment | All features of runlevel 3, but also has support for GUI desktop environment. |
6 – Reboot | Changing to this runlevel will reboot the system. |
Boot to Another Runlevel
Changing your current runlevel to another is as simple as using the telinit command. Simply run telinit with the runlevel you wish to change to as the parameter. For example:
# telinit 6
will cause the system to reboot. Specifying any of the runlevels listed above will cause the system to change to that runlevel.
Change the Default Linux Runlevel
Suppose that your server is currently booting by default to runlevel 5 and you decide that you want it to boot to runlevel 3 from now on. You will want to edit the /etc/inittab file to change the default runlevel. Your /etc/inittab file is really nothing more than a bunch of comments teaching you about runlevels (which may be instructive to read) followed by a single all important line:
id:5:initdefault:
To:
id:3:initdefault:
From this point on, your server will boot to runlevel 3 until you change it again.
Customizing Services for Each System Runlevel
Each run level provides an opportunity for each service to be either enabled to run or not. The simplest way to modify runlevel states for services is to use the chkconfig command. By running the chkconfig command with the “–list” option, you get a list of all of your services as well as a list of the Linux System run levels on which each service will automatically start. When we boot to any one of these runlevels, this is the list of services that will startup at that boot time.
# chkconfig --list NetworkManager 0:off 1:off 2:on 3:on 4:on 5:on 6:off acpid 0:off 1:off 2:on 3:on 4:on 5:on 6:off auditd 0:off 1:off 2:on 3:on 4:on 5:on 6:off crond 0:off 1:off 2:on 3:on 4:on 5:on 6:off dnsmasq 0:off 1:off 2:off 3:off 4:off 5:off 6:off dovecot 0:off 1:off 2:on 3:on 4:on 5:on 6:off haldaemon 0:off 1:off 2:off 3:on 4:on 5:on 6:off htcacheclean 0:off 1:off 2:off 3:off 4:off 5:off 6:off httpd 0:off 1:off 2:on 3:on 4:on 5:on 6:off ip6tables 0:off 1:off 2:on 3:on 4:on 5:on 6:off iptables 0:off 1:off 2:on 3:on 4:on 5:on 6:off mdmonitor 0:off 1:off 2:on 3:on 4:on 5:on 6:off messagebus 0:off 1:off 2:on 3:on 4:on 5:on 6:off mysqld 0:off 1:off 2:on 3:on 4:on 5:on 6:off nagios 0:off 1:off 2:on 3:on 4:on 5:on 6:off netconsole 0:off 1:off 2:off 3:off 4:off 5:off 6:off netfs 0:off 1:off 2:off 3:off 4:off 5:off 6:off network 0:off 1:off 2:on 3:on 4:on 5:on 6:off nginx 0:off 1:off 2:off 3:off 4:off 5:off 6:off ntpd 0:off 1:off 2:on 3:on 4:on 5:on 6:off ntpdate 0:off 1:off 2:off 3:off 4:off 5:off 6:off php-fpm 0:off 1:off 2:off 3:off 4:off 5:off 6:off portreserve 0:off 1:off 2:on 3:on 4:on 5:on 6:off postfix 0:off 1:off 2:on 3:on 4:on 5:on 6:off rdisc 0:off 1:off 2:off 3:off 4:off 5:off 6:off restorecond 0:off 1:off 2:off 3:off 4:off 5:off 6:off rsyslog 0:off 1:off 2:on 3:on 4:on 5:on 6:off saslauthd 0:off 1:off 2:off 3:off 4:off 5:off 6:off snmpd 0:off 1:off 2:off 3:off 4:off 5:off 6:off snmptrapd 0:off 1:off 2:off 3:off 4:off 5:off 6:off spamassassin 0:off 1:off 2:on 3:on 4:on 5:on 6:off sshd 0:off 1:off 2:on 3:on 4:on 5:on 6:off udev-post 0:off 1:on 2:on 3:on 4:on 5:on 6:off varnish 0:off 1:off 2:on 3:on 4:on 5:on 6:off varnishlog 0:off 1:off 2:on 3:on 4:on 5:on 6:off varnishncsa 0:off 1:off 2:on 3:on 4:on 5:on 6:off vncserver 0:off 1:off 2:off 3:off 4:off 5:off 6:off vsftpd 0:off 1:off 2:off 3:off 4:off 5:off 6:off winbind 0:off 1:off 2:off 3:off 4:off 5:off 6:off wpa_supplicant 0:off 1:off 2:off 3:off 4:off 5:off 6:off xinetd 0:off 1:off 2:off 3:off 4:off 5:off 6:off
Change Linux Service Runlevels
We can manipulate the runlevels that a service will start on using the “chkconfig” command. By just “turning on” a service, it will set the service to automatically start on run levels 2,3,4, and 5.
# chkconfig postgresql-9.3 on # chkconfig --list postgresql-9.3 postgresql-9.3 0:off 1:off 2:on 3:on 4:on 5:on 6:off
Change the Runlevels for a Service
You can change which runlevel a service will start on using chkconfig with the “level” option as shown below:
# chkconfig --level 2345 <service name> on
Chkconfig Command Syntax
The chkconfig command will return the following helpful usage description
# chkconfig --help usage: chkconfig [--list] [--type <type>] [name] chkconfig --add <name> chkconfig --del <name> chkconfig --override <name> chkconfig [--level <levels>] [--type <type>] <name> <on|off|reset|resetpriorities>
Add a Service to the Linux Services Manager Using Chkconfig
To add a service to the Linux Services Manager, you will need to first create a script that will properly start, stop, and restart (at a minimum) the new service that you want to add to the services manager. This script file should be located in the /etc/init.d directory.
At the top of the script you can put several lines of header information that chkconfig will use as it adds your service script to the Linux Services Manager. These header lines include keywords such as “chkconfig”, “description”, and “processname” that are read by the chkconfig command as it adds the service script to the services manager list.
Below is an example Service Manager script template that you can use as you write your script. You will want to edit the template below and replace the items that I put in <> with the required items, then move the script to /etc/init.d, change the ownership and groupship to root:root, and then chown it to 755.
#!/bin/bash # # <mydaemon> - This shell script takes care of starting and stopping <mydaemon> # # chkconfig: - 2345 58 74 # The chkconfig numbers above have 3 fields: # 2345 - Tells this script to run on runlevels 2,3,4, and 5. # 58 - Tells the services manager to give this service a start # priority or order of #58 # 74 - Tells the services manager to give this service a stop # priority or order of #74 # description: <Put a short description about your daemon here.> # processname: <mydaemon> # Source function library. . /etc/init.d/functions # Source networking configuration. . /etc/sysconfig/network prog=<mydaemon> lockfile=/var/lock/subsys/$prog start() { # Start daemons. echo -n $"Starting $prog: " <Place the code that will startup your daemon service here.> RETVAL=$? echo [ $RETVAL -eq 0 ] && touch $lockfile return $RETVAL } stop() { [ "$EUID" != "0" ] && exit 4 echo -n $"Shutting down $prog: " <Place the code that will properly shutdown your daemon service here.> killproc $prog # killproc is an option for killing the process and is # defined in the /etc/init.d/functions script referrenced above. RETVAL=$? echo [ $RETVAL -eq 0 ] && rm -f $lockfile return $RETVAL } # See how we were called. case "$1" in start) start ;; stop) stop ;; status) status $prog ;; restart|force-reload) stop start ;; try-restart|condrestart) if status $prog > /dev/null; then stop start fi ;; reload) exit 3 ;; *) echo $"Usage: $0 {start|stop|status|restart|try-restart|force-reload}" exit 2 esac
place the script:
# mv /etc/init.d # chown root:root /etc/init.d/<myscript> # chown 755 /etc/init.d/<myscript>
Add Your Script to Services Manager Using Chkconfig
# chkconfig --add <myscript>
that should add our script to the services manager
# chkconfig --list <mydaemon> 0:off 1:off 2:on 3:on 4:on 5:on 6:off
Remove Your Script from the Services Manager Using Chkconfig
# chkconfig --del <myscript>
Additional Resources on Chkconfig and Linux Runlevels
That’s it, you should now know enough to manage your Linux systems runlevel and services. However, if you would like to learn more on the subject, you can use these additional resources:
http://www.centos.org/docs/rhel-sag-en-3/s1-services-chkconfig.html
http://www.linuxjournal.com/article/4445
Latest posts by Jeff Staten (see all)
- Configure Your HP Procurve Switch with SNTP - May 5, 2015
- Configuring HP Procurve 2920 Switches - May 1, 2015
- Troubleshooting Sendmail - November 28, 2014