简化SSH连接

ssh键认证

备将

如果在Mac上尚未生成.ssh/id_rsa.pub密钥,请执行以下命令在Mac上生成。

mac:~ $ ssh-keygen -t rsa -b 4096

我們將在拉茲貝利派(Raspberry Pi)上設置Mac端的公鑰,以便通過密鑰驗證進行登錄。

mac:~ $ ssh-copy-id -i .ssh/id_rsa.pub pi@raspberrypi.local
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: ".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: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
pi@raspberrypi.local's password: 

Number of key(s) added:        1

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

确认

mac:~ $ ssh pi@raspberrypi.local
Linux raspberrypi 6.1.21-v8+ #1642 SMP PREEMPT Mon Apr  3 17:24:16 BST 2023 aarch64

The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
Last login: Mon Jul 17 16:02:31 2023 from 240b:11:bc00:7d00:3c19:1e17:4098:36d5
pi@raspberrypi:~ $ 

无需输入密码,就可以进入。

设定能够使用昵称进行登录

创建设置文件

在 Mac 的 ~/.ssh/config 文件中,使用以下内容进行设置。可以选择追加或新建一个文件。

Host raspi 
HostName raspberrypi.local 
port 22
User pi 
IdentityFile ~/.ssh/id_rsa

用昵称登录

mac:~ $ ssh raspi
Linux raspberrypi 6.1.21-v8+ #1642 SMP PREEMPT Mon Apr  3 17:24:16 BST 2023 aarch64

The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
Last login: Mon Jul 17 16:09:13 2023 from 240b:11:bc00:7d00:3c19:1e17:4098:36d5
pi@raspberrypi:~ $