成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

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

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

[[284180]]

【51CTO.com快譯】大多數(shù)情況下有一臺(tái)JUMP服務(wù)器,Linux管理員無需密碼即可連接到所有其他Linux系統(tǒng)。

你可以一次連接到多個(gè)系統(tǒng)以排除故障。

你在使用多臺(tái)計(jì)算機(jī)時(shí),由于某個(gè)原因想重啟系統(tǒng),但可能重啟的是錯(cuò)誤的系統(tǒng),而不是實(shí)際的系統(tǒng)。

如果是非生產(chǎn)服務(wù)器,沒問題。但如果你同時(shí)重啟一臺(tái)重要的生產(chǎn)服務(wù)器,就要考慮清楚情況。本人不小心重啟過幾次。

這種情況下,如何防止意外關(guān)閉或重啟Linux系統(tǒng)呢?

沒錯(cuò),我們有一些方法可以防止這種情況,下面作詳細(xì)介紹。

方法1:如何使用molly-guard實(shí)用程序防止在Linux上意外關(guān)閉或重啟

molly-guard這個(gè)簡(jiǎn)單的應(yīng)用程序通過詢問主機(jī)名來保護(hù)機(jī)器免受意外關(guān)閉和重啟。 molly-guard主要用于保護(hù)SSH連接。

它僅適用于基于Debian的系統(tǒng),該項(xiàng)目已有多年未更新,但仍可以正常使用。

molly-guard如何工作?molly-guard安裝shell腳本,該腳本覆蓋現(xiàn)有的shutdown、restart、halt和poweroff等命令。

它運(yùn)行/etc/molly-guard/run.d/中一組可用的腳本,在molly-guard執(zhí)行實(shí)際命令之前,所有腳本都需要成功退出(它有幾道檢查機(jī)制)。

腳本先檢查是否從SSH執(zhí)行命令。如果是,shell腳本提示你輸入要執(zhí)行該功能的主機(jī)名,以防止意外關(guān)閉和重啟。

molly-guard將實(shí)際的二進(jìn)制文件轉(zhuǎn)移到/lib/molly-guard/。你可以通過直接運(yùn)行那些二進(jìn)制文件來繞過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)建測(cè)試用例

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

  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)機(jī)。 

  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 

運(yùn)行下列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)建測(cè)試用例。

針對(duì)重啟 

  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. 

針對(duì)關(guān)機(jī) 

  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命令顯示了下列輸出。然而,這其實(shí)并不關(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. 

運(yùn)行下列命令,啟用shutdown/restart命令。

  1. # systemctl disable reboot-guard 

參考:Red Hat網(wǎng)頁。

方法3:如何使用reboot-guard實(shí)用程序防止在Linux上意外關(guān)閉或重啟

阻止systemd啟動(dòng)的poweroff/reboot/halt目標(biāo),直到可配置條件檢查通過。

它只與Python 2兼容,所以確保你在系統(tǒng)上安裝了Python 2。我在CentOS 8上進(jìn)行了測(cè)試,系統(tǒng)之前未安裝Python 2,于是我安裝了。

將reboot-guard實(shí)用程序下載到“/usr/sbin”目錄下。 

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

運(yùn)行下列命令,讓rguard實(shí)用程序能夠阻止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)用程序的測(cè)試用例。

針對(duì)重啟 

  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. 

針對(duì)關(guān)機(jī) 

  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命令顯示了下列輸出。然而,這其實(shí)并不關(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. 

運(yùn)行下列命令以禁用rguard實(shí)用程序。 

  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ù)被人工或自動(dòng)啟動(dòng)。

掩蓋下列單元以阻止意外重啟/關(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

針對(duì)重啟 mctl reboot  

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

針對(duì)關(guān)機(jī) 

  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命令顯示了下列輸出。然而,這其實(shí)并不關(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. 

運(yùn)行下列命令來啟用它們。 

  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)建一個(gè)別名來阻止這種情況。 

  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" 

運(yùn)行下列命令使這個(gè)變更生效。

  1. # source .bashrc 

現(xiàn)在測(cè)試這些命令,靜觀結(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譯稿,合作站點(diǎn)轉(zhuǎn)載請(qǐng)注明原文譯者和出處為51CTO.com】

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

2016-12-13 23:08:48

Linux命令

2020-09-01 09:56:26

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

2019-11-27 08:00:00

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

2014-03-27 14:44:58

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

2014-04-01 11:13:32

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

2018-04-27 10:33:56

Linux命令chattr

2022-12-29 08:46:15

IT采購投資

2009-12-23 09:57:31

Linux關(guān)機(jī)或休眠

2022-12-07 11:24:51

首席信息官IT

2013-01-15 10:41:50

2010-04-27 16:07:41

Unix關(guān)機(jī)

2025-04-25 08:55:00

Pod運(yùn)維

2010-01-27 09:53:37

2019-04-15 16:05:55

2022-04-25 17:49:05

云計(jì)算云安全安全

2024-07-09 15:46:56

2020-06-17 10:52:00

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

2020-12-21 09:21:24

微軟Edge瀏覽器

2011-01-04 14:27:50

安裝linux方法

2020-05-28 13:33:30

React Hook前端開發(fā)
點(diǎn)贊
收藏

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

主站蜘蛛池模板: 午夜精品一区二区三区在线视频 | 国产yw851.c免费观看网站 | 国产精品久久久久久亚洲调教 | 国产高清视频在线观看 | 欧美中文字幕一区二区 | 最近日韩中文字幕 | 色婷婷综合久久久中字幕精品久久 | 日本午夜在线视频 | 亚洲精品久久久久久久久久久久久 | 亚洲三级在线观看 | 欧美日韩久久精品 | 不卡在线视频 | 欧美精品一区二区三区在线 | 天天操夜夜艹 | 亚洲欧美成人影院 | 欧美一区二区三区在线观看 | 色综合av| 国产精品亚洲成在人线 | 久久精品一区二区 | 青青草原精品99久久精品66 | 国产av毛片 | 久久精品久久综合 | 成人免费精品 | 久久久成人动漫 | 精品视频导航 | 免费h在线 | 99re在线视频 | 日韩免费一区二区 | 日本久久www成人免 成人久久久久 | 亚洲国产精品99久久久久久久久 | 狠狠干网站 | 在线免费观看a级片 | 日韩欧美在线观看视频网站 | 91精品国产综合久久久亚洲 | 久久草在线视频 | 欧美精品一区在线 | 亚洲成av人片在线观看 | 另类一区 | 亚洲www啪成人一区二区 | 亚洲一二三在线观看 | 欧美日韩国产高清 |