【Linux】解决chown导致无法连接虚拟机的问题

误操作场景

误执行了命令:chmod 777 .* 或chown -R * ,导致整个操作系统权限混乱,部分命令、设备失效,这时候千万不能重启,想办法把每个文件的权限修改回去就可以了。

执行后状态

  • ping ServiceIP - 能够ping通
  • ssh root@ServiceIP - 报错:ssh_exchange_identification: read: Connection reset by peer
  • 误操作后,没有退出当前用户,su - root报错:认证失败

解决办法

1)建议从root用户切换至其他普通用户
2)解决思路就是:恢复默认权限,换个想法就是通过拷贝其他正常机器的权限复制到本机器。

复制A机器权限:
getmod.sh

#!/bin/bash

getfacl / -R >/root/facl.txt
find / -type f -perm -04000 >/root/file400.txt
find / -type f -perm -02000 >/root/file200.txt
chmod +x getmod.sh

将/root下的文件facl.txt、file400.txt、file200.txt拷贝到B机器的/root目录下:
resumemod.sh

#!/bin/bash

cd /
setfacl --restore=/root/facl.txt
cd /root
chmod +x changemod.sh
./changemod.sh
chmod +x resumemod.sh

执行权限修改:
changemod.sh

#!/bin/bash

for i in `cat /root/file400.txt`
do
    chmod u+s $i
done

for j in `cat /root/file200.txt`
do
    chmod g+s $i
done

chmod o+t /tmp /var/tmp

所有操作执行完毕之后,A机器将与B机器中文件的权限属主组完全相同。
再次登录即可成功!

猜你喜欢

转载自blog.csdn.net/changqing5818/article/details/78829618
今日推荐