SSH是一種能夠以安全的方式提供遠(yuǎn)程登錄的協(xié)議,也是目前遠(yuǎn)程管理Linux系統(tǒng)的首選方式。它是以密文傳輸來保證安全。一般情況下在裝Linux系統(tǒng)的過程中是默認(rèn)安裝的。
想要使用ssh協(xié)議來遠(yuǎn)程管理Linux系統(tǒng),需要部署sshd服務(wù)程序。sshd是基于ssh協(xié)議開發(fā)的一款遠(yuǎn)程管理服務(wù)程序,不僅使用快捷方便,而且提供兩種安全驗證的方法。
– 基于口令的驗證-用于賬戶和密碼來驗證登錄
– 基于密鑰的驗證-需要在本地生成密鑰文件,然后把密鑰對中的公鑰文件上傳至服務(wù)器,并與服務(wù)器中的公鑰進(jìn)行比較,一致則可遠(yuǎn)程登錄。
實驗環(huán)境:
一個RedHat7系統(tǒng)作為ssh服務(wù)器,當(dāng)前用戶是linuxmi,IP地址是192.168.182.188。
另一個Redhat7系統(tǒng)作為客戶機(jī),當(dāng)前用戶是linuxidc,IP地址是192.168.10.80。
兩個主機(jī)確保能夠互聯(lián)互通。
用戶口令驗證登錄
在Redhat7系統(tǒng)中已經(jīng)默認(rèn)安裝并啟動了sshd服務(wù),接下來在客戶機(jī)上使用ssh命令進(jìn)行遠(yuǎn)程連接。
[linuxidc@RedHat7-2 ~]$ ssh linuxmi@192.168.182.188 //ssh遠(yuǎn)程登錄linuxmi用戶的服務(wù)器
The authenticity of host ‘192.168.182.188 (192.168.10.70)’ can’t be established.
ECDSA key fingerprint is 93:8e:e7:3f:9a:22:6f:66:3a:f7:57:68:a1:57:3b:09.
Are you sure you want to continue connecting (yes/no)? yes //確認(rèn)連接
Warning: Permanently added ‘192.168.182.188’ (ECDSA) to the list of known hosts.
linuxmi@192.168.182.188’s password: //輸入目標(biāo)服務(wù)器的linuxmi用戶的密碼
Last login: Wed May 23 03:36:52 2018
[linuxmi@RedHat7-1 ~]$ exit //退出遠(yuǎn)程登錄
登出
Connection to 192.168.182.188 closed.
安全密鑰驗證
第1步: 在客戶端主機(jī)中生成密鑰對。
[linuxidc@RedHat7-2 ~]$ ssh-keygen -t rsa //創(chuàng)建密鑰對
Generating public/private rsa key pair.
Enter file in which to save the key (/home/linuxidc/.ssh/id_rsa): //按回車鍵默認(rèn)指定私鑰位置在宿主目錄中的隱藏文件夾.ssh下
Enter passphrase (empty for no passphrase): //設(shè)置私鑰短語123123
Enter same passphrase again: //確認(rèn)所設(shè)置的短語
Your identification has been saved in /home/linuxidc/.ssh/id_rsa.
Your public key has been saved in /home/linuxidc/.ssh/id_rsa.pub.
The key fingerprint is:
d0:b6:b9:96:b2:00:b8:f3:ea:a5:61:96:64:bd:b0:54 linuxidc@RedHat7-2
The key’s randomart image is:
+–[ RSA 2048]—-+
| |
| . |
| E . o |
| .o o o |
|.=.. S |
|+.+.. o |
|o* o. . + |
|oo+ . + |
|o+. . |
+—————–+
[linuxidc@RedHat7-2 ~]$ ls -lh ~/.ssh/id_rsa* //確認(rèn)生成的密鑰文件
-rw——-. 1 linuxidc zhangsan 1.8K 5月 22 19:50 /home/linuxidc/.ssh/id_rsa
-rw-r–r–. 1 linuxidc zhangsan 400 5月 22 19:50 /home/linuxidc/.ssh/id_rsa.pub
新生成的密鑰對文件,id_rsa是私鑰文件,權(quán)限默認(rèn)為600,id_rsa.pub是公鑰文件,用來提供給SSH服務(wù)器。
第2步: 修改服務(wù)器主機(jī)ssh配置文件,使其只允許密鑰驗證,指定公鑰數(shù)據(jù)文件位置。
以root管理員身份修改配置文件(/etc/ssh/sshd_config)
vim /etc/ssh/sshd_config
PubkeyAuthentication yes //啟用密鑰對驗證
AuthorizedKeysFile .ssh/authorized_keys //指定公鑰庫數(shù)據(jù)文件
第3步: 把客戶端主機(jī)中生成的公鑰文件傳送至服務(wù)器主機(jī)。
[linuxidc@RedHat7-2 ~]$ ssh-copy-id -i ~/.ssh/id_rsa.pub linuxmi@192.168.182.188 //上傳公鑰庫文件至服務(wù)器
/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed — if you are prompted now it is to install the new keys
linuxmi@192.168.182.188’s password: //輸入服務(wù)器linuxmi用戶的密碼
Number of key(s) added: 1
Now try logging into the machine, with: “ssh ‘linuxmi@192.168.182.188′”
and check to make sure that only the key(s) you wanted were added.
[linuxmi@RedHat7-1 ~]$ tail -1 ~/.ssh/authorized_keys //顯示公鑰庫文件信息
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC/p8OHTUBJMKqJbxxwUiNJvKVHv8KSMywr
tFB3BEsC02MyU29NKXkGUVM/lC++7b/bK1j/xVg6gJXqCHk2lNrMk/jHjvR6qR7aLYhzWlCa
oDW0/Df9V9nrJNIg82DbXHUziwe6WoR9l+pzzQqYyI1Yq0iPTD4VZM5T94wRMX4taSgO8EQ
umWEeGtoHX/vgklapyMaG3ncA4SBxC0G4JUHo3q2KAfJ4eECrZ9LBwVsPq+4exlzDSeXmGh
aZO+VGo6Kbp7Q6ReA5U1YUbfsa9nKyAexiKxyzaGMXzBEri/aXGUpDibBWzRT4JDocF7PV
wHr+sshYqt4ULdG0wj91SK+D
linuxidc@RedHat7-2
[linuxmi@RedHat7-1 ~]$ ls -l ~/.ssh/authorized_keys //查看公鑰庫文件
-rw——-. 1 linuxmi lisi 400 5月 23 04:07 /home/linuxmi/.ssh/authorized_keys
注意任何用戶對公鑰庫文件不能有寫入的權(quán)限。
第4步: 重新啟動sshd服務(wù)程序
service sshd restart
第5步: 在客戶端主機(jī)使用密鑰對驗證
[linuxidc@RedHat7-2 ~]$ ssh linuxmi@192.168.182.188 //遠(yuǎn)程登錄服務(wù)器
Enter passphrase for key ‘/home/linuxidc/.ssh/id_rsa’:123123 //輸入私鑰短語,以便調(diào)用私鑰文件進(jìn)行匹配
Last login: Wed May 23 03:37:45 2018 from 192.168.10.80
[linuxmi@RedHat7-1 ~]$
所以使用密鑰對驗證的方式登錄時,不需要知道目標(biāo)服務(wù)器用戶的密碼,只需要驗證客戶端用戶的私鑰短語,檢查公私鑰文件是否匹配,這樣更安全。