How to run sudo command without a password on a Linux or Unix
Source: nixCraft
I‘m a new Unix system user. How do I use sudo
command without a password on a Linux or Unix-like systems?
Some times 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:
- Backup your
/etc/sudoers
file by typing the following command:
sudo cp /etc/sudoers /root/sudoers.bak
- Edit the
/etc/sudoers
file by typing thevisudo
command:
sudo visudo
- Append/edit the line as follows in the
/etc/sudoers
file for user named ‘vivek
‘ to run ‘/bin/kill
‘ and ‘systemctl
‘ commands:
vivek ALL = NOPASSWD: /bin/systemctl restart httpd.service, /bin/kill
- Save and exit the file.
How do I test it?
Simply run /bin/kill to kill any process without a password:
[vivek@server ]$ sudo /bin/kill pid-here
OR
[vivek@server ]$ sudo /bin/systemctl restart httpd.service