How to use a Private key for SSH authentication
Tutorials Sep 11, 2024
What’s a private key?
Even the most complex passwords cannot match the cryptographic strength of private keys. With SSH, private keys significantly improve security by relieving users of the burden of remembering complex passwords (or worse yet, writing them down).
Let’s first Open a terminal and generate a private key.
Step 1 : Check to see if you already have an SSH key.
$ ls ~/.ssh
Output:
Step 2 : Create SSH key.
$ ssh-keygen
Output:
$ Enter file in which to save the key (/home/ubuntu/.ssh/id_rsa):
To use the default filename, press ENTER and proceed.
In the next part, enter a passphrase to secure your key pairs. You can skip this by pressing ENTER.
$ Enter passphrase (empty for no passphrase):
$ Enter same passphrase again:
Step 3 : Copy public key to the remote host.
$ ls ~/.ssh
Output:
To use the SSH key pair you created, we must first copy the public key to the remote server. Fortunately, OpenSSH includes the ssh-copy-id function to do this.
$ ssh-copy-id remote_server_user@remote_server_ip
Step 4 : SSH using The Private Key.
$ ssh remote_server_user@remote_server_ip
You have now successfully utilized SSH key-based authentication. By eliminating password login, you may provide an extra degree of protection.
Conclusion
This tutorial has shown you how to create SSH key pairs and copy the keys to remote hosts. The steps mentioned above allow you to authenticate SSH sessions without using a password. Furthermore, you may control numerous servers at the same time with a single key pair.