molly-guard : How To Avoid Accidental Shutdown Or Reboot In Linux

molly-guard is a handy utility which protects machines from accidental shutdowns/reboots by asking your hostname. Many of the Linux professional, working more then one system when they have issues. molly-guard was primarily designed to shield SSH connections.

For some reason they want to reboot the system to resolve the issue in production, in such a case they might reboot wrong system instead of actual system. Even i also did the same accidentally many times.

In such kind of scenario molly-guard comes in handy and doing the job greatly without any failure. It’s available only for Debian based systems, that to the project was not update from many years but still working good and i didn’t find any good alternative too. If anyone found, let me know, will have a look and cover in our upcoming article.

How molly-guard works ? molly-guard installs a shell script that overrides the existing shutdown, reboot, halt & poweroff commands. It runs a set of scripts which is available in /etc/molly-guard/run.d/, which all have to exit successfully, before molly-guard invokes the real command (It has a couple of checks).

The script first tests whether the command is being executed from SSH. If yes, the shell script prompts you to enter the name of the host you wish to perform the activity which 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.

How to install molly-guard

As i told beginning of the article, molly-guard is available only for Debian based systems. Simple fire the following command with help of apt command or apt-get command to install it.

$ sudo apt-get install molly-guard

Perform the test

I’m going to perform reboot & shutdown command to check whether the app is working fine or not.

$ sudo reboot
W: molly-guard: SSH session detected!
Please type in hostname of the machine to reboot: ^C
Good thing I asked; I won't reboot ubuntu.daygeek ...

Reboot the system, after brought down.

$ sudo shutdown -r now
W: molly-guard: SSH session detected!
Please type in hostname of the machine to shutdown: ^C
Good thing I asked; I won't shutdown ubuntu.daygeek ...

Either halt or powered off after it has been brought down.

$ sudo shutdown -h now
W: molly-guard: SSH session detected!
Please type in hostname of the machine to shutdown: ^C
Good thing I asked; I won't shutdown ubuntu.daygeek ...

Halt the System.

$ sudo halt
W: molly-guard: SSH session detected!
Please type in hostname of the machine to halt: ^C
Good thing I asked; I won't halt ubuntu.daygeek ...

Poweroff the System.

$ sudo poweroff
W: molly-guard: SSH session detected!
Please type in hostname of the machine to poweroff: ^C
Good thing I asked; I won't poweroff ubuntu.daygeek ...

Magesh Maruthamuthu

Love to play with all Linux distribution

You may also like...

Shares
Close
Please support the site
By clicking any of these buttons you help our site to get better