Configuring CentOS Run Levels and Services

Configuring CentOS Run Levels and Services

Understanding CentOS Run Levels

CentOS can be configured to boot into one of a number of different run levels. During the boot sequence, a process named init looks in the /etc/inittab file to find the default run level. Having identified the run level it proceeds to execute the corresponding startup scripts located in the /etc/rc.d sub-directory.

For example, if a default run level of 5 is configured the init process will work through the list of startup scripts located in /etc/rc.d/rc5.d. These startup scripts start either with the letter “S” or “K” followed by a number and then a (hopefully) descriptive word. For example the startup script for NFS (Networked File System) is typically K20nfs whilst the startup script for the sshd service might be called S55sshd.

Scripts that start with an “S” are invoked before those prefixed with a “K”. The number in the filename controls the order in which the script will be executed with that group (either “S” or “K”). You wouldn’t, for example, want to start NFS before the basic networking is up and running. It is also worth noting that the files in the rc.d sub-directories are not the actual scripts themselves but rather symbolic links to the actual files located in /etc/rc.d/init.d.

Understanding CentOS Services

A service is essentially a process that typically runs in the background to provide specific functionality. The sshd service, for example, is the background process (also referred to as a daemon) that provides secure shell access to the system. Different run levels are configured to automatically launch different collections of services, depending on the functionality that is to be provided at that particular level.

 

CentOS Run levels Descriptions

As previously outlined, CentOS 6 can be booted into one of a number of run levels. The default run level to which the system is configured to boot will, in turn, dictate which services are started:

  • Run level 0 – The halt run level. This is the run level at which the system shuts down. For obvious reasons it is unlikely you would want this as your default run level.
  • Run level 1 – Causes the system to start up in a single user mode under which only the root user can log in. In this mode the system does not start any networking, X windowing or multi-user services. This run level is ideal for system administrators to perform system maintenance or repair activities.
  • Run level 2 – Boots the system into a multi-user mode with text based console login capability. This run level does not, however, start the network.
  • Run level 3 – Similar to run level 2 except that networking services are started. This is the most common run level for server based systems that do not require any kind of graphical desktop environment.
  • Run level 4 – Undefined run level. This run level can be configured to provide a custom boot state.
  • Run level 5 – Boots the system into a networked, multi-user state with X Window System capability. By default the graphical desktop environment will start at the end of the boot process. This is the most common run level for desktop or workstation use.
  • Run level 6 – Reboots the system. Another runlevel that, for obvious reasons, you are unlikely to want as your default.

Configuring the Default CentOS 6 Run level

The default run level for an CentOS 6 system is defined within the /etc/inittab file. To identify the current default level or change the default to a different setting, load this file into an editor (keeping in mind that root privileges will be required).

The relevant section of a sample /etc/inittab file reads as follows:

# Default runlevel. The runlevels used by RHS are:
#   0 - halt (Do NOT set initdefault to this)
#   1 - Single user mode
#   2 - Multiuser, without NFS (The same as 3, if you do not have networking)
#   3 - Full multiuser mode
#   4 - unused
#   5 - X11
#   6 - reboot (Do NOT set initdefault to this)
#
id:3:initdefault:


The key line in the example above is the init default setting:

id:3:initdefault:

This tells the init process that the default run level for the system is run level 3. To change to a different run level simply change the number to the desired runlevel and save the /etc/inittab file.

 

Change default runlevel in centos 7

The default runlevel can be set either by using the systemctl command or making a symbolic link of runlevel targets to the default target file.

Method 1

Let’s check the current run level by issuing the following command.

systemctl get-default

Output:

runlevel5.target

Before changing the default runlevel, we have to check out the available targets.

systemctl list-units --type=target

The output will look like below.

UNIT                LOAD   ACTIVE SUB    DESCRIPTION
basic.target        loaded active active Basic System
cryptsetup.target   loaded active active Encrypted Volumes
getty.target        loaded active active Login Prompts
graphical.target    loaded active active Graphical Interface
local-fs-pre.target loaded active active Local File Systems (Pre)
local-fs.target     loaded active active Local File Systems
multi-user.target   loaded active active Multi-User System
network.target      loaded active active Network
nfs.target          loaded active active Network File System Server
paths.target        loaded active active Paths
remote-fs.target    loaded active active Remote File Systems
slices.target       loaded active active Slices
sockets.target      loaded active active Sockets
swap.target         loaded active active Swap
sysinit.target      loaded active active System Initialization
timers.target       loaded active active Timers

LOAD   = Reflects whether the unit definition was properly loaded.
ACTIVE = The high-level unit activation state, i.e. generalization of SUB.
SUB    = The low-level unit activation state, values depend on unit type.

Change default to runlevel 3 (nothing but a multi-user.target).

systemctl set-default multi-user.target

Confirm the default runlevel.

systemctl get-default

Output:

multi-user.target

Reboot and check it out.

reboot