How To Avoid Accidental Shutdown Or Reboot In Linux
Sometimes, I forget there is an important job is running on my VPS and remote systems. Without verifying anything, I just shutdown or reboot the VPS. The result? The running job is terminated due to my ignorance. I have to start over the job from the beginning. Even though, there are ways to move the running job from one Terminal to another, I just forget things often. Later, I regret for being so naive and stupid. Well, not anymore! Today, I have encountered with 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.
In this brief tutorial, let us learn how to avoid accidental shutdown or reboot over SSH session in Unix-like operating systems.
Avoid Accidental Shutdown Or Reboot In Linux Using molly-guard
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 as 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.
It is time to test this 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 ...
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 it just did.
Alright fellow Linux administrators, and users, don’t suffer from accidental shutdown or reboot. Keep this utility in your arsenal to prevent yourself from unknowingly or accidentally shutdown or reboot your production server.
Hope this helps. If you find our guides useful, please share them on your social, professional networks, so other users can also benefit. I will be back soon with another useful and interesting guide. Stay tuned with OSTechNix.
Thanks for stopping by!
Help us to help you:
- Subscribe to our Email Newsletter : Sign Up Now
- Support OSTechNix : Donate Via PayPal
- Download free E-Books and Videos : OSTechNix on TradePub
- Connect with us: Facebook | Twitter | Google Plus | LinkedIn | RSS feeds
Have a Good day!!