Linux用戶寶典:避免或防止意外關(guān)機或重啟的5種方法
譯文【51CTO.com快譯】大多數(shù)情況下有一臺JUMP服務(wù)器,Linux管理員無需密碼即可連接到所有其他Linux系統(tǒng)。
你可以一次連接到多個系統(tǒng)以排除故障。
你在使用多臺計算機時,由于某個原因想重啟系統(tǒng),但可能重啟的是錯誤的系統(tǒng),而不是實際的系統(tǒng)。
如果是非生產(chǎn)服務(wù)器,沒問題。但如果你同時重啟一臺重要的生產(chǎn)服務(wù)器,就要考慮清楚情況。本人不小心重啟過幾次。
這種情況下,如何防止意外關(guān)閉或重啟Linux系統(tǒng)呢?
沒錯,我們有一些方法可以防止這種情況,下面作詳細(xì)介紹。
方法1:如何使用molly-guard實用程序防止在Linux上意外關(guān)閉或重啟
molly-guard這個簡單的應(yīng)用程序通過詢問主機名來保護機器免受意外關(guān)閉和重啟。 molly-guard主要用于保護SSH連接。
它僅適用于基于Debian的系統(tǒng),該項目已有多年未更新,但仍可以正常使用。
molly-guard如何工作?molly-guard安裝shell腳本,該腳本覆蓋現(xiàn)有的shutdown、restart、halt和poweroff等命令。
它運行/etc/molly-guard/run.d/中一組可用的腳本,在molly-guard執(zhí)行實際命令之前,所有腳本都需要成功退出(它有幾道檢查機制)。
腳本先檢查是否從SSH執(zhí)行命令。如果是,shell腳本提示你輸入要執(zhí)行該功能的主機名,以防止意外關(guān)閉和重啟。
molly-guard將實際的二進制文件轉(zhuǎn)移到/lib/molly-guard/。你可以通過直接運行那些二進制文件來繞過molly-guard。
如何在Debian/Ubuntu上安裝molly-guard?
正如本文開頭所說,molly-guard應(yīng)用程序僅適用于基于Debian的系統(tǒng)。使用apt命令或apt-get命令來安裝。
- $ sudo apt install molly-guard
使用molly-guard創(chuàng)建測試用例
我會執(zhí)行重啟和關(guān)閉命令,檢查molly-guard應(yīng)用程序是否按預(yù)期運行。
- $ 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 ubuntu.daygeek ...
被關(guān)閉后停止或關(guān)機。
- $ 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 ubuntu.daygeek ...
停止系統(tǒng)。
- $ 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 ubuntu.daygeek ...
關(guān)閉系統(tǒng)。
- $ 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 ubuntu.daygeek ...
說明:molly-guard應(yīng)用程序無法阻止systemctl shutdown和systemctl reboot命令。
方法2:如何使用systemd定制服務(wù)防止在Linux上意外關(guān)閉或重啟
為此,創(chuàng)建定制服務(wù)來阻止shutdown/restart命令。
創(chuàng)建下列單元文件:
- # vi /etc/systemd/system/reboot-guard.service
- [Unit]
- Description=Reboot Guard
- [Service]
- ExecStart=/bin/true
- [Install]
- RequiredBy=shutdown.target
單元文件-2:
- # /etc/systemd/system/start-reboot-guard.service
- [Unit]
- Description=Start Reboot Guard
- [Service]
- ExecStart=/bin/systemctl enable reboot-guard
- [Install]
- WantedBy=multi-user.target
運行下列systemctl命令以激活reboot-guard服務(wù)。
- # systemctl daemon-reload
- # systemctl enable reboot-guard start-reboot-guard
- Created symlink /etc/systemd/system/shutdown.target.requires/reboot-guard.service → /etc/systemd/system/reboot-guard.service.
- Created symlink /etc/systemd/system/multi-user.target.wants/start-reboot-guard.service → /etc/systemd/system/start-reboot-guard.service.
使用reboot-guard服務(wù)創(chuàng)建測試用例。
針對重啟
- # systemctl reboot
- Failed to reboot system via logind: Transaction contains conflicting jobs 'stop' and 'start' for shutdown.target. Probably contradicting requirement dependencies configured.
- Failed to start reboot.target: Transaction contains conflicting jobs 'stop' and 'start' for shutdown.target. Probably contradicting requirement dependencies configured.
- See system logs and 'systemctl status reboot.target' for details.
針對關(guān)機
- # systemctl poweroff
- Failed to power off system via logind: Transaction contains conflicting jobs 'stop' and 'start' for poweroff.target. Probably contradicting requirement dependencies configured.
- Failed to start poweroff.target: Transaction contains conflicting jobs 'stop' and 'start' for poweroff.target. Probably contradicting requirement dependencies configured.
- See system logs and 'systemctl status poweroff.target' for details.
至于reboot、init 0和init 6之類的老式工具,我沒看到什么影響,但shutdown命令顯示了下列輸出。然而,這其實并不關(guān)閉系統(tǒng)。
- # reboot
- # init 6
- # poweroff
- # init 0
- # shutdown
- Shutdown scheduled for Sun 2019-11-10 21:59:17 IST, use 'shutdown -c' to cancel.
運行下列命令,啟用shutdown/restart命令。
- # systemctl disable reboot-guard
參考:Red Hat網(wǎng)頁。
方法3:如何使用reboot-guard實用程序防止在Linux上意外關(guān)閉或重啟
阻止systemd啟動的poweroff/reboot/halt目標(biāo),直到可配置條件檢查通過。
它只與Python 2兼容,所以確保你在系統(tǒng)上安裝了Python 2。我在CentOS 8上進行了測試,系統(tǒng)之前未安裝Python 2,于是我安裝了。
將reboot-guard實用程序下載到“/usr/sbin”目錄下。
- # cd /usr/sbin
- # curl -kO https://raw.githubusercontent.com/ryran/reboot-guard/master/rguard
- # chmod +x rguard
運行下列命令,讓rguard實用程序能夠阻止reboot/shutdown。
- # rguard -1
- WARNING: ☹ Blocked poweroff.target
- WARNING: ☹ Blocked reboot.target
- WARNING: ☹ Blocked halt.target
創(chuàng)建rguard應(yīng)用程序的測試用例。
針對重啟
- # systemctl reboot
- 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).
- Failed to start reboot.target: Operation refused, unit reboot.target may be requested by dependency only (it is configured to refuse manual start/stop).
- See system logs and 'systemctl status reboot.target' for details.
針對關(guān)機
- # systemctl poweroff
- 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).
- Failed to start poweroff.target: Operation refused, unit poweroff.target may be requested by dependency only (it is configured to refuse manual start/stop).
- See system logs and 'systemctl status poweroff.target' for details.
至于reboot、init 0和init 6之類的老式工具,我沒看到什么影響,但shutdown命令顯示了下列輸出。然而,這其實并不關(guān)閉系統(tǒng)。
- # reboot
- # init 6
- # poweroff
- # init 0
- # shutdown
- Shutdown scheduled for Sun 2019-11-10 23:46:24 IST, use 'shutdown -c' to cancel.
運行下列命令以禁用rguard實用程序。
- # rguard -0
- WARNING: ☻ Unblocked poweroff.target
- WARNING: ☻ Unblocked reboot.target
- WARNING: ☻ Unblocked halt.target
方法4:如何使用systemctl命令防止在Linux上意外關(guān)閉或重啟
此外,可以使用systemctl命令來掩蓋服務(wù)。掩蓋服務(wù)可阻止服務(wù)被人工或自動啟動。
掩蓋下列單元以阻止意外重啟/關(guān)閉。
- # systemctl mask reboot.target
- Created symlink /etc/systemd/system/reboot.target → /dev/null.
- # systemctl mask poweroff.target
- Created symlink /etc/systemd/system/poweroff.target → /dev/null.
- # systemctl mask halt.target
- Created symlink /etc/systemd/system/halt.target → /dev/null.
針對重啟 mctl reboot
- Failed to reboot system via logind: Access denied
- Failed to start reboot.target: Unit reboot.target is masked.
- # syste
針對關(guān)機
- # systemctl poweroff
- Failed to power off system via logind: Access denied
- Failed to start poweroff.target: Unit poweroff.target is masked.
至于reboot、poweroff、init 0和init 6之類的老式工具,我沒看到什么影響,但shutdown命令顯示了下列輸出。然而,這其實并不關(guān)閉系統(tǒng)。
- # reboot
- # init 6
- # poweroff
- # init 0
- # shutdown
- Shutdown scheduled for Sun 2019-11-10 23:59:09 IST, use 'shutdown -c' to cancel.
運行下列命令來啟用它們。
- # systemctl unmask reboot.target
- Removed /etc/systemd/system/reboot.target.
- # systemctl unmask poweroff.target
- Removed /etc/systemd/system/poweroff.target.
- # systemctl unmask halt.target
- Removed /etc/systemd/system/halt.target.
方法5:如何使用alias命令防止在Linux上意外關(guān)閉或重啟
此外,可以創(chuàng)建一個別名來阻止這種情況。
- # vi .bashrc
- alias reboot="echo -e 'Is \033[1;31m$HOSTNAME\033[0m the correct hostname you want to restart?' If yes, run /sbin/reboot"
- alias shutdown="echo -e 'Is \033[1;31m$HOSTNAME\033[0m the correct hostname you want to shutdown?' If yes, run /sbin/shutdown"
運行下列命令使這個變更生效。
- # source .bashrc
現(xiàn)在測試這些命令,靜觀結(jié)果。
- # shutdown
- Is CentOS6.2daygeek.com the correct hostname you want to shutdown? If yes, run /sbin/shutdown
- # reboot
- Is CentOS6.2daygeek.com the correct hostname you want to restart? If yes, run /sbin/reboot
原文標(biāo)題:5 Methods to Avoid or Prevent Accidental Shutdown or Reboot on Linux,作者:Magesh Maruthamuthu
【51CTO譯稿,合作站點轉(zhuǎn)載請注明原文譯者和出處為51CTO.com】