What is SSH, and how do you set up key-based login to a server?
SSH (Secure Shell) lets you log into a remote computer's command line over an encrypted connection. It's how most people manage Linux servers.
Passwords vs keys
You can log in with a password, but SSH keys are both more secure and more convenient. A key pair has two parts:
- Private key: stays on your computer. Never share it.
- Public key: goes on the server. It's safe to share.
The server checks that you hold the private key matching the public key, without the private key ever being sent.
Setting it up
- Generate a key pair on your computer:
ssh-keygen -t ed25519. Accept the default location and consider adding a passphrase. - Copy the public key to the server. Many hosts let you paste it during server creation, or use
ssh-copy-id user@server. - Log in:
ssh user@server. You shouldn't be asked for the server password. - Disable password login in the SSH server config once keys work, then restart the SSH service. Keep your current session open while testing so you don't lock yourself out.
Handy extras
- A
~/.ssh/configfile lets you create short aliases for servers. - An SSH agent remembers your passphrase for the session.
Common mistakes
- Wrong file permissions on
~/.ssh, which makes SSH refuse the key - Uploading the private key instead of the public one
Did you get locked out of a server while learning SSH? What tip would you give someone setting it up for the first time?
Comments · 3
The advice to keep your existing session open while testing before disabling password login is the sort of step that saves a lot of pain. I'd also suggest putting a passphrase on the key, since a copied private key without one is usable by anyone.
If you manage several servers, the
~/.ssh/configaliases mentioned here are worth setting up right away. They turn long commands into something likessh web1.Use ed25519 keys and put a passphrase on them. A stolen laptop with an unprotected key is the worst-case scenario.