One-stop solution: SSH Public Key remote login, connection to Linux or Github/Gitee

1. Local Vscode remote password-free login to Linux

1. VScode adds Remote-SSH extension

  • Open the extension store in VScode, search for Remote-SSH
    Remote-SSH
  • In Vscode, use the command ctrl + shift + p (Windows system) to open the command panel, enter Remote-SSH, and click ***Remote-SSH:Open SSH Configurationo File***, as shown in the figure below:
    insert image description here
  • Enter the C:\Users\username.ssh\config file and enter the following:
Host xxx     
  HostName  xxx	
  User xxx
  IdentityFile xxx

Among them,
- Host can be started casually (it can be the same as HostName)
- HostName writes the Linux server or virtual machine ip to be connected (query command: ifconfig -a )
- User writes the user name to log in, such as root
- IdentityFile writes the one generated later SSH private key, generally in the C:\Users\username.ssh folder (this folder is a hidden folder)

2. Configure the secret key

  • Enter the command line under the Windows system (keyboard: win+R, enter cmd, or use other methods), and enter the command in the command line:
# 注意,如果你直接使用这样的命令,然后一路回车,
# 会默认在C:\Users\用户名\.ssh文件夹下生成  id_rsa、id_rsa.pub两个文件
# id_rsa(私钥,放在客户端,这种情况下就是你的Windows系统)
# id_rsa.pub(公钥,放在服务端,这种情况下就是你的Linux虚拟机或者你的服务器)
ssh-keygen -t rsa # 或 ssh-keygen
  • If you are not messing around like me, such as connecting to multiple remote servers and distinguishing the generated key files, then this step can be skipped and go to the next step
# 事实上,ssh-keygen后面可以跟很多参数,比如:
#	-t = The type of the key to generate	(密钥的类型)
#	-C = comment to identify the key	(用于识别这个密钥的注释,一般写邮箱) 
# 	为了区分生成的秘钥文件,可以用-f参数指定生成的秘钥文件名(我就是这样做的)
ssh-keygen -t rsa -f C:\Users\用户名\.ssh\Linux_id_rsa
  • Copy Linux_id_rsa.pub to the server ~/.ssh directory (there are many ways to do this, you can use other connection tools such as xftp), and then execute in Linux bash:
cd ~/.ssh
# 为了保险起见,可以进入Linux_id_rsa.pub文件查看是否正常
cat -A Linux_id_rsa.pub
# 比如Windows上传到Linux系统的文本文件,一般会有 ^M ,如果你没有出现这种情况,则跳过接下来的一条命令:
dos2unix Linux_id_rsa.pub	# 你也可以通过其他方式,自行百度
# Linux_id_rsa.pub(公钥文件)没有什么问题之后,执行以下命令:
cat Linux_id_rsa.pub >> authorized_keys	# 经过验证,此步应该是必须
# authorized_keys文件中存储着本地系统可以允许远端计算机系统ssh免密码登陆的账号信息
# 你也可以通过xftp连接linux,然后打开Linux_id_rsa.pub将其中的内容复制到authorized_keys文件(若不存在则创建)
  • After generating the secret key, you only need to go back to the local Windows or VScode, and write in the config file (mentioned before) under the C:\Users\username.ssh folder:
Host xxx     
  HostName  xxx	
  User xxx
  IdentityFile "C:\Users\*****\.ssh\id_rsa"

Among them, IdentityFile corresponds to the location of the generated private key file.

  • Next, you can log in to the Linux virtual machine or remote server without secret in VScode.

2. Linux virtual machine (or Linux server) connection management Github

The basic steps of Linux connection management Github are the same as the above process, but at this time, Linux is equivalent to the client, and Github is equivalent to the server.

1. Execute the following commands sequentially in bash to generate a secret key

# 因为我后面还有连接Gitee,为了区分,指定生成了秘钥文件名
ssh-keygen -t rsa -C "your email" -f ~/.ssh/github_id_rsa
# 以上命令执行过程中,会问你两次
# 第一次是:Enter passphrase (empty for no passphrase):	直接回车就行
# 第二次是问你确认以上passphrase,再次直接回车就行
# 如果你不使用 -f参数指定文件名,那么会问你三次,其中第一次是问你把秘钥文件放在哪,
# 若是直接回车的话,会默认放在~/.ssh文件夹下,并且默认秘钥文件名为:id_rsa和id_rsa.pub
# 接下来问的两次和之前相同

