1. 程式人生 > 實用技巧 >centos 7 互信【ssh】

centos 7 互信【ssh】

centos 7 互信【ssh】

環境

虛擬機器+三個節點 centos 7.5

1.修改主機名字

命令格式:

hostnamectl set-hostname <hostname>【永久修改,重啟生效】\
hostname master【臨時修改,重啟失效】

示例:


hostnamectl set-hostname master
hostname master

三個節點以此類推

2.修改對映關係

  1. 在master節點上修改/etc/hosts檔案,新增如下內容:


$ vim /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
# 以下是新增的
192.168.204.10 master
192.168.204.11 slave1
192.168.204.12 slave2
  1. 將master節點的hosts檔案分發到slave節點:


scp /etc/hosts root@slave1:/etc/
scp /etc/hosts root@slave2:/etc/

這個過程會讓你輸入密碼~~

3.啟動ssh無密碼登入

  1. 在master節點的/etc/ssh/sshd_config檔案中去掉註釋


vim /etc/ssh/sshd_config

RSAAuthentication yes #開啟私鑰驗證
PubkeyAuthentication yes #開啟公鑰驗證

在這個檔案中只有“Authentication:”選項,修改這個就行~~

  1. 再次通過scp命令將sshd_config檔案分發到其他節點


scp /etc/ssh/sshd_config root@slave1:/etc/ssh/
scp /etc/ssh/sshd_config root@slave2:/etc/ssh/

4.生成公、私鑰

  1. 在每個節點輸入:【執行過程一律回車】


ssh-keygen -t rsa -P ''

結果如下:


[root@master ~]# ssh-keygen -t rsa -P ''
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
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:
22:42:2d:15:39:cc:f6:4a:9c:da:57:5b:55:b8:18:5d root@master
The key's randomart image is:
+--[ RSA 2048]----+
| ooo . +E |
| o* . + |
| oo.+ + . |
| . .+ . . o . |
| .+....So |
| ..o.... |
| . |
| |
| |
+-----------------+
  1. 在master節點執行:


cat id_rsa.pub >> authorized_keys
  1. 檢視slaves節點的id_rsa.pub檔案,將檔案內容複製到master節點的authorized_keys檔案中

  2. 分發authorized_keys檔案到slaves節點


scp authorized_keys root@slave1:~/.ssh/
scp authorized_keys root@slave2:~/.ssh/
  1. 重啟sshd服務:【每個節點都要重啟】


systemctl restart sshd.service

5.驗證ssh無密碼登入


[root@master]#
[root@master]# ssh slave1
Last login: Tue Jun 26 11:35:53 2018 from master
[root@slave1 ~]#

第一次會提示你輸入密碼,在選擇yes/no的 時候,輸入yes


Tips.當使用以上方式達不到免密登陸,請使用ssh-copy-id命令

語法格式:


ssh-copy-id [-i [identity_file]] user@machine

選項
-i:指定公鑰檔案

例項1:不指定公鑰檔案


[root@master]# ssh-copy-id root@slave1
[root@master]# ssh-copy-id root@slave2

例項2:指定公鑰檔案


[root@master]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@slave1
[root@master]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@slave2

如果提示:


[root@master]# ssh-copy-id:command not found

執行以下命令:


[root@master]# yum -y install openssh-clients