1.Linux下產生金鑰
ssh-keygen的指令手冊,透過」man ssh-keygen「指令:
〜透過指令」ssh使用者的根目錄產生一個「.ssh」的資料夾
進入「.ssh」會產生以下幾個檔案
『檔案記錄多台機器的公鑰
id_rsa : 產生的私鑰檔案id_rsa.pub : 產生的公鑰檔案
know_hosts : 已知的主機帳號符合至少兩個條件:1) .ssh目錄的權限必須是700
2) .ssh/authorized_keys文件檔案必須為600 2)
常用以下幾種方法:
2.1 透過ssh-copy-id的方式
指令: ssh-copy-id -i ~/.ssh/id_rsa.put 舉例
root@192.168.91.135's password:
Now try logging into the machine, with "ssh '192.168.91.135'", and check in:. ssh/authorized_keysto make sure we haven't added extra keys that you weren't expecting.
[root@test .ssh]# ssh root@192.168.91.13555:4135log 2016 from 192.168.91.133
[root@localhost ~]# 常見錯誤: 。。[root-7.19. .135解決方法:yum -y install openssh-clients
㜀2個『21㟜〜㟎㟜〜㟜㟎21㟜〜㟎㟜〜2㜀㟜〜㟣㟜〜2㟜〜㟜㟜〜)
指令:scp -p ~/.ssh/id_rsa.pub root@
:/root/.ssh/authorized_keys
舉例:
[root@test .ssh]# scp -p ~/.ssh/id_rsa.pub root@192.168.91.135: /root/.ssh/authorized_keys
root@192.168.91.135's password:
id_rsa.pub 100% 408 0.4KB/s 00:000
[root@test .s [root@test .ssh]#
[root@test .ssh]# ssh root@192.168.91.135
:pub_key 將檔案」 > 遠端/$ pub_sh/41/1/1/p pub_sh. /authorized_keys //將內容追加到authorized_keys檔案中, 不過登入遠端伺服器來執行此指令
密 2.3透過批次實現批次
2.3.1 將需要做免密操作的機器hosts加到/etc/ansible/hosts下:
[Avoid 4
2.3.2 執行指令進行免密操作
ansible
-m authorized_key -a "user=root key='{{ lookup('file','/root/.ssh/id_rsa.pub') }}'" -k
範例:🀜 @test sshpass-1.05]# ansible test -m authorized_key -a "user=root key='{{ lookup('file','/root/.ssh/id_rsa.pub') }}'" -k
192.168.91.135 | success >> {
"changed": true,
"key": "ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEArZI4kxlYuw7j1nt5ueIpTPWfGBJoZ8Mb02OJHR8yGW7A3izwT3/uhkK7RkaGavBbAlprp5bxp3i0TyNxa/apBQG5NiqhYO8YCuiGYGsQAGwZCBlNLF3gq1/18B6FV5moE/8yTbFA4dBQahdtVP PejLlSAbb5ZoGK8AtLlcRq49IENoXB99tnFVn3gMM0aX24ido1ZF9RfRWzfYF7bVsLsrIiMPmVNe5KaGL9kZ0svzoZ708yjWQQCEYWp0m+sODbtGPC34HMGAHjFlsC/SJffLuT/ug /hhCJUYeExHIkJF8OyvfC6DeF7ArI6zdKER7D8M0SM WQmpKUltj2nltuv3w== root@localhost.localdomain",
。 ,
"manage_dir": true,
"path": null,
" state": "present",
"unique": false,
"user": "root"
}
[root@test sshpass-1.05]#34345% _rsa.pub文件的內容拷貝至遠端伺服器的~/.ssh/authorized_keys檔案中