How to execute sudo without passwordIf you are the only sysadmin or developer, you can skip password when you run
sudo command. By default,
sudo asks for the password. Here is how you can run
sudo without having to enter your password.
sudo (“superuser do”) is nothing but a tool for Linux or Unix-like systems to run commands/programs as another user. Typically as a root user or another user. You can delegate common tasks such as reboot the server or restart the Apache or make a backup using
sudo for unprivileged users.
By default,
sudo needs that a user authenticates using a password before running a command. Sometimes you may need to run a command with root privileges, but you do not want to type a password using
sudo command. This is useful for scripting or any other purpose. This can be achieved by editing
/etc/sudoers file and setting up correct entries. You need to consider any security consequence of allowing a
sudo command execute without a password.
To run sudo command without a password:1. Backup your
/etc/sudoers file by typing the following command:
sudo cp /etc/sudoers /root/sudoers.bak
2. Edit the
/etc/sudoers file by typing the
visudo command:
sudo visudo
3. Append/edit the line as follows in the
/etc/sudoers file for user named ‘akhil’ to run ‘
/bin/kill’ and ‘
systemctl’ commands:
akhil ALL = NOPASSWD: /bin/systemctl restart httpd.service, /bin/kill
4. Save and exit the file.
To execute ALL sudo commands without password:Type the following command as root user:
# visudo
Or
$ sudo visudo
Append the following entry to run ALL command without a password for a user named akhil:
akhil ALL=(ALL) NOPASSWD:ALL
Save and close the file. Now you can run any command as root user:
$ sudo /etc/init.d/nginx restart
$ sudo /sbin/reboot
$ sudo apt-get install htop
## get root shell ##
$ sudo -i
Thank you all!!