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?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

 your example above should work without a problem! What is the exact  error message you get?

beer9Author Commented:
It is asking me the root user's password of the remote host
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


/.ssh/id_dsa root

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

 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.


... seems I missed "sudo" in front of the final "rm" in step (1,2). Sorry!
beer9Author Commented:
Thank you :-)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Unix OS

From novice to tech pro — start learning today.