環境
-
虛拟機+三個節點 centos 7.5 【ubuntu的配置類似】
目錄
- 1.修改主機名字
- 2.修改映射關系
- 3.啟動ssh無密碼登入
- 4.生成公、私鑰
- 5.驗證ssh無密碼登入
- Tips.當使用以上方式達不到免密登陸,請使用ssh-copy-id指令
指令格式:
hostnamectl set-hostname #【永久修改,重新開機生效】
hostname master #【臨時修改,重新開機失效】
示例:
hostnamectl set-hostname master
hostname master
三個節點以此類推
在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
将master節點的hosts檔案分發到slave節點:
scp /etc/hosts root@slave1:/etc/
scp /etc/hosts root@slave2:/etc/
這個過程會讓你輸入密碼
在master節點的/etc/ssh/sshd_config檔案中去掉注釋
vim /etc/ssh/sshd_config
RSAAuthentication yes #開啟私鑰驗證 這是需要添加的
PubkeyAuthentication yes #開啟公鑰驗證,去掉注釋即可
在這個檔案中隻有“Authentication:”選項,修改這個就行~
再次通過scp指令将sshd_config檔案分發到其他節點
scp /etc/ssh/sshd_config root@slave1:/etc/ssh/
scp /etc/ssh/sshd_config root@slave2:/etc/ssh/
在每個節點輸入:【執行過程一律回車】
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.... |
| . |
| |
+-----------------+
在master節點執行:
cd /root/.ssh
cat id_rsa.pub >> authorized_keys
檢視slaves節點的id_rsa.pub檔案,将檔案内容複制到master節點的authorized_keys檔案中
分發authorized_keys檔案到slaves節點
scp authorized_keys root@slave1:~/.ssh/
scp authorized_keys root@slave2:~/.ssh/
重新開機sshd服務:【每個節點都要重新開機】
systemctl restart sshd.service
[root@master]# ssh slave1
Last login: Tue Jun 26 11:35:53 2018 from master
[root@slave1 ~]#
第一次會提示你輸入密碼,在選擇yes/no的 時候,輸入yes
文法格式:
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:指定公鑰檔案