Centos7实现ssh免密登陆

1通过ssh-copy-id的方式

 1 [root@localhost ~]# ssh-copy-id -i root@worker1
 2 /usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
 3 /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
 4 /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
 5 root@worker1's password:
 6 
 7 Number of key(s) added: 1
 8 
 9 Now try logging into the machine, with:   "ssh 'root@worker1'"
10 and check to make sure that only the key(s) you wanted were added.

2通过scp将内容写到对方的文件中

1 [root@test .ssh]# scp -p ~/.ssh/id_rsa.pub root@192.168.91.135:/root/.ssh/authorized_keys
2 root@192.168.91.135's password: 
3 id_rsa.pub 100% 408 0.4KB/s 00:00 
4 [root@test .ssh]# 
5 [root@test .ssh]# 
6 [root@test .ssh]# 
7 [root@test .ssh]# ssh root@192.168.91.135
8 Last login: Mon Oct 10 01:27:02 2016 from 192.168.91.133
9 [root@localhost ~]#

或者

$ scp ~/.ssh/id_rsa.pub root@<remote_ip>:pub_key //将文件拷贝至远程服务器
$ cat ~/pub_key >>~/.ssh/authorized_keys //将内容追加到authorized_keys文件中, 不过要登录远程服务器来执行这条命令

3通过Ansible实现批量免密 

[Avoid close]
192.168.91.132
192.168.91.133
192.168.91.134
//执行命令进行免密操作
ansible <groupname> -m authorized_key -a "user=root key='{{ lookup('file','/root/.ssh/id_rsa.pub') }}'" -k

示例:
[root@test sshpass-1.05]# ansible test -m authorized_key -a "user=root key='{{ lookup('file','/root/.ssh/id_rsa.pub') }}'" -k
  SSH password: ----->输入密码
  192.168.91.135 | success >> {
  "changed": true, 
  "key": "ssh-rsa    AAAAB3NzaC1yc2EAAAABIwAAAQEArZI4kxlYuw7j1nt5ueIpTPWfGBJoZ8Mb02OJHR8yGW7A3izwT3/uhkK7RkaGavBbAlprp5bxp3i0TyNxa/apBQG5NiqhYO8YCuiGYGsQAGwZCBlNLF3gq1/18B6FV5moE/8yTbFA4dBQahdtVP PejLlSAbb5ZoGK8AtLlcRq49IENoXB99tnFVn3gMM0aX24ido1ZF9RfRWzfYF7bVsLsrIiMPmVNe5KaGL9kZ0svzoZ708yjWQQCEYWp0m+sODbtGPC34HMGAHjFlsC/SJffLuT/ug/hhCJUYeExHIkJF8OyvfC6DeF7ArI6zdKER7D8M0SM  WQmpKUltj2nltuv3w== [email protected]", 
  "key_options": null, 
  "keyfile": "/root/.ssh/authorized_keys", 
  "manage_dir": true, 
  "path": null, 
  "state": "present", 
  "unique": false, 
  "user": "root"
  }

猜你喜欢

转载自www.cnblogs.com/999jiutian/p/8931090.html
今日推荐