How do I copy files that need root access with scp?
Solution 1:
You're right, there is no sudo
when working with scp
. A workaround is to use scp
to upload files to a directory where your user has permissions to create files, then log in via ssh and use sudo
to move/copy files to their final destination.
scp -r folder/ [email protected]:/some/folder/you/dont/need/sudo
ssh [email protected]
$ sudo mv /some/folder /some/folder/requiring/perms
# YOU MAY NEED TO CHANGE THE OWNER like:
# sudo chown -R user:user folder
Another solution would be to change permissions/ownership of the directories you uploading the files to, so your non-privileged user is able to write to those directories.
Generally, working in the root
account should be an exception, not a rule - the way you phrasing your question makes me think maybe you're abusing it a bit, which in turn leads to problems with permissions - under normal circumstances you don't need super-admin privileges to access your own files.
Technically, you can configure Ubuntu to allow remote login directly as root
, but this feature is disabled for a reason, so I would strongly advice you against doing that.
Solution 2:
Quick way
From server to local machine:
ssh user@server "sudo cat /etc/dir/file" > /home/user/file
From local machine to server:
cat /home/user/file | ssh user@server "sudo tee -a /etc/dir/file"
Solution 3:
Another method is to copy using tar + ssh instead of scp:
tar -c -C ./my/local/dir \
| ssh [email protected] "sudo tar -x --no-same-owner -C /var/www"