How to Copy Files to Remote Server with Sudo Privileges
Today, I come across a situation where I have to push a file to the remote server’s directory which was owned by user root.
It can be done with the help of ssh command but, it will require 2 steps to be performed. i.e first copy the file to remote server’s writable directory e.g /tmp and from there copy/move the file to the required directory with sudo command/access.
In this post, we are going to see the trick to copy a file to remote server’s directory owned by root user with sudo privileges.
I have a file with name puppet.repo on the source server which I need to copy/transfer to the remote server’s directory /etc/yum.repos.d/. Here, /etc/yum.repos.d is owned by root.
I have a user with name serveradmin which has sudo access on remote server. Below is the command which can be used to accomplish this task.
# rsync --rsync-path="sudo rsync" puppet.repo serveradmin@<remote server="" ip="">:/etc/yum.repos.d/</remote>
Here, serveradmin: User on remote server having sudo access. puppet.repo: Local file which need to transfer. [Remote Server IP]: IP of Remote Server.