101.3. Change runlevels / boot targets and shutdown or reboot system

101.3 Change runlevels / boot targets and shutdown or reboot system

Weight:3

Description: Candidates should be able to manage the SysVinit runlevel or systemd boot target of the system. This objective includes changing to single user mode, shutdown or rebooting the system. Candidates should be able to alert users before switching runlevels / boot targets and properly terminate processes. This objective also includes setting the default SysVinit runlevel or systemd boot target. It also includes awareness of Upstart as an alternative to SysVinit or systemd.

Key Knowledge Areas:

  • Set the default runlevel or boot target

  • Change between runlevels / boot targets including single user mode

  • Shutdown and reboot from the command line

  • Alert users before switching runlevels / boot targets or other major system events

  • Properly terminate processes

Terms and Utilities:

  • /etc/inittab

  • shutdown

  • init

  • /etc/init.d/

  • telinit

  • systemd

  • systemctl

  • /etc/systemd/

  • /usr/lib/systemd/

  • wall

In previous lesson we explain the sequence of system boot in liunx, then we get introduced to Sysv, Upstart and system as different service managers in linux world. In this lesson we learn how to take control over our linux system services using these service managers.

Lets start with SysV. SysV is is nothing more than many executable scripts which are run after init process. How we can define which service should be run when computer starts ? I'm glad you asked. It seems that huge text configuration files would be needed, there is where runlevels come to play.

runlevels

A runlevel is one of the modes that a Unix -based operating system will run in. Each runlevel has a certain number of services stopped or started, giving the user control over the behavior of the machine. runlevels avoid of having a few large files to edit by hand.

Conventionally, seven runlevels exist, numbered from zero to six. And there are some differences between Debian and RedHat based systems:

Runlevel

Debian

RedHat

0

Halt

Halt

1

Single User Mode

Single User Mode

2

Full,Multi-User,GUI

Multi-User, No Net

3

Nothing

Multi-User, with Net, No GUI

4

Nothing

Not used

5

Nothing

Full,Multi-User,GUI

6

Reboot

Reboot

CentsOS 5 was the last version which used SysV and from CentOS 7 Systemd is used.

runlevel

To find the current and previous runlevels , the runlevel command is used:

In the above output, the letter ‘N’ indicates that the runlevel has not been changed since the system was booted. And, 5 is the current runlevel.

Now that we now about runlevels, How we can switch between them? well just tell 'init' what runlevel you like.

telinit

telinit is used to change the SysV system runlevel.

Lets try it on a CentOS5 machine:

and system goes to runlevel 3 lets go back to runlevel 5:

init

init (as a command) is a process control initialization like telinit.

telinit vs init

telinit is a smaller tool that informs init when it needs to switch runlevels. So we can use "telinit" to "tell init" that it needs to switch runlevel. telinit is actually linked to init command and it is possible to use init command instead but it is not recommanded.

There are several ways to change runlevels. To make a permanent change, we can edit /etc/inittab and change the default level that we just saw above.

/etc/inttab

After the Linux kernel has booted, the init program reads the /etc/inittab file to determine the behavior for each runlevel. Unless the user specifies another value as a kernel boot parameter, the system will attempt to enter (start) the default runlevel.(CentOs5)

the default runlevel is determined from theid:entry in /etc/inittab. How run levels are set up by default and how they are configured depends in part on the particular distribution you are running.

The format of each line in inittab file is as follows:

id:runlevel:action:process

Here is a description of these fields:

  • id (identification code) – consists of a sequence of one to four characters that identifies its function.

  • runlevels – lists the run levels to which this entry applies.

  • action – specific codes in this field tell init how to treat the process. Possible values include: initdefault, sysinit, boot, bootwait, wait, and respawn.

  • process – defines the command or script to execute.

Now lets see how SysV implements the concept of run levels.

/etc/init.d

/etc/init.d contains scripts used by the System V init tools (SysVinit).

As we said, in SysV, init program is the first process that is run and consequently some infrastructure services are started. Files in /etc/init.d are shell scripts that respond to start, stop, restart, and (when supported) reload commands to manage a particular service. But how SysV determine which services inside /etc/init.d should be started or stopped depend on default runlevel ?Lets draw a picture:

/etc/rc.d/

SysV uses grouping. Scripts of each runlevel are grouped and placed in /etc/rc{runlevel}.d/ where runlevel is the runlevel.

As many services might be existed in different runlevels, the real script files are hold in /etc/init.d and /etc/rc{runlevel}.d/just point to required ones.

Lets take a look at rc.5 for example:

Each script in each runlevel is run with its startup or shutdown functions depending on if that runlevel is going up or going down. S means starting script and K shows that it is a killing script .The sequence of actions is defined by the numbers. try cat command to see what is inside :

And all of these places were places that SysV uses to manage scripts and runlevels. In Sysv system we can manage services with servicecommand andchkconfigcommand to define how and when services are started. We will talk about them later.

rc.local

But what about /etc/rc.local ? This file runs after all other init level scripts have run, so it's safe to put various commands that you want to have issued upon startup.This is also a good place to place "troubleshooting" scripts in. But do not forget rc.local may not work properly in Upstart and Systemd. Test it, search it and do required configuration before using in production environment.

Lets go back to our topic and get familiar with runlevels equivalent in systemd. Although /etc/inittab still exist in Systemd systems but that is not part of configuration, we have something called "Targets".

systemd targets

