how to reboot, logout or shutdown linux machines from command line

All desktop environments has menu options to shut down and reboot machines. Most times there are also keyboard shortcuts that the X or the desktop support that allow you to shut down machines. The most commonly used keyboard shortcut is Ctrl/+/Alt/+/Del.

Although these menu options and keyboard shortcuts makes it easier, it is beneficial to know the command line options that are available that allows you to reboot and shutdown Linux systems. It is extremely useful when you work remotely on a system or when you do not direct access to the X session.

It goes without saying that you will need to be superuser to execute these commands.

Shutdown

There are three commonly used commands that shuts down the system in a secure and graceful way: halt, poweroff and shutdown. Usually, there are one of the two things you might want to do: halt the system, ie. shut down all process without powering down the system or shut down all processes and power down the system.

All commands allows you to do either of this in some fashion usually with the command line options. In fact in most modern Linux systems, the halt and poweroff commands actually call the shutdown command internally. In some older systems and BSD like operating systems, the halt and poweroff  would immediately bring the system to a halt without gracefully shutting down or killing processes. You can override this behavior with the -f option.

halt command

The halt command allows you to halt the system, without actually powering down. The system will need to go through a power cycle, such as powering off and on manually to get the system to boot again.

You can use the -p option with the halt command to actually power off the system.

bash$ halt -p

poweroff command

poweroff is another command which allows you to power off the system after shutting down. This is equivalent to the halt command above with the -p option.

bash$ poweroff

shutdown command

If there is a single command that you need to know, then it is the shutdown command. It is basically the most versatile with plenty of options to perform all tasks. Executing the command with out any options will shutdown Linux and Unix system immediately.

You can specify the time when you want to shut down as a command line argument in two different formats: An absolute time in the format HH:MM or a relative time in the format +M. (where H is hours and M is minutes)

bash$ shutdown 16:45

The above command will perform a shutdown at 16:45 hrs. To shutdown 15 minutes from now, use the command below. You can use the argument now to shut down immediately.

bash$ shutdown +15

or

bash$ shutdown now

The -h option can be used to halt and poweroff the system after shutdown. You can also provide a message for the shutdown, which is beneficial if it is a multi-user system.

bash$ shutdown -h +15
"System Going Down in 15 minutes...Please Save your work ASAP."

Reboot

While shutdown is a useful operation, the reboot is probably much more popular option. You can reboot the system gracefully with either the reboot or shutdown command.

reboot command

This will reboot the system immediately. It does not provide much "useful" options and is usually used without any arguments.

bash$ reboot

shutdown command

You can use the shutdown command, with the -r option in initiate a reboot of the system.

bash$ shutdown -r +15
"System will be rebooting in 15 mins."

Logout from Desktop Environment or X11

Logging out of your desktop environment (DE) is usually done with the options provided by the desktop itself. There are usually a graphical interface or menu options provided for this. Usually pressing Ctrl/+/Alt/+/Del will prompt you for logout. X11 itself does not have the concept of user logins, so the best bet is to kill the X and start it again.

If you want to logout of the DE from command line, then it will vary depending on the DE. For example in KDE, you could use the following command to logout.

bash$ qdbus org.kde.ksmserver /KSMServer logout 0 0 0

If you use Gnome, then your command to do it is, something like:

bash$ DISPLAY=:0 gnome-session-quit --force

Restart X11

You might just want to restart the X11 or Xorg server instead of rebooting the whole system. You can do this by restarting the X display manager, which is usually xdm.

bash$ /etc/init.d/xdm restart

If you use kdm or gdm, then you might want to restart that.

bash$ /etc/init.d/kdm restart
bash$ /etc/init.d/gdm restart

For all practical purposes, if you can remember the shutdown command and its options then that should take of almost all your needs to reboot, restart and shutdown Linux and most Unix systems.