Set up ssh to log in to the Linux server and use ssh on git


Preface

When we use a remote Linux server or use git, we need to enter a password to log in and operate. It is a bit inconvenient to enter the password every time when performing frequent operations. Then we can use the convenience of ssh to authenticate and log in, which saves time. Go to the step of entering the password, because you don’t have to enter the password frequently and also avoid the risk of password leakage, killing two birds with one stone.


environment

system software Version
Windows 10
Windows terminal 1.9
Cloud server CentOS 7

Configuration

client

  1. Open a terminal in Windows and enter the command:ssh-keygen -t rsa -b 4096
  2. According to the prompt Enter file in which to save the key, enter the key file name.
  3. Follow the prompt Enter passphrase (empty for no passphrase), enter the password of the key file, and press Enter directly without setting a password.
  4. A pair of key/public key files totaling 2 files will be generated in the C:\Users\xxx(user).ssh directory, or in the upper directory.
  5. Upload the public key (*.pub suffix file) to the remote Linux server. You can choose to use the FTP tool or the scp command.
  6. Open terminal-Settings-Configure the specified server segment-Command line input: ssh [email protected] -p 22 -i C:\Users\hhh\.ssh\你刚才生成的私钥, save

Service-Terminal

  1. Open the ssh configuration file: vim /etc/ssh/sshd_config
  2. Configuration item setting: PubkeyAuthentication yes
  3. The ssh-key public key file stores the directory configuration item: AuthorizedKeysFile .ssh/authorized_keys, which is in ~/.ssh/authorized_keysthis file by default.
  4. Open it ~/.ssh/authorized_keys, paste the contents of the public key file obtained above here, and save it.

possible problems

Are you still prompted to enter a password when logging in using the key command? ?

It may be due to permission issues with the .ssh/authorized_keys file.
Also pay attention to which Linux user directory the .ssh/authorized_keys file is placed in.

If you see: in the sshd service log Authentication refused: bad ownership or modes for file /root/.ssh/authorized_keys, it is a permission issue. Solution: Use command: chmod 644 .ssh/authorized_keysAdjust file permissions.


How does git use ssh-key

The above is what I will talk about today. This article only briefly introduces the use of ssh-key, and ssh can also be used for git account verification. The difference in operation method is that the remote server manually puts the public key into the specified directory. The git configuration is Go to the git console page to bind the public key to the account, and then you can use it in the same way.

Guess you like

Origin blog.csdn.net/wangxudongx/article/details/125363267