halt Command: Tutorial & Examples
Shut Down Or Halt The Linux System From The Command Line
The halt command is a fundamental utility used to stop all CPU functions and safely bring a Linux system to a halt from the command line. Typically requiring
root or superuser privileges, it instructs the kernel to terminate all processes and unmount file systems, effectively stopping system activity. Depending on
system configuration and options, halt may power off the machine or simply stop the CPU. It is an essential tool for system administrators managing servers
and virtual machines without graphical interfaces.
What halt Does
The primary purpose of the halt command is to stop the CPU and all system activity, effectively bringing the system to a complete stop. Unlike shutdown, which gracefully stops services and notifies logged-in users before halting, halt immediately signals the kernel to stop
the system. On some systems, invoking halt with specific options also powers off the machine, but in others, it only stops the CPU without turning off
hardware. The exact behavior depends on the Linux distribution, kernel support, and init system.
How halt Works
When executed, halt sends a signal to the Linux kernel to terminate all running processes, unmount file systems safely, and stop CPU operations.
Traditionally, this was done by invoking system calls like reboot(LINUX_REBOOT_CMD_HALT) to halt the system without powering off. Modern Linux distributions
often use systemd, where halt is a symbolic link to systemctl halt or systemctl poweroff.
The command's effect depends on the underlying init system:
- SysVinit and similar:
haltcalls scripts in/etc/init.d/or/etc/rc.d/to stop services and unmount filesystems before halting the CPU. - systemd:
halttypically maps to a systemctl command that manages system states, ensuring orderly shutdown and hardware power off if requested.
If the hardware supports it and the -p option is used, the machine powers off after halting.
Common Command Line Parameters
The halt command supports several options to control its behavior:
-f
Force immediate halt without calling shutdown scripts.
This skips unmounting filesystems and can cause file-system-corruption.-p
Power off the machine after halting if hardware and kernel support it.-w
Do not halt the system; only write a shutdown record to the/var/log/wtmpfile.-h
Halt the system (default behavior). Sometimes implicit.--help
Display a help message showing available options.
Practical Examples Using halt
Here are practical examples demonstrating common halt usage scenarios:
Halt the system safely (requires root privileges):
sudo haltSample output: Usually no output is shown. The system begins the halt process.
Halt and power off the machine:
sudo halt -pThis instructs the system to power off the machine after halting.
Force immediate halt without running shutdown scripts (use with caution):
sudo halt -fThis can be used in emergencies but risks disk-error and data loss.
Write shutdown record without halting:
sudo halt -wUpdates the shutdown logs but leaves the system running.
Check if halt is a symbolic link to systemctl (on systemd systems):
ls -l $(which halt)Sample output:
lrwxrwxrwx 1 root root 9 Apr 12 10:00 /usr/bin/halt -> systemctlSchedule a system halt in 10 minutes using
at:echo "sudo halt" | at now + 10 minutesUsing halt in a script to safely stop the system:
#!/bin/bash echo "System will halt in 1 minute. Please save your work." sleep 60 sudo halt -p
Cheatsheet
Quick reference for common halt options:
halt: Halt the system (default).halt -p: Halt and power off.halt -f: Force immediate halt (dangerous).halt -w: Write shutdown record only.halt --help: Show help message.
Potential Problems and Troubleshooting
Using
halt -fwithout proper preparation can cause file-system-corruption or disk-error because filesystems might not be unmounted cleanly.If the machine does not power off after
halt, tryhalt -por usepoweroff.On systemd systems,
haltmay be a symlink tosystemctl. Confirm with:ls -l $(which halt)Permission denied errors indicate you are not running as root or via sudo.
Remote use of
haltvia SSH should be done cautiously to avoid disrupting other users or network-failure.If the system fails to halt properly, check logs in
/var/log/syslogor viajournalctlfor errors.After improper halts, running a file system check with
fsckmay be necessary.
Comparison With Alternatives
shutdown: Provides a graceful shutdown by notifying users and stopping services before halting or rebooting. Example:shutdown -h now.poweroff: Powers off the system immediately, similar tohalt -p.reboot: Halts and then restarts the system.- On modern systems using systemd, these commands are often symbolic links or wrappers around
systemctlcommands managing system power states.
Use shutdown for planned maintenance, halt for stopping CPU without rebooting, and poweroff to turn off hardware.
Tips and Best Practices
- Prefer
shutdownfor planned halts to avoid data loss. - Use
halt -fonly in emergencies. - Check your distribution’s behavior for
haltbecause implementations vary. - Notify logged-in users before halting remote servers.
- Automate halts safely with cron or at.
- Always verify system logs after halting to detect issues.
- Test halt commands on non-critical systems first.
Security Considerations
Only trusted users should be allowed to execute halt, as improper use can disrupt services and compromise system availability. Control access
through sudo configuration. For example, restrict halt permission by adding to /etc/sudoers:
youruser ALL=(ALL) NOPASSWD: /sbin/halt
Avoid exposing halt commands via network services or scripts accessible by untrusted users.
Scripting And Automation With halt
The halt command can be used in scripts to automate system shutdowns. For example, to halt a system at a scheduled time, combine halt with cron or at:
Add to crontab (
crontab -e) to halt at 11 PM daily:0 23 * * * /sbin/halt -pUse in maintenance scripts with appropriate warning messages and logging.
Always ensure scripts run with sufficient privileges and notify users when possible.
Advanced Usage And Lesser-Known Features
- Some systems support additional options like
--no-wallto suppress broadcast messages. - Embedded systems or rescue environments may use
haltwithout powering off to preserve hardware state. - Combining
haltwith kernel parameters can influence behavior during startup/shutdown. haltcan be used in recovery scenarios when normal shutdown fails.
See Also
shutdownpoweroffreboot- systemd
cronat- disk-error
- network-failure
- file-system-corruption
- root
- sudo
/var/log/wtmp
Further Reading
- Linux for Hackers by Mark Reed (partner link)
- How Linux Works by Brian Ward (partner link)
- Linux for Beginners by Jason Cannon (partner link)
- Expert Linux Administration Guide by Vishal Rai (partner link)
As an Amazon Associate, I earn from qualifying purchases.