How to get public key ssh key of git

created at 07-09-2021 views: 4

Recently I found that my memory is diminishing, so this is a very detailed practice that will only be used once for a long time. After a long time, it is easy to be forgotten. Almost done.

Usually when we want to clone code from a new github repository. We need to configure our local git public key in the github repository to clone the code normally.

Why do I need ssh key?

Use the SSH protocol to connect to remote servers and services and authenticate to them. SSH keys can be used to connect to GitHub without the need to provide a username or password every time you visit.

1 First : check the public key

check whether there is a public key:

cat ~/.ssh/id_rsa.pub

If cat: /Users/********/.ssh/id_rsa.pub: No such file or directory is displayed, it means that the public key has not been created, and you need to create the public key yourself.

2 How to generate a public key:

2.1 You can enter:
Designated mailbox: ssh-keygen -t rsa -C "mmqiu@thoughtwros.com"
Or do not specify the mailbox: ssh-keygen

Then press Enter all the way and use the default value, because this Key is only used for simple services, so there is no need to set a password.

xx@MacBook-Air ~ % ssh-keygen           
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/xinna/.ssh/id_rsa): 
/Users/xinna/.ssh/id_rsa already exists.
Overwrite (y/n)? y
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /Users/xinna/.ssh/id_rsa.
Your public key has been saved in /Users/xinna/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:UmEi7XKNfPMu1ZkNPtJsGGkCyTrfOjTrF+IQAn7sqwE xinna@sionas-MacBook-Air.local
The key's randomart image is:
+---[RSA 2048]----+
|    .o..o        |
|.    .=o .       |
|...  + +.  .     |
| ..o= =.= + .    |
|E o. *.oS= B =   |
|.  .. =.o = X .  |
| .  .+ = + o .   |
|  ..  = o .      |
| ..  ..o .       |
+----[SHA256]-----+
————————————————

It first asks you to confirm the location where the public key is saved (.ssh/id_rsa), there is no need to enter it here, just press Enter.
Then it will let you repeat a password twice. If you don't want to enter the password when using the public key, you can leave it blank and press Enter.

If the server needs a public key, you can directly configure id_rsa.pub in the .ssh directory, id_rsa is the private key and must be kept secret

2.2 Use cat ~/.ssh/id_rsa.pub to view the public key

xinna@sionas-MacBook-Air ~ % cat ~/.ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDkL0lFa+JqVPDiFeE2BvaMwWH496wkc7e+IVOZoJ+UzooK7B6/IBZ7HcBoHs8ckF3DdFpdJonwGkNFlCnpVDs+wxamdUgPqARJq7nEe5DeG5UQ4LCyZSwRA1OUOl/3gpAK+oYm/v11EgdGoajYJDUCTTp5fsSxaCl4s/Ym8uqUWR+Ba4Aoz117ka4i6yX1EiFfkv2ztbES+Lq9wNs57vK/ktoQmBLslGq2HqTt//WrJJkK2C0HlVSV9lkLZgH1qd5K7dxP1rY7IOrdwvhtwVUGE5V3fmBWGqnlZWJ2tkYczlnPWb0uX0FqP1qod8GZGBoJfgVhGShQBFMXdgqY+V9h xinna@sionas-MacBook-Air.local

cat ~/.ssh/id_rsa.pub

check the public key

Clone the code from github via access_token

For public code, you can clone without user-related information, but for private projects, you must have user-related information to clone or pull. It is impossible to write the user name and password in some scripts, so there is access_token .

get private token method from GitHub

1. On the Github personal Settings page, find Developer Settings at the bottom:

personal Settings page

2. Click Personal access tokens in Developer Settings:

Personal access token

3. Enter the description of the Token, and then check the permissions of the Token, generally check all of them, and click Generate:
note

  • When generating the token, you need to select the repo option, otherwise the git clone will fail.
  • The generated token needs to be kept safe like a password, because you will not be able to see the token after you leave this page.

4. The Token code is generated by the system, copy and save it locally, because it will not be seen when you open it again.

git use access_token clone code
clone command:

git clone https://<access_token>@github.com/xx.git


# for example:
git clone https://1737a2a66d27524992499242f2@github.com/username/projectName.git
git
Please log in to leave a comment.