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