当前位置: 首页 > 科技观察

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

时间:2023-03-11 21:08:54 科技观察

【.com快速翻译】在大多数情况下,有一个JUMP服务器,Linux管理员可以在没有密码的情况下连接到所有其他Linux系统。您可以一次连接到多个系统以进行故障排除。您正在使用多台计算机并且出于某种原因想要重新启动系统,但它可能是错误的系统,而不是实际系统。如果它是非生产服务器,没问题。但是如果你同时重启了一个重要的生产服务器,你就得仔细考虑一下情况了。我不小心重启了几次。在这种情况下,如何防止Linux系统意外关机或重启呢?是的,我们有一些方法可以防止这种情况发生,下面将详细介绍。方法1:如何使用molly-guard实用程序在Linux上防止意外关机或重启molly-guard是一个简单的应用程序,它通过询问主机名来防止机器意外关机和重启。molly-guard主要用于保护SSH连接。它只适用于基于Debian的系统,该项目多年未更新,但仍然运行良好。molly-guard是如何工作的?molly-guard安装shell脚本来覆盖现有的命令,如关机、重启、暂停和关机。它运行/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命令安装。$sudoaptinstallmolly-guard使用molly-guard创建测试用例我将执行重启和关机命令来检查molly-guard应用程序是否按预期运行。$sudorebootW:molly-guard:SSHsessiondetected!Pleasetypeinhostnameofthemachinetoreboot:^CGoodthingIasked;Iwon'trebootubuntu.daygeek...在停止或关闭后被关闭。$sudoshutdown-hnowW:molly-guard:SSH会话检测到!请输入机器的主机名以关闭:^CGoodthingIasked;我不会关闭ubuntu.daygeek...停止系统。$sudohaltW:molly-guard:SSHsessiondetected!Pleasetypeinhostnameofthemachinetohalt:^CGoodthingIasked;Iwon'thaltubuntu.daygeek...关闭系统。$sudopoweroffW:molly-guard:SSHsessiondetected!Pleasetypeinhostnameofthemachinetopoweroff:^CGoodthingIasked;Iwon'tpoweroffubuntu.daygeek...描述:molly-guard应用程序无法阻止systemctlshutdown和systemctlreboot命令。方法2:如何使用Systemd自定义服务在Linux上防止意外关机或重启为此,创建一个自定义服务以防止关机/重启命令。创建以下单元文件:#vi/etc/systemd/system/reboot-guard.service[Unit]Description=RebootGuard[Service]ExecStart=/bin/true[Install]RequiredBy=shutdown.targetUnitfile-2:#/etc/systemd/system/start-reboot-guard.service[Unit]Description=StartRebootGuard[Service]ExecStart=/bin/systemctlenablereboot-guard[Install]WantedBy=multi-user.target运行以下systemctl命令激活reboot-guard服务。#systemctldaemon-reload#systemctlenablereboot-guardstart-reboot-guardCreatedsymlink/etc/systemd/system/shutdown.target.requires/reboot-guard.service→/etc/systemd/system/reboot-guard.service.Createdsymlink/etc/systemd/system/multi-user.target.wants/start-reboot-guard.service→/etc/systemd/system/start-reboot-guard.service。使用reboot-guard服务创建测试用例。针对重启#systemctlrebootFailedtorebootsystemvialogind:Transactioncontainsconflictingjobs'stop'and'start'forshutdown.target.Probablycontradictingrequirementdependenciesconfigured.Failedtostartreboot.target:Transactioncontainsconflictingjobs'stop'and'start'forshutdown.target.Probablycontradictingrequirementdependenciesconfigured.Seesystemlogsand'systemctlstatusreboot.target'fordetails.针对关机#systemctlpoweroffFailedtopoweroffsystemvialogind:Transactioncontainsconflictingjobs'stop'and'start'forpoweroff.target.Probablycontradictingrequirementdependenciesconfigured.Failedtostartpoweroff.target:Transactioncontainsconflictingjobs'stop'and'start'forpoweroff.target.Probablycontradictingrequirementdependenciesconfigured.Seesystemlogsand'systemctlstatuspoweroff.target'的详细信息。至于重新启动、初始化之谜和初始化6我没有看到任何效果,但shutdown命令显示以下输出。但是,这实际上并没有关闭系统。#reboot#init6#poweroff#init0#shutdownShutdownscheduledforSun2019-11-1021:59:17IST,使用'shutdown-c'取消。运行以下命令以启用关机/重启命令。#systemctldisablereboot-guard参考资料:RedHat网页。方法3:如何使用reboot-guard实用程序来防止Linux上的意外关机或重启,这些意外关机或重启会阻止systemd在通过可配置条件检查之前启动poweroff/reboot/halt目标。它仅与Python2兼容,因此请确保您的系统上安装了Python2。我在CentOS8上测试过,系统之前没有安装Python2,所以安装了。将reboot-guard实用程序下载到“/usr/sbin”目录。#cd/usr/sbin#curl-kOhttps://raw.githubusercontent.com/ryran/reboot-guard/master/rguard#chmod+xrguard运行以下命令以启用rguard实用程序以防止重启/关机。#rguard-1WARNING:?Blockedpoweroff.targetWARNING:?Blockedreboot.targetWARNING:?Blockedhalt.target为rguard应用程序创建一个测试用例。针对重启#systemctlrebootFailedtorebootsystemvialogind:Operationrefused,unitreboot.targetmayberequestedbydependencyonly(itisconfiguredtorefusemanualstart/stop).Failedtostartreboot.target:Operationrefused,unitreboot.targetmayberequestedbydependencyonly(itisconfiguredtorefusemanualstart/stop).Seesystemlogsand'systemctlstatusreboot.target'fordetails.针对关机#systemctlpoweroffFailedtopoweroffsystemvialogind:Operationrefused,unitpoweroff.targetmayberequestedbydependencyonly(itisconfiguredtorefusemanualstart/stop).Failedtostartpoweroff.target:Operationrefused,unitpoweroff.targetmayberequestedbydependencyonly(itisconfiguredtorefusemanualstart/stop).Seesystemlogsand'systemctlstatuspoweroff.target'了解详细信息。shutdown命令显示以下输出。但是,这实际上并没有关闭系统。#reboot#init6#poweroff#init0#shutdownShutdownscheduledforSun2019-11-1023:46:24IST,使用“shutdown-c”取消。运行以下命令以禁用rguard实用程序。#rguard-0警告:?Unblockedpoweroff.targetWARNING:?Unblockedreboot.targetWARNING:?Unblockedhalt.target方法4:如何在Linux上使用systemctl命令防止意外关机或重启此外,systemctl命令可用于屏蔽服务。隐藏服务可防止服务被手动或自动启动。屏蔽以下单元以防止意外重启/关闭。#systemctlmaskreboot.targetCreatedsymlink/etc/systemd/system/reboot.target→/dev/null.#systemctlmaskpoweroff.targetCreatedsymlink/etc/systemd/system/poweroff.target→/dev/null.#systemctlmaskhalt.targetCreatedsymlink/etc/systemd/system/halt.target→/dev/null。重启mctlrebootFailedtorebootsystemvialogind:AccessdeniedFailedtostartreboot.target:Unitreboot.targetismasked.#systeforshutdown#systemctlpoweroffFailedtopoweroffsystemvialogind:AccessdeniedFailedtostartpoweroff.target:Unitpoweroff.targetismasked。较旧的工具,我没有看到任何效果,但shutdown命令显示以下输出。但是,这实际上并没有关闭系统。#reboot#init6#poweroff#init0#shutdownShutdownscheduledforSun2019-11-1023:59:09IST,使用“shutdown-c”取消。运行以下命令以启用它们。#systemctlunmaskreboot.targetRemoved/etc/systemd/system/reboot.target.#systemctlunmaskpoweroff.targetRemoved/etc/systemd/system/poweroff.target.#systemctlunmaskhalt.targetRemoved/etc/systemd/system/halt.target。方法5:如何使用alias命令可以防止Linux上的意外关机或重启。此外,可以创建一个别名来防止这种情况。#vi.bashrcaliasreboot="echo-e'Is\033[1;31m$HOSTNAME\033[0mthecorrecthostnameyouwanttorestart?'如果是,运行/sbin/reboot"aliasshutdown="echo-e'Is\033[1;31m$HOSTNAME\033[0mthecorrecthostnameyouwanttoshutdown?'Ifyes,run/sbin/shutdown"运行以下命令使此更改生效。#source.bashrc现在测试这些命令并等待结果。#shutdownIsCentOS6.2daygeek.comthecorrecthostnameyouwanttoshutdown?如果是,运行/sbin/shutdown#rebootIsCentOS6.2daygeek.comthecorrecthostnameyouwanttorestart?如果是,运行/sbin/reboot合作站点转载请注明出处为.com]