LFCS - Shutdown and Runlevels

Jarret B

Well-Known Member
Staff member
Joined
May 22, 2017
Messages
372
Reaction score
432
Credits
13,798
Linux systems can be shutdown in many ways. Shutting down a server can be a daunting task if there are many users connecting to the server. Starting the server in various modes to allow for maintenance while preventing users from connecting to it can also be an important task. For an Administrator these abilities can be extremely important.

In this article I will cover the ways to shut down a system, warn users about the shutdown and how to change the way Linux boots up.

Shutdown Linux

There are many ways to shutdown the Linux Operating System (OS) and we will look into these.

From the Graphical User Interface (GUI) on Fedora you can go to the top right corner and select the zero with the partial vertical line and then select the same icon from the drop down list.

The better way is to perform the shutdown from a command-line in the Terminal. There are three basic commands which can be used (there are more but these are the main three to use):

  1. poweroff
  2. reboot
  3. halt
Each of these commands can perform any of the different types of shut down. We should cover what each method does to the server.

A ‘poweroff’ is a physical shutdown where the instruction is given to the Advanced Configuration and Power Interface (ACPI) to actually power off the machine. Before ACPI a user had to cause the OS to go into a powered off state where it was ready to be powered down. The user had to physically push the power button to power off the PC. With ACPI the OS performs its shutdown procedures and then issues the ACPI call to power off the system. ACPI sees this procedure as a ‘Mechanical Off’.

A ‘reboot’ causes the OS to perform its shutdown procedures to unload itself and the ACPI call is made to reset the system to boot again. ACPI sees this as a ‘Soft Off’.

The final shut down type is ‘halt’. To ‘halt’ a system is to prepare it for the actual ‘Mechanical Off’. Everything is done as it would be in a ‘poweroff’, but the ACPI call for a power off is never made. The system is ready to be physically powered off by the user. No applications will run after the halt issue is completed. Any programs set on a schedule will not start.

NOTE: Keep in mind that the ‘halt’ command on some systems will perform a full ‘poweroff’ procedure. To these systems a ‘poweroff’ and ‘halt’ command are the same thing. Make sure you know how your system behaves using the ‘halt’ command. The system will not allow any new processes to start until it has been rebooted.

The ‘shutdown’ command can be used to perform a ‘poweroff’ by default. There are four options used with the command:
  1. -P - ‘poweroff’ which is default if no option is specified
  2. -H - ‘halt’
  3. -r - ‘reboot’
  4. -c - cancel the command
Parameters may also be used to give a time for the shutdown to occur. Numbers can be used to specify the number of minutes to wait such as ‘5’ or ‘+5’. A specific time may also be set such as ‘21:00’ to specify 9:00 P.M. to perform the command. If no time is specified then the default is 1 minute. The word ‘now’ may be used to designate ‘+0’ minutes.

The last allowable parameter is a specific message to send to all users. The message should be placed in double quotes (“).

An example could be to shut down the server at 6:00 PM with a message to all users:

shutdown 18:00 “The server will shut down for maintenance at 6. Please save all work, close all programs and log out of your workstation.”

Once the command is entered you will have until 6 PM to cancel the command by issuing the following:

shutdown -c

The ‘-c’ parameter cancels the previous command issued that hasn’t occurred yet. A message is also sent out that the impending server shut down has been canceled.

NOTE: Root privileges are not needed for the shutdown command.

Once the timer for the shut down has 5 minutes remaining a file is created at /run/nologin. The contents of the file are simply ‘System is going down.’. Once this file is created then only the Root user can login to the server. The Root is allowed to login to issue a cancel command if needed.

Reboot

The reboot command will by default reboot the system but you can issue parameters to make it perform a shutdown or a system halt.

With no parameters specified the command will cause a reboot. If you wish to perform a shutdown you can use either the ‘-p’ or ‘--poweroff’ parameter. For a ‘halt’ you use the ‘--halt’ option.

A default message will be sent to all users connected to the server and any commands will occur immediately.

NOTE: The command requires Root privileges to be performed.

Halt

As with the other commands you can use the ‘halt’ command to perform any of the commands to shut down the server.

With no parameters the default method of shutting down the system will be a ‘halt’. To ‘poweroff’ the machine use either ‘-p’ or ‘--poweroff’ parameters. For a reboot use the ‘--reboot’ option.

NOTE: The ‘halt’ command requires Root privileges. If you are not issuing the command as Root then you will be prompted to authenticate to perform the command with Root privileges.

Keep in mind that the ‘shutdown’ command allows for more versatility in scheduling a time as well as sending a message to users.

Run-levels

Now that you can control the shutting down of a server you may want to be able to control its boot up as well. The boot up is controlled by the run-level.

To see the current run-level you can issue one of the following commands:

who -r
runlevel
systemctl get-default


For any system running a GUI desktop the run-level should be ‘5’. The run-levels are as follows:
  • 0 – System is halted.
  • 1 – Single-user mode. This is considered an Administrative state and non-essential services are not started.
  • 2 – Multi-user mode which may have some non-essential services started.
  • 3 – Multi-user mode that is fully functional with no GUI (headless).
  • 4 – Not a normal run-level mode to be used. This is a user-defined run-level depending on the Linux OS.
  • 5 – Multi-user mode with a GUI desktop (normal mode).
  • 6 – System is to be rebooted.

To change the default run-level you need to know the target name. The names are used with the command ‘systemctl get-default’ and are as follows:
  • 0 – poweroff.target
  • 1 – rescue.target
  • 2, 3, 4 – multi-user.target
  • 5 – graphical.target
  • 6 – reboot.target
If you wanted to shut down the server you could place the server at run-level 1 or the ‘poweroff.target’. To reboot the server you could use run-level 6 or the ‘reboot.target’. The default on a server with a GUI is the ‘graphical.target’.

To change the run-level use the following command:

systemctl set-default <target>

The command will set the new default for when the system is rebooted. If you want the change to take effect immediately you need to add the parameter ‘isolate’. If the server is in the GUI mode (6) and you want to switch to multi-user mode immediately you can issue:

systemctl isolate multi-user.target

The GUI should immediately stop and the server go to a command-line interface (CLI).

NOTE: If you are not the Root user then you will be prompted for the authentication, maybe twice.

After you switch from one mode to another you will be required to log back into the system. Your User session will be terminated so make sure any data is saved and applications are closed.

The run-levels are not specific to servers or even Red Hat systems. These commands can work on other Linux systems as well. Give the commands a careful try and have fun.
 
Last edited by a moderator:

Members online

No members online now.

Top