生成SSH密钥对(如果还没有)
ssh-keygen -t rsa -b 4096
按照提示操作,可以按回车键接受默认的文件名和位置(通常在~/.ssh/id_rsa和~/.ssh/id_rsa.pub)。
将公钥复制到远程服务器
使用ssh-copy-id命令将公钥复制到远程服务器上。首先,你需要知道远程服务器的IP地址或主机名以及用户名。例如,如果你的远程服务器是example.com,你可以使用:
ssh-copy-id user@example.com
这里user是你的远程服务器用户名。系统会提示你输入远程服务器的密码。完成此步骤后,你应该能够无密码登录到远程服务器
在本地Mac上,尝试通过SSH连接到远程服务器以验证免密登录是否设置成功
ssh user@example.com
如果不需要输入密码即可登录,说明SSH免密登录配置成功
提示以下错误,则删除.ssh下面的known_hosts
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/Users/zxh/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: ERROR: @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
ERROR: @ WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! @
ERROR: @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
ERROR: IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
ERROR: Someone could be eavesdropping on you right now (man-in-the-middle attack)!
ERROR: It is also possible that a host key has just been changed.
ERROR: The fingerprint for the ED25519 key sent by the remote host is
ERROR: SHA256:hfkhDs2s99gGheDVnRGpy99D9f8jgl8VC9Id+zOv0cQ.
ERROR: Please contact your system administrator.
ERROR: Add correct host key in /Users/zxh/.ssh/known_hosts to get rid of this message.
ERROR: Offending ECDSA key in /Users/zxh/.ssh/known_hosts:6
ERROR: Host key for 1.95.140.140 has changed and you have requested strict checking.
ERROR: Host key verification failed.