fleeting/fb

本机启用ssh免密自动登陆
Edited: Sunday 29 June 2025

aliases:

  • vscode中启用ssh自动登陆
    date created: 11 May 2022
    date modified: 14 March 2023
    title: 本机启用ssh自动登陆
  1. 本地生成 RSA 密钥, 参考:https://www.jianshu.com/p/09b81c07e03f
  2. 输入 (多个用户登录输入多次,将本机的证书公钥拷贝至远程机器):
1ssh-copy-id -i ~/.ssh/id_rsa.pub username@192.168.2.22

成功后则会提示:

1/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/Users/username/.ssh/id_rsa.pub"
2/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
3/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
4username@192.168.2.22's password: 
5
6Number of key(s) added:        1
7
8Now try logging into the machine, with:   "ssh 'username@192.168.2.22'"
9and check to make sure that only the key(s) you wanted were added.
  1. 然后你登录服务器就会自动登录了
1ssh username@192.168.2.22

如果本地之前已经配置好[[ssh config 文件]],则这边好像不用配了,vscode也能直接连上ssh

  1. 这时候在 vscode 中安装好 Remote-SSH 插件, 点击 vscode 左下角绿色箭头打开远程连接, 选择 open configuration file, 输入:
 1Host 2.22_username
 2  HostName 192.168.2.22
 3  User username
 4  PreferredAuthentications publickey
 5  IdentityFile "/Users/username/.ssh/id_rsa"
 6
 7Host 2.22_username2
 8  HostName 192.168.2.22
 9  User username2
10  PreferredAuthentications publickey
11  IdentityFile "/Users/username/.ssh/id_rsa"

保存后, 再次用 vscode 登录即可