GENERATING SSH KEY
What is SSH?
SSH also known as Secure Shell or Secure Socket Shell, is a network protocol that gives users, particularly system administrators, a secure way to access a computer over an unsecured network.
SSH uses cryptography to authenticate and encrypt connections between devices.
SSH runs on top of the TCP/IP protocol suite — which much of the Internet relies upon. TCP stands for Transmission Control Protocol and IP stands for Internet Protocol.
SSH is “secure” because it incorporates encryption and authentication via a process called public key cryptography. Public key cryptography is a way to encrypt data, or sign data, with two different keys. One of the keys, the public key, is available for anyone to use. The other key, the private key, is kept secret by its owner. Because the two keys correspond to each other, establishing the key owner’s identity requires possession of the private key that goes with the public key.
The process of generating SSH keys involves creating a key pair on your local machine.
- Private Key: This key should be kept secret and secure on your machine. It’s used to decrypt information and prove your identity when connecting to a server via SSH.
- Public Key: This key can be freely shared with servers you want to connect to. It allows the server to verify your identity during the SSH connection process.
Here’s a breakdown of how to generate SSH keys on different operating systems:
Linux and macOS:
- Open your terminal application.
- Type the following command, replacing
your_email@example.com
with your actual email address:
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
This command specifies the type of key (RSA is common), key size (4096 bits is recommended for security), and adds a comment with your email address for identification.
Press Enter when prompted for a location to save the key pair. The default location is usually ~/.ssh/
. You can accept the default or specify a different location.
If you want additional security, you can set a passphrase for your private key. When prompted, enter a strong passphrase and type it again for confirmation.
Windows (using Git Bash):
- If you don’t have Git Bash installed, you can download Git for Windows which includes it: https://git-scm.com/downloads
- Open Git Bash.
- Follow steps 2–4 from the Linux/macOS instructions above.
Once the process is complete:
- Your private key will be saved as
id_rsa
(or the filename you specified) in the chosen directory. - Your public key will be saved as
id_rsa.pub
in the same directory.
View Your Public Key:
Once the key pair is generated, you’ll see output indicating where your public key is saved. By default, it’s saved in the same location as the private key but with a .pub
extension. You can view the public key by typing: cat ~/.ssh/id_rsa.pub
Replace ~/.ssh/id_rsa.pub
with the path to your public key if you saved it in a different location or gave it a different name.
Copy Your Public Key
Copy the entire contents of the public key, starting with ssh-rsa
and ending with your email address or username.
That’s it! You’ve successfully generated an SSH key pair. Now you can use the public key to authenticate with services that support SSH key authentication, such as GitHub, GitLab, or your server.