How to permanently add private ssh key(macOS, Windows, Ubuntu)?
In our increasingly connected world, the security of our digital interactions is more critical than ever. Just like you lock your front door when you leave the house, you need to secure your online presence too.
One of the keys to this virtual lock is called an SSH Key. If you've ever tried accessing remote servers or pushing commits to your Git repository, you're probably familiar with the importance of these keys.
Welcome to your personal guide on 'How to Permanently Add a Private SSH Key on macOS, Windows, and Ubuntu'. I've crafted this post with you in mind, giving you the tools to navigate the world of SSH keys with ease and confidence, no matter the operating system you're using.
Whether you're a veteran sysadmin, a coder in the making, or just someone who loves exploring the tech world, this guide is for you.
Understanding and managing SSH keys is not just for the 'tech gurus' anymore. It's a skill that is becoming as essential as knowing how to create a password. And I'm here to make this journey as smooth as possible for you.
I'll take you through the process of adding a private SSH key, permanently, on your macOS, Windows, or Ubuntu system. By the end of this guide, you'll be navigating SSH communications like a pro, ensuring your digital interactions are not just seamless, but also secure.
Table of Content
- Generate SSH key pai on macOS, Windows, Linux
- macOS - adding private key permanently to ssh-agent
- Windows - adding private key permanently to ssh-agent
- Linux - adding private key permanently to ssh-agent
- How to verify git SSH connection and SSH key?
- Troubleshooting your github SSH Connection?
1. Generate SSH key pai on macOS, Windows, Linux
Irrespective of what operating system you are using there are three ways to work with your GitHub repository by cloning the repository via HTTPS, SSH and GitHub CLI.
Well all the methods(HTTPS, SSH, GitHub CLI) are secured and there is nothing wrong with those methods. But when we talk about the scalability and automation then I would personally like to stick with the SSH Method where I need to generate the SSH Key pair(Public & Private)
Here are some benefits which are really awesome -
-
Effortless Authentication: SSH keys offer a hassle-free way to authenticate yourself on Git. Once you've set them up, you won't have to keep typing your username and password every time you push or pull changes. It's like having a VIP pass to your favorite concert - no waiting in line, just straight in!
-
Enhanced Security: With SSH keys, you're significantly stepping up your security game. These keys are nearly impossible to crack, compared to traditional passwords. It's like swapping your old padlock for a high-tech security system.
-
One Key, Multiple Repos: Do you manage multiple Git repositories? With an SSH key, you can authenticate yourself across all your Git repos. No need to remember multiple sets of credentials. It's the ultimate one-key-fits-all solution!
-
Ideal for Automation: If you're a fan of automating your work, SSH keys will be your new best friend. They enable automated scripts to interact securely with your Git repositories. It's like hiring a virtual assistant that works tirelessly to help you manage your repos.
-
Two-Factor Authentication: SSH keys pair perfectly with two-factor authentication for enhanced security. Together, they form a powerful shield that keeps intruders at bay. It's like having a double lock system on your precious data!
Let's break down the steps and how we can achieve these on macOS
1.1 Generate new SSH key
Step 1: Open Terminal
Our adventure begins with Terminal. How to find the terminal -
macOS - You'll find this handy app in your Utilities folder within Applications, if you are using macOS.
Windows - Click on the Windows icon or press the Windows key on your keyboard. Click on the Windows PowerShell app in the search results.
Linux - The most common keyboard shortcut to open the terminal in Linux is Ctrl + Alt + T
. If your distribution has a search function (like Ubuntu's "Dash" or GNOME's "Activities" overview), you can type "Terminal" into the search bar, and it should pop right up
1.2 Check for Existing SSH Keys
Before we create a new SSH key, let's check if you have any existing ones. Type the following command and hit Enter:
1#List all the ssh keys
2ls -al ~/.ssh
If you see any files named id_rsa.pub or file ending with the extension *.pub, you already have an SSH key. If not, let's create a new one!
1.3 Generate a New SSH Key
To create a new key, type the following command in Terminal:
1#Generate the new key
2ssh-keygen -t ed25519 -C "your_email@example.com"
Replace "your_email@example.com" with your actual email.
This command will create a new SSH key using the ED25519 algorithm, which is currently the most secure option. After hitting Enter, Terminal will ask where to save the key. Simply press Enter again to accept the default location(~/.ssh).
1.4 Secure Your SSH Key with a Passphrase
Next, you'll be asked to enter a passphrase. Think of this as a password for your SSH key. Choose a strong one, type it in, and hit Enter. You'll need to enter it again for confirmation.
Remember this passphrase! You'll need it later when adding your SSH key to the ssh-agent and when pushing or pulling from your Git repositories.
Note - The passphrase is not compulsory, so you can skip this part also
1.5 Start the SSH Agent
Now that we have our key, let's start up the SSH agent, a handy program that handles keys for us.
Type the following command and hit Enter:
1#Start SSH Agent
2eval "$(ssh-agent -s)"
1.6 Add Your SSH Key to the SSH Agent
Finally, let's add our shiny new SSH key to the SSH agent. Type in this command:
1ssh-add -K ~/.ssh/id_ed25519
Remember to replace "id_ed25519" with the name of your key if you chose a different name.
2. macOS - adding private key permanently to ssh-agent as well as keychain
2.1 Update ~/.ssh/config and load private keys
- First check if you have the ~/.ssh/config file present or not. If the file is missing then create using the following command
1# Create config file with touch 2 3touch ~/.ssh/config
- Let's open the ~/.ssh/config file add the following lines to it but make sure you should put the correct path and correct ssh key file name
1# Add following lines into the ~/.ssh/config file 2 3Host github.com 4 AddKeysToAgent yes 5 UseKeychain yes 6 IdentityFile ~/.ssh/id_ed25519
2.2 Add ssh private key to keychain
The last improvement which we can do with SSH Key is to add it the macOS Keychain. Run the following command to do that -
1ssh-add --apple-use-keychain ~/.ssh/id_ed25519
3. Windows - adding private key permanently to ssh-agent
3.1 Check for Existing SSH Keys
First, let's check if you already have SSH keys. To do this, we can either use Git Bash or Power shell
Run the following list command to view all the keys -
1# List all the ssh keys present in windows
2
3ls -al ~/.ssh
3.2 Add Your SSH Key to the SSH-Agent
Now, let's add the new SSH key to the SSH-Agent.
- First, ensure the SSH-Agent is running by typing this command in Git Bash:
1#Make sure your ssh agent is running 2 3eval $(ssh-agent -s)
- Then, add your SSH key to the SSH-Agent with this command:
1# Add ssh key permanently to ssh agent 2 3ssh-add ~/.ssh/id_ed25519
4. Linux - adding private key permanently to ssh-agent
4.1 Check for Existing SSH Keys
First, let's check if you already have SSH keys. To do this just open the terminal
Run the following list command to view all the keys -
1# List all the ssh keys present in windows
2
3ls -al ~/.ssh
4.2 Add Your SSH Key to the SSH-Agent
Now, let's add the new SSH key to the SSH-Agent.
- First, ensure the SSH-Agent is running by typing this command in Git Bash:
1#Make sure your ssh agent is running 2 3eval $(ssh-agent -s)
- Then, add your SSH key to the SSH-Agent with this command:
1# Add ssh key permanently to ssh agent 2 3ssh-add ~/.ssh/id_ed25519
5. How to verify git SSH connection and SSH key?
Here's what you need to do:
- Open your Terminal (macOS or Linux) or Git Bash (Windows).
- Type the following command and hit Enter:
1#Replace git@github.com with you repository link 2 3ssh -T git@github.com
6. Troubleshooting your github SSH Connection?
In many cases you might end up with the following error -
ssh: Could not resolve hostname github.com:rahulwagh/jhooq.git: nodename nor servname provided, or not known
Here are the steps for troubleshooting the error -
1. Check HTTPs port - In many cases the communication is restricted on normal http port and you need to provide the secure HTTPS port 443.
1#Verify the connection over the HTTPs port 443
2
3ssh -T -p 443 git@ssh.github.com
2. Add verbose when testing connection- To debug the issue in more indepth you should add verbose to test connection command. Here the example command -
1# Add verbose to ssh test connection command
2
3ssh -vT git@github.com
3. Check SSH Agent is running and using right SSH key- You also need to make sure that you SSH Agent is working in the background and your have the correct key configured.
Here is the command to start the SSH agent -
1# Start the ssh agent
2
3$ eval "$(ssh-agent -s)"
Verify the key by listing them with the following command -
1# Following command will list all the keys
2# Check if you key is loaded or not
3
4ssh-add -l -E sha256
4. Error: Bad file number connection timed out- When you work in the corporate office then for security rasons firewalls and proxies only allow HTTPS traffic, so in such case you need to use the remote URL method to clone the repository -
1# Use HTTPS: remote URL method to clone the repository
2
3git clone https://github.com/rahulwagh/jhooq.git
Refer to this article for error related to SSL certificate problem: self signed certificate in certificate chain