/ 开发笔记

Mac如何配置使用ssh key登录ubuntu

首先,你要先有一个密钥。
然后,执行以下命令将密钥上传到ubuntu服务器上

ssh-copy-id <ubuntu_user>@<ubuntu_host>

其中
<ubuntu_user> 指要使用这个key登录的远程主机的用户
<ubuntu_host> 指要登录远程主机的ip或者域名

系统会提示你输入该用户的登录密码。
完成后,你可以直接用以下命令直接登录了:

ssh <ubuntu_user>@<ubuntu_host>

示例

My-MacBook-Pro:~ xxx$ ssh-copy-id xxx@xxxx.com
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/Users/xxxx/.ssh/id_rsa.pub"
The authenticity of host 'xxxx.com (xxx.xxx.xxx.xxx)' can't be established.
ECDSA key fingerprint is SHA256:XZBYWpmhN61XwwCkPx0tEVKwntV7gzGS5hGwTld28cc.
Are you sure you want to continue connecting (yes/no)? yes
/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: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
xxx@xxxx.com's password:

Number of key(s) added: 1

Now try logging into the machine, with: "ssh 'xxx@xxxx.com'"
and check to make sure that only the key(s) you wanted were added.

注意,如果你的系统没有安装 ssh-copy-id,则需要先安装这个命令:

brew install ssh-copy-id