How to avoid accidentally shutting down or rebooting in Linux

Sometimes we forget that an important task is being performed on a VPS or on a remote system. Without checking, we just shutdown or restart the VPS. Result? The work is stopped because of our carelessness. And we must start from the very beginning. Even though there are ways to move work from one terminal to another, we just forget things like that. There is a utility called molly-guardwhich is used to prevent the Linux system from accidentally shutting down or rebooting.

molly-guard installs a script that overrides existing commands / reboot / halt / poweroff / coldreboot / pm-hibernate / pm-suspend and is the first to run a set of scripts that should pass successfully molly-guardbefore the real command is run. One of the scripts validates existing SSH sessions. When you run any of the above commands in an SSH session, the shell script will ask you for the hostname you want to shutdown or reboot. This should adequately prevent you from accidentally shutting down and rebooting. molly-guard will direct the real binaries to / lib / molly-guard /… You can, of course, bypass molly-guard by executing these binaries directly.

In this quick tutorial, you will learn how to avoid accidentally shutting down or rebooting via SSH session on Unix-like operating systems.

Avoiding accidentally shutting down or rebooting Linux with molly-guard

Installation

molly-guard is available in the default repository on Debian, Ubuntu and other DEB based systems like Linux Mint, Elementary OS, etc.

To install molly-guard on DEB based systems, run the following command:

sudo apt-get install molly-guard

For distributed RPMs, download the Molly-guard.deb package, and convert as a native Linux package as described in the next tutorial. In the next tutorial, go to the section called “Convert RPM to DEB packages and vice versa” and convert DEB molly-guard package to RPM package.

Application

It’s time to check out this utility.

Now try shutting down or rebooting the remote system or VPS via SSH:

 $ 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 destroyer ...

See? molly-guard asks for the hostname of your Ubuntu server. Press CTRL + C to avoid disconnecting.

Similarly, you can try all the power off commands, system reboots. molly-guard kept asking for the hostname of my Ubuntu system to proceed.

[email protected]:~$ 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 destroyer ...

[email protected]:~$ 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 destroyer ...

[email protected]:~$ 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 destroyer ...

[email protected]:~$ 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 destroyer ...

If you really want to shutdown or reboot your system, just enter your hostname.

W: molly-guard: SSH session detected!
Please type in hostname of the machine to halt: destroyer
Connection to 192.168.43.2 closed by remote host.
Connection to 192.168.43.2 closed.

As you can see, in the above output, we have entered the hostname of the Ubuntu system (e.g. destroyer). Now, molly-guard knows that we really want to shutdown our system anyway, so we just did that.

We think that such a utility will help Linux administrators avoid problems from accidental shutdown or reboot. Add this utility to your arsenal to prevent yourself from accidentally shutting down or rebooting your production server.

Hope this helps. If you find our guide helpful, please share it on social media!

Sidebar