Linux用户宝典:避免或防止意外关机或重启的5种方法

译文
系统 Linux
大多数情况下有一台JUMP服务器,Linux管理员无需密码即可连接到所有其他Linux系统。你在使用多台计算机时,由于某个原因想重启系统,但可能重启的是错误的系统,而不是实际的系统。这种情况下,如何防止意外关闭或重启Linux系统呢?

[[284180]]

【51CTO.com快译】大多数情况下有一台JUMP服务器,Linux管理员无需密码即可连接到所有其他Linux系统。

你可以一次连接到多个系统以排除故障。

你在使用多台计算机时,由于某个原因想重启系统,但可能重启的是错误的系统,而不是实际的系统。

如果是非生产服务器,没问题。但如果你同时重启一台重要的生产服务器,就要考虑清楚情况。本人不小心重启过几次。

这种情况下,如何防止意外关闭或重启Linux系统呢?

没错,我们有一些方法可以防止这种情况,下面作详细介绍。

方法1:如何使用molly-guard实用程序防止在Linux上意外关闭或重启

molly-guard这个简单的应用程序通过询问主机名来保护机器免受意外关闭和重启。 molly-guard主要用于保护SSH连接。

它仅适用于基于Debian的系统,该项目已有多年未更新,但仍可以正常使用。

molly-guard如何工作?molly-guard安装shell脚本,该脚本覆盖现有的shutdown、restart、halt和poweroff等命令。

它运行/etc/molly-guard/run.d/中一组可用的脚本,在molly-guard执行实际命令之前,所有脚本都需要成功退出(它有几道检查机制)。

脚本先检查是否从SSH执行命令。如果是,shell脚本提示你输入要执行该功能的主机名,以防止意外关闭和重启。

molly-guard将实际的二进制文件转移到/lib/molly-guard/。你可以通过直接运行那些二进制文件来绕过molly-guard。

如何在Debian/Ubuntu上安装molly-guard?

正如本文开头所说,molly-guard应用程序仅适用于基于Debian的系统。使用apt命令或apt-get命令来安装。

  1. $ sudo apt install molly-guard 

使用molly-guard创建测试用例

我会执行重启和关闭命令,检查molly-guard应用程序是否按预期运行。 

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

被关闭后停止或关机。 

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

停止系统。 

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

关闭系统。 

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

说明:molly-guard应用程序无法阻止systemctl shutdown和systemctl reboot命令。

方法2:如何使用systemd定制服务防止在Linux上意外关闭或重启

为此,创建定制服务来阻止shutdown/restart命令。

创建下列单元文件: 

  1. # vi /etc/systemd/system/reboot-guard.service  
  2. [Unit]  
  3. Description=Reboot Guard  
  4. [Service]  
  5. ExecStart=/bin/true  
  6. [Install]  
  7. RequiredBy=shutdown.target 

单元文件-2: 

  1. # /etc/systemd/system/start-reboot-guard.service  
  2. [Unit]  
  3. Description=Start Reboot Guard  
  4. [Service]  
  5. ExecStart=/bin/systemctl enable reboot-guard  
  6. [Install]  
  7. WantedBy=multi-user.target 

运行下列systemctl命令以激活reboot-guard服务。 

  1. # systemctl daemon-reload  
  2. # systemctl enable reboot-guard start-reboot-guard  
  3. Created symlink /etc/systemd/system/shutdown.target.requires/reboot-guard.service → /etc/systemd/system/reboot-guard.service.  
  4. Created symlink /etc/systemd/system/multi-user.target.wants/start-reboot-guard.service → /etc/systemd/system/start-reboot-guard.service. 

使用reboot-guard服务创建测试用例。

针对重启 

  1. # systemctl reboot  
  2. Failed to reboot system via logind: Transaction contains conflicting jobs 'stop' and 'start' for shutdown.target. Probably contradicting requirement dependencies configured.  
  3. Failed to start reboot.target: Transaction contains conflicting jobs 'stop' and 'start' for shutdown.target. Probably contradicting requirement dependencies configured.  
  4. See system logs and 'systemctl status reboot.target' for details. 

针对关机 

  1. # systemctl poweroff  
  2. Failed to power off system via logind: Transaction contains conflicting jobs 'stop' and 'start' for poweroff.target. Probably contradicting requirement dependencies configured.  
  3. Failed to start poweroff.target: Transaction contains conflicting jobs 'stop' and 'start' for poweroff.target. Probably contradicting requirement dependencies configured.  
  4. See system logs and 'systemctl status poweroff.target' for details. 

至于reboot、init 0和init 6之类的老式工具,我没看到什么影响,但shutdown命令显示了下列输出。然而,这其实并不关闭系统。 

  1. # reboot  
  2. # init 6  
  3. # poweroff  
  4. # init 0  
  5. # shutdown  
  6. Shutdown scheduled for Sun 2019-11-10 21:59:17 IST, use 'shutdown -c' to cancel. 

运行下列命令,启用shutdown/restart命令。

  1. # systemctl disable reboot-guard 

参考:Red Hat网页。

方法3:如何使用reboot-guard实用程序防止在Linux上意外关闭或重启

阻止systemd启动的poweroff/reboot/halt目标,直到可配置条件检查通过。

它只与Python 2兼容,所以确保你在系统上安装了Python 2。我在CentOS 8上进行了测试,系统之前未安装Python 2,于是我安装了。

将reboot-guard实用程序下载到“/usr/sbin”目录下。 

  1. # cd /usr/sbin  
  2. # curl -kO https://raw.githubusercontent.com/ryran/reboot-guard/master/rguard  
  3. # chmod +x rguard 

