[root@localhost soft]# ssh-keygenGenerating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
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:pvR6iWfppGPSFZlAqP35/6DEtGTvaMY64otThWoBTuk root@localhost.localdomain
The key's randomart image is:+---[RSA 2048]----+
| . o. |
|.o . . |
|+. o . . o |
| Eo o . + |
| o o..S. |
| o ..oO.o |
| . . ..=*oo |
| ..o *=@+ . |
| .oo=+@+.o.. |
+----[SHA256]-----+123456789101112131415161718192021
生成之后会在用户的根目录生成一个 “.ssh”的文件夹,进入“.ssh”会生成以下几个文件
[root@localhost ~]# ls -a. .. anaconda-ks.cfg .ansible .bash_history .bash_logout .bash_profile .bashrc .cache .config .cshrc .scalac .scala_history .ssh .tcshrc .viminfo12
进入“.ssh”会生成以下几个文件
[root@localhost .ssh]# lsauthorized_keys id_rsa id_rsa.pub known_hosts12
authorized_keys:存放远程免密登录的公钥,主要通过这个文件记录多台机器的公钥
id_rsa : 生成的私钥文件
id_rsa.pub : 生成的公钥文件
know_hosts : 已知的主机公钥清单
如果希望ssh公钥生效需满足至少下面两个条件:
1) .ssh目录的权限必须是700
2) .ssh/authorized_keys文件权限必须是600
二、远程免密登录
1.原理:
- 客户端向服务器端发出连接请求
- 服务器端向客户端发出自己的公钥
- 客户端使用服务器端的公钥加密通讯密钥然后发给服务器端
- 如果通讯过程被截获,由于窃听者即使获知公钥和经过公钥加密的内容,但不拥有私 钥依然无法解密(RSA算法)
- 服务器端接收到密文后,用私钥解密,获知通讯密钥
2.常用以下几种方法:
2.1通过ssh-copy-id的方式
[root@localhost ~]# ssh-copy-id -i root@worker1/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.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 keysroot@worker1's password:
Number of key(s) added: 1Now try logging into the machine, with: "ssh 'root@worker1'"and check to make sure that only the key(s) you wanted were added.12345678910
2.2通过scp将内容写到对方的文件中
[root@test .ssh]# scp -p ~/.ssh/id_rsa.pub root@192.168.91.135:/root/.ssh/authorized_keysroot@192.168.91.135's password: id_rsa.pub 100% 408 0.4KB/s 00:00 [root@test .ssh]# [root@test .ssh]# [root@test .ssh]# [root@test .ssh]# ssh root@192.168.91.135Last login: Mon Oct 10 01:27:02 2016 from 192.168.91.133[root@localhost ~]#123456789
或者
$ scp ~/.ssh/id_rsa.pub root@
2.3通过Ansible实现批量免密
将需要做免密操作的机器hosts添加到/etc/ansible/hosts下:
[Avoid close]192.168.91.132192.168.91.133192.168.91.1341234
执行命令进行免密操作
ansible
示例:
[root@test sshpass-1.05]# ansible test -m authorized_key -a "user=root key='{{ lookup('file','/root/.ssh/id_rsa.pub') }}'" -kSSH password: ----->输入密码
192.168.91.135 | success >> {
"changed": true,
"key": "ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEArZI4kxlYuw7j1nt5ueIpTPWfGBJoZ8Mb02OJHR8yGW7A3izwT3/uhkK7RkaGavBbAlprp5bxp3i0TyNxa/apBQG5NiqhYO8YCuiGYGsQAGwZCBlNLF3gq1/18B6FV5moE/8yTbFA4dBQahdtVP PejLlSAbb5ZoGK8AtLlcRq49IENoXB99tnFVn3gMM0aX24ido1ZF9RfRWzfYF7bVsLsrIiMPmVNe5KaGL9kZ0svzoZ708yjWQQCEYWp0m+sODbtGPC34HMGAHjFlsC/SJffLuT/ug/hhCJUYeExHIkJF8OyvfC6DeF7ArI6zdKER7D8M0SMWQmpKUltj2nltuv3w== root@localhost.localdomain",
"key_options": null,
"keyfile": "/root/.ssh/authorized_keys",
"manage_dir": true,
"path": null,
"state": "present",
"unique": false,
"user": "root"}12345678910111213