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.

crashingdaily ALL= NOPASSWD:/usr/bin/rsync

Then use the --rsync-path option to specify the sudo wrapper.

rsync -a -e "ssh" --rsync-path="sudo rsync" crashingdaily@server.remotehost.com:/u02/data_pump_dir/ /archive

Option 2. For interactive usage, I can pre-activate sudo and then run rsync as in Option 1.

stty -echo; ssh server.remotehost.com sudo -v; stty echo

rsync -a -e "ssh" --rsync-path="sudo rsync" crashingdaily@server.remotehost.com:/u02/data_pump_dir/ /archive

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.

#!/bin/sh
su – -c "rsync $*"

Then call that script with the --rsync-path option.

rsync -a -e "ssh" --rsync-path=/usr/local/bin/su-rsync crashingdaily@other.remotehost.com:/u02/data_pump_dir/ /archive

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.

Related:

rsync

Re: how to use option for rsync

rsync using sudo via remote shell

About these ads