1. Overview
Linux is quite a robust operating system and, as such, Linux servers rarely need to be rebooted. However, sometimes there are reasons we do need to reboot our system.
For example, if we’re running Linux on our personal computer, rebooting and shutting down the system could be everyday operations.
In this tutorial, we’ll learn some useful commands to reboot and shut down a Linux system from the command line.
2. Rebooting or Shutting Down a System
Several commands can reboot or shut down a system, such as shutdown, reboot, halt, and poweroff.
Since rebooting or shutting down a system will stop all running processes, we need to have root permission to execute those commands.
Most modern Linux distros have adopted systemd as their system and service manager. *In systemd-managed systems, r**ebooting and shutting down the system are controlled centrally by systemctl.*
For example, let’s check those commands on a Ubuntu system:
$ file /sbin/{halt,poweroff,reboot,shutdown}
/sbin/halt: symbolic link to /bin/systemctl
/sbin/poweroff: symbolic link to /bin/systemctl
/sbin/reboot: symbolic link to /bin/systemctl
/sbin/shutdown: symbolic link to /bin/systemctl
It might be a little surprising to learn that all those commands are linking to the same command: systemctl. This is because the systemctl program uses a technique to change its behavior based on how it was executed.
In the next sections, we’ll take a closer look at these commands.
3. The shutdown Command
We can use the shutdown command to reboot or shut down a Linux system safely.
The syntax of using the shutdown command is straightforward:
shutdown [OPTIONS...] [TIME] [MESSAGE]
When we execute the shutdown without any options, it will power-off the machine in one minute:
root# date +'%F %T'
2020-04-04 20:55:43
root# shutdown
Shutdown scheduled for Sat 2020-04-04 20:56:50 CEST, use 'shutdown -c' to cancel.
If a regular user has been configured to gain root permission by using sudo, we can also power-off the machine without switching to the root user:
$ sudo shutdown
3.1. Common Options
Now, let’s take a look at the common options supported by the shutdown command.
We can pass the -r option to it to reboot our system instead of powering off the machine:
root# shutdown -r
Shutdown scheduled for Sat 2020-04-04 21:56:50 CEST, use 'shutdown -c' to cancel.
The -H option tells the shutdown command to halt the system:
root# shutdown -H
Shutdown scheduled for Sat 2020-04-04 21:56:50 CEST, use 'shutdown -c' to cancel.
Halting a system and powering-off a machine are slightly different:
- halt: terminate all running processes and shut down all CPUs
- power off: the same as halt, and then attempts to cut power from the system
3.2. Schedule a Shutdown Operation
Sometimes we don’t want to shut down or reboot the system in one minute. Instead, we can schedule the shutdown operation at a specified time.
The shutdown command supports two different time formats for scheduling:
- absolute time: HH:mm
- relative time: +NumberOfMinutes
For example, we can schedule a reboot of the system for 8:08 AM:
root# shutdown -r 08:08
Shutdown scheduled for Sun 2020-04-05 08:08:00 CEST, use 'shutdown -c' to cancel.
Let’s see another example where we schedule a system power-off 30 minutes from now:
root# shutdown +30
Shutdown scheduled for Sun 2020-04-04 22:28:00 CEST, use 'shutdown -c' to cancel.
If we want to shut down the system immediately, we can use +0 or a convenient alias, now:
root# shutdown now
Since Linux is a multi-user operating system, we may want to broadcast a message to all currently logged in users about the planned shutdown or reboot:
root# shutdown -r +45 "Attention: This system will restart in 45 minutes!"
Shutdown scheduled for Sun 2020-04-04 22:48:00 CEST, use 'shutdown -c' to cancel.
We can cancel a scheduled shutdown process using the -c option:
root# shutdown -c "Scheduled reboot is cancelled."
4. The reboot, halt, and poweroff Commands
The reboot, halt, and poweroff commands can also reboot or shut down our system.
Since they’re similar and support the same options, we’re going to talk about these commands from the perspective of functionality.
4.1. To Reboot a System
We can reboot a system using the reboot command:
root# reboot
We can also pass the –reboot option to the halt and poweroff commands and let them reboot the machine:
root# halt --reboot
root# poweroff --reboot
4.2. To Halt a Machine
As the name says, the halt command is for halting the machine. We can halt a machine simply by executing the command:
root# halt
We can also pass the –halt option to the reboot and poweroff commands and let them halt the machine:
root# reboot --halt
root# poweroff --halt
4.3. To Power Off a Machine
When we want to power off a machine safely, we can use the poweroff command. Without any arguments, executing this command will power off a machine:
root# poweroff
We can also pass the -p option to the reboot and halt commands and let them stop the system and cut power:
root# reboot -p
root# halt -p
4.4. The -f Option
All three commands support a -f option to reboot, halt, or power off the machine forcibly.
To understand what the force option means, let’s take a closer look at the background of these three commands.
When we execute these commands without the -f option, they will invoke the shutdown tool with appropriate options.
For example, in a systemd system:
reboot
--(invokes)--> shutdown -r now
--(invokes)--> systemctl isolate reboot.target
The reboot.target in the command above is a system target for systemctl. Some predefined tasks in the system targets will be executed by the systemctl command — for example, stopping all running processes, unmounting mounted devices safely, and so on.
However, if we pass the -f option to the three commands, they won’t invoke the shutdown command. Instead, they make a reboot() system call to reboot the system directly.
In a systemd system, the command reboot -f will call systemctl –force reboot.target. The –force option will let systemctl skip the system target and make the reboot() system call directly.
We should use the -f option with caution because this may result in data loss.
5. Conclusion
In this article, we’ve learned how to reboot and shut down a Linux system from the command-line. We’ve seen that many commands can do the reboot or shut down job.
Since rebooting or shutting down a system will kill all running processes, we should always think twice before we launch the commands, particularly when we do it on a public server.