DEV Community

Smitter
Smitter

Posted on • Edited on

Use SUDO on Linux(Ubuntu,Debian) without password

Linux has, in recent years, become the leading operating system on servers. Many programmers and developers tend to choose Linux OS over the other OSes because it allows them to work more effectively and quickly, considering that, Linux just gives you much more control over your tools, hardware and overall work environment.

Every developer must not use Linux as there are distributions that are really user friendly and you don’t need to have intricate knowledge of the system, but with the mentioned advantages of Linux including its malleability, It will surely be a good reason to give Linux a try.

Most of the times as a programmer using Linux, you will use the command line to perform operations like moving and renaming files, install software, etc. You may as well need to edit configuration files of installed softwares. Installation of software in Linux such as installing MongoDB database locally, without the correct permissions, Linux will deny to perform this action as such an action needs jumped up privileges.
Such an error may be annoying to someone starting out hacking code on Linux

Apt install perm error
Mainly because the requested action needs super user privileges which need to be enabled on your user account.

Granting passwordless root privileges to action



$ sudo apt-get install mongodb-org


Enter fullscreen mode Exit fullscreen mode

By default, doing this will ask for password:
$ [sudo] password for username:

Always having to retype the password can be annoying especially if your password is lengthy.

To avoid always having type password again and again, you just need to add one line of code to this file /etc/sudoers
To edit this file, you need to use visudo command which is advisable for catching possible errors made to the file.



$ sudo visudo /etc/sudoers 


Enter fullscreen mode Exit fullscreen mode

Then enter your password as prompted. Just this time!
[sudo] password for <username>:

You can add the following command at the end of the file(recommended for identification purposes of custom modifications) or elsewhere you find it convenient
<username> ALL=(ALL:ALL) NOPASSWD: ALL

Your resulting file will look like



 GNU nano 5.4                                    /etc/sudoers.tmp *                                           
# See the man page for details on how to write a sudoers file.
#
Defaults        env_reset
Defaults        mail_badpass
Defaults        secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"

# Host alias specification

# User alias specification

# Cmnd alias specification

# User privilege specification
root    ALL=(ALL:ALL) ALL

# Allow members of group sudo to execute any command
%sudo   ALL=(ALL:ALL) ALL

# My configurations
<username>     ALL=(ALL:ALL) NOPASSWD: ALL


Enter fullscreen mode Exit fullscreen mode

Save and exit the file.

Yay! You can now use sudo without typing that annoying lengthy password on your user account. Just that simple!

Note:

  1. Replace <username> with the username you are currently logged in as. You can find out by typing on the terminal: whoami or echo $USER.
  2. You can also avoid using password by editing the /etc/sudoers file with %sudo ALL=(ALL:ALL) NOPASSWD: ALL. The downside of doing this way is that, other users created on the system who are members of sudo, will also be able to execute commands without password, of which you may not want this behavior as it poses threat to security.
  3. If you are still getting a password prompt, consider removing your user account from the sudo group by running sudo deluser <username> sudo

Top comments (0)