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 sudoers
orcat > sudoers
? With greatsudo
comes great responsibility.sudoers
file are# # This file MUST be edited with the 'visudo' command as root. #
vi
vsnano
, butvisudo
vs any other means of editing the file.visudo
can usenano
or whatever Editor is set as default.visudo
checks that/etc/sudoers
has valid syntax after editing, no matter what editor is used, and refuses to replace it if it's invalid.sudo "$EDITOR" /etc/sudoers
does 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 usesu
instead).