📜  ssh key git - Shell-Bash (1)

📅  最后修改于: 2023-12-03 15:05:21.456000             🧑  作者: Mango

SSH Key Git - Shell/Bash

If you are a programmer or a developer who uses Git to manage your code, you already know that every time you push or pull from a remote repository, Git requires you to authenticate by typing in your username and password. This can be time-consuming and annoying, especially if you frequently interact with Git.

SSH key Git is a secure way to authenticate with Git without having to type in your username and password every time. An SSH key is a cryptographic key pair that is used to securely identify you to Git. SSH keys are comprised of a public key and a private key. The private key is kept on your local machine and should never be shared with anyone. The public key is uploaded to the remote Git server and is used to verify that you are who you say you are.

In this tutorial, we will walk through the steps of generating an SSH key and using it to authenticate with Git through the shell/Bash.

Step 1: Check for existing SSH keys

Before creating a new SSH key, it’s important to check whether there are existing SSH keys on your machine. Enter the following command in your terminal:

ls -al ~/.ssh

This will list all files in your ~/.ssh/ directory. If you already have an SSH key, you should see files named id_rsa and id_rsa.pub. If you don't see these files, proceed to step 2.

Step 2: Generate a new SSH key

To generate a new SSH key, enter the following command:

ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

This command will generate a new RSA key pair, with a key length of 4096 bits, and a comment that will identify the key.

You will be prompted where to save the key. Press Enter to accept the default location (~/.ssh/id_rsa), or enter a new file path if desired. You will also be prompted to enter a passphrase. A passphrase is an extra layer of security for your key, and is optional but recommended. If you enter a passphrase, you will be prompted to enter it again to confirm.

Once the key is generated, you should see output that looks like this:

Generating public/private rsa key pair.
Enter file in which to save the key (/Users/you/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /Users/you/.ssh/id_rsa.
Your public key has been saved in /Users/you/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:abcdefghijklmnopqrstuvwxyz1234567890 your_email@example.com
Step 3: Add your public key to your Git account

Now that you have generated your SSH key, you need to add your public key to your Git account. Navigate to your Git account and go to your Settings. Select SSH and GPG keys on the left-hand menu, then click New SSH key.

Copy the contents of your id_rsa.pub file (you can use the cat command to print the contents in the terminal) and paste them into the Key field. Give the key a descriptive title for your reference, then click Add SSH key.

Your public key is now added to your Git account, and can be used to authenticate with Git.

Step 4: Test your SSH connection with Git

To test your SSH connection, enter the following command:

ssh -T git@github.com

This command will attempt to connect to the Git server using SSH. You may be prompted to accept the server's RSA key fingerprint. Type yes to continue.

If the connection is successful, you should see output that looks like this:

Hi username! You've successfully authenticated, but GitHub does not provide shell access.

Congratulations! You have now successfully authenticated with Git using your SSH key.

Conclusion

Using an SSH key to authenticate with Git can save time and increase security. By following these steps, you should now have a better understanding of how to create and use an SSH key in the shell/Bash. Happy coding!