SSH connection Git

Time:2022-3-21

When we submit code, the first thing we need to do is to let us connect to the remote warehouse server, that is, we need to know the remote server address, prove our identity and log in to the remote server.

this is actually asshConnection, let me tell yousshAll right.

we can usessh [email protected] -p portMake an SSH connection and enter the password. Here are four data they need to remember:Remote server IPRemote server SSH port numberuser namepassword。 Let’s analyze them one by one (the order is disrupted):

  1. Remote server IP, which can be modified byC:\Windows\System32\drivers\etcinhostFile resolution. This is common in native niginx testing, where an easy to remember alias is used instead of an IP address
  2. User name and password, they are together, used to identify users. This is also a good solution. You can generate public and private keys locally. When interacting with the remote server, the private key is used to encrypt the data, and the remote server uses the public key to verify the signature.
  3. The remote server port number is generally 22, but coincidentally, our company gitlab is a custom, which is difficult to remember.

We can find that it is difficult to remember four data. However, the SSH port number of a server is fixed and the password of an account is fixed. Can we remember only two data and fill in the remaining two automatically?

There are solutions, and they are here.sshfolderconfigIn the file

What does the config file look like? Let’s have a look

# gitlab
Host gitlab. xxx. Net # alias, equivalent to Baidu com
Hostname gitlab. xxx. Net # host name / address, which can be IP or domain name
Port 33363 # port number
User season # user name 
Preferredauthentications publickey # encryption method - public key
IdentityFile C:\Users\season\. ssh\id_ rsa_ XXX # encrypted file

# gitee
Host gitee.com
Hostname gitee.com
User season-gitee
Port 22
PreferredAuthentications publickey
IdentityFile C:\Users\season\.ssh\id_rsa_gitee

oneconfigIn the file, multiple can be configured and used#notes

configThe file contains: server address + port number, user name + authentication information (password, etc.), which can solve the problem of too much data, which is not easy to remember and inconvenient to manage.

then configure our public key to the remote server, and the related operations of SSH are completed. At this time, you can usessh -T [email protected]Test


although the test is successful at this time, there is a problem that needs to be solved, that isgitThe user name and password in have not been configured. This is very simple. Just configure it.

Configure user name and password respectively. It can be configured globally or according to the project. If the user information is the same, configure global; If different accounts correspond to different user information, it should be configured honestly according to the project

//Current user
$ git config --global user.name season
$ git config --global user.email [email protected]

//Current project
$ git config user.name season
$ git config user.email [email protected]

In the end:

  1. .sshFolderconfigThe file may not exist and needs to be added manually.
  2. The command to generate the key isssh-keygen -t rsa -C "[email protected]"
  3. configIn the fileuser, ingitIt’s useless in,gitOnly care about the key, which can verify user information, so usessh -TDuring the test, no matter what the user writes, welcome XXX will be returned. This name is usually the account name.
  4. In fact, the public key still has a smaller chance than winning the 100e lottery, but we can’t think it will never happen. But it’s much safer than using a password, isn’t it?

Finish, sprinkle flowers

reference resources:
SSH user profile config manages SSH sessions