Setting up SSH keys on Ubuntu 20.04 involves a few steps. Here’s a guide:
-
Check for Existing SSH Keys
-
Check for existing SSH keys:
Run this command to see if you already have SSH keys:
ls ~/.ssh/id_*.pub
If you see files named
id_rsa.pub or id_dsa.pub
, you have existing keys.
-
Check for existing SSH keys:
-
Generate New SSH Key Pair (if needed)
-
Generate a new SSH key pair:
If you don’t have keys or wish to create a new one, use:
ssh-keygen -t rsa -b 4096 -C "[email protected]"
This command creates a new SSH key using the provided email. Press Enter to accept the default file location (
~/.ssh/id_rsa
) and optionally set a passphrase for added security.
-
Generate a new SSH key pair:
-
Add SSH Key to SSH Agent
-
Start the SSH agent:
eval "$(ssh-agent -s)"
-
Add your SSH private key to the SSH agent:
ssh-add ~/.ssh/id_rsa
(Replace
~/.ssh/id_rsa
with the path to your private key if it's different.)
-
Start the SSH agent:
-
Copy the Public Key to the Remote Server
-
Copy the public key to the server:
Use
ssh-copy-id
to copy your public key to the remote server. Replaceusername
andremote_host
with your server’s username and IP address or domain name:ssh-copy-id username@remote_host
Enter your password when prompted.
-
Copy the public key to the server:
-
Test SSH Connection
-
Connect to the server using SSH:
ssh username@remote_host
If everything is set up correctly, it should log you in without asking for a password.
-
Connect to the server using SSH:
-
Disable Password Authentication (Optional but Recommended for Security)
-
Edit SSH configuration file:
Open the SSH configuration file using a text editor (like nano or vim):
sudo nano /etc/ssh/sshd_config
-
Disable Password Authentication:
Find the line
PasswordAuthentication
and set it tono
:PasswordAuthentication no
-
Restart SSH service:
Restart the SSH service to apply changes:
sudo systemctl restart sshd
-
Edit SSH configuration file:
That should get you set up with SSH keys on Ubuntu 20.04! Remember, using SSH keys for authentication is a secure method for accessing remote servers.