树莓派-配置免密登录云主机

树莓派频繁登陆腾讯云主机,配置免密登录云主机,省的来回输入密码

知识补充

1. 生成ssh密匙

1
ssh-keygen -t rsa -P '' -f ~/.ssh/id_rsa
  • 参数说明:
    • -t 加密算法类型,这里是使用rsa算法
    • -P 指定私钥的密码,不需要可以不指定
    • -f 指定生成秘钥对保持的位置

2. 复制SSH密钥到目标主机,开启无密码SSH登录

1
ssh-copy-id [-i [identity_file]] [user@]machine
  • 参数说明:
    • -i:指定公钥文件

生成ssh密匙

1
ssh-keygen -t rsa

一路回车

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
pi@raspberrypi:~/.ssh $ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/pi/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/pi/.ssh/id_rsa.
Your public key has been saved in /home/pi/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:fCy8svfejc6O3P7KNvx5xz4b/GT5EizIZbE8IGq0eIU pi@raspberrypi
The key's randomart image is:
+---[RSA 2048]----+
| . |
| E o . . |
| o + . o o |
| . =o . * |
| o S.o+ o |
| +o . + .|
| . . . . *o|
| o.. *+o.=B|
| .. o==@===B|
+----[SHA256]-----+

copy 公钥到云主机

1
ssh-copy-id -i ~/.ssh/id_rsa.pub hostname@host
1
2
3
4
5
6
7
8
9
10
pi@raspberrypi:~/.ssh $ ssh-copy-id -i ~/.ssh/id_rsa.pub root@188.131.137.32
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/pi/.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
root@188.131.137.32's password:

Number of key(s) added: 1

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

验证

1
2
3
4
5
pi@raspberrypi:~/.ssh $ ssh root@188.131.137.32
Last failed login: Thu Mar 18 18:05:56 CST 2021 from 107.175.50.219 on ssh:notty
There were 14 failed login attempts since the last successful login.
Last login: Thu Mar 18 17:54:17 2021 from 120.244.218.59
[root@VM-0-12-centos ~]#