欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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

程序员文章站 2024-03-14 09:30:22
...

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]omputer:~$ 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]:~$