Linux:远程Ubuntu服务器误修改sudoers 导致无法sudo的解决办法

发布时间 2023-10-08 11:16:54作者: Oontinue

问题复现

本来想用腾讯云测试一下用户权限管理,给test用户设置面密码登录,修改/etc/sudoers如下

test ALL=(ALL:ALL) NOPASSWD: ALL

手抖忘了写NOPASSWD后面的冒号,导致关闭后sudo直接报错

(base) ubuntu@VM-16-11-ubuntu:~$ sudo vim /etc/sudoers
>>> /etc/sudoers: syntax error near line 34 <<<
sudo: parse error in /etc/sudoers near line 34
sudo: no valid sudoers sources found, quitting
sudo: unable to initialize policy plugin

明显/etc/sudoers改错了想重新进入修改,但是发现普通用户想访问/etc/sudoers需要sudo权限,但是这个问题不解决无法sudo,这下完了搞得以后都不能再sudo了


解决方案

先试一下直接pkexec visudo行不行

(base) ubuntu@VM-16-11-ubuntu:/etc$ pkexec visudo
==== AUTHENTICATING FOR org.freedesktop.policykit.exec ===
Authentication is needed to run `/usr/sbin/visudo' as the super user
Authenticating as: ubuntu,,, (ubuntu)
Password: 
polkit-agent-helper-1: error response to PolicyKit daemon: GDBus.Error:org.freedesktop.PolicyKit1.Error.Failed: No session for cookie
==== AUTHENTICATION FAILED ===
Error executing command as another user: Not authorized

This incident has been reported.

不行啊,发现一篇大佬的文章亲测可行记录一下防止以后再犯
https://blog.csdn.net/caijiapeng0102/article/details/84848473

远程ssh登录开两个终端,第一个终端

(base) ubuntu@VM-16-11-ubuntu:/etc$ echo $$
2235

第二个终端

(base) ubuntu@VM-16-11-ubuntu:~$ pkttyagent --process 2235

切回第一个终端

(base) ubuntu@VM-16-11-ubuntu:/etc$ pkexec visudo

再到第二个终端输入密码

(base) ubuntu@VM-16-11-ubuntu:~$ pkttyagent --process 2235
==== AUTHENTICATING FOR org.freedesktop.policykit.exec ===
Authentication is needed to run `/usr/sbin/visudo' as the super user
Authenticating as: ubuntu,,, (ubuntu)
Password: 
==== AUTHENTICATION COMPLETE ===

此时再回到第一个终端开始出现sudoers文件内容了,开始修改加上冒号,然后ctrl+x+y退出,ok了又可以sudo了



作者:xiaogp
链接:https://www.jianshu.com/p/adb555a87bca
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。