How do I sudo a command in a script without being asked for a password?


I have a script that uses two commands that require sudo. I'd like to run the script like this: sudo But I don't want to be prompted for a password. I have tried editing /etc/sudoers by adding the line: "user ALL=(ALL:ALL) NOPASSWD: /home/user/", but it still prompts me for a password. I've also tried adding the commands to /etc/sudoers, but it ignores all but the last command. E.g. if I enter: "user ALL=(ALL:ALL) NOPASSWD: command1" and on the next line "user ALL=(ALL:ALL) NOPASSWD: command2", it will only run command2 without asking for a password.

UPDATE: Actually, adding those two commands to /etc/sudoers does work. Earlier, I tried "user ALL=NOPASSWD: command", which worked as described...

0 0

‘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 the visudo 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...

0 0

Using wildcards in the sudoers file

In the sudoers file, you can use wildcards (*), thus creating the possibility to run a script with arguments.

An example

I tested it with a shockingly simple python script. In the sudoers file I had to use the absolute path to python: /usr/bin/python in the line of the sudoers file:

jacob ALL=NOPASSWD: /usr/bin/python /home/jacob/Bureaublad/*

Then my (python script) code was:

#!/usr/bin/python import sys s1 = sys.argv[1] s2 = sys.argv[2] print(s1) print(s2)

Then I ran in a terminal:

~$ sudo python '/home/jacob/Bureaublad/' monkey banana monkey banana

without asking for my password.

Similarly, you should be able to achieve what you want by adding the following line to the sudoers file:

ALL=NOPASSWD: /path/to/java *

And run the script by:

sudo java

and you will not be asked for your password.

More information

An interesting source on how to...

0 0

Question: How can I use sudo without entering a password on Linux?

sudo allows a system administrator to grant certain users (or groups) a permission to run privileged commands on Linux system. In order to run a command with sudo, users have to type their own password at a password prompt.

To allow a specific user to use sudo without a password, use visudo command to edit sudo configuration as follows. I assume that the user name for whom you want to disable password prompt is "alice".

$ sudo visudo


Ctrl-X to leave, save your changes, and you're done. This setting will disable a password prompt for alice when she runs any command in sudo.

To allow a user to use sudo passwordlessly for specific commands only (e.g., /usr/sbin/tcpdump and /sbin/reboot), do the following.

$ sudo visudo

alice ALL=(ALL) NOPASSWD: /usr/sbin/tcpdump, /sbin/reboot

To allow all members of a specific group (e.g.,...

0 0

The sudo Terminal command can be used by administrators to execute commands as a different user (for example, as root). When executing this command, you will be prompted to enter the password for the administrator account you are currently logged in as.

In Mac OS X v10.5 through 10.5.8, if you press the Return key at the password prompt without entering a password (even if the user has no password, which is not recommended), the command entered will not execute and you will be returned to a command prompt.

In Mac OS X v10.6 or later, if you press the Return key at the password prompt without entering a password, the message "Sorry, try again." will be displayed and you will be prompted for a password again.

If your administrator account has no password (a blank password), you must give that user a password before using the sudo command.

After you are done using the sudo command, you can change your account password again, although it is recommended that...

0 0
Post by underh20

Hello all,

Our server is running Solaris 10.

I need to start up and shut down Websphere using the script (see end)

via "wasadm" user / "wasgroup" group without prompting for password.

I have the following entries in the sudoers file. Unfortunately, it's

been giving

me compilation errors. Any idea how I can fix this and make the

script using sudo

without asking for password ?

Thanks for your kind assistance, Bill




# Host alias specification


Host_Alias WS=atlantis,


# User alias specification


User_Alias WEBUSER=ws1boy


# Cmnd alias specification


Cmnd_Alias SU_WAS= /bin/su - wasadm

Cmnd_Alias ...

0 0

Sudo is well known for its ability to provide very limited scope superuser privileges to otherwise normal users on Unix systems. Thus the name "sudo" (for "superuser do"). Users who run commands with sudo generally have to enter their own passwords to confirm their identities before sudo will run the commands with the authority of root. But this doesn't always have to be the case. Let's take a look at what you have to do to configure exceptions to this behavior. How do you allow a particular user to run a particular command via sudo without having to enter a password?

First, why might you want to do this? Well, you might want to put a sudo command in a script. If you do, you could send the password to sudo with an "echo password | sudo -S command" type of construct, but putting passwords in scripts is generally frowned on by the security-conscious admins among us.

Instead, you could configure the account that will run the script to not required to supply a password...

0 0

In this article, we’re going to discuss Sudo, one of the frequently used commands in Linux. Sudo command stands for “superuser do”, It requires users to authenticate their password several times and asks them for confirmation to execute the commands before checking the file.

Key Benefit of Using this Tip

To avoid the tiresome process of inserting a password every time when you are working in Linux, our simple guidance provides you a solution to bypass the process of entering the password every time. However, you will need to consider certain security issues and consequences for allowing Sudo command without a password.

Run Sudo Command without a Password

We need to take /etc/sudoers file backup by typing the following command –

$ sudo cp /etc/sudoers /root/sudoers.bak

Edit the /etc/sudoers file by typing the visudo command:

$ sudo visudo

The output should be like the below image:

Append/edit the line as follows in the...

0 0

What if you want one user to run a command as an another system user without exchanging passwords. For example, you may want an user john to run a find command or custom bash shell script as an user greg or even as a user root ( superuser ) without password exchange. In this case a sudo utility with its /etc/sudoers configuration file will be your friend. This utility is very widely used but at the same time very little understood by Linux users of all levels.

This short article describes some basic of sudo usage and format of sudoers configuration file.

First we need to make sure that sudo and /etc/sudoers the sudo configuration file is available. To do that run:

$ which sudo


$ sudo -V

The first command should reveal a location of a sudo binary executable and the second program will output a version number of sudo command its self. The sudo configuration file sudoers is in most cases located in /etc/sudoers. You can use ls command to locate this...

0 0

Please note: Any method which involves putting your login password in plain text, in a command or in a file, is insecure and should NOT be used!

The correct way to do it to setup sudo such that only the one specific command you need, i.e. echo date... > rtc..., is allowed to run WITHOUT needing the password.

Step 1. Create a shell script with just that command

Step 2. Set up sudo to allow to execute without requiring a password

Step 3. Modify your python script to call

Now your script should run without requiring a password AND without compromising the security of your account, your data or your system!

Alternative only for wakealarm (not for general use!):

In this specific case only, since the /sys/class/rtc/rtc0/wakealarm file only controls the wake-up alarm for the system and is otherwise harmless, another alternative to avoid the password is either to take ownership of that file with chown (if...

0 0

Root sudo timeouts are the easiest and safest way of doing this, ill lay out all examples but be warned it is very risky any way you do this although this way is much safer

sudo visudo

This opens an editor and points it to the sudoers file -- Ubuntu defaults to nano, other systems use Vi. You're now a super user editing one of the most important files on your system. No stress!

(Vi specific instructions noted with (vi!). (Ignore these if you're using nano)

Use the arrow keys to move to the end of the Defaults line.

(vi!) press the A (capital "a") key to move at the end of the current line and enter editing mode (append after the last character on the line).

Now type


where X is the timeout expiration in minutes. If you specify 0 you will always be asked the password. If you specify a negative value, the timeout will never expire. E.g. Defaults env_reset,timestamp_timeout=5 (vi!) hit Escape to return to command mode. Now,...

0 0

The main two commandline possibilities are:

Use su and enter the root password when prompted. Put sudo in front of the command, and enter your password when prompted.

sudo (preferred when not running a graphical display)

This is the preferred method on most systems, including Ubuntu, Linux Mint, (arguably) Debian, and others. If you don't know a separate root password, use this method.

Sudo requires that you type your own password. (The purpose is to limit the damage if you leave your keyboard unattended and unlocked, and also to ensure that you really wish to run that command and it wasn't e.g. a typo.) It is often configured to not ask again for a few minutes so you can run several sudo commands in succession.


sudo service apache restart

If you need to run several commands as root, prefix each of them with sudo. Sometimes, it is more convenient to run an interactive shell as root. You can use sudo -i for that:

$ sudo -i #...
0 0

I discovered elevate today which "executes a command with UAC privilege elevation. This is useful for working inside command prompts or with batch files." It's not the same as sudo, it changes the executing user to Administrator, but its syntax is a lot more straightforward to use than runas, and it can keep the current directory, enabling the use of relative paths.

Synopsis: elevate [(-c | -k) [-n] [-u]] [-w] command Options: -c Launches a terminating command processor; equivalent to "cmd /c command". -k Launches a persistent command processor; equivalent to "cmd /k command". -n When using -c or -k, do not pushd the current directory before execution. -u When using -c or -k, use Unicode; equivalent to "cmd /u". -w Waits for termination; equivalent to "start /wait command".

Elevate's purpose isn't to work around or bypass UAC (User Account Control), but to work with it. As long as UAC is enabled there has to be some kind of prompt at some point in the process. If you...

0 0


I have tried the above in an Ubuntu OS. It does not work and even with many other variations made to the sudoers file, I am always asked for a password when I execute sudo command.

Which version of Ubuntu are you using? It flawlessly works from at least 7.04.

8.0x Server Version I will try your fix again on Friday. thanks

I overlooked the fact that it must be at the end of the file. In my version, the last line of code gives all users in the admin group root access but with a password required. If your line precedes this, then the effects are undone by the last line of code because my user login is also a member (unintentional) of admin.

Hi, i tried previous command and it works. I have Ubuntu Jaunty 9.04 alpha 6

hai its working fine in Kubuntu 8.04 sudo visudo jaleel ALL=NOPASSWD: ALL save and reboot thanks a lot

'ya dont need to reboot..


0 0

sudo can do just that for you :)

It needs a bit of configuration though, but once done you would only do this:

sudo -u user2 -s

And you would be logged in as user2 without entering a password.


To configure sudo, you must edit its configuration file via: visudo. Note: this command will open the configuration using the vi text editor, if you are unconfortable with that, you need to set another editor (using export EDITOR=) before executing the following line. Another command line editor sometimes regarded as easier is nano, so you would do export EDITOR=/usr/bin/nano. You usually need super user privilege for visudo:

sudo visudo

This file is structured in different section, the aliases, then defaults and finally at the end you have the rules. This is where you need to add the new line. So you navigate at the end of the file and add this:

user1 ALL=(user2) NOPASSWD: /bin/bash

You can replace also /bin/bash by ALL and then you...

0 0

Warning: Directly logging in as root is like playing with fire, because one little typo is enough to lose critical data or make your system unbootable. Note that desktop environments will also function incorrectly if you login to them as root.

See these questions for the reasons behind why sudo is preferred and why root-login is disabled by default:

What are the benefits of sudo over su? Why is there no option to login as root?

You may have noticed that you can’t login to root account on Ubuntu, this is because root doesn’t actually have a password set.

Type the following command:

sudo passwd

The following message will appear:

[sudo] password for [username]: [Type your user password and press return]

After that another message will appear, the following:

Type new UNIX password: [Type the root password you want]

Now the last thing you need to do, the following:

Retype new UNIX password: [Retype the root...

0 0