1

plz explain why use visudo to edit /etc/sudoers file and not vi /etc/sudoers and how does the file get corrupt. How can we restore the corrupt file?

3
  • 1
    This is a similar question answered in a different site. The answer is also relevant for Ubuntu unix.stackexchange.com/questions/27594/… – Katu Oct 31 '18 at 10:54
  • If you use visudo, a validation of the file is done before you are returned to shell, allowing many bad typos or errors to be caught and allow them to be fixed (ie. you are returned to editor). This prevents the user being unable to use sudo to correct those errors (ie. used sudo vi in a latter command) if they are later detected. I usually used sudo vi myself, but am sure to have an escape (fix) path in place to allow correction (eg. another connection logged into root active etc). Do you have root enabled? If so login using it & edit the file (correcting your typos) using that. – guiverc Oct 31 '18 at 11:39
  • @guiverc please don't post answers in comments. – terdon Oct 31 '18 at 11:40
0

If you use visudo, a validation of the file is done before you are returned to shell, allowing many bad typos or errors to be caught (in the validation check), returning you to editor to have another chance to fix them.

This prevents the user from finding out about errors later, and being unable to use sudo vi to correct those errors in later commands because of easily-missed typos detected too late.

I commonly use sudo vi myself, but am sure to have an escape route in place to allow correction (eg. another connection logged into root active in case I make errors whilst I validate it myself etc).

Do you have root enabled? If so login using it & edit the file (correcting your typos) using root.

If you have more than one user with sudo privileges, you may find an earlier sudo privileged user (as defined in the file) can still use their sudo (thus allowing editing). This has worked for me in the past, so I assume the part of the file after the error isn't read (but this may have changed - it was years ago I found it saved me)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.