Logging into remote Linux servers repeatedly using a password is both insecure and inefficient. The industry standard for SSH authentication relies on public/private key pairs. While manually appending your public key to a remote server’s authorized_keys file can be error-prone, the ssh-copy-id command automates the entire process in a single, secure terminal command.
How to Generate an SSH Key Pair
Before you can copy a key to a remote server, you must generate a key pair on your local machine (the client). If you already have a key pair, you can skip this step.
- Open your terminal window.
- Type
ssh-keygen -t ed25519 -C "[email protected]"and press Enter. The ED25519 algorithm is currently recommended over RSA for superior security and performance. - When prompted for a file to save the key, press Enter to accept the default location (usually
~/.ssh/id_ed25519). - Enter a strong passphrase to encrypt your private key locally, or leave it blank if you want completely automated background access.
How to Use the ssh-copy-id Command
With your keys generated, you can now push the public key to the remote Ubuntu server.
- In your terminal, type the following command, replacing the username and IP address with your remote server’s details:
ssh-copy-id [email protected] - Press Enter. If this is the first time you are connecting to this specific server, you will see a warning about the host’s authenticity. Type yes and press Enter to accept the ECDSA key fingerprint.
- You will then be prompted to enter the password for the remote user. This is the last time you will need to type it.
- The utility will automatically read your public key (e.g.,
id_ed25519.pub), log into the remote server, and safely append the key to the~/.ssh/authorized_keysfile.
How to Verify Passwordless Login
To confirm the setup was successful, attempt to log into the remote server normally.
- Type
ssh [email protected]and press Enter. - You should immediately connect to the remote bash prompt without being asked for a server password.
If you set a local passphrase during the key generation phase, your local SSH agent may prompt you for that passphrase once per session to unlock the private key on your machine, but the remote server authentication is handled entirely by cryptography.