Sub topic 8
Generating an SSH key
This chapter assumes you can use Git from a terminal and know what a remote is. It also assumes OpenSSH is installed. Git for Windows, macOS, and most Linux distributions include it.
What you are creating
An SSH key pair is two files. The private key stays on your machine and proves your identity. The public key ends in .pub and is safe to share. In a later chapter you will add the public key to GitHub. Never upload or paste the private key anywhere.
Create an Ed25519 key
Ed25519 is a modern key type. Its keys are short and fast to use, and GitHub supports it.
ssh-keygen -t ed25519 -C "[email protected]"The -t option picks the key type. The -C option adds a comment to the public key. The comment is only a label, so an email address or a machine name both work.
ssh-keygen asks two questions:
- File location. Press Enter to accept the default,
~/.ssh/id_ed25519. If that file already exists, the tool asks before overwriting it. Answer no, and pick a different name such as~/.ssh/id_ed25519_work. - Passphrase. This password encrypts the private key on disk. If someone copies the file, they still cannot use it without the passphrase.
What the command produced
List the directory to confirm both files exist:
ls -l ~/.sshYou should see id_ed25519 and id_ed25519.pub. On Linux and macOS, OpenSSH refuses to use a private key that other users can read. ssh-keygen sets the correct permissions, so you rarely need to change them.
Common mistakes
- Sharing the wrong file. The file without
.pubis the private key. Only the.pubfile goes to GitHub. - Overwriting an existing key. Overwriting a key you already use locks you out of every service that trusts it. Read the prompt before you press Enter.
- Skipping the passphrase. An unencrypted key works for anyone who gets a copy of the file.
Check your understanding
Exercise
You run ssh-keygen and press Enter at both prompts. Which file do you later upload to GitHub, and what security setting did you skip?
Show answer
You upload the public key, ~/.ssh/id_ed25519.pub. By pressing Enter at the passphrase prompt you skipped the passphrase, so the private key is stored unencrypted on disk.
Exercise
You already have ~/.ssh/id_ed25519 for personal projects and want a second key for work. What should you do at the file location prompt?
Show answer
Enter a different path, such as ~/.ssh/ided25519work, so the existing key is not overwritten.