自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Linux用戶寶典:避免或防止意外關(guān)機或重啟的5種方法

譯文
系統(tǒng) Linux
大多數(shù)情況下有一臺JUMP服務(wù)器,Linux管理員無需密碼即可連接到所有其他Linux系統(tǒng)。你在使用多臺計算機時,由于某個原因想重啟系統(tǒng),但可能重啟的是錯誤的系統(tǒng),而不是實際的系統(tǒng)。這種情況下,如何防止意外關(guān)閉或重啟Linux系統(tǒng)呢?

[[284180]]

【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命令來安裝。

  1. $ sudo apt install molly-guard 

使用molly-guard創(chuàng)建測試用例

我會執(zhí)行重啟和關(guān)閉命令,檢查molly-guard應(yīng)用程序是否按預(yù)期運行。 

  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 ... 

被關(guān)閉后停止或關(guān)機。 

  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 ... 

停止系統(tǒng)。 

  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 ... 

關(guān)閉系統(tǒng)。 

  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應(yīng)用程序無法阻止systemctl shutdown和systemctl reboot命令。

方法2:如何使用systemd定制服務(wù)防止在Linux上意外關(guān)閉或重啟

為此,創(chuàng)建定制服務(wù)來阻止shutdown/restart命令。

創(chuàng)建下列單元文件: 

  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服務(wù)。 

  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服務(wù)創(chuàng)建測試用例。

針對重啟 

  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. 

針對關(guān)機 

  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命令顯示了下列輸出。然而,這其實并不關(guān)閉系統(tǒng)。 

  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網(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”目錄下。 

  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 

創(chuàng)建rguard應(yīng)用程序的測試用例。

針對重啟 

  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. 

針對關(guān)機 

  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命令顯示了下列輸出。然而,這其實并不關(guān)閉系統(tǒng)。 

  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上意外關(guān)閉或重啟

此外,可以使用systemctl命令來掩蓋服務(wù)。掩蓋服務(wù)可阻止服務(wù)被人工或自動啟動。

掩蓋下列單元以阻止意外重啟/關(guān)閉。 

  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

針對關(guān)機 

  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命令顯示了下列輸出。然而,這其實并不關(guān)閉系統(tǒng)。 

  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上意外關(guān)閉或重啟

此外,可以創(chuàng)建一個別名來阻止這種情況。 

  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 

現(xiàn)在測試這些命令,靜觀結(jié)果。 

  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 

原文標(biāo)題:5 Methods to Avoid or Prevent Accidental Shutdown or Reboot on Linux,作者:Magesh Maruthamuthu

【51CTO譯稿,合作站點轉(zhuǎn)載請注明原文譯者和出處為51CTO.com】

責(zé)任編輯:龐桂玉 來源: 51CTO
相關(guān)推薦

2016-12-13 23:08:48

Linux命令

2020-09-01 09:56:26

云端云計算云服務(wù)

2019-11-27 08:00:00

Linux系統(tǒng)用戶管理員

2014-03-27 14:44:58

數(shù)據(jù)丟失防護DLP數(shù)據(jù)保護

2014-04-01 11:13:32

數(shù)據(jù)丟失

2018-04-27 10:33:56

Linux命令chattr

2009-12-23 09:57:31

Linux關(guān)機或休眠

2022-12-29 08:46:15

IT采購投資

2022-12-07 11:24:51

首席信息官IT

2013-01-15 10:41:50

2025-04-25 08:55:00

Pod運維

2010-04-27 16:07:41

Unix關(guān)機

2010-01-27 09:53:37

2019-04-15 16:05:55

2020-06-17 10:52:00

DDoS攻擊網(wǎng)絡(luò)攻擊網(wǎng)絡(luò)安全

2024-07-09 15:46:56

2020-12-21 09:21:24

微軟Edge瀏覽器

2022-04-25 17:49:05

云計算云安全安全

2011-01-04 14:27:50

安裝linux方法

2015-04-30 07:14:58

點贊
收藏

51CTO技術(shù)棧公眾號