This guide provides a detailed look into how to use SSH with Git, including creating SSH keys, adding them to your GitHub account, and cloning repositories using SSH.
What is SSH?
SSH, or Secure Shell, is a cryptographic network protocol that enables secure communication over an unsecured network. It provides a secure channel over an unsecured network in a client-server architecture, offering strong authentication and encrypted data communications. In the context of Git, SSH is crucial for securely pushing to and pulling from repositories without having to enter your username and password each time.
Generating an SSH key for Git
Step 1: Generate a new SSH key
To create a new SSH key use the following command:
ssh-keygen -t ed25519 -C "your_email@example.com"
Replace "your_email@example.com"
with your email address. This command generates a new SSH key using the Ed25519 algorithm, which is considered secure and efficient.
Step 2: Save the SSH key
When you run the ssh-keygen
command, it will ask you to choose a location to save the key. You can press Enter to accept the default location (~/.ssh/id_ed25519
).
Step 3: Set a passphrase (optional)
After specifying the file location, you’ll be prompted to enter a passphrase. This adds an additional layer of security.
Adding your SSH key to the SSH agent
Adding your SSH key to the ssh-agent
can help you manage your SSH keys. The ssh-agent
is a program that holds private keys used for public key authentication (SSH) so that the user is not required to re-enter the passphrase every time. Here's how to add your SSH key to the ssh-agent
:
Step 1: Start the ssh-agent
Before you can add your SSH key to the ssh-agent
, you need to ensure that it's running. You can start it with the following command:
eval "$(ssh-agent -s)"
This command will start the ssh-agent
in the background and print the agent process ID. On some systems, the ssh-agent
might already be running or it might be started automatically at login.
Step 2: Add your SSH private key to the ssh-agent
To add your SSH private key to the ssh-agent
, use the ssh-add
command followed by the path to your private key file. If you created your key with a default name and location, you can simply use:
ssh-add ~/.ssh/id_rsa
If your private key file has a different name or is located in a different directory, you'll need to specify the full path to the file:
ssh-add ~/.ssh/your-custom-key-name
Step 3: Enter your passphrase (if set)
If your SSH key was created with a passphrase (which is recommended for additional security), you will be prompted to enter it when you add the key to the ssh-agent
. Once entered, the ssh-agent
will store the unlocked key in memory and manage it for subsequent authentications, so you won't need to enter the passphrase again during the current session.
Additional tips
- Check loaded SSH keys: You can check which keys are currently managed by the
ssh-agent
with the command:Terminalssh-add -l - Remove keys: If you need to remove keys from the
ssh-agent
, you can use:To remove all keys, you can use:Terminalssh-add -d path/to/keyTerminalssh-add -D
Adding your SSH key to your GitHub account
Step 1: Copy the SSH key to your clipboard
To copy the SSH public key to your clipboard, use the following command:
cat ~/.ssh/id_ed25519.pub | pbcopy
This command reads your public key and copies it into your clipboard.
Step 2: Add the SSH key to GitHub
- Log in to your GitHub account.
- Navigate to the SSH and GPG keys settings page.
- Click New SSH key or Add SSH key.
- Paste your key into the "Key" field.
- Add a descriptive title.
- Click Add SSH key.
Cloning a repository with SSH
Step 1: Find the SSH URL of the repository
Navigate to the repository on GitHub, click on Clone or download, and switch to Clone with SSH. Copy the SSH URL provided.
Step 2: Clone the repository
To clone a repository using SSH, use the following command:
git clone git@github.com:<REPO_OWNER>/<REPO_NAME>.git
Replace <REPO_OWNER>
and <REPO_NAME>
with the owner and name of the repo you'd like to clone.
For further reading see the official GitHub documentation.