ssh是記錄你密碼信息的, 沒有登錄過root (或是沒有執(zhí)行過ssh-keygen命令),是沒有.ssh 文件夾的
需求:當(dāng)你需要訪問一臺(tái)linux服務(wù)器或兩臺(tái)服務(wù)器互相免密訪問時(shí),ssh keys這時(shí)需要,創(chuàng)建辦法是當(dāng)前主機(jī)上執(zhí)行命令:
ssh-keygen 或 ssh-keygen -t rsa 或 ssh-keygen -t dsa
AB機(jī)器,如果A訪問B免密,就把A的公鑰給B,反之亦然
執(zhí)行后會(huì)在當(dāng)前用戶登錄目錄下生成.ssh目錄和兩個(gè)文件
使用ssh-keygen生成私鑰和公鑰
命令如下:
ssh-keygen -t rsa
參數(shù) -t rsa 表示使用rsa算法進(jìn)行加密,執(zhí)行后,會(huì)在/root當(dāng)前用戶/.ssh目錄下找到id_rsa(私鑰)和id_rsa.pub(公鑰)
也可以使用 dsa 加密算法進(jìn)行加密,命令如下:
ssh-keygen -t dsa
id_rsa.pub里是公鑰,如果需要登錄到遠(yuǎn)程主機(jī),需要到遠(yuǎn)程主機(jī)/root/root/.ssh目錄下,新建authorized_keys文件,并將id_rsa.pub里的內(nèi)容復(fù)制進(jìn)去:
# touch /root/.ssh/authorized_keys
這個(gè)操作看要不要登錄到遠(yuǎn)程的機(jī)器上,如果需要,就添加,不需要,可以不建。
注意:新建后,需要更改authorized_keys文件的用戶權(quán)限,不然文件無法生效,ssh公鑰生效需滿足至少下面兩個(gè)條件: 1、 .ssh目錄的權(quán)限必須是700 2 、.ssh/authorized_keys文件權(quán)限必須是600
執(zhí)行下面命令
chmod 600 ~/.ssh/authorized_keys
遠(yuǎn)程免密登錄
常用以下幾種方法:
3臺(tái)rhel7.4
HOSTNAME | IP | ROLE |
---|---|---|
server1 | 192.168.2.3 | Master |
server2 | 192.168.2.5 | Slave1 |
server3 | 192.168.2.10 | Slave2 |
2.1 通過ssh-copy-id的方式:
命令: ssh-copy-id -i ~/.ssh/id_rsa.pub <romte_ip>
舉例:
root用戶登錄遠(yuǎn)程root用戶(第一次需要密碼登錄) [root@linuxidc ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub 192.168.2.5 /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 keys root@192.168.2.5's password: Number of key(s) added: 1 Now try logging into the machine, with: "ssh '192.168.2.2'" and check to make sure that only the key(s) you wanted were added. [root@linuxidc ~]# [root@linuxidc ~]# ssh root@192.168.2.5 Last login: Thu Nov 15 16:23:42 2018 from 192.168.2.3 [root@D ~]# 常見錯(cuò)誤: [root@test ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub 192.168.2.5 -bash: ssh-copy-id: command not found //提示命令不存在 解決辦法:yum -y install openssh-clients root用戶遠(yuǎn)程非root用戶(普通用戶),第一次需要密碼登錄 [root@linuxidcjustyumserver ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub test@192.168.2.2 /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 keys test@192.168.2.2's password: Number of key(s) added: 1 Now try logging into the machine, with: "ssh 'Oracle@192.168.2.2'" and check to make sure that only the key(s) you wanted were added.
2.2、通過scp將內(nèi)容寫到對(duì)方的文件中
命令:scp -p ~/.ssh/id_rsa.pub root@<remote_ip>:/root/.ssh/authorized_keys
舉例:
# scp -p ~/.ssh/id_rsa.pub root@192.168.2.5:/root/.ssh/authorized_keys #root@10.40.34.183's password: id_rsa.pub # ssh root@192.168.2.5 Last login: Thu Nov 15 16:54:59 2018 from 192.168.2.3
也可以分為兩步操作:
# 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、每臺(tái)服務(wù)器下都輸入命令 ssh-keygen -t rsa,生成 key,一律不輸入密碼,直接回車,/root 就會(huì)生成 .ssh 文件夾。
在 Master 服務(wù)器下,合并公鑰到 authorized_keys 文件,進(jìn)入 /root/.ssh 目錄,通過 SSH 命令合并:
[root@linuxidc ~]# cd /root/.ssh/ [root@linuxidc .ssh]# cat id_rsa.pub >> authorized_keys [root@linuxidc .ssh]# ssh root@192.168.2.10 cat ~/.ssh/id_rsa.pub>> authorized_keys 這里的id_rsa.pub是slave服務(wù)器的,合并到Mastere服務(wù)器的文件中
把 Master 服務(wù)器的 authorized_keys復(fù)制到 Slave 服務(wù)器的 `/root/.ssh 目錄
[root@linuxidc.ssh]# scp authorized_keys root@192.168.2.10:/root/.ssh/
完成,ssh root@192.168.2.10 就不需要輸入密碼登錄了