Linux Shutdown Protection: Prevent Accidental Server Shutdowns

Quick Linux Tip #100:

Question: I keep typing shutdown on the wrong server. How do I protect against this?

Try:
sudo tee /etc/molly-guard/run.d/50-prod-check <<'EOF' > /dev/null
#!/bin/bash
if [[ -f /etc/is_production ]]; then
read -r -p "PRODUCTION server! Type the hostname to confirm shutdown: " input
[[ "$input" == "$(hostname)" ]] || exit 1
fi
EOF

Info: molly-guard intercepts shutdown, reboot, and halt commands over active SSH connections and demands explicit hostname confirmation to prevent catastrophic human errors.

Examples:

  • $ sudo apt install molly-guard  # Install the safety utility
  • $ echo 'ALWAYS_QUERY_HOSTNAME=1' | sudo tee -a /etc/molly-guard/rc  # Force hostname prompts globally
  • $ sudo chmod -x /sbin/shutdown  # Nuclear option (disable binary entirely, not recommended)

Note: By default, molly-guard only triggers on remote SSH sessions, leaving physical console access unaffected. A must-have safeguard for production server fleets.




LinuxTeck.com
linuxteck:~$ sudo tee /etc/molly-guard/run.d/50-prod-check <<'EOF' > /dev/null
#!/bin/bash
if [[ -f /etc/is_production ]]; then
  read -r -p "PRODUCTION server! Type the hostname to confirm shutdown: " input
  [[ "$input" == "$(hostname)" ]] || exit 1
fi
EOF

linuxteck@prod-db-01:~$ sudo chmod +x /etc/molly-guard/run.d/50-prod-check

linuxteck@prod-db-01:~$ sudo touch /etc/is_production

linuxteck@prod-db-01:~$ sudo shutdown now
W: molly-guard: SSH session detected!
PRODUCTION server! Type the hostname to confirm shutdown: prod-db-01
Shutdown proceeding...

linuxteck@prod-db-01:~$ sudo shutdown now
W: molly-guard: SSH session detected!
PRODUCTION server! Type the hostname to confirm shutdown: wrong
Shutdown aborted.
linuxteck@prod-db-01:~$

PREVIOUS ARTICLE Quick Linux Tip #99: Linux Incremental Backup: Create Backups with tar NEXT ARTICLE Quick Linux Tip #101: Linux Network Bandwidth Monitoring: Track TCP Connections
About John Britto

John Britto Founder & Chief-Editor @LinuxTeck. A Computer Geek and Linux Intellectual having more than 20+ years of experience in Linux and Open Source technologies.

View all posts by John Britto →

Leave a Reply

Your email address will not be published.