Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I have been trying to specify a script that I would like to run without being prompted for a password. After a number of google and stack searches and a number of different syntax changes, I still cannot get it to work. One peculiar thing is that when typing sudo visudo nano is opened to edit the file. I never specified nano as the editor, but I may have first edited the file with nano. Would that make nano the default editor? If so, could that cause problems. Thanks for the help.

Here is my sudoers file.

#
# This file MUST be edited with the 'visudo' command as root.
#
# Please consider adding local content in /etc/sudoers.d/ instead of
# directly modifying this file.
#
# See the man page for details on how to write a sudoers file.
#
Defaults        env_reset
Defaults        mail_badpass
Defaults        secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:$

# Host alias specification

# User alias specification

# Cmnd alias specification

# User privilege specification
root    ALL=(ALL:ALL) ALL

# Members of the admin group may gain root privileges
%admin ALL=(ALL) ALL

# Allow members of group sudo to execute any command
%sudo   ALL=(ALL) ALL
pat     ALL=NOPASSWD: /home/pat/test.sh

# See sudoers(5) for more information on "#include" directives:

#includedir /etc/sudoers.d
share|improve this question
    
So, why exactly do you think that it doesn't work? –  mikewhatever Jan 11 at 20:57
    
When running ./test.sh while in my home directory, I am prompted for a password. –  Pat Herrod Jan 11 at 21:46
    
How about sudo ./test.sh? –  mikewhatever Jan 11 at 21:59
    
Yeah, that works, but the purpose is to have a script with su pat -c ".... The problem is that script cant run with this code unless a user is there to input a password. –  Pat Herrod Jan 12 at 0:50

2 Answers 2

Based on your comments:

Realize that su, unless run from as root, requires you to provide the password of the user to su to. The reason sudo ./test.sh works is, you are running test.sh as root! (sudo defaults to root if you don't give it a -u switch.

So if you want the script to be able to su to another user it must be run as root (don't do this).

Why not just use "sudo -u pat" within the script, instead of "su pat -c"?

share|improve this answer

The default editor of visudo is nano, no problem.

1) You will need to make some changes at your line

pat     ALL=NOPASSWD: /home/pat/test.sh

It should contain the name of the computer and a space before and after the "=":

username computername = NOPASSWD: /path/to/script

Something like this:

pat patcomputer = NOPASSWD: /home/pat/test.sh

2) The script should be owned by root. Run this at terminal:

sudo chown root:root /home/pat/test.sh  

3) Set the right permissions:

sudo chmod 705 /home/pat/test.sh  

4) Enjoy!

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.