SSH(ssh: connect to host localhost port 22: Connection refused)問題的解決

我們在遠程ssh鏈接ubuntn服務器的時候,會出現一下的一個問題

ssh: connect to host localhost port 22: Connection refused

之所以出現這個問題,是因爲我們ubuntn系統默認是沒有安裝openssh-server的。

解決辦法就是安裝這個模塊

sudo apt-get install openssh-server

安裝完成後,我們再輸入下面的命令,就可以遠程登錄了

ssh [email protected]

再執行完命令的時候,還會出現一個問題

➜  ~ ssh  [email protected]
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@    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:mPfvt1SS+fncIH5f8DYiFtoII7eLtHdTJ1CbTRYx6oo.
Please contact your system administrator.
Add correct host key in /Users/用戶名/.ssh/known_hosts to get rid of this message.
Offending ECDSA key in /Users/用戶名/.ssh/known_hosts:6
ECDSA host key for xx.20.xxx.63 has changed and you have requested strict checking.
Host key verification failed.

原因:

因爲服務器的ip發生變更了
第一次SSH連接時,會生成一個認證,儲存在客戶端(也就是用SSH連線其他電腦的那個,自己操作的那個)中的known_hosts,但是如果服務器驗證過了,認證資訊當然也會更改,服務器端與客戶端不同時,就會跳出錯誤啦。

解決辦法:

輸入命令:ssh-keygen -R +輸入服務器的IP

 ~ ssh-keygen -R xx.20.xxx.63
# Host xx.20.xxx.63 found: line 36
/Users/用戶名/.ssh/known_hosts updated.

或者

rm ~/.ssh/known_hosts

上面兩個問題執行其中一個之後,在繼續執行ssh [email protected]

~ ssh  [email protected]
The authenticity of host '172.20.110.63 (172.20.110.63)' can't be established.
ECDSA key fingerprint is SHA256:mPfvt1SS+fncIH5f8DYiFtoII7eLtHdTJ1CbTRYx6oo.
Are you sure you want to continue connecting (yes/no/[fingerprint])? 

輸入yes

[email protected]'s password: 	[輸入密碼]
Welcome to Ubuntu 18.04.6 LTS (GNU/Linux 5.4.0-107-generic x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

26 updates can be applied immediately.
To see these additional updates run: apt list --upgradable

Your Hardware Enablement Stack (HWE) is supported until April 2023.

The programs included with the Ubuntu system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by
applicable law.

ubuntu@ubuntu-HP-Z4-G4-Workstation:~$	[進入服務器]
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章