Restart Ubuntu Server

One of the more common tasks you are going to need to know about when running any server, including an Ubuntu server, is how to reboot that server. There are different reasons you might need to reboot a server. It could be to solve a problem or just for a system update or patch. It is generally needed for intrusive hardware changes. This is usually going to be an actual shutdown though.

There are multiple options available to either reboot or shutdown an Ubuntu server. These generally work the same as for other distros. This information is, for the most part not distro specific. It should work as well on Red Hat as it does on Ubuntu. There are a few exceptions to this which we will note. These options all generally use built-in commands and don’t require extra software to be installed. We will focus on command line methods. If you are using a GUI and if you want to restart using the GUI, it is usually very easy. Most Ubuntu Linux desktop systems will have a button or menu option to reboot or shutdown a system. These are really easy to find and don’t need special instructions. The advantage to using the command line is that you will be able to reboot/shutdown in any situation whether using a desktop or a remote ssh connection.

Ubuntu Server Reboot Frequency

How often do you reboot? That depends on you and how you and your specific use case. Some people keep their servers up and running for years. This is dangerous because the longer a server has been up, the greater the chance that something will go wrong when you do reboot it. If an update was made to the system that breaks it, there is a good chance it won’t be discovered until you do reboot. If you wait a really long time and make multiple changes, there is a good chance you won’t know what broke or why. It also indicates that you probably haven’t been doing a lot of patching and updating. It is a good idea to reboot on a somewhat regular basis. Personally, I would say that monthly or weekly reboots are a good idea. This will also depend on how redundant your system is. How much impact does a reboot cause? While some people are proud of a high uptime, a lot of people do reboot regularly. The one good argument for actually keeping things up a long time would be to prove that it can stay up and that regular reboots aren’t just masking a problem like a memory leak.

Note on SystemD

Starting with Ubuntu 15.04 ( released 23 April 2015 ) system is now used by default. This means that reboot, halt, and poweroff aren’t all separate commands that call each other. They now just parse input and pass it off to a single program. They exist for "for compatibility only" and are basically just front ends that call systemd. This is going to be the same for other distros that use system as well.

Reboot Command

reboot -p     # shutdown
reboot           # reboot
reboot -f      # forcibly reboot, like pressing power button

As you would expect, this command reboots the system. With no args it just reboots ( actually calling shutdown ). With the -f flag, it reboots by just calling the reboot system call instead of calling shutdown. This is not as clean of a way to reboot a system and is not preferred. Using the -p flag it will actually power off the system instead of rebooting.

Shutdown Command

This is the preferred, normal way to either reboot or shutdown an Ubuntu Linux system. It allows you to specify a delay and it brings things down gracefully. It is what man of the other commands call anyway. Unless you want to use the systemd commands, use this.

Shutdown:
shutdown -h now
shutdown -h +5 "Server is shutting down in 5 minutes. Please save your work and close any files."
Restart:
shutdown -r +5 " Server is restarting down in 5 minutes. Please save your work and close any files.”
Cancel:
shutdown -c

Options to choose from:
-r     reboot after shutdown
-h     halt or power off after shutting down ( up to the system )
-H     halt after shutdown
-P     Power off after shutdown
-c     cancels a shutdown
-k     sends out a warning and disables logins, no real shutdown

Halt Command

The halt command instructs the system to stop all CPU functions. This doesn’t necessarily mean that the system will be powered off but in most cases it is the same thing. On most modern Linux systems, especially desktops, this will power off by default without the -p option.

halt
halt -p
halt -f

Halt can be called with no arguments to halt the system (usually powers off too). It can also be called with the -p option to ensure that the system powers off after shutting down. I usually just use the -p option out of habit. When you use -f or --force it will directly invoke the reboot system call instead of passing off to the shutdown command.

Poweroff Command

poweroff

This command basically works the same way as the halt command.

Init 6

You can use init or telinit to change reboot or shutdown a system. These commands just change the run level directly. Note that in systemd based systems, run levels don’t actually exist but are replaced with corresponding targets that serve the same purpose. These should work on systemd or non-systemd based hosts.

Either of these commands will shut down a system:
init 0
telinit 0

Either of these commands will reboot a system:
init 6
telinit 6

Also note that while these will unmounts file systems and stop processes gracefully, they won’t necessarily notify users or do other things you might want.

Using Upstart

We’re not really covering Upstart here. If you are using a system with Upstart (ex: Ubuntu 14 or RHEL 6) all of these commands should work the same anyway (except the systemd commands, you probably won’t have those).

Using SystemD - Systemctl

There are two new systemd commands that can be used to reboot or power off a system. They are named appropriately. If you like doing things this way, give it a shot.

systemctl poweroff
systemctl reboot

REISUB

There is a magic key combination, the magic sysRQ keys, that can be used when a system is hanging and you don’t otherwise have control of the system. This feature may or may not actually be enabled on a given Linux system. If you want to check, view the contents of this file /proc/sys/kernel/sysrq. If it is non-zero, it should be enabled. These key combinations are understood and interpreted by the Linux kernel. If there is a serious hardware failure or the kernel panic, it is unlikely to work. It requires the kernel to be working. Using this technique will also require you to be at an actual keyboard or serial console instead of just an SSH connection.

If you just want to reboot the system instantly (like reboot -f) you could press the following key combination:
[ALT] + [PrintScreen] + [B]

If you want to perform a graceful shutdown of a hung system you can use the following key combo. This will give processes a chance to shutdown gracefully and avoid having to run an fsck on reboot. It will help you to avoid corruption of the file system. First, hold down [ALT] - [PrintScreen]. While these are held down, type the following keys in order: R E I S U B. Make sure you pause for a few seconds between each key press.

Note – the SysRQ and is usually the same key as the Print Screen key.

These keystrokes do the following:
unRaw      This will take back keyboard control from X Windows, assuming you are running X.
tErminate      This will send SIGTERM to all processes, which allows them to terminate gracefully.
kIll      This will send SIGKILL to all processes, which forces them to stop immediately.
Sync      This runs sync, flushing cached data and writing it to disk.
Unmount      This doesn’t just unmounts but remounts all file systems as RO.
reBoot      This is the actual reboot step.

You can remember the key sequence as "Raising Elephants Is So Utterly Boring".

Get more info here:

Another article on the Linux reboot command.
Wikipedia Magic sysRQ Keys