What Is SSH
Imagine a padlock that you can copy as often as you like, and hand out freely, while there is only one key, which never leaves your pocket. Anyone can lock a box with your padlock, but only you can open it. If someone wants to check that a message really came from you, they can test it against your padlock.
SSH (Secure Shell) works with exactly that pair. You create a key pair on your computer: a public key, which you can give to anyone, and a private key, which stays on your computer. You give the public key to GitHub, GitLab or Bitbucket once. From then on, every time Git connects, your computer proves that it holds the matching private key, without ever sending it, and the platform knows it's you. No password is typed and none travels over the network.
In this lesson, you will learn how that proof works, how your computer makes sure it is talking to the real server, and when to choose SSH or HTTPS. The next lesson, "Adding an SSH Key", is the hands-on part.
The Key Pair
| Public key | Private key | |
|---|---|---|
| File | ~/.ssh/id_ed25519.pub | ~/.ssh/id_ed25519 |
| Who may see it | Anyone. You paste it into the platform's settings. | Only you. Never share, email or commit it. |
| What it does | Lets others check a signature | Lets you make a signature |
| If it leaks | Nothing bad happens | Someone can sign in as you: delete the key on the platform at once |
The two keys are created together and are mathematically linked: a signature made with the private key can be checked with the public key, but the private key can't be worked out from the public one.
How SSH Proves Who You Are
When Git connects to git@github.com over SSH, a short conversation happens, in a fraction of a second:
- Your computer connects and says "I'd like to sign in as
git, with this public key." - The platform looks up the key in its list of keys that users have added. It finds that the key belongs to Navika's account.
- The platform sends a random challenge.
- Your computer signs the challenge with the private key and sends back the signature.
- The platform checks the signature with the public key. If it matches, only the holder of the private key could have made it: Navika is signed in.
This is a simplified picture. In the real protocol, the "challenge" is a session identifier that both sides work out at the start of each connection, from random values that each side contributes. Your computer signs that identifier, so the result is the same: a signature that only the private key could make, and that is useless in any other connection.
Every platform uses the same user name, git, for everyone. The key decides which account you are.
How Your Computer Checks the Server
Proving who you are is half of it. Your computer also has to be sure it is talking to the real GitHub, not an impostor in between. Every SSH server has its own key pair, the host key. The first time you connect to a server, SSH shows the server key's fingerprint and asks:
The authenticity of host 'github.com' can't be established.
ED25519 key fingerprint is SHA256:<fingerprint>.
Are you sure you want to continue connecting (yes/no/[fingerprint])?Compare the fingerprint with the one the platform publishes in its documentation (search for "GitHub SSH key fingerprints", or the GitLab or Bitbucket equivalent). If they match, type yes. SSH then saves the host key in ~/.ssh/known_hosts, and checks it on every later connection. If it ever changes unexpectedly, SSH refuses to connect with a loud warning, which is exactly the protection you want.
SSH URLs
An SSH URL names the user git, the server, and the repository path:
| Platform | SSH URL |
|---|---|
| GitHub | git@github.com:navika-anamaneni/fee-tracker.git |
| GitLab | git@gitlab.com:navika-anamaneni/fee-tracker.git |
| Bitbucket | git@bitbucket.org:<workspace>/fee-tracker.git |
The colon form is a short way of writing ssh://git@github.com/navika-anamaneni/fee-tracker.git. SSH normally uses network port 22. Some office and school networks block that port. GitHub and GitLab also accept SSH on port 443, the port HTTPS uses, through ssh.github.com and altssh.gitlab.com, which the platforms' help pages explain.
Key Types and Passphrases
ssh-keygen can make several kinds of key. Use Ed25519: it is short, fast and secure, and all three platforms accept it. Use RSA with at least 3072 bits only for old systems that don't support Ed25519. Older types, such as DSA, are no longer accepted.
When you create a key, you can protect the private key file with a passphrase. Then a stolen copy of the file is useless without it. To avoid typing the passphrase at every push, an SSH agent holds the key, ready to use, in memory while you are logged in. Windows, macOS and Linux all include one, as the next lesson shows.
SSH or HTTPS?
| HTTPS with a token | SSH with a key | |
|---|---|---|
| Setup | Create a token, let a credential helper store it | Create a key pair, add the public key to your account |
| Typing passwords | Once, then the helper remembers | Never (or a passphrase once per login, with an agent) |
| Works on locked-down networks | Almost always (port 443) | Port 22 may be blocked; use the port 443 alternatives |
| Expires | Tokens usually expire, and must be renewed | Keys last until you delete them (some platforms let you set an expiry) |
| Typical use | Automated systems, quick setups, restricted networks | A developer's own computer |
Both are secure. Many developers use SSH on their own computers, and HTTPS tokens for build servers.
Common Mistakes
Sharing the wrong file. The file to paste into the platform ends in .pub. The one without .pub is the private key.
Typing yes without checking the fingerprint. On a trusted home network the risk is small, but on public Wi-Fi, check it against the platform's published fingerprints.
Ignoring a "REMOTE HOST IDENTIFICATION HAS CHANGED" warning. It can mean an attack. Check the platform's announcements before removing the old entry from known_hosts. (GitHub, for example, once replaced its RSA host key, and announced it.)
Using an SSH URL without having added a key. You get Permission denied (publickey). Add the key, or use the HTTPS URL.
One key for every computer. Make one key pair per computer. If a laptop is lost, you delete just its key.
Under the Hood
You can watch the key pair at work with ssh-keygen itself, which can sign and verify any file. This example uses a demo key in the lesson folder, not your real one in ~/.ssh:
mkdir ssh-demo
cd ssh-demo
ssh-keygen -q -t ed25519 -N "" -C "navika@example.com" -f demo-key
ls
cut -d " " -f 1,3 demo-key.pub
head -1 demo-keydemo-key
demo-key.pub
ssh-ed25519 navika@example.com
-----BEGIN OPENSSH PRIVATE KEY-----ssh-keygen made two files: demo-key (private) and demo-key.pub (public). The public key is one line: the key type, the key itself (left out here, because it is long and different every time), and a comment, usually your email. The private key file starts with a header that marks it as private.
Now Navika signs a message with the private key, and anyone with the public key checks it. The last sed only hides the key fingerprint, which is random and would differ for you:
echo 'Pay $12,000 to the school' > message.txt
ssh-keygen -q -Y sign -f demo-key -n file message.txt
echo "navika@example.com $(cut -d " " -f 1,2 demo-key.pub)" > allowed-signers
ssh-keygen -Y verify -f allowed-signers -I navika@example.com -n file -s message.txt.sig < message.txt | sed "s/SHA256:.*/SHA256:.../"Good "file" signature for navika@example.com with ED25519 key SHA256:...Good "file" signature: the signature matches the public key and the message. Now someone changes one character of the message:
echo 'Pay $92,000 to the school' > message.txt
ssh-keygen -Y verify -f allowed-signers -I navika@example.com -n file -s message.txt.sig < message.txtSignature verification failed: incorrect signature
Could not verify signature.The check fails. That is the whole idea behind SSH sign-in: the platform sends a challenge instead of a file, and only a matching private key can produce a signature that passes. Git can use the same kind of signature to sign commits, which is covered with security and signing.
Interview Corner
What is SSH, and why use it with Git?
SSH is a protocol for secure, authenticated connections. With Git, it lets you sign in to a hosting platform with a key pair instead of a password or token: the platform stores your public key, and your computer proves it holds the private key on every connection.
What is the difference between the public and the private key?
The public key can be shared freely and is used to verify signatures. The private key stays secret on your computer and is used to create them. The private key can't be derived from the public key.
What is known_hosts?
A file (~/.ssh/known_hosts) where SSH stores the host keys of servers you have connected to. SSH checks the server's key against it on every connection, and refuses if it changed, which protects against man-in-the-middle attacks.
Which SSH key type should you use?
Ed25519 (ssh-keygen -t ed25519). Use RSA with at least 3072 bits only where Ed25519 isn't supported.
HTTPS or SSH for Git?
Both are secure. HTTPS with a token works on nearly every network and suits automation. SSH avoids typing credentials once the key is added and suits developers' own machines.
Practice
Challenge: does the signature still match? In the same ssh-demo folder, put the original message back, Pay $12,000 to the school, and verify the old signature again, hiding the fingerprint as before.
What does Git print? The signature file wasn't changed. Before you open the solution, decide whether verification passes now.
Show solution
echo 'Pay $12,000 to the school' > message.txt
ssh-keygen -Y verify -f allowed-signers -I navika@example.com -n file -s message.txt.sig < message.txt | sed "s/SHA256:.*/SHA256:.../"Good "file" signature for navika@example.com with ED25519 key SHA256:...It passes again. The signature belongs to the exact content Pay $12,000 to the school. Change one character and it fails, change it back and it matches, because the content is the same as what was signed.
Checkpoint. Find the published SSH host key fingerprints for your platform in its documentation. Then explain to someone, in your own words, the five steps of an SSH sign-in, and why a stolen signature can't be used to sign in later.
Key Takeaways
Quick reference
| Term or command | Meaning |
|---|---|
~/.ssh/id_ed25519 | Your private key (keep secret) |
~/.ssh/id_ed25519.pub | Your public key (add to the platform) |
~/.ssh/known_hosts | Host keys of servers you trust |
git@github.com:<user>/<repo>.git | An SSH URL |
ssh-keygen -t ed25519 | Create a key pair |
ssh -T git@github.com | Test the connection (next lesson) |
Was this tutorial helpful?