The sudo command in Linux enables regular users to execute tasks that typically require administrative privileges. It functions by allowing users to run commands through sudo, rather than executing them directly. Before the command is executed, the user must authenticate their identity on the system.

By modifying the sudoers configuration file, you can allow specific users or group members to use sudo without needing to enter a password each time they run a command. This can be achieved using the visudo tool from the terminal.

Steps configure passwordless sudo in Linux:

  1. Open your preferred terminal application.
  2. Check your current sudo privileges for your user account.
    user@host:~$ sudo -l
    Matching Defaults entries for user on host:
        env_reset, mail_badpass,
        secure_path=/usr/local/sbin\:/usr/local/bin\:/usr/sbin\:/usr/bin\:/sbin\:/bin\:/snap/bin
    
    User user may run the following commands on host:
        (ALL : ALL) ALL
  3. Use the visudo command to open the sudoers configuration file.
    $ sudo visudo
  4. Add the line ALL = (ALL) NOPASSWD: ALL under your username (or any other user) to grant them the ability to run all commands without entering a password.
    user       ALL = (ALL) NOPASSWD: ALL

    Passwordless sudo could be assigned to a group by replacing the username with %groupname or by replacing it with ALL to allow passwordless sudo for all users in the system.

  5. Save the file and exit visudo.
  6. Verify the sudo privileges for your user account again to ensure the changes were successful.
    user@host:~$ sudo -l
    Matching Defaults entries for user on host:
        env_reset, mail_badpass,
        secure_path=/usr/local/sbin\:/usr/local/bin\:/usr/sbin\:/usr/bin\:/sbin\:/bin\:/snap/bin
    
    User user may run the following commands on host:
        (ALL : ALL) ALL
        (ALL) NOPASSWD: ALL
  7. Test running commands with sudo without having to enter a password.
    user@host:~$ sudo ls -la /root
    total 32
    drwx------  5 root root 4096 Jul 24 05:54 .
    drwxr-xr-x 19 root root 4096 Jul 24 05:53 ..
    -rw-------  1 root root   18 Jul 19 01:57 .bash_history
    -rw-r--r--  1 root root 3106 Aug  6  2018 .bashrc
    drwxr-xr-x  3 root root 4096 Jul 24 05:54 .local
    -rw-r--r--  1 root root  148 Aug  6  2018 .profile
    drwxr-xr-x  3 root root 4096 Apr 22 07:56 snap
    drwx------  2 root root 4096 Apr 22 07:56 .ssh
Discuss the article:

Comment anonymously. Login not required.