With SCP, you have to do in two steps, however, you can do it in one with rsync as follows:
rsync --rsync-path="sudo rsync" <LOCALFILE> USER@SERVER2:/root
Note: This does require NOPASSWD
sudo configuration. If you have to enter the password for sudo, then the two step way is needed.
To copy directory, you need to add -r
parameter. And -v
for verbose output.
To use above method with credentials, you need to add them into your ~/.ssh/config
file, e.g.
Host SERVER2
HostName server2.example.colm
User USER
#IdentityFile ~/.ssh/custom_key.pem
Similar question: http://superuser.com/questions/87597/how-to-perform-scp-as-a-sudo-user
– Kevin Panko – 2010-05-07T21:45:59.827i've closed the older question as a duplicate of this one, since the question asker never regained ownership of that question. – quack quixote – 2010-05-08T00:51:13.913
I have answered on another posting how you can customize scp do the sudo for you directly. This is similar to what WinSCP does.
– YoYo – 2016-09-02T22:56:31.777