Generally, when you enter sudo command in Linux, you will be asked for a password. But if you need to frequently use sudo or if you are the only user on your system, this can be quite tedious. In this article, we will look at how to run sudo command without password in Linux. You can easily do this via /etc/sudoers file which contains the default security policies to be enforced on sudo users.
How to Run Sudo Command Without Password in Linux
The authentication parameters in sudoers file determine if users need to be authenticated when they use sudo command. It also determines for which commands, they need to be authenticated. By default, it is enabled to ask for password for all sudo commands for all users & groups. It can be overwritten with the following configuration syntax
user_list host_list=effective_user_list tag_list command_list
In the above configuration,
user_list
– users, groups or a user alias that existhost_list
– hosts or a host alias on which users can run sudo.effective_user_list
– users they must be running as or a run as alias.tag_list
– tags such as NOPASSWD.command_list
– commands or a command alias to be run by user(s) using sudo.
To allow a user test_user to to run all commands using sudo but without password, open sudoers file with the following command.
$ sudo visudo
Add the following line
test_user ALL=(ALL) NOPASSWD: ALL
In the above configuration we use the tag NOPASSWD to disable password prompt for the user test_user while running sudo command.
If, instead of a user, you want all members of a specific group to be able to run sudo commands without password, replace username with group name and prefix it with % symbol. Here is an example to allow users of group data to run sudo commands without password.
%data ALL=(ALL) NOPASSWD: ALL
In the above configuration, we mention ALL as the last argument, for command list. It means that no password will be asked for any command run with sudo keyword.
However, if you don’t want to give so much access but want to disable password prompt only for specific command such as /bin/kill then replace ALL at the end with this command.
%data ALL=(ALL) NOPASSWD: /bin/kill
If you want to allow multiple users to run sudo without password, add them in a comma separated manner. Here is an example to disable sudo password for users test_user and read_user.
test_user,read_user ALL=(ALL) NOPASSWD: ALL
Similarly, you can allow them to run multiple commands (/bin/kill and /bin/rm) instead of all commands, by listing them one after the other in a comma-separated manner
test_user,read_user ALL=(ALL) NOPASSWD: /bin/kill,/bin/rm
In fact, you can even combine users and group permissions together in a single line.
test_user,%data ALL=(ALL) NOPASSWD: /bin/kill,/bin/rm
You may even add separate permissions one below the other in sudoers file. It need not have to be all on a single line.
Save and close the file, when you have made all changes. In this article, we have looked at how to allow users and groups to run sudo command without password, by modifying sudoers file. It allows you to edit authentication permissions for one or more users & groups, for one or more, or even all commands.
Also read:
How to Save Dictionary to File in Python
How to Kill Unresponsive Process in Linux
How to Lock User After Failed Login Attempts in Linux
How to Find Failed SSH Login Attempts in Linux
How to Schedule Shutdown in Linux
Related posts:
Sreeram has more than 10 years of experience in web development, Python, Linux, SQL and database programming.