本章節,要講的是SSH遠端連線的機制與原理,以及SSH的使用方式。
OpenSSH實做了Secure Shell或是稱作SSH Protocol,它提供了一種加解密之安全連線方式供遠端的使用者可以透過此加密通訊協定進行遠端連線與存取遠端的主機。以下是一些常見的SSH指令的用法:
[rockylinux@workstation ~]$ ssh rockylinux@localhost
rockylinux@localhost's password:
Activate the web console with: systemctl enable --now cockpit.socket
Last login: Thu Sep 30 23:36:11 2021
[rockylinux@workstation ~]$
從上述的執行指令可以得知,這就是一個基本的SSH指令用法,遠端連上去之後,基本上與在桌面環境上使用終端機的操作一模一樣,當操作完成之後,要離開並關閉這個連線,執行exit
或是logout
之指令即可,相關的指令執行後所輸出的訊息如下:
[rockylinux@workstation ~]$ ssh rockylinux@localhost
rockylinux@localhost's password:
Activate the web console with: systemctl enable --now cockpit.socket
Last login: Fri Oct 1 00:58:54 2021 from 192.168.0.9
[rockylinux@workstation ~]$ logout
Connection to localhost closed.
[rockylinux@workstation ~]$ ssh rockylinux@localhost
rockylinux@localhost's password:
Activate the web console with: systemctl enable --now cockpit.socket
Last login: Fri Oct 1 00:58:57 2021 from ::1
[rockylinux@workstation ~]$ exit
logout
Connection to localhost closed.
[rockylinux@workstation ~]$
還記得前面的章節有提到w
這個指令嗎?這個指令可以查看目前有多少的使用者使用SSH來登入到此主機上,相關的指令執行所輸出的結果如下:
[rockylinux@workstation ~]$ w
01:01:18 up 3:14, 2 users, load average: 0.01, 0.00, 0.00
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
rockylin pts/0 192.168.0.9 00:58 2.00s 0.11s 0.02s w
rockylin tty2 tty2 23:36 3:13m 40.23s 0.27s /usr/libexec/tracker-miner-fs
[rockylinux@workstation ~]$
當作業系統安裝好之後,主機金鑰便會自己產生,並存放到/etc/ssh/
之目錄檔裡面,而這個資料夾也是存放SSH server之設定檔,在這個目錄底下有sshd_config
便是可以調整與設定SSH server了,若要觀看更多有關於此設定檔的設定內容說明,可以使用sudo man /etc/ssh/sshd_config
指令來做到。
當使用SSH第一次連到遠端的主機的時候,會詢問公開金鑰的fingerprint是否要信任遠方主機傳過來的公開金鑰,需先確認主機的公鑰,當輸入「yes」的時候,就會將此公開金鑰存到當前使用者家目錄中的.ssh目錄中,並儲存成known_hosts檔案,接著自此之後再用此指令連上遠端的時候,便不再詢問並直接連上,而當known_hosts所存放的fingerprint有所不同時,在使用SSH進行連線的時候,則會跳出警告,則表示hash值驗證有誤,若確定這個是正常的行為,像是:主機重灌就有可能會發生,因為主機的host key已經重新產一組,則移除host上之fingerprint,並重新建立與儲存host之公鑰之雜湊資訊到known_hosts中,相關的指令執行所輸出的訊息如下:
[rockylinux@workstation ~]$ ssh rockylinux@127.0.0.1
The authenticity of host '127.0.0.1 (127.0.0.1)' can't be established.
ECDSA key fingerprint is SHA256:N6ZipKZheNYa53L7olZj/hgn1dmz4POF3HzQuTvgQjI.
Are you sure you want to continue connecting (yes/no/[fingerprint])?
從上面可以發現到,這是第一次連線到rockylinux@127.0.0.1之主機,因此跳出這樣的提示的訊息詢問,這個可以知道是使用ECDSA之Host public key的,因此回到此虛擬主機並使用下列的指令將fingerprint給輸出出來:
[rockylinux@workstation ~]$ ssh rockylinux@127.0.0.1
The authenticity of host '127.0.0.1 (127.0.0.1)' can't be established.
ECDSA key fingerprint is SHA256:N6ZipKZheNYa53L7olZj/hgn1dmz4POF3HzQuTvgQjI.
Are you sure you want to continue connecting (yes/no/[fingerprint])? ^C
[rockylinux@workstation ~]$ ssh-keygen -lf /etc/ssh/ssh_host_ecdsa_key.pub
256 SHA256:N6ZipKZheNYa53L7olZj/hgn1dmz4POF3HzQuTvgQjI no comment (ECDSA)
[rockylinux@workstation ~]$
從上述的指令,這就可以發現fingerprint是相同的,接著就可以放心地按下「yes」並將此訊息存放到known_hosts的檔案中,相關的執行指令的操作如下:
[rockylinux@workstation ~]$ ssh rockylinux@127.0.0.1
The authenticity of host '127.0.0.1 (127.0.0.1)' can't be established.
ECDSA key fingerprint is SHA256:N6ZipKZheNYa53L7olZj/hgn1dmz4POF3HzQuTvgQjI.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '127.0.0.1' (ECDSA) to the list of known hosts.
rockylinux@127.0.0.1's password:
Activate the web console with: systemctl enable --now cockpit.socket
Last login: Fri Oct 1 00:59:07 2021 from ::1
[rockylinux@workstation ~]$ exit
logout
Connection to 127.0.0.1 closed.
[rockylinux@workstation ~]$ cat ~/.ssh/known_hosts
127.0.0.1 ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBKnKNWcTQ7yTDdfdZ3rcg7Pg1H+an8BmXZxuyK5CP/BVE/qFVq5Q7EeA2e8M5Qhnw7RS34VdRIVhmt5y3dHqc2o=
[rockylinux@workstation ~]$
接著使用exit
指令離開此遠端shell,為了要模擬遠端主機所存取的Host public key有問題,以root使用者並用手動的方式將ssh_host_ecdsa_key
與ssh_host_ecdsa_key.pub
並將sshd服務重新啟動,這時候就會重新產生一組全新的ssh_host_ecdsa_key
與ssh_host_ecdsa_key.pub
檔案了,相關的指令執行輸出的訊息如下:
[rockylinux@workstation ~]$ sudo rm /etc/ssh/ssh_host_ecdsa_key*
[rockylinux@workstation ~]$ ls /etc/ssh/
moduli ssh_config.d ssh_host_ed25519_key ssh_host_rsa_key
ssh_config sshd_config ssh_host_ed25519_key.pub ssh_host_rsa_key.pub
[rockylinux@workstation ~]$ sudo systemctl restart sshd
[rockylinux@workstation ~]$ ls /etc/ssh/
moduli ssh_config.d ssh_host_ecdsa_key ssh_host_ed25519_key ssh_host_rsa_key
ssh_config sshd_config ssh_host_ecdsa_key.pub ssh_host_ed25519_key.pub ssh_host_rsa_key.pub
[rockylinux@workstation ~]$
接著再使用ssh rockylinux@127.0.0.1
指令進行遠端登入,則會發生fingerprint有問題的錯誤了,相關執行指令的輸出訊息如下:
[rockylinux@workstation ~]$ ssh rockylinux@127.0.0.1
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
Someone could be eavesdropping on you right now (man-in-the-middle attack)!
It is also possible that a host key has just been changed.
The fingerprint for the ECDSA key sent by the remote host is
SHA256:jtNpnVUOQFBlVo5shPfHdnogsQq/LtxbFLmgaeJJDjI.
Please contact your system administrator.
Add correct host key in /home/rockylinux/.ssh/known_hosts to get rid of this message.
Offending ECDSA key in /home/rockylinux/.ssh/known_hosts:1
ECDSA host key for 127.0.0.1 has changed and you have requested strict checking.
Host key verification failed.
[rockylinux@workstation ~]$
從上述的警告訊息得知,存放在known_hosts對應到此主機的fingerprint hash與遠端的不同,確認過遠端主機是刪除相關的公私鑰並重新啟動SSH server服務重新產一組的原因之後,可以將此行刪除,接著再次執行ssh rockylinux@127.0.0.1
指令,則會詢問fingerprint是不是對的問題了,那就重複上述的步驟進行驗證與登入,相關的指令執行所輸出的訊息如下:
[rockylinux@workstation ~]$ cat ~/.ssh/known_hosts
127.0.0.1 ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBKnKNWcTQ7yTDdfdZ3rcg7Pg1H+an8BmXZxuyK5CP/BVE/qFVq5Q7EeA2e8M5Qhnw7RS34VdRIVhmt5y3dHqc2o=
[rockylinux@workstation ~]$ vim ~/.ssh/known_hosts
[rockylinux@workstation ~]$ cat ~/.ssh/known_hosts
[rockylinux@workstation ~]$ ssh rockylinux@127.0.0.1
The authenticity of host '127.0.0.1 (127.0.0.1)' can't be established.
ECDSA key fingerprint is SHA256:jtNpnVUOQFBlVo5shPfHdnogsQq/LtxbFLmgaeJJDjI.
Are you sure you want to continue connecting (yes/no/[fingerprint])?
有關於SSH連線之間做了什麼事情,由下列的示意圖所示:
另外,SSH有分版本,分別為1與2,目前都是使用第2版,而第2版就會有四種host key在作業系統安裝的時候給產生出來,主要有RSA、DSA、ECDSA與ECD25519等幾種,相關的所自動產生的所有Host公鑰可以在/etc/ssh目錄底下找到,相關的指令執行所輸出的訊息如下:
[rockylinux@workstation ~]$ ls /etc/ssh/
moduli ssh_config.d ssh_host_ecdsa_key ssh_host_ed25519_key ssh_host_rsa_key
ssh_config sshd_config ssh_host_ecdsa_key.pub ssh_host_ed25519_key.pub ssh_host_rsa_key.pub
ssh rockylinux@localhost
指令登入並驗證fingerprint。.ssh/known_hosts
檔案刪除,並再執行一次ssh rockylinux@localhost
觀察輸出的訊息,並試著修復。