Transferring my public key to a remote server allows the server to readily identify both me and my client machine. Before I can transfer the public key to the remote server, I need to add the private key to my session keyring. This requires two commands. (One command is to invoke the ssh-agent, and the other command actually adds the private key to the keyring.):
donnie@linux-0ro8:~> exec /usr/bin/ssh-agent $SHELL
donnie@linux-0ro8:~> ssh-add
Enter passphrase for /home/donnie/.ssh/id_rsa:
Identity added: /home/donnie/.ssh/id_rsa (/home/donnie/.ssh/id_rsa)
donnie@linux-0ro8:~>
Finally, I can transfer my public key to my CentOS server, which is at address 192.168.0.101:
donnie@linux-0ro8:~> ssh-copy-id donnie@192.168.0.101
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
donnie@192.168.0.101's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'donnie@192.168.0.101'"
and check to make sure that only the key(s) you wanted were added.
donnie@linux-0ro8:~>
The next time that I log in, I'll use the key exchange, and I won't have to enter a password:
donnie@linux-0ro8:~> ssh donnie@192.168.0.101
Last login: Wed Nov 1 20:11:20 2017
[donnie@localhost ~]$
So, now you're wondering, "How is that secure if I can log in without entering my password?" The answer is that once you close the client machine's terminal window that you used for logging in, the private key will be removed from your session keyring. When you open a new terminal and try to log in to the remote server, you'll see this:
donnie@linux-0ro8:~> ssh donnie@192.168.0.101
Enter passphrase for key '/home/donnie/.ssh/id_rsa':
Now, every time I log into this server, I'll need to enter the passphrase for my private key. (That is, unless I add it back to the session keyring with the two commands that I showed you in the preceding section.)