Authors Top

If you have a few years of experience in the Linux ecosystem, and you’re interested in sharing that experience with the community, have a look at our Contribution Guidelines.

1. Introduction

Through the years, machines have refined to the point of performing and synchronizing billions of sensitive but complex actions per second. Because of this, how we switch off any operating system (OS) without losing or corrupting data can be a critical consideration.

In this tutorial, we learn about ways to bring a Linux system down. First, we talk about the usual Linux shutdown process. Next, we go through the standard stop and restart commands, some of their historical background, and contemporary functions. Finally, we explore how the methods we discuss got centralized into a single binary.

We tested the code in this tutorial on Debian 11 (Bullseye) with GNU Bash 5.1.4. It should work in most POSIX-compliant environments.

2. Linux Shutdown Process

Of course, what goes up, must come down. If there is a Linux boot process, we also need steps to stop.

Basically, a shutdown means that the OS gives up control of the hardware. However, before doing that, an OS usually performs maintenance, which ensures several things:

  • ties to devices such as file handles and block special files are gracefully severed
  • all software is in a proper state
  • each hardware component is in the proper state
  • next boots are stable

In fact, abrupt stops may damage the filesystem or even the circuitry and hardware, depending on its exact nature.

For this reason, Linux usually goes through at least a minimal set of operations before giving up control of the hardware:

  1. Warn users
  2. Perform scheduled tasks
  3. Sync and unmount storage filesystems
  4. Stop system services
  5. Detach and power off devices
  6. Unload kernel modules
  7. Move through and stop targets (run levels)
  8. Stop and prevent user sessions
  9. Disable terminal services
  10. Disable security mechanisms
  11. Disable logging

While the order is more or less as listed, not all distributions and installations follow the exact same process, so a step can get delayed, omitted, or intertwined.

Perhaps the most critical steps relate to the sensitive structure of storage components. Leaving at least the OS configuration in a coherent state while unmounting filesystems is paramount.

So, with our basic understanding of the shutdown process Linux follows, let’s explore some commands to initiate it.

3. halt

The halt command simply stops the OS without consideration for the hardware state after that.

To understand the idea behind halt, we turn to the Advanced Configuration and Power Interface (ACPI), which provides the OS with the means to power off attached devices:

On older or specialty machines without a convenient ACPI-controlled power button, halt usually performs the necessary stop procedures for everything the OS has control over but doesn’t power off anything. Essentially, this leaves the machine running but without third-party OS control. At that point, an operator can physically switch the power off.

Critically, there are very old versions of halt that don’t go through a graceful stop process, instead directly literally halting the OS.

Due to the advent and widespread use of ACPI, halt now usually incorporates the protocol, becoming very similar, if not equivalent to the next Linux command.

4. poweroff

Indeed, the poweroff command was initially just an ACPI call to directly power off the machine without any shutdown procedure. In other words, it was the equivalent of holding down the power button or pulling the plug.

However, due to the aforementioned repercussions of simply switching off a complex system of hardware and software, this theoretical complement to halt came to include it. Similarly, contemporary versions of halt include the poweroff ACPI call, if supported.

Consequently, the two commands are usually pretty much equivalent.

5. reboot

The function of reboot is to bring the system down and then bring it back up.

How it does that usually depends on the options. Before ACPI, reboot just did a halt and restart without power operations. Contemporary versions usually go through the whole process:

  • graceful stop
  • power off
  • power on
  • graceful start

Because of the merge between halt and poweroff, as well as the fact that reboot combines them, all three commands are commonly symbolic links to the same binary.

6. shutdown

Since, in the past, halt, poweroff, and reboot were distinct options for stopping, shutdown was meant to further combine maintenance, termination, and restarts into one:

  • perform the proper tasks to gracefully stop the OS
  • power off the system
  • optionally power the system back on

Whether this is done by calling them in order or via a custom binary depends on the version and implementation.

Since shutdown includes all previous commands, we can often use it to call them via flags:

  • -H or –halt for halt
  • -P or –poweroff for poweroff
  • -R or –reboot for reboot

After exploring the historic commands for stopping and restarting a system, let’s bring them all together.

7. OS Stop Control

At this point, the names of stop commands can just be semantically different, while the underlying code paths remain basically the same. Consequently, we can have unexpected combinations:

$ halt --poweroff
$ poweroff --reboot
$ reboot --halt

Critically, the specific actions of each command depend on many factors:

  • philosophy of the developer or distribution
  • init system
  • toolset

Some environments merge halt and reboot in the same binary, while shutdown and poweroff are a different binary. Others combine everything into one.

In fact, in many recent versions of Linux, halt, poweroff, reboot, and shutdown are all the systemctl command with their own set of subcommands for each function.

In addition, there is usually a -f, -ff, or –force switch, which, like the original halt, doesn’t contact the system manager further before performing any stop operations. This means we jump over the shutdown functionality and any graceful stops, risking system damage but ensuring the action completes as quickly as possible.

Due to all of the above, referring to the documentation of a UNIX or Linux distribution can be vital to how the options we have for system state control are implemented.

8. Summary

In this article, we looked at the usual commands for stopping and restarting a Linux system, some of their history, and their current status quo.

In conclusion, while there were historical differences between halt, poweroff, and shutdown based on technicalities, their contemporary codebase is much more unified, if not equivalent, depending on the system.

Authors Bottom

If you have a few years of experience in the Linux ecosystem, and you’re interested in sharing that experience with the community, have a look at our Contribution Guidelines.

Comments are closed on this article!