使用SSH服務遠端登入Linux主機
SSH是一種能夠以安全的方式提供遠端登入的協議,也是目前遠端管理Linux系統的首選方式。它是以密文傳輸來保證安全。一般情況下在裝Linux系統的過程中是預設安裝的。
想要使用ssh協議來遠端管理Linux系統,需要部署sshd服務程式。sshd是基於ssh協議開發的一款遠端管理服務程式,不僅使用快捷方便,而且提供兩種安全驗證的方法。
- 基於口令的驗證-用於賬戶和密碼來驗證登入
- 基於金鑰的驗證-需要在本地生成金鑰檔案,然後把金鑰對中的公鑰檔案上傳至伺服器,並與伺服器中的公鑰進行比較,一致則可遠端登入。
實驗環境:
一個RedHat7系統作為ssh伺服器,當前使用者是linuxmi,IP地址是192.168.182.188。
另一個Redhat7系統作為客戶機,當前使用者是linuxidc,IP地址是192.168.10.80。
兩個主機確保能夠互聯互通。
使用者口令驗證登入
在Redhat7系統中已經預設安裝並啟動了sshd服務,接下來在客戶機上使用ssh命令進行遠端連線。
[linuxidc@RedHat7-2 ~]$ ssh [email protected] //ssh遠端登入linuxmi使用者的伺服器
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 //確認連線
Warning: Permanently added '192.168.182.188' (ECDSA) to the list of known hosts.
[email protected]'s password: //輸入目標伺服器的linuxmi使用者的密碼
Last login: Wed May 23 03:36:52 2018
[linuxmi@RedHat7-1 ~]$ exit //退出遠端登入
登出
Connection to 192.168.182.188 closed.
安全金鑰驗證
第1步: 在客戶端主機中生成金鑰對。
[linuxidc@RedHat7-2 ~]$ ssh-keygen -t rsa //建立金鑰對
Generating public/private rsa key pair.
Enter file in which to save the key (/home/linuxidc/.ssh/id_rsa): //按回車鍵預設指定私鑰位置在宿主目錄中的隱藏資料夾.ssh下
Enter passphrase (empty for no passphrase): //設定私鑰短語123123
Enter same passphrase again: //確認所設定的短語
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* //確認生成的金鑰檔案
-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是私鑰檔案,許可權預設為600,id_rsa.pub是公鑰檔案,用來提供給SSH伺服器。
第2步: 修改伺服器主機ssh配置檔案,使其只允許金鑰驗證,指定公鑰資料檔案位置。
以root管理員身份修改配置檔案(/etc/ssh/sshd_config)
vim /etc/ssh/sshd_config
PubkeyAuthentication yes //啟用金鑰對驗證
AuthorizedKeysFile .ssh/authorized_keys //指定公鑰庫資料檔案
第3步: 把客戶端主機中生成的公鑰檔案傳送至伺服器主機。
[linuxidc@RedHat7-2 ~]$ ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected] //上傳公鑰庫檔案至伺服器
/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
[email protected]'s password: //輸入伺服器linuxmi使用者的密碼
Number of key(s) added: 1
Now try logging into the machine, with: "ssh '[email protected]'"
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
注意任何使用者對公鑰庫檔案不能有寫入的許可權。
第4步: 重新啟動sshd服務程式
service sshd restart
第5步: 在客戶端主機使用金鑰對驗證
[linuxidc@RedHat7-2 ~]$ ssh [email protected] //遠端登入伺服器
Enter passphrase for key '/home/linuxidc/.ssh/id_rsa':123123 //輸入私鑰短語,以便呼叫私鑰檔案進行匹配
Last login: Wed May 23 03:37:45 2018 from 192.168.10.80
[linuxmi@RedHat7-1 ~]$
所以使用金鑰對驗證的方式登入時,不需要知道目標伺服器使用者的密碼,只需要驗證客戶端使用者的私鑰短語,檢查公私鑰檔案是否匹配,這樣更安全。
Linux公社的RSS地址 :ofollow,noindex" target="_blank">https://www.linuxidc.com/rssFeed.aspx
本文永久更新連結地址:https://www.linuxidc.com/Linux/2018-11/155521.htm