SSH Key-Based Authentication
Generate SSH key pairs, deploy public keys to servers, and disable password authentication for enhanced security.
SSH Key-Based Authentication is a free Linux Server Deployment & SSH Mastery lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Linux Server Deployment & SSH Mastery learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Unlock SSH with Key Pairs
Welcome! In this lesson, you'll learn about SSH key-based authentication. This method is far more secure and convenient than using traditional passwords for logging into your server.
SSH keys act like a digital handshake, proving your identity without needing to type a password every time.
Public & Private Key Magic
SSH key authentication relies on a pair of cryptographic keys:
- Private Key: This key stays on your local machine and must be kept secret. Think of it as the unique key to your digital lock.
- Public Key: This key is placed on the server you want to access. It's like a special lock that can only be opened by your private key.
When you try to connect, the server challenges your client, which then uses your private key to prove its identity.
Generating Your SSH Keys
You can generate your own SSH key pair using the ssh-keygen command on your local machine. By default, it creates an RSA key pair.
When prompted, you can choose a passphrase to protect your private key. This adds an extra layer of security, requiring you to enter the passphrase before using the key.
ssh-keygenCustomizing Key Generation
For stronger security, it's recommended to use newer key types like ED25519. You can also specify a custom filename for your keys.
-t ed25519: Specifies the ED25519 algorithm.-f ~/.ssh/my_server_key: Creates files namedmy_server_keyandmy_server_key.pub.
Always use a strong passphrase for your private key!
ssh-keygen -t ed25519 -f ~/.ssh/my_server_keyUnderstanding Key Files
After generation, your keys are typically stored in the ~/.ssh/ directory on your local machine. You'll find two files:
id_ed25519(orid_rsa): This is your private key. Keep it absolutely secure and never share it.id_ed25519.pub(orid_rsa.pub): This is your public key. This is the file you'll copy to your server.
The permissions for these files are also very important for security!
Deploying Your Public Key Easily
The easiest way to get your public key onto a server is using the ssh-copy-id command. It securely copies your public key to the server's ~/.ssh/authorized_keys file.
You'll need to use your password the first time you run this command for a new server. After that, password login is often no longer needed.
ssh-copy-id user@your_server_ipManual Public Key Deployment
If ssh-copy-id isn't available or you prefer a manual method, you can copy your public key to the server yourself.
First, ensure the ~/.ssh directory exists and has correct permissions (700) on the server. Then, append your public key to the ~/.ssh/authorized_keys file (permissions 600).
cat ~/.ssh/id_ed25519.pub | ssh user@your_server_ip \
"mkdir -p ~/.ssh && chmod 700 ~/.ssh && \
cat >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys"Connecting with Your New Keys
Once your public key is on the server, you can connect using SSH. If you used the default key name (e.g., id_rsa or id_ed25519), SSH will automatically find it.
If you used a custom key name (e.g., my_server_key), you'll need to specify it using the -i flag.
ssh -i ~/.ssh/my_server_key user@your_server_ipDisabling Password Login for Security
For enhanced security, it's highly recommended to disable password authentication on your server once key-based login is working.
Edit the SSH daemon configuration file (/etc/ssh/sshd_config) on the server, change PasswordAuthentication yes to no, and restart the SSH service (e.g., sudo systemctl restart sshd or sudo service ssh restart).
# On the server, edit /etc/ssh/sshd_config
PasswordAuthentication no
# Then restart SSH service
sudo systemctl restart sshdKey Authentication Check
You've learned how SSH key-based authentication works, how to generate keys, and how to deploy them. Let's test your knowledge!
Recap: Secure SSH with Keys
Great job! You've mastered SSH key-based authentication. You learned:
- The difference between public and private keys.
- How to generate key pairs using
ssh-keygen. - How to deploy your public key to a server with
ssh-copy-idor manually. - How to connect securely without passwords.
- Why and how to disable password authentication on your server.
SSH keys are a fundamental tool for secure and efficient server management. Keep practicing!
Frequently asked questions
Is the “SSH Key-Based Authentication” lesson free?
Yes — the full text of “SSH Key-Based Authentication” is free to read here on the web, and the Linux Server Deployment & SSH Mastery course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Linux Server Deployment & SSH Mastery course, upgrade to CoddyKit PRO.
What will I learn in “SSH Key-Based Authentication”?
Generate SSH key pairs, deploy public keys to servers, and disable password authentication for enhanced security. You practise Linux Server Deployment & SSH Mastery with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Linux Server Deployment & SSH Mastery?
No prior experience is required. Linux Server Deployment & SSH Mastery on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “SSH Key-Based Authentication” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Linux Server Deployment & SSH Mastery lesson?
Yes. Every Linux Server Deployment & SSH Mastery lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Understanding the SSH Protocol
- Configuring Your SSH Client
- SSH Key-Based Authentication
- Hardening the SSH Server