A quick way to run rsync (via sudo on the remote server) is to use the –rsync-path option (asuming that sudo is configured to run without a password) also verify that you have !tty_tickets in your sudoers file.
rsync -avz --rsync-path='sudo rsync' user@server:/dir/to/backup /destination/directory/
To do this interactivly (without being able to do it password less)
user@bacup$ stty -echo;ssh user@server sudo -v; stty echo
user@bacup$ rsync -avz --rsync-path='sudo rsync' user@server:/dir/to/backup /destination/directory/
(Or as one line)
stty -echo; rsync -azv --rsync-path='sudo rsync' user@server:/dir/to/backup /destination/directory/; stty echo
“stty -echo” turns off input echo (so that your password will not be shown as you type it)
“stty echo” turns the input echo back on (so you can see what you are doing)
If you try to use sudo to get to a directory (for instance if the user calling sudo don’t have permissions for that folder) then you will see an error message like this
user@server:~$ sudo cd /var/log/
sudo: cd: command not found
While I don’t know how to get sudo to work with cd I can offer some workarounds.
As far as I can tell the problem with sudo cd is that if it would have worked the user would be in a directory that he/she has no permissions to so nothing is gained and having a folder that the user don’t have permissions to as the working directory might cause problems in more way than one.
- Use “sudo ls” and then continue with the next command as sudo without entering that folder; for instance
sudo ls /var/log
sudo cat /var/log/syslog
- Open a shell using sudo:
this will give you a shell with su permissions
- simulate initial login using sudo:
this will also give you su permissions but with the difference that it will be like logging in like root (home dir, profile, variables etc) [this would be equivalent of running “sudo su”]
- Use sudo to start a new shell and do all you need to do in this shell
sudo sh -c 'cd /var/spool/exim4/;ls'
(don’t miss the single quotes in the command)
To give some more details on the difference between -i and -s here is a excerpt from the man pages
The -i (simulate initial login) option runs the shell specified in the passwd(5) entry of the target user as a login shell. This means that login-specific resource files such as .profile or .login will be read by the shell. If a command is specified, it is passed to the shell for execution. Otherwise, an interactive shell is executed. sudo attempts to change to that user’s home directory before running the shell. It also initializes the environment, leaving DISPLAY and TERM unchanged, setting HOME, SHELL, USER, LOGNAME, and PATH, as well as the contents of /etc/environment on Linux and AIX systems. All other environment variables are removed.
The -s (shell) option runs the shell specified by the SHELL environment variable if it is set or the shell as specified in passwd(5). If a command is specified, it is passed to the shell for execution. Otherwise, an interactive shell is executed.
Use visudo to edit /etc/sudoers
user host = NOPASSWD: /bin/kill, /bin/ls, /usr/bin/lprm
user, host and or the list of commands to be run can be swapped with the keyword ALL.
However that is probably not a good idea in most cases.
more info on this is in the manpages sudoers(5)
If using sudo gives the error message “unable to resolve host hostname” then one solution is to add the hostname in /etc/hosts
127.0.0.1 hostname.domain.com hostname localhost
This error message will occur if sudo is not able to ask for the password (no tty is present) (like for instance when trying to sudo rsync).
One solution to this is to add the line “Defaults visiblepw” to the /etc/sudoers file.
[Todd C. Miller Todd.Miller at courtesan.com said on sudo-users: Recent versions of sudo will refuse to prompt for a password if no tty is present, since it is not possible to turn off echo in this case. You can restore the old behavior with a line like above]