I do not have a root password for many of the servers I interact with so I can not SSH directly in as the root user. Also, the ssh daemons are wisely configured with ‘PermitRootLogin’ set to ‘no’ so a password would be moot anyway. I do have sudo permissions on the servers so I can connect under my username and sudo the privileged commands as needed. Glazed-eye screen-staring started when I needed to rsync a remote directory that was read-only for root. How do I get rsync to run under sudo on the remote server? I did some searching and here are some options I found.
Option 1. Set NOPASSWD in the /etc/sudoers file.
Then use the --rsync-path option to specify the sudo wrapper.
Option 2. For interactive usage, I can pre-activate sudo and then run rsync as in Option 1.
The “stty -echo” and “stty echo” is used to temporarily disable the display of the keyboard input to prevent the sudo password from being displayed.
Credits: Wayne Davison and Julian Cowley
Option 3. If sudo is not available, there is possibly an option to use “su”. I was unable to get this to work. su seems to insist on a tty – I get the error ‘standard in must be a tty’. (In this case I do have a root password to use with su, so that’s not an issue).
Create a wrapper script, /usr/local/bin/su-rsync, on the remote server and make it executable.
su – -c "rsync $*"
Then call that script with the --rsync-path option.
Credit: Wayne Davison
Option 4. Set ‘PermitRootLogin’ to ‘yes’ on the remote server and use SSH key authentication to login directly as the root user. This isn’t really an option for me but I throw it out there for sake of completeness.