Home Command line utilities How To Avoid Accidental Shutdown Or Reboot In Linux

How To Avoid Accidental Shutdown Or Reboot In Linux

By sk
Published: Last Updated on 2.4K views

In this brief tutorial, let us learn how to avoid accidental shutdown or reboot over SSH session in Linux and Unix-like operating systems using molly-guard utility.

Sometimes, you may forget there is an unfinished job is running on your system and shutdown or reboot it. The result? The running job is terminated and you have to start over the job from the beginning. Even though, there are ways to move the running job from one Terminal to another, we just forget things often. Later, we regret for not being cautious. Well, not anymore! Today, I have encountered with a life-saving utility called molly-guard, which is used to prevent the Linux systems from accidental shutdown or reboot.

molly-guard 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 you run any of the aforementioned four commands over an SSH session, the shell script asks you to enter the name of the host you want to shut down or reboot. This should adequately prevent you from accidental shutdowns and reboots. molly-guard diverts the real binaries to /lib/molly-guard/. You can, of course, bypass molly-guard by running those binaries directly.

Install molly-guard in Linux

molly-guard is available in the default repositories of Debian, Ubuntu and other DEB based systems such as Linux Mint, Elementary OS etc.

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

$ sudo apt-get install molly-guard

For RPM based distributions, download the molly-guard.deb package, and convert to native Linux package as described in the following guide. In the following guide, go to the section titled "Convert RPM to DEB packages and vice versa" and convert molly-guard deb package to RPM package.

Avoid accidental shutdown or reboot in Linux using molly-guard

It is time to test the molly-guard utility.

Now, try to shutdown or reboot your remote system or vps over 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 ubuntuserver ...

See? molly-guard is asking the hostname of my Ubuntu server. I hit CTRL+C to avoid the shutdown.

Similarly, I tried all commands to poweroff, reboot my system. molly-guard is kept asking my Ubuntu system's hostname in-order to proceed.

sk@ubuntuserver:~$ 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 ubuntuserver ...

sk@ubuntuserver:~$ 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 ubuntuserver ...

sk@ubuntuserver:~$ 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 ubuntuserver ...

sk@ubuntuserver:~$ 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 ubuntuserver ...
Avoid accidental shutdown or reboot over SSH session

Avoid accidental shutdown or reboot over SSH session

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

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

As you see in the above output, I entered my Ubuntu system's hostname (eg. ubuntuserver). Now, molly-guard knows that I really wanted to shutdown my system anyway, so let it to shutdown.

Alright fellow Linux administrators, and users, don't suffer from accidental shutdown or reboot. Keep this utility in your arsenal to prevent your production Linux systems from accidental shutdown and reboot.

You May Also Like

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. By using this site, we will assume that you're OK with it. Accept Read More