1. Generate public key and private key locally
ssh-keygen -t rsa -b 4096
The above command generates two files, id_rsa and id_rsa.pub, in the .ssh folder of the user root directory, id_rsa is the private key file, and id_rsa.pub is the public key file
2. Copy the content of the public key to the authorized_keys of the remote server
Log in to the server, open the .ssh folder, create the authorized_keys file, modify the file permission to 600, and use vim to copy the local public key content to authorized_keys
cd ~/.ssh
touch authorized_keys
chmod 600 authorized_keys
vim authorized_keys # 这步把id_rsa.pub的内容拷贝进去,保存退出
3. Modify the vscode configuration file and specify the location of the private key
Open the above config file and add the IdentityFile field:
Host Server
HostName 目标机ip
User liaobaoxin
IdentityFile "/Users/baoxin/.ssh/id_rsa" #私钥地址
After that, you can log in remotely without password according to the previous process.
4. Springboard machine password-free login
Follow step 2 to create authorized_keys files in the .ssh folder of both the springboard machine and the target machine , copy the local public key content, change the file permissions to 600, modify the vscode configuration file, and add the following to both the springboard machine and the target machineIdentityFile [本地私钥地址]
:
Host JumpMachine
#跳板机ip
HostName 跳板机ip
#登入的用户名,可以不是root
User liaobaoxin
#上文提到的sshd服务端口
Port 22
IdentityFile "/Users/baoxin/.ssh/id_rsa"
#目标机名称,可以随便取
Host TargetMachine
#目标机ip
HostName 目标机ip
#登入的用户名,可以不是root
User liaobaoxin
IdentityFile "/Users/baoxin/.ssh/id_rsa"
#特别注意这里不需要设置端口了
#起跳转作用的代码,可以直接复制,注意JumpMachine需要与前面跳板机名称相同
ProxyCommand ssh -W %h:%p JumpMachine
Log in to the target machine remotely without entering a password