Advertisement
v1ral_ITS

sudo with no password always

Jun 16th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.53 KB | None | 0 0
  1. How to run sudo command without a password on a Linux or Unix
  2.  
  3.  
  4. ‘m a new Unix system user. How do I use sudo command without a password on a Linux or Unix-like systems? I log in as tom@my-cloud-server-ip and disabled root login for ssh. After login, I need to run some commands as root user. I am the only sysadmin using my server. How do I run or execute sudo command without a password for a user named Tom under Debian/Ubuntu/CentOS Linux cloud server?
  5.  
  6. 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.
  7.  
  8. By default, sudo needs that a user authenticates using a password before running a command. 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.
  9. How to to run sudo command without a password:
  10.  
  11.     Backup your /etc/sudoers file by typing the following command:
  12.     sudo cp /etc/sudoers /root/sudoers.bak
  13.     Edit the /etc/sudoers file by typing the visudo command:
  14.     sudo visudo
  15.     Append/edit the line as follows in the /etc/sudoers file for user named ‘vivek’ to run ‘/bin/kill’ and ‘systemctl’ commands:
  16.     vivek ALL = NOPASSWD: /bin/systemctl restart httpd.service, /bin/kill
  17.     Save and exit the file.
  18.  
  19. How do I execute ALL sudo commands without password?
  20.  
  21. Type the following command as root user:
  22. # visudo
  23. Or
  24. $ sudo visudo
  25. Append the following entry to run ALL command without a password for a user named tom:
  26.  
  27. tom ALL=(ALL) NOPASSWD:ALL
  28.  
  29. Here is my sample config file:
  30. Fig.01: How to execute sudo without password for tom user
  31. Fig.01: How to execute sudo without password for tom user
  32.  
  33. Save and close the file. Now you can run any command as root user:
  34.  
  35. $ sudo /etc/init.d/nginx restart
  36.  $ sudo /sbin/reboot
  37.  $ sudo apt-get install htop
  38.  ## get root shell ##
  39.  $ sudo -i
  40.  
  41. Please
  42. make sure only tom can login via ssh keys
  43.  
  44. .
  45. How do I test it?
  46.  
  47. Simply run /bin/kill to kill any process without a password:
  48. [vivek@server ]$ sudo /bin/kill pid-here
  49. OR
  50. [vivek@server ]$ sudo /bin/systemctl restart httpd.service
  51.  
  52. For more info read man pages: sudoers(5),visudo(8)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement