Possible Duplicate:
scp to remote server with sudo

What is the best way of doing SCP from one box to the other as a sudo user. There are two servers

Server A


User : deploy

Server B


User : deploy
Sudo user : /usr/local/bin/tester

all i want is to copy files from server A to Server B as a sudo user...

In order to do this, first i normally login as deploy user on the target server and then switch as a sudo user without password.

after that SCP to copy file, this is the normal way i perform this activity...

In order to auotmate i have written script

ssh deploy@lnx120 
sudo /usr/local/bin/tester "./tester/deploy.sh"

I have generated the private key for deploy user, so it allows me to login as deploy user without password. afterthar the sudo command is executed it will switch the user to tester...

after that nothing happens.. i mean the script is not getting executed ... is there any way to accomplish this in a different way...

marked as duplicate by quack quixote May 8 '10 at 0:50

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

migrated from stackoverflow.com Dec 25 '09 at 15:04

This question came from our site for professional and enthusiast programmers.

  • 1
    Please clarify/simplify what you are trying to do. I am finding the use of the term "sudo user" to be ambiguous. It seems that you are trying to automate an scp transfer, but what are the other limiting factors? – pcapademic Dec 25 '09 at 22:21

You can make a wrapper around ssh, like this:

use strict;
my $added = 0;
$added ||= s/^scp /sudo $&/ for @ARGV;
exec "ssh", @ARGV;

Make it executable, and run scp like this:

scp -S ./ssh-wrapper somefile anotherfile hostname.domain.tld:path/

This will only work if sudo on the remote server doesn't require a password (or if it's cached), but better than nothing.


If I understand correctly, you need sudo because the file is somewhere your regular user does not have access to? And you do not have the root password, or do not want to use it for some reason?

One solution would be to copy the file to a location where you regular user has access to (and if necessary change the access rights on the file as well), and then perform the scp.

  • 3
    SSH is likely disabled for root. – Jeremy L Dec 25 '09 at 15:50