While learning Linux and exploring SSH, I came across something that caught my attention: Cryptographic Authentication.
We've been using different forms of authentication for decades like passwords, PINs, OTPs, biometrics, and so on. But SSH introduced me to another interesting approach: authenticating using cryptographic key pairs.
I'd heard about public and private keys before, but I wanted to understand what actually happens behind the scenes when we authenticate ourselves using them.
So, let's break it down!
1. First, what exactly is SSH?
SSH (Secure Shell) is a network protocol that allows us to securely communicate with and remotely access another machine over an unsecured network.
For example, imagine you have your local machine and a remote Linux server. You want to access that server, execute commands, manage files, or deploy an application without physically accessing it.
That's where SSH comes in.
ssh username@server_ip This establishes a secure connection between your local machine (client) and the remote machine (server).
But before allowing access, the server needs to authenticate you. Similarly, your client also needs to verify that it's connecting to the genuine server.
Traditionally, client authentication could be done using a username and password.
However, there's another approach: Public Key Authentication.
2. Why do we need SSH keys?
Let's talk about password-based authentication first.
You enter your password, and the server verifies whether it matches the expected credentials.
Although SSH encrypts the communication channel, password-based authentication still has certain limitations:
- Weak or reused passwords.
- Password guessing and brute-force attempts.
- Password theft through phishing or compromised systems.
- Managing credentials across multiple servers.
Instead of repeatedly relying on passwords, SSH supports authentication using cryptographic key pairs.
And this is where asymmetric cryptography comes into the picture.
3. A little cryptography: Symmetric vs Asymmetric Encryption
Before understanding SSH key authentication, let's understand two fundamental cryptographic concepts.
Symmetric Cryptography
In symmetric cryptography, the same secret key is used for both encryption and decryption.
Imagine having a locked box and a key.
You use that key to lock the box, and the receiver uses the same key to unlock it.
Both parties must securely possess the same secret key.
Some examples include:
- AES (Advanced Encryption Standard)
- ChaCha20
Asymmetric Cryptography
Asymmetric cryptography works differently.
It uses a pair of mathematically related keys:
- Public Key: Can be shared with others.
- Private Key: Must remain secret with its owner.
These keys are mathematically related, but knowing the public key does not practically allow someone to derive the private key.
One important application of asymmetric cryptography is digital signatures.
For example, you can use your private key to create a digital signature, and someone else can use your corresponding public key to verify that signature.
Some examples of asymmetric cryptographic algorithms include:
- RSA
- Ed25519
- ECDSA
Here's an interesting distinction: SSH uses asymmetric cryptography for authentication, but it also uses symmetric cryptography to encrypt the actual communication after establishing the connection.
Why?
Because symmetric encryption is generally much more efficient for encrypting large amounts of data.
4. Public and Private Keys: How do they work together?
Let's take a simple example.
Imagine A wants to authenticate herself to a remote server.
She generates a pair of keys:
A's Key Pair ┌──────────────────┐ │ │ │ Public Key │ │ │ │ Private Key │ │ │ └──────────────────┘ The public key can be shared with the server, while the private key stays safely on A's machine.
Now, here's the interesting part.
The server doesn't need A's private key to verify her identity.
Instead, A uses her private key to create a digital signature.
The server then uses A's public key to verify that signature.
If verification succeeds, the server gets cryptographic proof that the client possesses the corresponding private key.
The private key never needs to leave A's machine.
This is the fundamental idea behind public-key authentication.
5. What actually happens during SSH authentication?
Let's say you're connecting from your local machine to a remote Linux server using SSH keys.
Here's what happens at a high level:
Step 1: Key generation
You generate a public-private key pair on your local machine.
Step 2: Public key registration
You copy your public key to the remote server and add it to the user's ~/.ssh/authorized_keys file.
Step 3: SSH connection
You initiate an SSH connection:
ssh username@server_ip Step 4: Authentication request
Your SSH client tells the server that it wants to authenticate using a particular public key.
The server checks whether that public key is authorized for the requested account.
Step 5: Digital signature
The client proves that it possesses the corresponding private key by creating a digital signature over data associated with the SSH connection and authentication request.
The private key stays on the client machine.
Step 6: Signature verification
The server uses the registered public key to verify the signature.
If the signature is valid and the key is authorized, authentication succeeds.
You get access to the remote machine!
But where does hashing come into the picture?
This was another interesting part for me.
Digital signatures generally involve cryptographic hash functions.
A hash function takes input data and produces a fixed-length digest.
For example:
Input Data | v Hash Function | v Fixed-length Digest The signing algorithm uses the private key to produce a signature associated with the message or its digest.
The verification algorithm uses the corresponding public key to check that signature.
One important clarification: the server isn't simply comparing a hash generated from the public key with a hash generated from the private key.
It's verifying a cryptographic signature using the mathematical relationship between the key pair.
The exact signing and verification process depends on the algorithm being used.
That's what makes the process interesting!
6. Let's implement it ourselves!
Understanding the theory is one thing. Actually implementing it between two machines makes everything much clearer.
Let's assume:
- Machine A: Your local Linux machine (SSH client).
- Machine B: A remote Linux machine (SSH server).
Step 1: Generate an SSH key pair
On Machine A, execute:
ssh-keygen -t ed25519 -C "your-email@example.com" This generates two files:
~/.ssh/id_ed25519 ~/.ssh/id_ed25519.pub Here:
-
id_ed25519→ Private key. -
id_ed25519.pub→ Public key.
Keep the private key secure. Never share or upload it.
You can also protect your private key with a passphrase.
Step 2: Copy the public key to the remote server
If ssh-copy-id is available:
ssh-copy-id username@server_ip This installs your public key into the remote user's ~/.ssh/authorized_keys file.
Alternatively, you can manually append the contents of your .pub file to that file.
Step 3: Connect using SSH
Now try:
ssh username@server_ip If everything is configured correctly, you should be able to authenticate using your SSH key instead of entering your account password.
You can also use verbose mode to inspect what's happening:
ssh -v username@server_ip This is particularly useful for understanding and debugging SSH authentication.
7. Where else did I use this?
After experimenting with SSH authentication between machines, I encountered the same concept again while working with AWS EC2 and GitHub.
AWS EC2
When launching an EC2 instance, AWS allows you to create or select a key pair.
You download the private key (.pem) and use it from your local machine:
ssh -i my-key.pem ubuntu@EC2_PUBLIC_IP The corresponding public key is configured on the instance, allowing you to authenticate without sharing your private key with the server.
GitHub SSH Authentication
I also used SSH keys to authenticate with GitHub when cloning repositories.
Instead of using HTTPS credentials, you can register your public SSH key with your GitHub account.
Then:
git clone git@github.com:username/repository.git GitHub verifies your identity using the corresponding public key.
This was another practical example that helped me understand how the same underlying cryptographic idea is used across different systems.
8. A few things worth remembering
- Never share your private key with anyone.
- Never commit private keys to GitHub or expose them in public repositories.
- Use a passphrase to protect your private key where appropriate.
- Verify server host keys instead of blindly accepting unknown ones.
- Keep your SSH server configuration and authorized keys properly managed.
Your public key is meant to be shared. Your private key is not.
Final Thoughts
What started as curiosity while learning Linux turned into a much more interesting exploration of cryptography, digital signatures and secure authentication.
I initially thought SSH key authentication was simply about generating two keys and connecting to a remote server.
But understanding how the client proves ownership of a private key without actually sending it to the server made the whole mechanism much more fascinating.
There's still a lot more to explore in cryptography if we go deeper into it; hashing algorithms, digital signature creation, and the mathematics behind public-private key verification.
I'll probably explore those concepts in more detail as I learn more.
But for now, that was my exploration of what happens behind the scenes of cryptographic authentication!
Sometimes, understanding what happens behind a simple command makes you appreciate the engineering underneath it.




