tech.log

memo.txt

/etc/sudoersがsyntax errorでsudo使えなくなった時の対処法

ちょっと理由があって/etc/sudoersをいじってて途中で保存しようとしたら、つい勢い余って:wqしてしまって詰んだのでメモ。

$ sudo vim
sudo: >>> /etc/sudoers: syntax error near line 32 <<<
sudo: parse error in /etc/sudoers near line 32
sudo: no valid sudoers sources found, quitting
sudo: unable to initialize policy plugin

元に戻そうにもsudoが使えなくなったから/etc/sudoersを書き換えられなくなってしまって、これはどうしたもんかなと少し困った。
rootでログインすればsudoなしで行けるか、ととりあえずsu -をためしてみるけどrootのパスワード思い出せなかった(これも困るけど)。

'etc sudoers syntax error'とかでググったらこんな解決法っぽいの見つけた。
How to modify a invalid /etc/sudoers file? It throws out an error and not allowing me to edit again

この質問者の人も自分と全く同じ状況のよう。
それに対する解答が以下の通り。

On a modern Ubuntu system (and many other GNU/Linux distributions), fixing a corrupted sudoers file is actually quite easy, and doesn't require rebooting, using a live CD, or physical access to the machine.

To do this via SSH, log in to the machine and run the command `pkexec visudo`. Assuming you (or some other user) are authorized to run programs as root with PolicyKit, you can enter your password, and then it will run visudo as root, and you can fix your /etc/sudoers.

`pkexec visudo`でスーパーユーザとしてvisudoを実行できるらしい。

$ pkexec visudo
==== AUTHENTICATING FOR org.freedesktop.policykit.exec ===
Authentication is needed to run `/usr/sbin/visudo' as the super user
Authenticating as: (本名),,, ((ユーザ名))
Password: 
==== AUTHENTICATION COMPLETE ===

GNU nanoエディタが起動して問題なく/etc/sudoersのエラーを修正することができた。`sudo vim`も出来るようになった。よかった。

pkexecについてはまた今度。