1. 程式人生 > >Ubuntu SSH - ssh: connect to host ****** port 22: Connection refused

Ubuntu SSH - ssh: connect to host ****** port 22: Connection refused

Ubuntu SSH - ssh: connect to host ****** port 22: Connection refused

OpenSSH is the premier connectivity tool for remote login with the SSH protocol. It encrypts all traffic to eliminate eavesdropping, connection hijacking, and other attacks. In addition, OpenSSH provides a large suite of secure tunneling capabilities, several authentication methods, and sophisticated configuration options.
eavesdrop ['iːvzdrɒp]:n. 屋檐上流下來的水 vi. 偷聽,竊聽
hijack ['haɪdʒæk]:vt. 搶劫,揩油 vi. 攔路搶劫 n. 劫持,威逼,敲詐

ssh: connect to host ****** port 22: Connection refused

ssh: connect to host 192.168.2.56 port 22: Connection refused
ssh: connect to host localhost port 22: Connection refused

[email protected]:~$ ssh [email protected]
ssh: connect to host 192.168.2.56 port 22: Connection refused
[email protected]
:~$
[email protected]:~$ ssh localhost
ssh: connect to host localhost port 22: Connection refused
[email protected]:~$

判斷是否安裝 ssh 服務

Ubuntu 預設並沒有安裝 ssh 服務,如果通過 ssh 連線 Ubuntu,需要手動安裝 openssh-server。

[email protected]:~$ ssh localhost
ssh: connect to host localhost port 22: Connection refused
[email protected]
:~$ [email protected]:~$ ps -e | grep ssh [email protected]:~$

安裝 openssh-server

用 ps -e | grep ssh 命令來看下,openssh-server 安裝上去沒有。sshd 就是我們所安裝的。

[email protected]:~$ sudo apt-get install openssh-server
[sudo] password for IBM: 
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following additional packages will be installed:
  ncurses-term openssh-sftp-server ssh-import-id
Suggested packages:
  ssh-askpass rssh molly-guard monkeysphere
The following NEW packages will be installed:
  ncurses-term openssh-server openssh-sftp-server ssh-import-id
0 upgraded, 4 newly installed, 0 to remove and 322 not upgraded.
Need to get 633 kB of archives.
After this operation, 5,136 kB of additional disk space will be used.
Do you want to continue? [Y/n] Y
Get:1 http://cn.archive.ubuntu.com/ubuntu xenial/main amd64 ncurses-term all 6.0+20160213-1ubuntu1 [249 kB]
Get:2 http://cn.archive.ubuntu.com/ubuntu xenial-updates/main amd64 openssh-sftp-server amd64 1:7.2p2-4ubuntu2.4 [38.7 kB]
Get:3 http://cn.archive.ubuntu.com/ubuntu xenial-updates/main amd64 openssh-server amd64 1:7.2p2-4ubuntu2.4 [335 kB]
Get:4 http://cn.archive.ubuntu.com/ubuntu xenial/main amd64 ssh-import-id all 5.5-0ubuntu1 [10.2 kB]
Fetched 633 kB in 7s (83.9 kB/s)                                               
Preconfiguring packages ...
Selecting previously unselected package ncurses-term.
(Reading database ... 184475 files and directories currently installed.)
Preparing to unpack .../ncurses-term_6.0+20160213-1ubuntu1_all.deb ...
Unpacking ncurses-term (6.0+20160213-1ubuntu1) ...
Selecting previously unselected package openssh-sftp-server.
Preparing to unpack .../openssh-sftp-server_1%3a7.2p2-4ubuntu2.4_amd64.deb ...
Unpacking openssh-sftp-server (1:7.2p2-4ubuntu2.4) ...
Selecting previously unselected package openssh-server.
Preparing to unpack .../openssh-server_1%3a7.2p2-4ubuntu2.4_amd64.deb ...
Unpacking openssh-server (1:7.2p2-4ubuntu2.4) ...
Selecting previously unselected package ssh-import-id.
Preparing to unpack .../ssh-import-id_5.5-0ubuntu1_all.deb ...
Unpacking ssh-import-id (5.5-0ubuntu1) ...
Processing triggers for man-db (2.7.5-1) ...
Processing triggers for ufw (0.35-0ubuntu2) ...
Processing triggers for systemd (229-4ubuntu21.1) ...
Processing triggers for ureadahead (0.100.0-19) ...
ureadahead will be reprofiled on next reboot
Setting up ncurses-term (6.0+20160213-1ubuntu1) ...
Setting up openssh-sftp-server (1:7.2p2-4ubuntu2.4) ...
Setting up openssh-server (1:7.2p2-4ubuntu2.4) ...
Creating SSH2 RSA key; this may take some time ...
2048 SHA256:VKvdcDxChl0z+xJ84O/VDjQWhR0c7PJWAJ2C+kD0jyQ [email protected] (RSA)
Creating SSH2 DSA key; this may take some time ...
1024 SHA256:bMHTBGJvRzmqWeA+kuHFGifIjWg2SDwjs+sVzBgq9Ig [email protected] (DSA)
Creating SSH2 ECDSA key; this may take some time ...
256 SHA256:EvlBk/pUm3nBLH5dOdVihIRVyXftkEVz3StM9rthX8M [email protected] (ECDSA)
Creating SSH2 ED25519 key; this may take some time ...
256 SHA256:EMZZCbuDWBMwN2uvFTjv1feMTJSm+41pQgndcLyfYsk [email protected] (ED25519)
Setting up ssh-import-id (5.5-0ubuntu1) ...
Processing triggers for systemd (229-4ubuntu21.1) ...
Processing triggers for ureadahead (0.100.0-19) ...
Processing triggers for ufw (0.35-0ubuntu2) ...
[email protected]:~$ 
[email protected]:~$ ps -e | grep ssh
 3059 ?        00:00:00 sshd
[email protected]:~$

ssh 服務開啟,啟動 ssh 服務:

sudo service sshd start
sudo service sshd restart

ssh localhost 命令用來連線本機。
ssh [email protected](or IP)
由於預設關閉了 ssh 服務,連線無法建立,第一次連線需要手動輸入 yes,才能建立連線。

[email protected]:~$ ssh localhost
The authenticity of host 'localhost (127.0.0.1)' can't be established.
ECDSA key fingerprint is SHA256:EvlBk/pUm3nBLH5dOdVihIRVyXftkEVz3StM9rthX8M.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'localhost' (ECDSA) to the list of known hosts.
[email protected]'s password: 
Welcome to Ubuntu 16.04.3 LTS (GNU/Linux 4.10.0-28-generic x86_64)

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

316 packages can be updated.
107 updates are security updates.


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.

[email protected]:~$ 
[email protected]:~$ exit
logout
Connection to localhost closed.
[email protected]:~$
[email protected]:~$ ssh [email protected]
The authenticity of host '192.168.2.56 (192.168.2.56)' can't be established.
ECDSA key fingerprint is SHA256:EvlBk/pUm3nBLH5dOdVihIRVyXftkEVz3StM9rthX8M.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.2.56' (ECDSA) to the list of known hosts.
[email protected]'s password: 
Welcome to Ubuntu 16.04.3 LTS (GNU/Linux 4.10.0-28-generic x86_64)

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

316 packages can be updated.
107 updates are security updates.

Last login: Tue Oct 16 17:12:43 2018 from 127.0.0.1
[email protected]:~$ 
[email protected]:~$ exit
logout
Connection to 192.168.2.56 closed.
[email protected]:~$