4.3 About Run Levels

4.3.1 Displaying the Run Level
4.3.2 Changing the Run Level
4.3.3 Shutting down the System
4.3.4 About Service Scripts
4.3.5 About the Service Configuration GUI
4.3.6 Starting and Stopping Services
4.3.7 Configuring Services to Start at Different Run Levels

Run levels allow you to start a system with only the services that are required for a specific purpose. For example, a server can run more efficiently at run level 3, because it does not run the X Window System at that run level. It is best to perform diagnostics, backups, and upgrades at run level 1 when only root can use the system. Each run level defines the services that init stops or starts. For example, run level 3 starts network services and run level 5 starts the X Window System, whereas run level 1 stops both of these services.

The following run levels are defined by default under Oracle Linux:

0

Halts the system.

1

Single-user text mode.

2

Full multiuser text mode without NFS support.

3

Full multiuser text mode with NFS support.

4

Not used, but can be user defined.

5

Full multiuser graphical mode with an X-based login screen.

6

Reboots the system.

4.3.1 Displaying the Run Level

To display the previous and current run level, use the runlevel command, for example:

# runlevel
N 5

If there is no previous run level, N is displayed. In this example, the current run level is 5.

The who -r command also displays the current run level, for example:

# who -r
run-level 5  2013-03-15 09:24

For more information, see the runlevel(8) and who(1) manual pages.

4.3.2 Changing the Run Level

Use the telinit command to change the system run level, for example:

# telinit 1

which place the system in single-user mode.

Tip

telinit 0 halts a system immediately.

telinit 6 reboots a system immediately.

You can also use the init command to change run level. Any instance of init that does not have a process ID of 1 invokes telinit to change the run level.

For more information, see the telinit(8) manual page.

4.3.3 Shutting down the System

Although you can use telinit to halt and reboot a system, the shutdown command is preferred as it notifies all logged-in users of the impending action.

For example, to shutdown a system in 5 minutes time and then reboot it:

# shutdown –r 5
The system is going down for reboot in 5 minutes!

To shutdown a system immediately:

# shutdown –h now

For more information, see the shutdown(8) manual page.

4.3.4 About Service Scripts

The /etc/init.d directory contains the scripts that are used to start, stop, or otherwise control the operation of system services. When the system changes run level, init, under the control of the /etc/init/rc.conf file, calls the /etc/rc script to start the services that are required for the new run level and to stop any currently running services that are not required.

For each run level N, there is an /etc/rcN.d directory that contains the scripts that init uses to start and stop services. For example, /etc/rc3.d is the directory for run level 3. Each script in an /etc/rcN.d directory is actually a symbolic link to a script in /etc/init.d. Using symbolic links allows you to reconfigure run levels without affecting the scripts that the symbolic links reference.

The name of each symbolic link begins with either K (for kill) or S (for start), followed by a sequence number that indicates the order in which init should kill or start the services. init first stops each of the K* services in the order of their sequence numbers by executing each K script with the argument stop. init then starts each of the S* services in the order of their sequence numbers by executing each S script with the argument start. If symbolic links have the same sequence number, init stops or starts them in alphabetic order.

Note

The entries for init.d, rc, rc.local, rc.sysinit, and rcN.d in /etc are actually symbolic links to entries in the /etc/rc.d directory. These links ensure compatibility with UNIX System V (SysV).

To customize service initialization, you can add scripts to /etc/init.d and create symbolic links to these scripts in the /etc/rcN.d directories, naming the links with an initial K or S according to whether init should stop or start the service in each run level N.

Alternatively, you can add commands to the /etc/rc.local script to start services or initialize devices.

For more information on writing SysV init scripts, see /usr/share/doc/initscripts*/sysvinitfiles.

4.3.5 About the Service Configuration GUI

You can use the Service Configuration GUI (system-config-services) to start, stop, and restart a service, to enable or disable the system from running the service, and to customize the run levels at which the system runs the service. Alternatively, you can use commands such as service and chkconfig.

Figure 4.1 shows the Service Configuration GUI with the NetworkManager service selected.

Figure 4.1 Service Configuration

The figure shows the Service Configuration GUI with the NetworkManager service selected.


4.3.6 Starting and Stopping Services

To start a service, either run its /etc/init.d script directly with the start argument, for example:

# /etc/init.d/sshd start
Starting sshd:                                             [  OK  ]

or use the service command with the start argument:

# service sshd start
Starting sshd:                                             [  OK  ]

Similarly, to stop a service, use the stop argument:

# service sshd stop
Stopping sshd:                                             [  OK  ]
Note

Changing the state of a service only lasts as long as the system remains at the same run level. If you stop a service and then change the system's run state to one in which the service if configured to run (for example, by rebooting the system), the service restarts.

Some scripts take other arguments, such as restart, reload, and status. Omitting an argument displays a usage message:

# service sshd
Usage: /etc/init.d/sshd {start|stop|restart|reload|force-reload|condrestart|try-restart|status}

If supported, you can use the status argument to view the status of a service:

# service sshd status
openssh-daemon (pid  12101) is running...         

For more information, see the service(8) manual page.

4.3.7 Configuring Services to Start at Different Run Levels

You can use the chkconfig command to define in which run levels a service should run, to list startup information for services, and to check the state of services. chkconfig changes the configuration of the symbolic links in the /etc/rcN.d directories.

Note

You cannot use chkconfig to change the current state of a service. To stop or start a service, use the service command.

To list the run-level configuration of all services that run their own daemon:

# chkconfig --list
NetworkManager 	0:off	1:off	2:on	 3:on	4:on	 5:on	6:off
abrt-ccpp      	0:off	1:off	2:off	3:on	4:off	5:on	6:off
abrt-oops      	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
...

The command lists each service, followed by its configured state for each run level. For example, the abrtd service is configured to run at run levels 3 and 5.

To view the configuration of a particular service, such as ntpd, specify its name as an argument:

# chkconfig --list ntpd
ntpd           	0:off	1:off	2:on	3:on	4:on	5:on	6:off

To enable a service to run at run levels 2,3, 4, or 5, use the on argument:

# chkconfig httpd on
# chkconfig --list httpd
httpd          	0:off	1:off	2:on	3:on	4:on	5:on	6:off

To enable the service for certain run levels only, specify the --level option followed by a concatenated list of the run levels, for example:

# chkconfig --level 35 httpd on
# chkconfig --list httpd
httpd          	0:off	1:off	2:off	3:on	4:off	5:on	6:off

Similarly, the off argument disables a service for run levels 2, 3, 4, and 5, or for the run levels specified by --level, for example:

# chkconfig sshd off
# chkconfig --list sshd
sshd           	0:off	1:off	2:off	3:off	4:off	5:off	6:off
# chkconfig sshd on
# chkconfig --list sshd
sshd           	0:off	1:off	2:on	3:on	4:on	5:on	6:off
# chkconfig --level 34 sshd off
# chkconfig --list sshd
sshd           	0:off	1:off	2:on	3:off	4:off	5:on	6:off

The chkconfig command changes the configuration of a service to run at a particular run level. It does not change the current state of a service as this example shows:

# runlevel
N 5
# chkconfig sshd off
# chkconfig --list sshd
sshd           	0:off	1:off	2:off	3:off	4:off	5:off	6:off
# service sshd status
openssh-daemon (pid  12101) is running...

The changed configuration of the sshd service takes effect only when you next change the system's run state, for example, by rebooting the system. You could, of course, use the service sshd stop command to stop the service.

For more information, see the chkconfig(8) manual page.