使用SSH服務遠程登錄Linux主機
想要使用ssh協議來遠程管理Linux系統,需要部署sshd服務程序。sshd是基於ssh協議開發的一款遠程管理服務程序,不僅使用快捷方便,而且提供兩種安全驗證的方法。
- 基於口令的驗證-用於賬戶和密碼來驗證登錄
- 基於密鑰的驗證-需要在本地生成密鑰文件,然後把密鑰對中的公鑰文件上傳至服務器,並與服務器中的公鑰進行比較,一致則可遠程登錄。
實驗環境:
一個Redhat7系統作為ssh服務器,當前用戶是lisi,IP地址是192.168.10.70。
另一個Redhat7系統作為客戶機,當前用戶是zhangsan,IP地址是192.168.10.80。
兩個主機確保能夠互聯互通。
用戶口令驗證登錄
在Redhat7系統中已經默認安裝並啟動了sshd服務,接下來在客戶機上使用ssh命令進行遠程連接。
[zhangsan@RedHat7-2 ~]$ ssh [email protected] //ssh遠程登錄lisi用戶的服務器 The authenticity of host ‘192.168.10.70 (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.10.70‘ (ECDSA) to the list of known hosts. [email protected]‘s password: //輸入目標服務器的lisi用戶的密碼 Last login: Wed May 23 03:36:52 2018 [lisi@RedHat7-1 ~]$ exit //退出遠程登錄 登出 Connection to 192.168.10.70 closed.
安全密鑰驗證
第1步: 在客戶端主機中生成密鑰對。
[zhangsan@RedHat7-2 ~]$ ssh-keygen -t rsa //創建密鑰對 Generating public/private rsa key pair. Enter file in which to save the key (/home/zhangsan/.ssh/id_rsa): //按回車鍵默認指定私鑰位置在宿主目錄中的隱藏文件夾.ssh下 Enter passphrase (empty for no passphrase): //設置私鑰短語123123 Enter same passphrase again: //確認所設置的短語 Your identification has been saved in /home/zhangsan/.ssh/id_rsa. Your public key has been saved in /home/zhangsan/.ssh/id_rsa.pub. The key fingerprint is: d0:b6:b9:96:b2:00:b8:f3:ea:a5:61:96:64:bd:b0:54 zhangsan@RedHat7-2 The key‘s randomart image is: +--[ RSA 2048]----+ | | | . | | E . o | | .o o o | |.=.. S | |+.+.. o | |o* o. . + | |oo+ . + | |o+. . | +-----------------+
[zhangsan@RedHat7-2 ~]$ ls -lh ~/.ssh/id_rsa* //確認生成的密鑰文件
-rw-------. 1 zhangsan zhangsan 1.8K 5月 22 19:50 /home/zhangsan/.ssh/id_rsa
-rw-r--r--. 1 zhangsan zhangsan 400 5月 22 19:50 /home/zhangsan/.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步: 把客戶端主機中生成的公鑰文件傳送至服務器主機。
[zhangsan@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: //輸入服務器lisi用戶的密碼
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.
[lisi@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
zhangsan@RedHat7-2
[lisi@RedHat7-1 ~]$ ls -l ~/.ssh/authorized_keys //查看公鑰庫文件
-rw-------. 1 lisi lisi 400 5月 23 04:07 /home/lisi/.ssh/authorized_keys
註意任何用戶對公鑰庫文件不能有寫入的權限。
第4步: 重新啟動sshd服務程序
service sshd restart
第5步: 在客戶端主機使用密鑰對驗證
[zhangsan@RedHat7-2 ~]$ ssh [email protected] //遠程登錄服務器
Enter passphrase for key ‘/home/zhangsan/.ssh/id_rsa‘:123123 //輸入私鑰短語,以便調用私鑰文件進行匹配
Last login: Wed May 23 03:37:45 2018 from 192.168.10.80
[lisi@RedHat7-1 ~]$
所以使用密鑰對驗證的方式登錄時,不需要知道目標服務器用戶的密碼,只需要驗證客戶端用戶的私鑰短語,檢查公私鑰文件是否匹配,這樣更安全。
使用SSH服務遠程登錄Linux主機