Use molly-guard to protect your Linux/Unix machines from being mistakenly shut down/restarted
I am going! Again. I thought I was logged on to my home server. As a result, the database server is restarted. In addition, I sometimes enter the "shutdown-h 0" command in the wrong terminal. I know that some people often make this mistake.
My anger is intolerable
Is there a way to solve this problem? Is it true that I can only endure the pain of random restart and shutdown? Although people always make mistakes, they cannot make mistakes again.
Recently, I complained on tweet:
I always make the same mistake over and over again: (I should have entered:sudo virsh reboot d1
But I always lost or restarted my computer.sudo reboot d1
-- NixCraft (@ nixcraft) February 19,7 7
Some suggestions were received. Let's give it a try.
Introduce molly guard to you
Molly-Guard tries to prevent you from accidentally shutting down or restarting the Linux server. The package description In Debian/Ubuntu is as follows:
This package will install a shell script to block existingshutdown
/reboot
/halt
/poweroff
/coldreboot
/pm-hibernate
/pm-suspend*
Command.molly-gurad
A series of scripts will be run first. The real commands will be called only when all scripts return success. One script checks whether an SSH session exists. If the command is called through an SSH Session, the shell script prompts you to enter the name of the host to close. This should be enough to prevent unexpected shutdown or restart.
It seems that molly-guard is still a proprietary term Jargon File:
A barrier used to prevent a barrier from being clumsy or accidentally touched by a red switch. The organic glass, which was initially referred to as a temporary big red button on IBM 4341, was restarted twice in a day by a programmer's toddler daughter (Molly. Later, it was used to cover the stop/restart buttons on disks and network devices. In the hardware directory, you rarely see a boring description like "guarded button ".
How to install molly guard
Useapt-get
Command orapt
Command to search for and install molly-guard:
$ apt search molly-guard
$ sudoapt-get install molly-guard
Result:
Fig.01: Installing molly guard on Linux
Test
Inputreboot
Commands andshutdown
Command:
$ sudoreboot
#reboot
$ shutdown-h 0
#sudoshutdown-h 0
### running wrong command such as follows instead of
### sudo virsh reboot vm_name_here
$ sudoreboot vm_name_here
Result:
Fig.02: Molly guard saved my butt ;\)
I like molly-guard very much. Therefore, I add downstream contentapt-debian-ubuntu-common.yml
File:
- apt:
name: molly-guard
Yes. I used Ansible to automatically install it on all Debian and Ubuntu servers.
Related: My 10 major UNIX Command Line errors
What if I don't have molly-guard on Linux distributions or Unix systems (such as FreeBSD?
Don't worry, set the shell alias:
##bash shell example ###
aliasreboot="echo 'Are you sure?' If so, run /sbin/reboot"
aliasshutdown="echo 'Are you sure?' If so, run /sbin/shutdown"
You can also temporarily cancel the alias mechanism to run real commands. For example, you can run reboot as follows:
# \reboot
Or
# /sbin/reboot
In addition, you can write a shell/perl/python script to call these commands and request confirmation.reboot
/halt
/shutdown
.
Via: https://www.cyberciti.biz/hardware/how-to-protects-linux-and-unix-machines-from-accidental-shutdownsreboots-with-molly-guard/
Author: Vivek Gite Translator: lujun9972 proofreaders: wxy
This article was originally compiled by LCTT and launched with the honor of Linux in China