运行下列命令,让rguard实用程序能够阻止reboot/shutdown。 

  1. # rguard -1  
  2. WARNING: ☹ Blocked poweroff.target  
  3. WARNING: ☹ Blocked reboot.target  
  4. WARNING: ☹ Blocked halt.target 

创建rguard应用程序的测试用例。

针对重启 

  1. # systemctl reboot  
  2. Failed to reboot system via logind: Operation refused, unit reboot.target may be requested by dependency only (it is configured to refuse manual start/stop).  
  3. Failed to start reboot.target: Operation refused, unit reboot.target may be requested by dependency only (it is configured to refuse manual start/stop).  
  4. See system logs and 'systemctl status reboot.target' for details. 

针对关机 

  1. # systemctl poweroff  
  2. Failed to power off system via logind: Operation refused, unit poweroff.target may be requested by dependency only (it is configured to refuse manual start/stop).  
  3. Failed to start poweroff.target: Operation refused, unit poweroff.target may be requested by dependency only (it is configured to refuse manual start/stop).  
  4. See system logs and 'systemctl status poweroff.target' for details. 

至于reboot、init 0和init 6之类的老式工具,我没看到什么影响,但shutdown命令显示了下列输出。然而,这其实并不关闭系统。 

  1. # reboot  
  2. # init 6  
  3. # poweroff  
  4. # init 0  
  5. # shutdown  
  6. Shutdown scheduled for Sun 2019-11-10 23:46:24 IST, use 'shutdown -c' to cancel. 

运行下列命令以禁用rguard实用程序。 

  1. # rguard -0  
  2. WARNING: ☻ Unblocked poweroff.target  
  3. WARNING: ☻ Unblocked reboot.target  
  4. WARNING: ☻ Unblocked halt.target 

方法4:如何使用systemctl命令防止在Linux上意外关闭或重启

此外,可以使用systemctl命令来掩盖服务。掩盖服务可阻止服务被人工或自动启动。

掩盖下列单元以阻止意外重启/关闭。 

  1. # systemctl mask reboot.target  
  2. Created symlink /etc/systemd/system/reboot.target → /dev/null 
  3. # systemctl mask poweroff.target  
  4. Created symlink /etc/systemd/system/poweroff.target → /dev/null 
  5. # systemctl mask halt.target  
  6. Created symlink /etc/systemd/system/halt.target → /dev/null

针对重启 mctl reboot  

  1. Failed to reboot system via logind: Access denied  
  2. Failed to start reboot.target: Unit reboot.target is masked. 
  1. # syste

针对关机 

  1. # systemctl poweroff  
  2. Failed to power off system via logind: Access denied  
  3. Failed to start poweroff.target: Unit poweroff.target is masked. 

至于reboot、poweroff、init 0和init 6之类的老式工具,我没看到什么影响,但shutdown命令显示了下列输出。然而,这其实并不关闭系统。 

  1. # reboot  
  2. # init 6  
  3. # poweroff  
  4. # init 0  
  5. # shutdown  
  6. Shutdown scheduled for Sun 2019-11-10 23:59:09 IST, use 'shutdown -c' to cancel. 

运行下列命令来启用它们。 

  1. # systemctl unmask reboot.target  
  2. Removed /etc/systemd/system/reboot.target.  
  3. # systemctl unmask poweroff.target  
  4. Removed /etc/systemd/system/poweroff.target.  
  5. # systemctl unmask halt.target  
  6. Removed /etc/systemd/system/halt.target. 

方法5:如何使用alias命令防止在Linux上意外关闭或重启

此外,可以创建一个别名来阻止这种情况。 

  1. # vi .bashrc  
  2. alias reboot="echo -e 'Is \033[1;31m$HOSTNAME\033[0m the correct hostname you want to restart?' If yes, run /sbin/reboot"  
  3. alias shutdown="echo -e 'Is \033[1;31m$HOSTNAME\033[0m the correct hostname you want to shutdown?' If yes, run /sbin/shutdown" 

运行下列命令使这个变更生效。

  1. # source .bashrc 

现在测试这些命令,静观结果。 

  1. # shutdown  
  2. Is CentOS6.2daygeek.com the correct hostname you want to shutdown? If yes, run /sbin/shutdown  
  3. # reboot 
  4. Is CentOS6.2daygeek.com the correct hostname you want to restart? If yes, run /sbin/reboot 

原文标题:5 Methods to Avoid or Prevent Accidental Shutdown or Reboot on Linux,作者:Magesh Maruthamuthu

【51CTO译稿,合作站点转载请注明原文译者和出处为51CTO.com】

责任编辑:庞桂玉 来源: 51CTO
相关推荐

2016-12-13 23:08:48

Linux命令

2020-09-01 09:56:26

云端云计算云服务

2018-04-27 10:33:56

Linux命令chattr

2014-03-27 14:44:58

数据丢失防护DLP数据保护

2014-04-01 11:13:32

数据丢失

2019-11-27 08:00:00

Linux系统用户管理员

2009-12-23 09:57:31

Linux关机或休眠

2022-12-07 11:24:51

首席信息官IT

2022-12-29 08:46:15

IT采购投资

2013-01-15 10:41:50

2010-04-27 16:07:41

Unix关机

2019-04-15 16:05:55

2020-12-21 09:21:24

微软Edge浏览器

2010-01-27 09:53:37

2020-06-17 10:52:00

DDoS攻击网络攻击网络安全

2022-04-25 17:49:05

云计算云安全安全

2015-04-30 07:14:58

2011-01-04 14:27:50

安装linux方法

2010-09-13 08:45:23

DIVFlash

2018-08-02 09:50:47

Linux命令用户信息
点赞
收藏

51CTO技术栈公众号