5 Ways to Shut Down Your Linux Computer From the Command Line
No operating system is perfect. Even if it was, there might be problems with drivers and apps. Linux is no exception. Despite being more stable than Windows (in many cases, not all!), there will probably come a time when you need to restart your Linux computer. This might be because something isn’t working. Alternatively, you might be connected via SSH to a remote computer or server and want it to restart or shut-down completely.
But how might you do this? Several commands are available for you to power off or restart (or reboot) a Linux computer via the command line.
Let’s look at them in turn, and examine how and when these commands should be used.
If you’re done with your Linux box, the solution to simply shut it down is to use the shutdown instruction, sent via SSH (if you’re using a Windows PC, you have several SSH tools to choose from). This can be used to either shut the computer off permanently (until such a time as you decide to reboot it) or to restart. The syntax for the command is:
shutdown [option] [time] [message]
For instance, to shut the computer down immediately, you would use:
shutdown -h now
Here, the -h means halt while now clearly means that the instruction should be carried out immediately. Different delays can be used. For instance, you might use +5 instead, which will tell the computer to run the shutdown procedure in five minutes.
If you want to include a message, this will be flashed up to all logged in users:
shutdown -h +5 "The server is shutting down, please save your work and log off."
Remember that you can find the full list of switches for these commands by inputting:
Restart With -r
An alternative option is to use the -r command to restart the computer. This is used in place of the -h, so to restart a computer or server, you might use:
shutdown -r +5 "The server is restarting in five minutes, please save your work and log off."
Any scheduled shutdown or restart can be cancelled by inputting the -c cancel command:
As the shutdown command features a restart option, it should come as no surprise to learn that the reboot command has a shutdown option.
The standard reboot command is:
This will prompt your computer to turn itself off, and back on again. However, should you wish to power off the device, then the -p switch will work:
Another option is to force a reboot. This can be useful if an app or service is hanging and you need to restart quickly:
This forcefully reboots your Linux box.
We’ve already seen the -h switch above, but halt can be used as a command all on its own. This will lead to the computer shutting down immediately, with a simple four letter word:
The -f switch can also be used with halt, but the results are inconsistent, and can lead to system stability issues.
You might prefer the terminology of the poweroff command. This does exactly the same as halt, except it takes over twice as long to type.
However, as well as using -f to force poweroff, you can also employ the -w switch to log the system reboot call to /var/log/wtmp. This is a potentially useful debugging tool, as is –verbose, which can help with issues with shutdown.
5. The Emergency Option: REISUB
All of the above commands can be used in situations when the system is running without any problems. But what if it crashes? What if the PC or server is hanging, and cannot be rebooted in the agreeable manner?
The answer then is a keyboard combination. If you’ve switched from Windows, you probably know that Ctrl + Alt + Del displays a menu with Shutdown as an option. If held for longer, the machine will shut down automatically. On a Mac, meanwhile, you would simply hold down the power button (an option that also works on Windows hardware).
In Linux, the keyboard combination is Alt + Print Screen + B to reboot. However, if this doesn’t work, or there is a more complex issue, you can alter the combination, using up to six keys.
This is known as REISUB, due to the following forced acronym:
- unRaw — Takes control of the keyboard back from the X display server.
- tErminate — Sends the termination signal SIGTERM to all processes, to terminate gracefully.
- kIll — As above, but the SIGKILL signal, which forces immediate termination to processes.
- Sync — Flushes data to disk.
- Unmount — This remounts all filesystems into a read-only state.
- reBoot — As you’d expect.
To make this work, you should hold down Alt + Print Screen, then the R E I S U B keys in that order. Leave a second or two between each keypress. Note that this method doesn’t typically work on machines with ARM architecture (such as the Raspberry Pi ).
Help, I Accidentally Shut Down My Linux PC or Server!
We’ve seen how to cancel a shutdown or restart command. However, it’s easy to find yourself initiating a shutdown command when a vital process is running — especially on a remote server. A way around this is to install molly-guard, which can override a shutdown by checking for certain parameters.
For instance, there is a script that checks for SSH sessions (which are different from FTP ). If you send a reboot, halt, poweroff, or shutdown command, molly-guard will demand the name of the host you intend to close.
To use this, install molly-guard in the terminal:
sudo apt-get install molly-guard
As molly-guard is running in the background, it will detect a command such as poweroff and report back that an SSH session has been detected. You then have the choice of entering the server’s hostname to confirm shutdown, or to press Ctrl + C to cancel. Useful!
Those five ways of shutting down a Linux computer from the command line are particularly useful as they can be used on the computer itself, or via a remote SSH. As these commands are so concise, they lend themselves to quick use — which might result in an accidental reboot from time to time! Fortunately, the molly-guard utility is enough to avoid this.