To generate a private key and public key pair for SSH access on Ubuntu, you can use the OpenSSH utility called
ssh-keygen. Here’s a step-by-step guide:
- Open a terminal on your Ubuntu machine.
- Run the following command to generate the key pair:
ssh-keygen -t rsa -b 4096 -C "email@example.com"
"firstname.lastname@example.org" with your own email address or any identifier you prefer.
- You will be prompted to specify the file location to save the key pair. Press Enter to accept the default location (
/home/your_username/.ssh/id_rsa) or provide a different path.
- You will then be prompted to enter a passphrase. A passphrase adds an extra layer of security to your private key, but it’s optional. You can press Enter to leave it empty, but it’s generally recommended to use a passphrase for added security. If you choose to use a passphrase, make sure to remember it, as you will need it whenever you use the private key.
- After generating the keys, you will see output similar to:
Your identification has been saved in /home/your_username/.ssh/id_rsa.
Your public key has been saved in /home/your_username/.ssh/id_rsa.pub.
- The private key is stored in the file
id_rsa, and the public key is stored in
id_rsa.pub. These are the files you’ll need for SSH access.
- You can now copy the public key (
id_rsa.pub) to the remote SSH server. You can use the
ssh-copy-idcommand to automate this process. Run the following command, replacing
server_addresswith the appropriate values:
ssh-copy-id -i ~/.ssh/id_rsa.pub username@server_address
This command will prompt you for the password of the remote user account.
- Once the public key is copied to the remote server, you should be able to authenticate using your private key without entering a password.
That’s it! You have successfully generated a private key and public key pair and copied the public key to the remote SSH server.