How do you exit a shell session and shut down Linux?
Leave a shell with exit (or Ctrl+D); shut the whole machine down with shutdown, whose general form is shutdown [-h|-r] TIME [message].
These are two very different scopes. exit ends your session — the shell process closes and, over SSH, you're disconnected — but the machine keeps running. shutdown powers down or reboots the entire system, which is why it needs root.
exit # end this shell session (Ctrl+D does the same)
sudo shutdown -h now # halt/power off immediately
sudo shutdown -r now # reboot immediately
shutdown -h +10 # power off in 10 minutes
shutdown -h 22:00 # power off at 22:00
shutdown -c # cancel a scheduled shutdown
The two arguments after shutdown follow the [-h|-r] TIME pattern:
| Part | Meaning |
|---|---|
-h |
Halt the system (on modern hardware this also cuts power) |
-r |
Reboot instead of powering off |
now |
Do it immediately |
+mm |
Wait mm minutes first |
hh:mm |
Do it at a clock time |
Why schedule rather than now? On a shared server, shutdown -h +10 "Maintenance — save your work" broadcasts a warning to every logged-in user and gives them ten minutes, instead of yanking the system out from under them.
Gotcha: a scheduled shutdown keeps counting in the background. If plans change, you must shutdown -c to cancel it — closing your terminal does not call it off.