How to Force Reboot Linux from 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.
Reboot and Shutdown
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 other examples in these lessons where you see sudo.
Rebooting a Linux System
Below is a command for initiating an immediate Linux system reboot:
sudo systemctl reboot
Shutting Down 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 abnormal 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.
Scheduling a Reboot or Shut Down 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.
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.
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
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
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
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'
Want to gain a deeper understanding of Linux's main concepts? Watch this video.
To learn more about Linux, check out our hands-on Linux course here
To get free hands-on experience in System Administration and DevOps tasks, please check out our KodeKloud Engineer program here