2. Create a warehouse (remote library) on github

  • I won't talk about this, it should be.

3. Add the generated public key to Github

  • Enter Github->login to personal account->click on the avatar in the upper right corner->select Settings- >select-> SSH and GPG keys- >select New SSH key- >paste the contents of the public key file github_id_rsa.pub into it (title title customize).

4. Test connection

ssh -vT [email protected]
  • If the execution is successful, and there is no known_hosts file in your ~/.ssh folder , the system will ask you once, just enter yes, and then a known_hosts file will be generated , which actually records the public key connected to Github.
  • If ***Permission denied (publickey)*** appears, it means that you have encountered a common problem, and I have been working on it for a long time~ To put it simply, the first
    method I took was to execute in bash:
# 将专用密钥添加到ssh-agent的高速缓存中
ssh-add ~/.ssh/github_id_rsa

After executing this command, it proves that it is indeed feasible, but when you see the word "cache" , you should be able to realize that there may be problems. Sure enough, when I turned off the computer happily, the problem was Permission denied (publickey) when I turned it on again the next day Appeared again! And also found another problem:

问题1:在Vscode中远程打开Linux终端,git push的时候,出现错误
问题2:重启电脑后,Permission denied(publickey)反复出现

There are two ways to solve the above two problems:

第一种方式(临时性):
之前通过ssh-add命令将密钥添加到ssh-agent
是只在Linux中进行的,为了VScode中同步,你需要在VScode中打开Linux终端,
然后执行相同的命令,这样你就可以同时在Linux远程服务器和本地VScode中远程同步Github了。
第二种方式(永久性):参见步骤5.

5. Configure the config file of ssh in Linux

  • Under Linux, enter the ~/.ssh folder, create a config file, and enter in the file:
Host github.com
    HostName github.com
    IdentityFile ~/.ssh/github_id_rsa
    PreferredAuthentications publickey
    User xxx

Host gitee.com
    HostName gitee.com
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/gitee_id_rsa
    User xxx
  • What this step accomplishes is: configure multiple ssh key pairs and manage multiple ssh permanently
  • Among them PreferredAuthentications publickey: PreferredAuthentications is to specify the order of client authentication methods, the default value of this option is: "gssapi-with-mic, hostbased, publickey, keyboard-interactive, password"
  • Among them, the Host can be started at will, and you can even configure multiple Hosts for the same HostName, refer to

6. Synchronize Github

  • Note: SSH is for remote password-free login. In fact, it is not necessary, but just for convenience. If SSH remote login is not performed, the following two situations will occur:
1.VScode远程连接Linux服务器时,每次打开VScode选择打开远程Linux文件时,
都需要输入密码;
2. Linux服务器或者本地Windows(在二者SSH连接之后),执行git push等命令
同步Github时,都要输入邮箱和密码。
  • Finally, host your own code on Github, open/create a folder on the Linux server or local Windows, and execute the following commands in sequence:
cd yourfile
git config --global user.name “your name”
git config --global user.email “your email”

git config --list # 列出关于当前git的所有配置信息
git init                                  # 初始化
git add .                                 # 添加文件
git commit -m "此次提交要备注的信息"        # 提交并备注信息

# 提交到Github
# 添加远程仓库
git remote add origin [email protected]:yourname/test.git
# 推送
git push -u origin master

3. Summary

The above accomplishes the following:

  • VScode remote password-free login to Linux (it can be a virtual machine or a Linux server)
  • Linux synchronizes Github/Gitee, and the operation of synchronizing Gitee is roughly the same as synchronizing Github
  • The git synchronization process is also applicable to local computer synchronization Github/Gitee

4. Reference

VScode + Remote-SSH Secret-free connection server
git Github
Permission denied (publickey)
Github permission denied: ssh add agent has no identities
Configure multiple ssh key pairs and permanently manage multiple ssh
Gitee configure multiple accounts Detailed
ssh: ssh-agent、 ssh-add

that's all

Guess you like

Origin blog.csdn.net/zp_stu/article/details/127298027