Centos8中恢復(fù)根目錄為默認(rèn)權(quán)限
本文中介紹如何從意外運(yùn)行# chmod -R 777 /命令,在Centos8操作系統(tǒng)上恢復(fù)默認(rèn)權(quán)限。
本文中我們將故意在一個(gè)測(cè)試服務(wù)器上運(yùn)行chmod 777命令,并嘗試通過(guò)運(yùn)行兩個(gè)命令進(jìn)行恢復(fù)。就是如下兩條命令:
- # rpm --setugids -a
- # rpm --setperms -a
當(dāng)再測(cè)試機(jī)上面執(zhí)行下面命令之后,所有文件權(quán)限都會(huì)變成777。
- [root@localhost ~]# chmod -R 777 /
列出根目錄下面的內(nèi)容:
- [root@localhost ~]# ls -al /
下面是SSH相關(guān)的重要文件,需要有正確的權(quán)限和所有權(quán)。但是,由于運(yùn)行chmod 777,以下所有文件都具有錯(cuò)誤的權(quán)限。
- [root@localhost ~]# ll /etc/ssh/
- total 588
- -rwxrwxrwx. 1 root root 563386 May 11 2019 moduli
- -rwxrwxrwx. 1 root root 1727 May 11 2019 ssh_config
- drwxrwxrwx. 2 root root 28 Dec 29 2019 ssh_config.d
- -rwxrwxrwx. 1 root root 4444 May 11 2019 sshd_config
- -rwxrwxrwx. 1 root ssh_keys 480 Dec 29 2019 ssh_host_ecdsa_key
- -rwxrwxrwx. 1 root root 162 Dec 29 2019 ssh_host_ecdsa_key.pub
- -rwxrwxrwx. 1 root ssh_keys 387 Dec 29 2019 ssh_host_ed25519_key
- -rwxrwxrwx. 1 root root 82 Dec 29 2019 ssh_host_ed25519_key.pub
- -rwxrwxrwx. 1 root ssh_keys 1799 Dec 29 2019 ssh_host_rsa_key
- -rwxrwxrwx. 1 root root 382 Dec 29 2019 ssh_host_rsa_key.pub
帶有chmod 777權(quán)限的SSH
下面嘗試通過(guò)SSH遠(yuǎn)程登錄服務(wù)器。由于主機(jī)密鑰文件權(quán)限都變成了777了,所以登錄會(huì)被拒絕。
下面再終端中ssh登錄本機(jī)出現(xiàn)下面問(wèn)題:
下面使用Xshell登錄服務(wù)器,同樣不能登錄成功:
- [C:\~]$ ssh root@192.168.43.131
- Connecting to 192.168.43.131:22...
- Connection established.
- To escape to local shell, press 'Ctrl+Alt+]'.
- Connection closing...Socket close.
- Connection closed by foreign host.
- Disconnected from remote host(192.168.43.131:22) at 10:28:06.
- Type `help' to learn how to use Xshell prompt.
恢復(fù)權(quán)限
需要恢復(fù)權(quán)限,我們需要加載Centos8的系統(tǒng)鏡像,開(kāi)機(jī)啟動(dòng)光盤(pán)鏡像:
在VMware Workstation中,加載光盤(pán),并開(kāi)機(jī)器用。開(kāi)機(jī)按F2,進(jìn)入BIOS,切換到Boot選項(xiàng)卡。將CD-ROM Drive移動(dòng)到Hard Drive上面。按F10保存并重啟。
選擇Troubleshooting,然后選擇進(jìn)入救援模式。
當(dāng)進(jìn)入下面界面時(shí),選擇1,直接進(jìn)入shell界面。
使用chroot命令將/mnt/sysroot切換為根目錄:
- sh-4.4# chroot /mnt/sysroot
使用以下兩個(gè)命令運(yùn)行,以恢復(fù)所有文件,目錄和配置的權(quán)限。運(yùn)行此命令時(shí),它將引發(fā)權(quán)限被拒絕錯(cuò)誤,并且無(wú)法訪問(wèn)錯(cuò)誤。只是忽略錯(cuò)誤。
- # rpm --setugids -a
- # rpm --setperms -a
參數(shù)說(shuō)明:
--setugids - 設(shè)置RPM包文件的用戶/組所有權(quán)。
--setperms - 設(shè)置RPM包文件的權(quán)限。
-a - 適用于所有已安裝的RPM軟件包。
完成操作之后,退出,并重啟:
- # exit
- # reboot
查看權(quán)限、SSH連接服務(wù)器測(cè)試
下面登錄系統(tǒng)之后,看一下根目錄的權(quán)限是否恢復(fù)正常:
- # ls -l /
運(yùn)行ssh登錄,發(fā)現(xiàn)不能登錄。使用netstat -tlunp發(fā)現(xiàn)ssh端口沒(méi)有監(jiān)聽(tīng)中,使用systemctl status sshd發(fā)現(xiàn)服務(wù)沒(méi)有啟動(dòng),啟動(dòng)sshd服務(wù)時(shí)發(fā)現(xiàn)不能啟動(dòng),下面查找原因:
發(fā)現(xiàn)密鑰文件的權(quán)限還是777,沒(méi)有還原:
下面設(shè)置密鑰文件權(quán)限:
- # chmod 644 /etc/ssh/ssh_config
- # chmod 600 /etc/ssh/sshd_config
- # chmod 640 /etc/ssh/ssh_host_rsa_key /etc/ssh/ssh_host_ecdsa_key /etc/ssh/ssh_host_ed25519_key
- # chmod 644 /etc/ssh/ssh_host_rsa_key.pub /etc/ssh/ssh_host_ecdsa_key.pub /etc/ssh/ssh_host_ed25519_key.pub
下面再次啟動(dòng)sshd試一下:
- # systemctl enable sshd --now
- # netstat -tlunp
可以看到啟動(dòng)成功了。ssh遠(yuǎn)程登錄試一下,看到登錄成功了。
總 結(jié)
就這樣,我們已經(jīng)成功還原了已安裝的RPM軟件包的權(quán)限并恢復(fù)了服務(wù)器。切勿在任何文件系統(tǒng)或配置上使用chmod 777。
本文轉(zhuǎn)載自微信公眾號(hào)「Linux就該這么學(xué)」,可以通過(guò)以下二維碼關(guān)注。轉(zhuǎn)載本文請(qǐng)聯(lián)系Linux就該這么學(xué)公眾號(hào)。