Note: I would not do the following. It is not safe, and also there are enough other possibilites to destroy your system. Why exactly picking this one?
With great sudo rights, comes great responsibility.
However, to keep even root from editing sudoers file, you can make it immutable by running:
sudo chattr +i /etc/sudoers
Then add the followign aliases to your .rc or .profile file, e.g. .bashrc:
alias visudo="sudo chattr -i /etc/sudoers; sudo visudo; sudo chattr +i /etc/sudoers;"
alias sudo='sudo '
It will change visudo command to first reset immutable flag, then run visudo , at the end make it immutable again. See here why we need the second alias.
After resourcing the file or restarting the shell, you can use sudo visudo as normal, but not edit it otherwise.
This is just the general idea, it needs to be enhanced to enable other arguments of visudo and the files below /etc/sudoers.d/. Instead of an alias, you could also use a function or script.
Note, this is not really safe:
- the moments you have
visudo running, the file is not immutable
- any
sudo user can run chattr -i to unset the immutable flag at any time
- you train yourself a behavior to not think before doing stuff
rm sudoersorcat > sudoers? With greatsudocomes great responsibility. – pLumo May 05 '21 at 08:29sudoersfile are `#This file MUST be edited with the 'visudo' command as root.
#`
– pLumo May 05 '21 at 08:32cat > sudoers? – Gqqnbig May 05 '21 at 08:40sudo nano /etc/sudoersor maybesudo mcedit /etc/sudoers- what if vi/vim is not "my favorite editor of all times" and I like and have installed something else, for example Mindnight Commander which features "mcedit"? – ivan.ukr May 05 '21 at 08:49vivsnano, butvisudovs any other means of editing the file.visudocan usenanoor whatever Editor is set as default. – pLumo May 05 '21 at 08:54visudochecks that/etc/sudoershas valid syntax after editing, no matter what editor is used, and refuses to replace it if it's invalid.sudo "$EDITOR" /etc/sudoersdoes not do any checking, which means you can easily find yourself with a broken sudoers file (and unable to use sudo to fix it - better hope that root has a password so you can usesuinstead). – cas May 05 '21 at 09:20