Linux下實(shí)現(xiàn)免密碼登錄(超詳細(xì))
1.Linux下生成密鑰
ssh-keygen的命令手冊,通過”man ssh-keygen“命令:
通過命令”ssh-keygen -t rsa“
生成之后會在用戶的根目錄生成一個 “.ssh”的文件夾
進(jìn)入“.ssh”會生成以下幾個文件
authorized_keys:存放遠(yuǎn)程免密登錄的公鑰,主要通過這個文件記錄多臺機(jī)器的公鑰
id_rsa : 生成的私鑰文件
id_rsa.pub : 生成的公鑰文件
know_hosts : 已知的主機(jī)公鑰清單
如果希望ssh公鑰生效需滿足至少下面兩個條件:
1) .ssh目錄的權(quán)限必須是700
2) .ssh/authorized_keys文件權(quán)限必須是600
2.遠(yuǎn)程免密登錄
原理圖:
常用以下幾種方法:
2.1 通過ssh-copy-id的方式
命令: ssh-copy-id -i ~/.ssh/id_rsa.put <romte_ip>
舉例:
[root@test .ssh]# ssh-copy-id -i ~/.ssh/id_rsa.pub 192.168.91.135 root@192.168.91.135's password: Now try logging into the machine, with "ssh '192.168.91.135'", and check in: .ssh/authorized_keys to make sure we haven't added extra keys that you weren't expecting. [root@test .ssh]# ssh root@192.168.91.135 Last login: Mon Oct 10 01:25:49 2016 from 192.168.91.133 [root@localhost ~]#
常見錯誤:
[root@test ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub 192.168.91.135
-bash: ssh-copy-id: command not found //提示命令不存在
解決辦法:yum -y install openssh-clients
2.2 通過scp將內(nèi)容寫到對方的文件中
命令:scp -p ~/.ssh/id_rsa.pub root@<remote_ip>:/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:00 [root@test .ssh]# [root@test .ssh]# [root@test .ssh]# [root@test .ssh]# ssh root@192.168.91.135 Last login: Mon Oct 10 01:27:02 2016 from 192.168.91.133 [root@localhost ~]#
也可以分為兩步操作:
$ scp ~/.ssh/id_rsa.pub root@<remote_ip>:pub_key //將文件拷貝至遠(yuǎn)程服務(wù)器
$ cat ~/pub_key >>~/.ssh/authorized_keys //將內(nèi)容追加到authorized_keys文件中, 不過要登錄遠(yuǎn)程服務(wù)器來執(zhí)行這條命令
2.3 通過Ansible實(shí)現(xiàn)批量免密
2.3.1 將需要做免密操作的機(jī)器hosts添加到/etc/ansible/hosts下:
[Avoid close]
192.168.91.132
192.168.91.133
192.168.91.134
2.3.2 執(zhí)行命令進(jìn)行免密操作
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== root@localhost.localdomain", "key_options": null, "keyfile": "/root/.ssh/authorized_keys", "manage_dir": true, "path": null, "state": "present", "unique": false, "user": "root" } [root@test sshpass-1.05]#
2.4 手工復(fù)制粘貼的方式
將本地id_rsa.pub文件的內(nèi)容拷貝至遠(yuǎn)程服務(wù)器的~/.ssh/authorized_keys文件中
以上所述是小編給大家介紹的Linux下實(shí)現(xiàn)免密碼登錄,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
通過 SSH 在遠(yuǎn)程 Linux 系統(tǒng)上運(yùn)行命令的方法
這篇文章主要介紹了通過 SSH 在遠(yuǎn)程 Linux 系統(tǒng)上運(yùn)行命令的方法,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2019-10-10linux 上在指定時間段內(nèi)每隔多少分鐘自動執(zhí)行任務(wù)計(jì)劃crontab
這篇文章主要介紹了linux 上在指定時間段內(nèi)每隔多少分鐘自動執(zhí)行任務(wù)計(jì)劃crontab,需要的朋友可以參考下2019-11-11CentOS環(huán)境下安裝Redis3.0及phpredis擴(kuò)展測試示例
這篇文章主要介紹了CentOS環(huán)境下安裝Redis3.0及phpredis擴(kuò)展測試,結(jié)合實(shí)例形式分析了CentOS下Redis安裝相關(guān)步驟、命令及phpredis擴(kuò)展的測試代碼,需要的朋友可以參考下2018-04-04linux(ubuntu)用戶連續(xù)N次輸入錯誤密碼進(jìn)行登陸時自動鎖定X分鐘
這篇文章主要介紹了linux(ubuntu)用戶連續(xù)N次輸入錯誤密碼進(jìn)行登陸時,自動鎖定X分鐘,需要的朋友可以參考下2019-09-09CentOS設(shè)置靜態(tài)IP的方法總結(jié)
這篇文章主要介紹了CentOS設(shè)置靜態(tài)IP的方法總結(jié)的相關(guān)資料,需要的朋友可以參考下2017-06-06