• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1924
  • Last Modified:

How to use scp using sudo ?

I am trying to doing scp a file from remote host to local location, file in the remote host has permission -rwx------ and owned by root and also permision in local directory has drwx------ and owned by root. I am trying to do like this to do a scp using ..


sudo scp hostname:/path/to/file.txt /destination/file.txt

but it doesn't seems to work.. as I can't read file on the remote host. How can I achieve it?
0
beer9
Asked:
beer9
  • 3
  • 2
2 Solutions
 
woolmilkporcCommented:
Hi,
 
 your example above should work without a problem! What is the exact  error message you get?

 wmp
0
 
beer9Author Commented:
It is asking me the root user's password of the remote host
0
 
TintinCommented:
Assuming you have set up ssh keys for the root users, then the reason the sudo doesn't work is that your HOME directory isn't changed when you sudo so ssh/scp can't find the root ssh keys.

There is the sudo -i option to simulate logging in as the user, but probably better to use

scp -i /root/.ssh/id_dsa root@hostname:/path/to/file.txt /destination/file.txt

Note, if you are on a Unix flavour where root's home dir is /, change

/root/.ssh/id_dsa root

to

/.ssh/id_dsa root
0
Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

 
woolmilkporcCommented:
Well,
 
 I rather assume that root himself is not allowed (and cannot be allowed) to  ssh to hostname without password (ssh keys).

We could try a multi-step process to achieve what you want.

1) Use ssh and sudo to get the file in question to a readable (non-root) remote location and state.
2) Use just scp to copy the inermediate file to a local directory writeable for you (non-root), then delete it.
3) Use sudo locally to move the file to the final local destination and set appropriate permissions.

1,2) ssh -tt hostname "sudo cp /path/to/file.txt /tmp ; sudo chmod o+r /tmp/file.txt ; scp /tmp/file.txt myhost:/tmp; rm /tmp/file.txt"
3) sudo mv /tmp/file.txt /destination/file.txt ; sudo chmod o-r /destination/file.txt

"myhost" is your local host, "hostname" is the remote host!

Looks very unelegant, but for the moment I see no better way with scp.
 
wmp
 



 

0
 
woolmilkporcCommented:
... seems I missed "sudo" in front of the final "rm" in step (1,2). Sorry!
0
 
beer9Author Commented:
Thank you :-)
0

Featured Post

Take Control of Web Hosting For Your Clients

As a web developer or IT admin, successfully managing multiple client accounts can be challenging. In this webinar we will look at the tools provided by Media Temple and Plesk to make managing your clients’ hosting easier.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now