1. 生成秘鑰對
[root@localhost ~]# ssh-keygen -t rsa #生成密鑰對,-t代表類型,有RSA和DSA兩種
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): #密鑰文件默認存放位置,按Enter即可
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase): #輸入密鑰鎖碼,或直接按 Enter 留空
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa. #生成的私鑰
Your public key has been saved in /root/.ssh/id_rsa.pub. #生成的公鑰
The key fingerprint is:
SHA256:K1qy928tkk1FUuzQtsdfsdf6dfdfPvHw9lQ+KNuZ4 root@localhost.localdomain
The key's randomart image is:
+---[RSA 2048]----+
| +. |
| o * . |
| . .O + |
| . *. * |
| S =3+ |
| . =... |
| .oo =+ov+ |
| ==o+B*3o. |
| oo.3=EXO. |
+----[SHA256]-----+
2. 分發公鑰
ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.1.2
#修改權限
chmod 600 /root/.ssh/authorized_keys
3. 鎖定秘鑰文件,不能被更改
chattr +i /root/.ssh/id_rsa.pub
chattr +i /root/.ssh/authorized_keys