Tell me more ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I'm writing a script which is to log onto a bunch of remote machines and run a command on them. I've set up keys so the user running the script does not have to type the password of each machine, but only type in the passphrase in the beginning of the script.

The problem is that the command on the remote machines requires sudo to run. And at the same time the whole point of the script is to rid the user of having to type in passwords multiple times. Is there way to avoid typing in the password for sudo? Changing permissions of the command on the remote machines is not an option.

share|improve this question

2 Answers

up vote 6 down vote accepted

For sudo you can allow a user to run sudo without asking for the password, try man sudoers. You can edit the file /etc/sudoers by issuing the visudo command. It has to be that special because otherwise the file is not correctly reloaded. The resulting lines (here taken from the examples in the file itself) should be:

## Allows people in group wheel to run all commands
# %wheel    ALL=(ALL)   ALL

## Same thing without a password
%wheel  ALL=(ALL)   NOPASSWD: ALL
share|improve this answer
Thank you. It's funny since my colleague proposed a solution in the same lines as this, just before you posted your answer. Can you add that the file to edit is /etc/sudoers and it can also be edited by running sudo /usr/sbin/visudo – Wesho Feb 25 '10 at 10:07
6  
If you know the command you want to run, you might not want to specify ALL commands.. Bit of a security hole, y'know. – Tom O'Connor Feb 25 '10 at 10:43
2  
@Tom O'Connor: Yep, I provided only the needed command. Will put an example here for the sake of completeness: %wesho ALL=NOPASSWD: /sbin/service httpd – Wesho Feb 25 '10 at 10:52
Ah, fair enough. :) – Tom O'Connor Feb 25 '10 at 11:40

@Wesho,

You can do what DaDaDom said (it will work and it is simple) or your may want to beef up your setup by using a PAM module called pam-ssh-agent-auth.

The process for Debian/Ubuntu systems is reasonably simple:

$ sudo aptitude install libssl-dev libpam0g-dev build-essential checkinstall
$ wget "http://downloads.sourceforge.net/project/pamsshagentauth/pam_ssh_agent_auth/v0.9.3/pam_ssh_agent_auth-0.9.3.tar.bz2"
$ tar -xjvf pam_ssh_agent_auth-0.9.3.tar.bz2
$ cd pam_ssh_agent_auth-0.9.3

$ ./configure --libexecdir=/lib/security --with-mantype=man

$ make
$ sudo checkinstall

The edit the sudo configuration:

$ sudo visudo

Add the following:

Defaults env_keep += SSH_AUTH_SOCK

Continue by changing the sudo PAM settings:

$ sudo vi /etc/pam.d/sudo

Add the auth line just above the 2 existing @include lines:

auth [success=2 default=ignore] pam_ssh_agent_auth.so file=~/.ssh/authorized_keys
@include common-auth
@include common-account

VoilĂ !

sudo with no auth but relying on SSH Agent to perform strong authentication, instead of simply removing the password from the sudo configuration.

share|improve this answer
You may also want to check this – Andre de Miranda Aug 21 '11 at 5:26
authorized_keys MUST be locked down so only root can change it - otherwise you might as well disable the password. – ijk Feb 20 '12 at 23:58
explanation of success=2 syntax – ijk Feb 21 '12 at 0:05

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.