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/sudoersfile by typing the following command:
sudo cp /etc/sudoers /root/sudoers.bak
- Edit the
/etc/sudoersfile by typing the
- Append/edit the line as follows in the
/etc/sudoersfile for user named ‘
vivek‘ to run ‘
/bin/kill‘ and ‘
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
[vivek@server ]$ sudo /bin/systemctl restart httpd.service