scp, ssh and rsync without prompting for password – howto

Using scp, rsync and ssh requires the password unless you add the public key from src host to authorised_keys on destination host.

scp and rsync commands are used to transfer or backup files between known hosts or by the same user on both the hosts.

Lets say you want to copy between two hosts SOURCE and DESTINATION. SOURCE is the host where you would run the scp, ssh or rsync command.

On the SOURCE host, run

# ssh-keygen -t rsa

It will prompt for a passphrase but do not enter anything. Instead, just press the enter key. It’ll generate an identification (private key) and a public key. Do not ever share the private key with anyone!
The public key will be generated in ~/.ssh/id_rsa.pub.

For example in root/.ssh/id_rsa.pub

Copy id_rsa.pub file to DESTINATION inside the /root/.ssh/
On DESTINATION host, login as the remote user which you plan to use (in this case root) and copy the contents of id_rsa.pub to /root/.ssh/authorized_keys

# cat id_rsa.pub >> /root/.ssh/authorized_keys
# chmod 700 /root/.ssh/authorized_keys

If this file authorized_keys does not exists, the command above will create it. Make sure you remove permission for others to read this file.

On some distros, ssh by default does not allow root to log in. To enable root login, edit /etc/ssh/sshd_config and changing the option of PermitRootLogin from no to yes (on DESTINATION host). Restart sshd to apply changes and that is it.

In case you want to ssh, scp or rsync from DESTINATION to SOURCE host you will be asked for password. You can reverse the steps above (generate the public key on DESTINATION and copy it to the SOURCE host) and it will work in both directions.

In case that one server gets hacked, the other one will be too 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.