I have two programs in Java that I'm calling from bash script:

java program1 arg ... arg
sudo java program2 arg ... arg

I want to run program2 as sudo without being asked for password. I'm aware of sudoers file, but I'm not sure how to configure it to run java programs as sudo without being asked for password. Any thoughts?

1 Answer 1


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/pscript_1.py*

Then my (python script) code was:


import sys

s1 = sys.argv[1]
s2 = sys.argv[2]


Then I ran in a terminal:

~$ sudo python '/home/jacob/Bureaublad/pscript_1.py' 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:

<your_username> ALL=NOPASSWD: /path/to/java <your_script>*

And run the script by:

sudo java <script> <args>

and you will not be asked for your password.

More information

An interesting source on how to use the sudoers file might be this one.

  • @MarkoGulin could you give me some feedback on the answer, did it work for you, did you run into difficulties etc.? Dec 10, 2014 at 8:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .