ssh: connect to host xxx.xxx.xxx.xxx port 22: Connection refused

工具/原料:Ubuntu

在采用scp在不同机器之间进行文件拷贝时出现标题所示的错误,原因可能是:

1.sshd 未启动

2.sshd 未安装

3.防火墙

4需重新启动ssh 服务

查看sshd进程方式:ps -e | grep ssh,如果类似如下

14688 ?        00:00:00 ssh-agent
24647 pts/2    00:00:00 ssh

没有sshd,则说明sshd未启动,尝试启动:

service sshd restart(需要root权限)

如果出现:

Failed to restart ssh.service: Unit ssh.service not found.

说明sshd未安装,或者通过查看/etc/init.d/目录下是否有ssh,若没有则说明需要安装sshd.

安装sshd:

sudo apt-get install openssh-server

安装完成后ps -e | grep ssh查看是否启动。

注意:安装完后,查看/etc/ssh/sshd_config中是否有"PermitRootLoginwithout-password“这一项,若有加#注释掉,然后增加一句"PermitRootLogin   yes",保存退出即可。

如果sshd已经安装,且也已经启动,但还是连接不上,试着重启ssh

service ssh restart

若还是不行,那就可能是防火墙的问题了,关闭防火墙

sudo ufw disable

然后检查是否能够连接成功

ssh localhost

出现

The authenticity of host 'localhost (127.0.0.1)' can't be established.
ECDSA key fingerprint is SHA256:wCcI3OLsC4FcUDhMmuJZ3WLf4uK0BMphJaRkFIo/zqg.
Are you sure you want to continue connecting (yes/no)? 

说明连接成功。

原文地址:https://www.cnblogs.com/jiandahao/p/6125544.html