Oops! I did it again. I thought I was logged into my home server. Turns out I rebooted the db server. Another my not so favorite is typing “shutdown -h 0” into the wrong terminal. I know a few people who have admitted to doing that here.

My anger that can’t be contained

Is there any end to the madness? Do I need to suffer from accidentally random reboots and shutdowns? After all, it is human nature to make mistakes, but one should not keep on making the same mistakes again and again.

Recently I tweeted my frustration:

I come across quite a few suggestion on Twitter. Let us try out those.

Say hello to molly guard app that protects Linux box from accidental shutdowns/reboots

Molly-Guard try to block you from accidentally running or shutting down or rebooting Linux servers. From the Debian/Ubuntu package description:

The package installs a shell script that overrides the existing shutdown/reboot/halt/poweroff/coldreboot/pm-hibernate/pm-suspend* commands and first runs a set of scripts, which all have to exit successfully, before molly-guard invokes the real command. One of the scripts checks for existing SSH sessions. If any of the four commands are called interactively over an SSH session, the shell script prompts you to enter the name of the host you wish to shut down. This should adequately prevent you from accidental shutdowns and reboots. molly-guard diverts the real binaries to /lib/molly-guard/. You can bypass molly-guard by running those binaries directly.

It seems molly-guard has the entry in the Jargon File:

A shield to prevent tripping of some Big Red Switch by clumsy or ignorant hands. Originally used of the plexiglass covers improvised for the BRS on an IBM 4341 after a programmer’s toddler daughter (named Molly) frobbed it twice in one day. Later generalized to covers over stop/reset switches on disk drives and networking equipment. In hardware catalogues, you’ll see the much less interesting description “guarded button”.

How to install molly guard

Type the following command to search and install molly-guard using apt-get command or apt command:
$ apt search molly-guard
$ sudo apt-get install molly-guard

Sample outputs:

Fig.01: Installing molly guard on Linux

Test it

Type the reboot command or shutdown command:
$ sudo reboot
# reboot
$ shutdown -h 0
# sudo shutdown -h 0
### running wrong command such as follows instead of
### sudo virsh reboot vm_name_here
$ sudo reboot vm_name_here

Sample outputs:

Fig.02: Molly guard saved my butt ;)

I liked molly-guard so much. I updated my apt-debian-ubuntu-common.yml Ansbile config file with the following lines:
 - apt:
       name: molly-guard

That’s right. It is now part of all of my Debian and Ubuntu servers automation tasks done using Ansible tool.

Related: My 10 UNIX Command Line Mistakes

What if molly-guard not available on my Linux distro or Unix system like FreeBSD?

Fear not, set shell aliases:

## bash shell example ###
alias reboot="echo 'Are you sure?' If so, run /sbin/reboot" 
alias shutdown="echo 'Are you sure?' If so, run /sbin/shutdown"

You can temporarily get rid of an aliases and run actual command such as reboot:
# \reboot
# /sbin/reboot


You learned how to use molly-guard that overrides the existing shutdown, reboot, halt, poweroff, coldreboot, pm-hibernate, and pm-suspend* Linux commands. No more accidental shutdowns/reboots for your server. Another option is to write a shell/perl/python script calling these and asking confirmation for reboot/halt/shutdown options.

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 2 comments so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersncdu pydf
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
2 comments… add one

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum