What is the difference between these commands for bringing down a Linux server?
And now, the systemd answer.
You're using, per the tag on your question, Red Hat Enterprise Linux. Since version 7, that has used systemd. None of the other answers are correct for the world of systemd; nor even are some of the assumptions in your question.
- Forget about runlevels; they exist, but only as compatibility shims. The systemd documentation states that the concept is "obsolete". If you're starting to learn this stuff on a systemd operating system, don't start there.
- Forget about the manual page that marcelm quoted; it's not from the right toolset at all, and is a description of another toolset's command, incorrect for systemd's. It's the one for the
halt
command from the van Smoorenburg "System 5"init
utilities. - Ignore the statements that
/sbin/halt
is a symbolic link to/sbin/reboot
; that's not true with systemd. There is no separatereboot
program at all. - Ignore the statements that
halt
orreboot
invoke ashutdown
program with command-line arguments; they are also not true with systemd. There is no separateshutdown
program at all.
Every system management toolset has its version of these utilities. systemd, upstart, nosh, van Smoorenburg init
, and BSD init
all have their own halt
, poweroff
, and so forth. On each their mechanics are slightly different. So are their manual pages.
In the systemd toolset halt
,poweroff
,reboot
, telinit
, and shutdown
are all symbolic links to /bin/systemctl
. They are all backwards compatibility shims, that are simply shorthands for invoking systemd's primary command-line interface: systemctl
. They all map to (and in fact are) that same single program. (By convention, the shell tells it which name it has been invoked by.)
targets, not runlevels
Most of those commands are shorthands for telling systemd, using systemctl
, to isolate a particular target. Isolation is explained in the systemctl
manual page (q.v.), but can be, for the purposes of this answer, thought of as starting a target and stopping any others. The standard targets used in systemd are listed on the systemd.special
(8) manual page.
The diagrams on the bootup
(7) manual page in the systemd toolset, in particular the last one, show that there are three "final" targets that are relevant here:
halt.target
— Once the system has reached the state of fully isolating this target, it will have called thereboot(RB_HALT_SYSTEM)
system call. The kernel will have attempted to enter a ROM monitor program, or simply halted the CPU (using whatever mechanism is appropriate for doing so).reboot.target
— Once the system has reached the state of fully isolating this target, it will have called thereboot(RB_AUTOBOOT)
system call (or the equivalent with the magic command line). The kernel will have attempted to trigger a reboot.poweroff.target
— Once the system has reached the state of fully isolating this target, it will have called thereboot(RB_POWER_OFF)
system call. The kernel will have attempted to remove power from the system, if possible.
These are the things that you should be thinking about as the final system states, not run levels. Notice from the diagram that the systemd target system itself encodes things that are, in other systems, implicit rather than explicit: such as the notion that each of these final targets encompasses the shutdown.target
target, so that one describes services that must be stopped before shutdown by having them conflict with the shutdown.target
target.
systemctl
tries to send requests to systemd-logind
when the calling user is not the superuser. It also passes delayed shutdowns over to systemd-shutdownd
. And some shorthands trigger wall
notifications. Those complexities aside, which would make this answer several times longer, assuming that you are currently the superuser and not requesting a scheduled action:
systemctl isolate halt.target
has the shorthands:shutdown -H now
systemctl halt
- plain unadorned
halt
systemctl isolate reboot.target
has the shorthands:shutdown -r now
telinit 6
systemctl reboot
- plain unadorned
reboot
systemctl isolate poweroff.target
has the shorthands:shutdown -P now
telinit 0
shutdown now
systemctl poweroff
- plain unadorned
poweroff
systemctl isolate rescue.target
has the shorthands:telinit 1
systemctl rescue
systemctl isolate multi-user.target
has the shorthands:telinit 2
telinit 3
telinit 4
systemctl isolate graphical.target
has the shorthand:telinit 5
After parsing the various differing command-line syntaxes, these all eventually end up in the same code paths inside the systemctl
program.
Notes:
- The traditional behaviour of option-less
shutdown now
has been to switch to single-user mode. This is not the case with systemd.rescue.target
— single-user mode being renamed rescue mode in systemd — is not reachable with theshutdown
command. telinit
really does wholly ignore all of thoserunlevelN.target
anddefault.target
symbolic links in the filesystem that the manual pages describe. The aforegiven mappings are hardwired into thesystemctl
program, in a table.- systemd has no notion of a current run level. The operation of these commands is not conditional upon any "if you are in run-level N".
- The
--force
option to thehalt
,reboot
, andpoweroff
commands is the same as saying--force --force
to thesystemctl halt
,systemctl reboot
, andsystemctl poweroff
commands. This makessystemctl
try to callreboot()
directly. Normally it just tries to isolate targets. telinit
is not the same asinit
. They are different programs in the systemd world, the latter being another name for thesystemd
program, not for thesystemctl
program. Thesystemd
program is not necessarily compiled with any van Smoorenburg compatibility at all, and on some systemd operating systems complains about being invoked incorrectly if one attemptsinit N
.
Further reading
- Are there any good reasons for halting system without cutting power?
- Why does `init 0` result in "Excess Arguments" on Arch install?
- Stephen Wadeley (2014). "8. Managing Services with systemd" Red Hat Enterprise Linux 7 System Administrators' Guide. Red Hat.
- Lennart Poettering (2013-10-07).
systemctl
. systemd manual pages. freedesktop.org. - Lennart Poettering (2013-10-07).
systemd.special
. systemd manual pages. freedesktop.org. - Lennart Poettering (2013-10-07).
bootup
. systemd manual pages. freedesktop.org. - Jonathan de Boyne Pollard (2018).
init
. nosh Guide. Softwares.
halt
instructs the hardware to stop all CPU functions, but leaves it in a powered-on state. This usually means someone has to reboot or shut the machine down manually by pressing the power button afterwards. The specific way to achieve this is architecture specific, but for instance the x86 instruction set provides theHLT
instructions which halts the central processing unit (CPU) until the next external interrupt is fired.poweroff
, likehalt
, stops the CPU but also sends an ACPI hardware signal which will instruct the system to commence with a complete and immediate shutdown. This is roughly equivalent to pressing the power button on a typical desktop computer.
Both halt
and poweroff
are usually symbolic links to the reboot
executable, which will usually invoke the shutdown
tool with the appropriate arguments (-h
, -P
or r
) depending on if halt
, poweroff
or reboot
was used to invoke the tool. However, When the --force
option is passed to reboot
, or when in runlevel 0 or 6, reboot
will invoke the reboot()
system call with an appropriate command code itself.