Get your server issues fixed by our experts for a price starting at just 25 USD/Hour. Click here to register and open a ticket with us now!

Author Topic: How to execute sudo without password  (Read 1113 times)

0 Members and 1 Guest are viewing this topic.

akhilt

  • Guest
How to execute sudo without password
« on: July 14, 2018, 11:08:14 am »
How to execute sudo without password

If 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:
   
Code: [Select]
sudo cp /etc/sudoers /root/sudoers.bak2. Edit the /etc/sudoers file by typing the visudo command:
   
Code: [Select]
sudo visudo3. Append/edit the line as follows in the /etc/sudoers file for user named ‘akhil’ to run ‘/bin/kill’ and ‘systemctl’ commands:
   
Code: [Select]
akhil ALL = NOPASSWD: /bin/systemctl restart httpd.service, /bin/kill4. Save and exit the file.

To execute ALL sudo commands without password:

Type the following command as root user:
Code: [Select]
# visudoOr
Code: [Select]
$ sudo visudo
Append the following entry to run ALL command without a password for a user named akhil:
Code: [Select]
akhil ALL=(ALL) NOPASSWD:ALL
Save and close the file. Now you can run any command as root user:

Code: [Select]
$ sudo /etc/init.d/nginx restart
$ sudo /sbin/reboot
$ sudo apt-get install htop
## get root shell ##
$ sudo -i

Thank you all!! 8)