sudo allows a normal user to run commands with administrative priviledge if configured. This works by a user running any of the commands via the sudo itself. This normally requires the user to first authenticate to the system before the command is actually executed.

sudo could be configured so that a select user or group does not need to enter their password to authenticate when running their command via sudo. This could be done by editing the sudoers file using the visudo tool at the terminal.

Setting up passwordless sudo in Linux:

  1. Launch your favorite terminal application.
  2. Display current sudo privilege for your user.
    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. Edit sudoers file using visudo.
    $ sudo visudo
  4. Add ALL = (ALL) NOPASSWD: ALL line to your username (or for any other user) so your user could run all commands without 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. Exit visudo by saving the file.
  6. Check sudo privilege for your user again to confirm changes are 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. Try running commands using sudo and no password should be prompted.
    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.

Share!