Key Takeaways
- SSH keys are a secure way to access remote servers without passwords. They consist of a public and private key.
- Use the ssh-keygen command on Linux to generate an SSH key pair, and consider adding a passphrase for added security.
- Copy your SSH public key to the remote server using either ssh-copy-id or by manually pasting the key into the authorized_keys file.
An SSH key lets you keep your remote server accounts safe while granting passwordless access. It’s easy to generate one on Linux.
What Is an SSH Key?
An SSH key is a cryptographically generated key that lets you log on to remote machines. An SSH key is a “key pair” containing a public key and a private key. You can copy the public key to remote machines, and they will use it to authenticate you. The private key, as its name implies, is private and should stay on your local machine.
Don’t share your private SSH key with anyone!
At login time, the remote server will use SSH to compare your public key with your private one. If they match, you’re authorized to enter the remote system. This key pair system lets you set up accounts easily while maintaining security.
How to Generate a Remote Key
To generate an SSH key pair, just use the “ssh-keygen” command on the Linux command line. It will prompt you to select a file location, then enter and confirm a passphrase if you choose to use one. The passphrase allows you to add an extra layer of security to your key.
If you use a passphrase, don’t forget it! You won’t be able to log in using that key if you forget it.
If you want to log in without a password, just leave the passphrase blank.
By default, ssh-keygen uses the Ed25519 algorithm. This is sufficient for most cases because it’s very secure. You can specify the type of algorithm you want using the -t flag. You shouldn’t normally need to do this unless the server doesn’t support whatever algorithm you’ve used. You can read the ssh-keygen manual page to learn how to generate a different key pair.
Getting Your New SSH Key to the Remote Machine
Now that you’ve generated your key pair, you need to copy your key over to the remote server. There are two ways to do this.
The first and easiest is to use the ssh-copy-id utility. To do so, just call it with the username and address of your remote server account:
ssh-copy-id [email protected]
Enter your password, and it will copy over your private key.
You’ll now be able to log into the remote server:
ssh [email protected]
If that doesn’t work, you’ll have to copy and paste your public key manually. Your public key will be a file ending in “.pub” in the .ssh directory in your home directory. Open it and copy and paste what’s in it into the .ssh/authorized_keys file on the remote machine.
Save the file, and you’ll now be able to log on to the server using SSH. Make sure that the file is only writeable by you, or you may run into errors.
