Enabling sudo NOPASSWD for a user in Just one command!

Enabling a user to run specific commands without entering a password in the sudoers file can be done using the NOPASSWD directive. This allows the user to execute the commands with elevated privileges without being prompted for their password.

Here is the oneliner (Do only if you know what it means ! )

echo "YourUserName ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers

Enabling sudo no password in sudoers Step by step

Here’s how to do it:

  1. Open the Terminal:
    Open a terminal on the system where you want to modify the sudoers file.
  2. Edit the sudoers File:
    To edit the sudoers file, you should use the visudo command, which provides a safe way to edit the file and prevent syntax errors that could lock you out of administrative access. In the terminal, type:
   sudo visudo
  1. Add NOPASSWD Entry:
    In the sudoers file, you’ll need to add a line to grant the specific user NOPASSWD privileges for certain commands. The syntax is:
   username ALL=(ALL) NOPASSWD: /path/to/command1, /path/to/command2

Replace:

  • username with the actual username you want to grant NOPASSWD privileges to.
  • /path/to/command1 and /path/to/command2 with the full paths to the commands you want the user to be able to run without a password. For example, to allow the user “john” to run the ls and cat commands without entering a password:
   john ALL=(ALL) NOPASSWD: /bin/ls, /bin/cat
  1. Save and Exit:
    After making the necessary changes, save the file and exit the editor.
  2. Test the Configuration:
    To test whether the configuration works as expected, open a new terminal and try running the specified command(s) as the user you’ve configured. You should be able to execute those commands without being prompted for a password.

Remember that editing the sudoers file requires administrative privileges, so ensure you have the necessary permissions before making any changes. Incorrect modifications to the sudoers file can lock you out of administrative access, so double-check your entries and syntax before saving the file.