Menu
User

DropVPS Team

Writer: Cooper Reagan

how to edit sudoers file in linux?

how to edit sudoers file in linux?

Publication Date

06/12/2025

Category

Articles

Reading Time

3 Min

Table of Contents

To safely edit the sudoers file in Linux, you should never open it with a regular text editor like nano or vim. Instead, always use the built-in visudo command, which prevents syntax errors that can lock you out of sudo access.

Use visudo

Open your terminal and run:

sudo visudo

This opens the /etc/sudoers file in a safe environment where syntax errors are caught before saving.

Depending on your default editor, you’ll either see it open in nano, vi, or something else. If you prefer a specific editor like nano:

sudo EDITOR=nano visudo

Add User to Sudoers File

Let’s say you want to give a user called john sudo privileges. Scroll down and find the line that looks like:

root    ALL=(ALL:ALL) ALL

Now add below it:

john    ALL=(ALL:ALL) ALL

This gives the john user full sudo access.

Give Sudo Access Without Password

If you want john to run sudo commands without entering a password, modify the line like this:

john    ALL=(ALL:ALL) NOPASSWD:ALL

Include Custom Sudoers Files

Instead of modifying /etc/sudoers directly, a better practice is to create a file in the /etc/sudoers.d/ directory.

Example:

sudo nano /etc/sudoers.d/john

Inside that file, add:

john    ALL=(ALL:ALL) NOPASSWD:ALL

Then validate syntax with:

sudo visudo -c

Lock Down Permissions

Make sure the custom file has correct permissions:

sudo chmod 0440 /etc/sudoers.d/john

Never use chmod 777 or any relaxed permissions on sudoers files—it’s a security risk and can break sudo entirely.

Avoid Common Mistakes

  • Never edit /etc/sudoers with nano /etc/sudoers

  • Always test changes with visudo -c

  • Backup your access with another terminal session when editing sudoers

  • Don’t mix tabs and spaces randomly; visudo is strict

If your system gets misconfigured, you may need to boot into recovery mode or use a live CD to fix sudo access manually—something you definitely want to avoid.

Linux VPS
U
Loading...

Related Posts