How to Force Reboot Linux from the Command Line
When a computer or server needs to be turned off, the term used to describe that process is "shutdown". When a computer or server needs to be turned off and back on, the term used to describe that process is "reboot".
This blog will show how to shut down and reboot a Linux system safely. It covers how to force a shutdown or a restart, schedule reboots, and set wall messages.
Using systemctl to Reboot and Shutdown a Linux Machine
To reboot or shut down a Linux machine, we’ll often use the systemctl
(system control) command.
Some commands require system administrator privileges. The root user has such privileges. So we can reboot a machine by simply typing systemctl reboot
. Regular users cannot use commands that change the system’s state. But they can temporarily get root privileges if they add sudo in front of their commands. So a regular user needs to type sudo systemctl reboot
instead.
All you need to remember is this: “If I am logged in as root, I don’t need sudo”. So you can skip writing the “sudo” word if you’re already logged in as root. This applies to all the examples in this blog.
How to Reboot a Linux System
Below is a command for initiating an immediate Linux system reboot:
sudo systemctl reboot
How to Shutdown a Linux System
Below is a command for initiating an immediate Linux system shutdown:
sudo systemctl poweroff
Commands for force shutting and force restarting Linux
Sometimes, you might find yourself in situations where the system refuses to reboot or shut down normally. That might be because some program is misbehaving, stuck in some way, and does not want to close properly. In such situations, you can force close all such programs and reboot in a more abrupt way (not recommended to do unless absolutely necessary).
sudo systemctl reboot --force
sudo systemctl poweroff --force
If not even this works, you can specify –force twice (only use as a last resort):
sudo systemctl reboot --force --force
This is exactly like pressing the reset button. The system reboots instantly and programs have no chance to close properly or save their data.
systemctl poweroff --force --force
This is exactly like unplugging a computer from its power source.
How to schedule a Reboot or Shutdown of a Linux System
You’ll often find that you need to reboot some servers in the middle of the night, say 2 or 3 AM. It’s inconvenient to have to wake up just to reboot some device, so you can instead instruct Linux to do this on its own.
The shutdown
command is better suited for scheduled reboots or shutdowns.
The command for shutting down Linux at a specific time
To shutdown at 02:00 AM:
sudo shutdown 02:00
The time is in a 24-hour format, so you can use anything between 00:00 and 23:59.
The command for shutting down Linux after specified minutes
If you want to shut down Linux after X minutes, use +X instead. For instance, to shutdown after 15 minutes, use this command:
sudo shutdown +15
The command for restarting Linux at a specified time
To schedule a reboot, add the -r reboot option. For instance, to reboot Linux at 2:00 AM, use this command:
sudo shutdown -r 02:00
The command for restarting Linux after specified minutes
To schedule a reboot, add the -r option followed by the minutes to wait before the reboot. For instance, to reboot after 15 minutes, use the command below:
sudo shutdown -r +15
The command for setting a wall message
You can also set what is called a wall message. If a few users are logged in to this Linux machine, the wall message will be shown to them before the server reboots or shuts down. This allows them to know in advance why and when the machine will become unavailable. It also gives them a chance to finish their work before this happens instead of abruptly being disconnected without knowing what happened.
You can write your wall message like this (between ‘ ‘ quotes):
sudo shutdown -r +1 'Scheduled restart to do an offline-backup of our database'
Check out our Linux Basics Course & Labs
Conclusion
If you are not logged in as root, use the sudo
before the shutdown
or reboot
command to ensure you have the necessary permissions to shut down the system. Also, save any unsaved work before shutting down to avoid data loss.
More on Linux:
- How to List All Groups in Linux
- How to Count the Number of Files in a Directory in Linux
- How to Find Out When a File Was Created in Linux
- How to Create a Soft (Symbolic) Link in Linux
- How to Unzip Files to a Specific Directory in Linux
- How to Make a Bash Script File Executable in Linux
- How to Search Packages With Apt Search Command