Generating an SSH key pair is a straightforward process. Here are the steps to do it on various operating systems. The basics apply to most Unix-like operating systems, including Linux and macOS, as well as Windows if you are using a terminal like Git Bash or Windows Subsystem for Linux (WSL).
For Linux and macOS
-
Open Terminal: You can find the terminal in your applications or by searching for it.
-
Run the SSH Keygen Command: Type the following command and press Enter:
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
-t rsa: specifies the type of key to create (RSA).
-b 4096: specifies the number of bits in the key. 4096 is generally recommended for security.
-C "your_email@example.com": adds a label (comment) to the key, typically your email.
-
Specify the File Location: You will be prompted to enter a file path to save the key. Press Enter to accept the default location (~/.ssh/id_rsa).
-
Set a Passphrase: You'll have the option to enter a passphrase for added security. This is optional but recommended. Enter a passphrase and confirm it.
-
Key Generation Complete: Upon completion, you should see output indicating where the keys were saved.
For Windows (using Git Bash or WSL)
-
Open Git Bash or WSL Terminal.
-
Run the SSH Keygen Command: As with Linux, use:
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
-
File Location: Accept the default path or specify a new one.
-
Set a Passphrase: Enter a passphrase if desired.
-
Completion Message: Check for confirmation of the key pair generation.
Locating Your SSH Keys
By default, your SSH keys will be stored in the ~/.ssh directory:
- The private key will be in
~/.ssh/id_rsa (do not share this key).
- The public key will be in
~/.ssh/id_rsa.pub (this key can be shared).
Adding Your SSH Key to the SSH Agent
If you want your SSH client to manage your keys automatically, add your SSH key to the ssh-agent:
-
Start the SSH agent:
eval "$(ssh-agent -s)"
-
Add your SSH private key:
ssh-add ~/.ssh/id_rsa
Copy the Public Key
To use your SSH key for authentication, you will need to copy the public key to the server you want to access. You can display the public key by running:
cat ~/.ssh/id_rsa.pub
You can then copy it to your clipboard and add it to your server's ~/.ssh/authorized_keys file.
If you have any specific questions or need further assistance, feel free to ask!