A restart is often the best solution to many computing problems. So, it pays to know how to shut down or restart Linux desktops and servers safely.
Linux systems can run for weeks and months without issues, and this is especially true for servers. But, there are times when a shutdown or reboot becomes inevitable, especially for maintenance.
Desktop users can easily use the GUI to shut down their systems. But for those times when it is impossible or you need to power down or restart a remote server, the following commands and options can be really helpful.
Linux shut down and restart commands
|sudo shutdown -h||Shutdown and power off|
|sudo shutdown -r||Reboot after shutdown|
|sudo shutdown hh:mm||Shutdown at hh:mm o’clock|
|sudo shutdown +m||Shutdown in ‘m’ minutes|
|sudo shutdown now||Shutdown now|
|sudo shutdown -c||Cancel a pending shutdown command|
|sudo halt||Shutdown the system|
|sudo reboot||Reboot a system|
|sudo poweroff||Shutdown the system|
|ssh user@host shutdown -h now||Shutdown a remote system immediately|
|sudo systemctl reboot||Uses the systemctl service|
1. sudo shutdown -h
The shutdown command is the most popular for shutting down a Linux system. The ‘sudo’ preceding it is necessary because you always need to be root or have super-user rights before you can shut down a system using the shell. This feature is for security purposes.
This command takes different options, depending on what you want to achieve. With the -h option here, you are telling it to shut down the system first, which includes stopping all running programs and background processes, before halting the OS or switching it off completely.
You should note that the -h option leaves the choice of either halting or powering off to the system. If you precisely want it powered off, however, then use the -P option. And to halt the system, use the -H option.
sudo shutdown -h #halt or power off sudo shutdown -P #power off after system shutdown sudo shutdown -H #only halt execution after shutdown
2. sudo shutdown -r
With shutdown -r, you are requesting a system reboot. As always with the shutdown program, all running processes on the system get terminated first, before a full power down and eventual system restart.
3. sudo shutdown hh:mm
You could also want to power down the system at a particular time for maintenance, in which case it is best to schedule things. Fortunately, the shutdown command accepts time options, and one of them lets you specify a time of your choice.
This format follows the 24-hour clock as hh:mm, with ‘h’ representing hours and ‘m’ representing minutes. Here is an example:
sudo shutdown 23:59 #shut down the system by 11:59 PM
4. sudo shutdown +m
There is another popular time option for the shutdown command, and this one lets you schedule a system power down using a count-down timer.
All you need is a ‘+’ and a number to begin the countdown. This number is interpreted in minutes and can get ridiculously high, reaching well over 1,000,000,000 (one billion) minutes!
sudo shutdown -r +50 #reboot the system in 50 minutes
5. sudo shutdown now
As the option’s name suggests, this takes the system down immediately, beginning with the halting of all running processes.
Please note that all the time-related options for the shutdown command alert currently logged in users that the system is going down.
This notification might be helpful when there is a count-down period or a scheduled time, but although it is also displayed for the ‘now’ option, the execution is immediate and leaves the user no time at all.
New users are also prevented from logging into the server when the remaining time to shutdown is less than 5 minutes.
6. sudo shutdown -c
The -c option is for canceling any scheduled power-downs. However, there is one small problem with this option because of the way the shutdown program works.
When you run shutdown with a time option, it broadcasts a message to all users currently logged into a shell. This is great, but unless you added an Ampersand after the command to fork the program, the displayed screen will make it impossible to enter another command.
sudo shutdown +8 #shut down in 8 minutes. You must log into a new shell to cancel it sudo shutdown +8& #shut down in 8 minutes, but run in the background to allow further input
7. sudo halt
The halt command will either halt the computer or call the shutdown command, depending on the system’s run level. You can also specify options to force certain behavior.
Linux run-levels are different stages of the system, where certain services are available. Run level 0 (zero) is ‘halt’, while run level 5 offers a graphical user interface (X server), and run level 6 is ‘reboot’.
sudo halt -f #force a system halt sudo halt -p #force a system poweroff
8. sudo reboot
The reboot command is nearly the same as the halt command. It will either reboot the system or call the shutdown command, depending on its run level. You can force its behavior using the -f and -p options.
sudo halt -f #force a system reboot sudo halt -p #force a system poweroff
9. sudo poweroff
The poweroff command is also the same as the halt command. It will either power off the system or call the shutdown command depending on the run level. You can force its behavior as well using the -f option.
sudo halt -f #force a system poweroff
10. ssh user@host shutdown -h now
All of the above commands can either be entered on a local machine that you have physical access to or over a secure shell connection after authentication.
However, you can also issue a shutdown or restart command to a remote server, without first logging into its shell. You do that with the following commands:
ssh root@host shutdown -h now #force a system shut down now as root ssh -t user@host sudo shutdown -h now #force a shut down now using sudo password ssh -t user@host shutdown -r 23:55 #Reboot the remote host by 11:55 PM
11. sudo systemctl reboot
Modern Linux distributions are shifting to systemd for managing services on the platform. Being a background process, it comes with its management utility called systemctl. This program makes it easier to start, stop, enable, disable, etc. services on any Linux server.
The benefits of using systemctl, in general, include more detailed error messages and better management of the system’s tools and services.
It also features preferential handling of important system events, such as the halt, reboot, and poweroff commands. With them, you can avoid entering preceding commands like start, stop, etc., for example:
sudo systemctl start halt #the start command is not necessary here sudo systemctl halt #works like a charm sudo systemctl poweroff #completely powers the system down sudo systemctl reboot #Restart
Coming to the end of this list of the different methods of shutting down and restarting a Linux system, I am certain you can also see the Linux philosophy of freedom here.