ssh config with sudo

9,214

Solution 1

I arrived at a solution based on Brian's idea of select the ssh key manualy. To that I've added the -F option that specifies the path to config. Now the argument I'm adding to rsync looks like this: -e "ssh -i $HOME/.ssh/id_rsa -F $HOME/.ssh/config".

Solution 2

To change the login user for an ssh connection originated by root, use the following syntax in /root/.ssh/config:

Host server
User nonrootusername

If you need an RSA key to log in to the remote server, specify the public key location to rsync as follows:

sudo rsync -e "ssh -i /path/to/rsa/key.pub" ...

Comment out that server alias; it's probably not going to help you at all on this.

Solution 3

I think you have to do it the "other way around", ssh into the remote machine and rsync from there, as per: http://en.wikibooks.org/wiki/OpenSSH/Cookbook/Automated_Backup

$ rsync -e "ssh -t -l bkupacct" --rsync-path='sudo rsync' -av [email protected]:/var/www/ /tmp/
Share:
9,214

Related videos on Youtube

cube
Author by

cube

Programming in C++, python and Rust. My computers run Gentoo and are always messed up in some way. I love playing with robots, but they don't like playing with me.

Updated on September 18, 2022

Comments

  • cube
    cube over 1 year

    I'm backing up my local machine to a remote server. Because I want to backup the whole local machine, even files that my local user doesn't normally have access to, I'm doing it using sudo, so the command looks like sudo rsync -M--fake-super --other-arguments / me@server:/backup/location.

    This has the problem that ssh tries to use root's ssh config, and certificates, which are obviously not set up. Recently I've started refering to the server by an alias defined in ~/.ssh/config and the method stopped working correctly.

    The question is: Is there a way that I can use the access rights of sudo, but while keeping the ssh configuration of my regular user?

  • hBy2Py
    hBy2Py over 9 years
    Nice extension, I hadn't found -F yet.
  • Michael
    Michael almost 7 years
    i'm having this same problem with other commands such as git and npm which invoke ssh indirectly... having to figure out how they invoke ssh and allow it to be configured for every single command that might use ssh is too much work