生成公钥
命令ssh-keygen可在机器上生成公钥和相关文件,等待输入的时候可以直接回车使用默认配置
[admin@iZ280626x93Z ~]$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/admin/.ssh/id_rsa): Created directory '/home/admin/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/admin/.ssh/id_rsa.
Your public key has been saved in /home/admin/.ssh/id_rsa.pub.
The key fingerprint is:
a4:ae:8b:25:cd:3f:89:2a:ad:46:47:55:a2:92:4f:4e admin@iZ280626x93Z
The key's randomart image is:
+--[ RSA 2048]----+
| ... |
| . ... |
| o E. . |
| *. o |
| .o . S |
| . + . |
|..o +... |
|...+.oo |
|ooo.o... |
+————————+
这个时候在用户目录下就会生成一个.ssh文件夹,里面有公钥相关文件,其中id_rsa.pub就是当前机器B的公钥,只要在机器A上的~/.ssh/authorized_keys文件中把B机器的公钥加进去,B机器就可以免密码登录A,用户名取决于公钥在A机器那个用户目录下。
➜ ~ cd ~/.ssh
➜ .ssh ls
id_rsa id_rsa.pub known_hosts
➜ .ssh
修改远程机器
vim authorized_keys //把B机器的公钥加进去
chmod 600 authorized_keys //修改文件权限
如果希望ssh公钥生效需满足至少下面两个条件:
- .ssh目录的权限必须是700
- .ssh/authorized_keys文件权限必须是600