scp command is used to copy files from source host to destination host. But we always need to enter a password on each time while using scp command. rsync command is used to synchronize data between hosts. By default rsync command uses ssh as well. Normally scp and rsync commands are used to transfer or backup files between known hosts. During perform this task we get prompting password every time which is really create hurdle.
You want to copy between two hosts src_hosts and dest_host. src_host is the host where you will run the ssh, scp or rsync command.
On src_host, run this command as the user that runs scp/ssh/rsync
[root@src_host .ssh]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
This will prompt for a passphrase. You will only press the enter key. It’ll then generate an identification (private key) and a public key. Remember that don’t share the private key with anyone. ssh-keygen command shows where it saved the public key. By default dest_host path is ~/.ssh/id_rsa.pub:
There are two ways to transfer public key (id_rsa.pub) file from src_host to dest_host on path ~/.ssh/authorized_keys.
[root@src_host .ssh]# cat ~/.ssh/id_rsa.pub | ssh root@dest_host.example.com ‘cat >> .ssh/authorized_keys’
[root@src_host .ssh]# ssh-copy-id -i ~/.ssh/id_rsa.pub dest_host.example.com
Now try logging into the machine, with “ssh ‘dest_host.example.com'”, and check in:
to make sure we haven’t added extra keys that you weren’t expecting.
If .ssh/authorized_keys file does not exists on dest_host, then the above command will create it.
[root@dest_host .ssh]# ll
-rw-r–r– 1 root root 1213 Mar 5 07:05 authorized_keys
-rw——- 1 root root 1675 Jun 13 2013 id_rsa
-rw-r–r– 1 root root 404 Jun 13 2013 id_rsa.pub
-rw-r–r– 1 root root 5124 Jul 29 2013 known_hosts
-rw-r–r– 1 root root 404 Jun 13 2013 local.pub
Note that by default ssh command does not allow root to login. We can allow root user to login by editing /etc/ssh/sshd_config and changing the option of PermitRootLogin from no to yes. After change in configuration file, we have to restart sshd to effect changes which we made.
Now we have done this, you can run ssh, scp and rsync on src_host connecting to dest_host and it won’t ask for the password. Note that this will still prompt for the password if you are running the commands on dest_host connecting to src_host. You can reverse (vice versa) the steps above (generate the public key on dest_host and copy it to src_host) and you have a two way setup ready. Enjoy!