My script is coded in a way that doesn't allow you to connect to a server directly by root. This code basically copies files a server to my computer and it works but I don't have access to many files because only root can access them. How can I connect to a server as a user and then copy its files by switching to root?

Code I want to change:

sshpass -p "password" scp -q -r [email protected]:some_directory copy_it/here/

In other words, I want to be able to remotely copy files which are only accessible to root on a remote server, but don't wish to access the remote server via ssh/scp directly as root.

Is it possible through only ssh and not sshpass?

  • Ummm... does changing username to root work?
    – derobert
    May 29, 2014 at 20:53
  • like i mentioned in the description, the script is coded in a way that doesn't allow access directly using root.
    – Redson
    May 29, 2014 at 20:58

2 Answers 2


You better use keys (not password) to automate the remote login.

If you want to get root access you will need password-less "su", or better configure "sudo" to be able to execute commands with root user.

Then you can use "ssh" (not scp) to login and execute commands on remote server. You can use tar/lszrz/whatever to transfer the files then.

Like this:

ssh remote.server.com sudo tar fc - /path/on/the/remote/server | tar fx -

This should copy the /path/on/the/remote/server to your current directory.

EDIT: Keep in mind that allowing your user to sudo tar is not security wise.

  • so are you saying that there may be security issues if you do sudo tar?
    – Redson
    May 29, 2014 at 22:13
  • If you can do 'sudo tar' w/o password, then your user can read/write any system file with the tar. i.e. /etc/passwd and /etc/shadow. Anyway, the whole concept in your OP is permitting this your user to read/write system files.
    – 0xAF
    May 30, 2014 at 5:21
  • Why dont you use rsync ? You will have more control over the permissions.
    – 0xAF
    May 30, 2014 at 8:22
  • I got the structure wrong. Now its fixed. I will post the solution when the website will let me (I have time constraint)
    – Redson
    May 30, 2014 at 20:41

so the approach would be to remotely execute commands which copy the files via sudo to /tmp on the remote machine, changes their owner to the connected user, and then remotely copy them from /tmp:

 ssh [email protected] "sudo cp -R some_directory /tmp"
 ssh [email protected] "sudo chown -R username:username /tmp/some_directory"
 scp -q -r [email protected]:/tmp/some_directory copy_it/here/ 
 ssh [email protected] "rm -r /tmp/some_directory"
  • This is resource hungry. If you're copying some_directory with 10GB data, then you will need 10GB more space on the remote machine and a lot of time to wait for the copy. If the remote machine is using SSD for example, you will shorten it's life every time with this extra copy. You will put a lot of load on the remote machine's IO operations, this will slow down every service on the machine and can(will) overload the machine (with IO operations). I believe that in the general case my option is better, though your case seems to be satisfied with your solution.
    – 0xAF
    Jun 3, 2014 at 20:09
  • Why not setting up an ssh server in your machine, sudoing in the remote and scping to your machine from there?
    – YoMismo
    Dec 4, 2014 at 14:40

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .