4

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

10.152.2.10

/home/oracle/export/files.txt

User : deploy

Server B

10.152.2.11

/home/oracle/import/

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

#!/bin/sh
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...

1
  • 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, 2009 at 22:21

2 Answers 2

10

You can make a wrapper around ssh, like this:

#!/usr/bin/perl
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.

1
  • can you decrit what the perl script does? Is is possible to have a push bash script instead?
    – Juh_
    Nov 2, 2023 at 16:08
0

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.

1
  • 3
    SSH is likely disabled for root.
    – Jeremy L
    Dec 25, 2009 at 15:50