Setting Up SSH Keys on Ubuntu 20.04
SSH keys let a server verify your private key instead of asking for the account password. The setup is simple, but the order matters: test the key before disabling password login, or you may lock yourself out.
Check before creating another key
Look at the SSH directory on your local Ubuntu machine:
ls -al ~/.ssh
Do not overwrite an existing private key unless you know where it is used. If you need a new one, Ed25519 is a sensible default on current OpenSSH installations:
ssh-keygen -t ed25519 -C "your_email@example.com"
Choose a descriptive filename if the default key is already in use, and protect the private key with a passphrase. The .pub file is the public key you can copy. The file without .pub stays private.
Copy the public key to the server
If ssh-copy-id is available, it handles the usual permissions and appends the key to the remote account:
ssh-copy-id username@remote_host
Replace the username and host with the real values. The command initially uses the account’s existing authentication method, commonly a password.
Now test the key in a second terminal while the current session remains open:
ssh username@remote_host
For a key stored under a custom name, specify it explicitly:
ssh -i ~/.ssh/my_server_key username@remote_host
Disable passwords only after the test works
Password authentication is configured on the remote server, not in the local client’s configuration. Edit the server’s SSH daemon settings only when you have working key access and another recovery route:
PasswordAuthentication no
Validate the SSH daemon configuration and reload the service using the commands appropriate for that server. Keep the existing session open until a new key-based login succeeds.
SSH keys improve authentication, but careless key storage can undo the benefit. Keep private keys off the server, use a passphrase where practical, and remove public keys that are no longer needed.