Photo by Jakub Żerdzicki on Unsplash
SSH keys are a secure way to authenticate when connecting to remote servers and services such as GitHub Repositories. In this article, we’ll walk through the process of generating SSH keys on three popular operating systems: Windows, Linux, and macOS.
Why Use SSH Keys?
Using SSH keys enhances security by replacing less secure password authentication. Keys are generated as a pair: a public key (which you share with remote servers) and a private key (which you keep secret). Once set up, you can log into your servers or repositories with ease.
Generating SSH Keys on Windows
Install Git for Windows:
Download and install Git for Windows, which includes Git Bash—a command-line environment that supports SSH.Open Git Bash:
Launch Git Bash from the Start menu.Generate the Key Pair:
Replaceemail@cool-domain.comwith your email address in the ssh-keygen command.
For example, you might run:
ssh-keygen -t rsa -b 4096 -C "email@cool-domain.com"
Explanation:
-
-t rsa: Specifies the type of key to create. -
-b 4096: Sets the key size to 4096 bits for enhanced security. -
-C "email@cool-domain.com": Adds a comment to help identify the key. This can be your email address, computer or device name, or any text that identifies what the key is for.
Follow the Prompts:
Press Enter to accept the default file location (typicallyC:\Users\{USERNAME}\.ssh\id_rsa). Choose a secure passphrase when prompted, or press Enter to continue without a passphrase.Complete the Process:
After confirmation, your SSH key pair is created and stored in your user’s.sshdirectory. Your public key will have a.pubextension. To view the public key, run:
cat ~/.ssh/id_rsa.pub
Generating SSH Keys on Windows
Using Git Bash
- Install Git for Windows: Download and install Git for Windows, which includes Git Bash — a command-line environment that supports SSH.
- Open Git Bash: Launch Git Bash from the Start menu.
- Generate the Key Pair: Run the following command, replacing 'email@cool-domain.com' with your email address:
ssh-keygen -t rsa -b 4096 -C 'email@cool-domain.com'
-t rsa: Specifies the type of key to create.
-b 4096: Sets the key size to 4096 bits for enhanced security.
-C 'email@cool-domain.com': Adds a comment to help identify the key. This can be your email address, computer or device name or anything that lets you know what this key is for
- Follow the Prompts: Press Enter to accept the default file location (typically C:\Users{USERNAME}.ssh\id_rsa). Choose a secure passphrase when prompted or press enter again to continue without a passphrase.
- Complete the Process: After confirmation, your SSH key pair is created and stored in your user’s .ssh directory. Your public key will have a .pub extension. You can view the public key by running cat ~/.ssh/id_rsa.pub.
Generating SSH Keys on Linux
Using the Terminal
- Open a Terminal Window: Most Linux distributions include a built-in terminal.
- Generate the Key Pair: Enter the following command: ssh-keygen -t rsa -b 4096 -C 'email@cool-domain.com' command options are identical to those used in Windows.
- Respond to Prompts: Press Enter to accept the default file location (typically ~/.ssh/id_rsa). Choose a secure passphrase when prompted or press enter again to continue without a passphrase.
- Locate Your Keys: Your new SSH key pair will be stored in the ~/.ssh/ directory. You can view the public key using: cat ~/.ssh/id_rsa.pub
Generating SSH Keys on macOS
Using the Terminal
- Open Terminal: Find Terminal in your Applications > Utilities folder, or use Spotlight to search for it.
- Generate the Key Pair: Run the following command in the terminal: ssh-keygen -t rsa -b 4096 -C 'email@cool-domain.com'
- Follow the Prompts: Press Enter to accept the default file location (typically ~/.ssh/id_rsa). Choose a secure passphrase when prompted or press enter again to continue without a passphrase.
- Verify Your SSH Keys: To display your public key, type: cat ~/.ssh/id_rsa.pub.
Conclusion
Congratulations you have successfully learnt how to generate SSH key pairs. This is a fundamental skill for your IT Toolbox. Remember to keep your private key safe and secure. In a later tutorial, we will learn different ways we can use these keys to enhance your IT workflows.
I encourage you to share your thoughts and ask questions in the comments!
Thank you!
Top comments (0)