Like runlevels there are some modes in systemd system that our system can run in, systemd runlevels are referred to as "targets". "targets" are described as a collection of services. Look at the equivalents:

In order to switch between Boot Targets we use systemctl tool.

systemctl

Systemctl is a systemd utility which is responsible for Controlling the systemd system and service manager. It can does lots of thing but what we need here and use it for is changing boot targets.

To see the current boot target use systemctl get-default command:

To change the target use systemctl isolate xxxxx.target , for example:

To set the default target, run systemctl set-default xxxxxx.target command :

But how systemd knows what to do and how to do things ? We have something called "Unit". The concept of "Unit Files" replaces the SysV init scripts for services.

systemd unit files

There are different types of unit files and the best way to describe unit files, is 'that is a thing which should be started'.Yes that is a thing because there are different kinds of unit files. Each unit file is a simple text file describing a unit, what it does, what needs to run before or afterward, and other details

Unit files can be stored in a few different places on your system. systemd looks for system unit files in this order:

  1. /etc/systemd/system:directory stores unit files that extend a service. This directory will take precedence over unit files located anywhere else in the system.

  2. /run/systemd/system:directory is the runtime location for unit files.

  3. /usr/lib/systemd/system:directory is the default location where unit files are installed by packages. Unit files in the default directory should not be altered.

Unit files in the earlier directories override later ones. Lets take a look at them:

1./etc/systemd/system

2./run/systemd/system

3./usr/lib/systemd/system

lets follow one of target unit files.Try ls -al *.target to see all of target files. As an instance default.target:

Lets take a look at that graphical.target:

And what it requires is multi-user.target, lets see:

and it requires basic.target :

Finally sysinit.target :

And sysint.target does not require any thing. It seems that we have reached top level target in our tree and we can go back.

Using unit files beside Targets concept make Systemd more flexible in comparison to SysV.

wall

There are times when multiple users are logged in to a server computer, and we need to, say, restart the server to perform some maintenance task. Of course, the correct way is to inform all those who are logged in about the maintenance activity.

wall (an abbreviation of write to all) is a Unix command-line utility that displays the contents of a file or standard input to all logged-in users. It is typically used by root to send out shutting down message to all users just before poweroff.(Ubuntu16)

Lets try sending out "we are going down" message:

and what will user1 receive:

options might not work is some old distributions.

shutdown

shutdown - Halt, power-off or reboot the machine

shutdown does its job by signalling the init process, asking it to change the runlevel. According to previous title, runlevel 0 is used to halt the system, runlevel 6 is used to reboot the system, and runlevel 1 is used to put the system into a state where administrative tasks can be performed (single-user mode).

We may specify a time string (which is usually “now” or “hh:mm” for hour/minutes):

halt vs poweroff ! it's a bit historical

  • halt was used before ACPI (Advanced Configuration and Power Interface)which today will turn off the power for us. It would halt the system and then print a message to the effect of "it's ok to power off now". Back then there were physical on/off switches, rather than the combo ACPI controlled power button of modern computers.

  • poweroff, naturally will halt the system and then call ACPI power off.

These days halt is smart enough to automatically call poweroff if ACPI is enabled. In fact, they are functionally equivalent now.

reboot

reboot command can be used to shutdown or reboot linux.

To reboot linux just call the reboot command directly without any options.

This will perform a graceful shutdown and restart of the machine. This is what happens when we click restart from your menu.

-f option will forcefully reboot the machine. This is similar to pressing the power button of the CPU. No shutdown takes place. The system will reset instantly.

halt

The next command is the halt command. This can shutdown a system but has some other options:

the halt command also has force option but try not to use it, because it might put your system in an in consistant state.

poweroff

There is another command exactly same as the halt command. It does the same things and takes the same options.

and we are done.

.

.

.

Sources:

https://www.linuxjournal.com/article/1274

https://en.wikipedia.org/wiki/Runlevel

https://www.geeksforgeeks.org/run-levels-linux/

https://www.ostechnix.com/check-runlevel-linux/

https://geek-university.com/linux/etc-inittab/https://developer.ibm.com/tutorials/l-lpic1-101-3/

https://www.liquidweb.com/kb/linux-runlevels-explained/

https://linux.die.net/man/8/telinit

https://unix.stackexchange.com/questions/434560/what-differences-it-will-make-if-i-use-telinit-6-instead-of-reboot-command

https://askubuntu.com/questions/5039/what-is-the-difference-between-etc-init-and-etc-init-d

https://www.systutorials.com/239880/change-systemd-boot-target-linux/

https://www.tecmint.com/change-runlevels-targets-in-systemd/

https://fedoramagazine.org/systemd-getting-a-grip-on-units/

https://www.linode.com/docs/quick-answers/linux-essentials/what-is-systemd/

https://www.howtoforge.com/linux-wall-command/

https://en.wikipedia.org/wiki/Wall_(Unix)

http://man7.org/linux/man-pages/man1/wall.1.html

http://man7.org/linux/man-pages/man8/shutdown.8.html

https://www.computerhope.com/unix/ushutdow.htm

https://unix.stackexchange.com/questions/42572/is-halt-the-same-as-shutdown-h-and-poweroff-the-same-as-shutdown-p/42581

https://www.tecmint.com/shutdown-poweroff-halt-and-reboot-commands-in-linux/

https://www.binarytides.com/linux-command-shutdown-reboot-restart-system/

Last updated

Was this helpful?