Create a public/private key pair.¶
What is the public key? The public key is the key that can be shared openly with anyone. It is used to encrypt data or verify digital signatures.
What is the private key? The private key is the secret key that is kept only by the owner. It is used to decrypt data encrypted with the public key or to create digital signatures.
What can this key pair be used for? Secure communication (encryption and decryption) Digital signatures and authentication SSH login to servers without passwords SSL/TLS certificates for secure websites
Why must the private key be protected? Anyone who obtains the private key can impersonate the owner. They can decrypt confidential data and gain unauthorized access to systems. If the private key is compromised, the security of the entire key pair is lost.
Install an SSH public key on a server and use it to log in.¶
SSH key authentication lets you log in to a server without typing a password. It uses a pair of cryptographic keys:
1. Public Key¶
Can be shared with anyone. Stored on the server (usually in ~/.ssh/authorized_keys). Used to verify that you own the matching private key.
2. Private Key¶
Must be kept secret. Stored on your computer (e.g., ~/.ssh/id_rsa or ~/.ssh/id_ed25519). Used to prove your identity.
How It Works¶
1. Generate a key pair¶
</> bash ssh-keygen
This creates:
Private key: id_ed25519 (or id_rsa) Public key: id_ed25519.pub (or id_rsa.pub)
2. Copy the public key to the server¶
</> bash ssh-copy-id user@server
or manually add the contents of .pub to: </> bash ~/.ssh/authorized_keys
3. Connect to the server¶
</> bash ssh user@server
Authentication process¶
The server finds your public key in authorized_keys. The server sends a challenge. Your SSH client uses the private key to sign the challenge. The server verifies the signature using the public key. If they match, you are logged in.
What Can the Key Pair Be Used For?¶
SSH login to Linux servers Git authentication (e.g., with GitHub) Secure file transfers (scp, sftp) Automated scripts and server management
Why Must the Private Key Be Protected?¶
</> bash Anyone who obtains your private key can potentially log in as you. To protect it:
Never share it.
Set correct permissions: </> bash chmod 600 ~/.ssh/id_ed25519 Use a passphrase when generating the key. Store backups securely.
Simple analogy:¶
Public key = padlock (you can give it to anyone). Private key = the only key that opens the padlock (keep it secret).