解决 ssh: connect to host xxx port 22: Connection refused&& Failed to restart sshd.service: Unit ssh.

1.安装 ssh server

sudo apt-get install openssh-server

sudo apt-get install openssh-client

2.查看 ssh 状态

sudo systemctl status  ssh

* ssh.service
   Loaded: masked (/dev/null; bad)
   Active: failed (Result: exit-code) since Fri 2019-04-12 14:45:09 CST; 1min 22s ago
 Main PID: 7966 (code=exited, status=255)

Apr 12 14:45:09 yy-pc systemd[1]: Starting OpenBSD Secure Shell server...
Apr 12 14:45:09 y-pc sshd[7966]: error: Bind to port 25 on 0.0.0.0 failed: Address already in use.
Apr 12 14:45:09 y-pc sshd[7966]: error: Bind to port 25 on :: failed: Address already in use.
Apr 12 14:45:09 y-pc sshd[7966]: fatal: Cannot bind any address.
Apr 12 14:45:09 y-pc systemd[1]: ssh.service: Main process exited, code=exited, status=255/n/a
Apr 12 14:45:09 y-pc systemd[1]: ssh.service: Failed with result 'exit-code'.
Apr 12 14:45:09 y-pc systemd[1]: Failed to start OpenBSD Secure Shell server.


3.修改 config 文件

sudo vi /etc/ssh/sshd_config

ssh 默认 22端口

打开注释掉的或者加上

Port 22

 

4. 重启ssh server

service sshd restart

出现了这个小问题:

Failed to restart ssh.service: Unit ssh.service is masked.

sudo systemctl unmask sshs

service sshd restart

sudo systemctl status  ssh

状态正常:

* ssh.service - OpenBSD Secure Shell server
   Loaded: loaded (/lib/systemd/system/ssh.service; enabled; vendor preset: enabled)
   Active: active (running) since Fri 2019-04-12 14:48:29 CST; 7min ago
  Process: 8276 ExecStartPre=/usr/sbin/sshd -t (code=exited, status=0/SUCCESS)
 Main PID: 8277 (sshd)
    Tasks: 1 (limit: 4915)
   CGroup: /system.slice/ssh.service
           `-8277 /usr/sbin/sshd -D

Apr 12 14:48:29 y-pc systemd[1]: Starting OpenBSD Secure Shell server...
Apr 12 14:48:29 y-pc sshd[8277]: Server listening on 0.0.0.0 port 22.
Apr 12 14:48:29 y-pc sshd[8277]: Server listening on :: port 22.
Apr 12 14:48:29 y-pc systemd[1]: Started OpenBSD Secure Shell server.
Apr 12 14:48:47 y-pc sshd[8285]: Authentication refused: bad ownership or modes for directory /home/yq
Apr 12 14:48:50 y-pc sshd[8285]: Accepted password for yq from 127.0.0.1 port 60136 ssh2
Apr 12 14:48:50 y-pc sshd[8285]: pam_unix(sshd:session): session opened for user yq by (uid=0)
Apr 12 14:49:02 y-pc sshd[8408]: Accepted password for yq from 192.168.20.194 port 1089 ssh2
Apr 12 14:49:02 y-pc sshd[8408]: pam_unix(sshd:session): session opened for user yq by (uid=0)

 

当你在使用 SSH 连接到主机时,出现 "ssh: connect to host XXX port 22: Connection refused" 的错误提示,通常表示无法建立 SSH 连接。 这个错误可能有以下几个常见的原因: 1. SSH 服务未启动:确认目标主机上是否已经启动了 SSH 服务。可以通过运行 `sudo service ssh status` 或 `sudo systemctl status ssh` 命令来检查 SSH 服务的状态。如果服务未运行,可以使用 `sudo service ssh start` 或 `sudo systemctl start ssh` 命令来启动 SSH 服务。 2. 防火墙或网络配置问题:可能是由于防火墙或网络配置的问题导致连接被拒绝。请确保目标主机和你的计算机之间的网络连接正常,并且没有防火墙规则阻止 SSH 连接。你可以尝试暂时关闭防火墙或者检查防火墙配置,确保允许 SSH 连接通过。 3. SSH 配置问题:可能是由于 SSH 服务器的配置问题导致连接被拒绝。可以检查 SSH 服务器配置文件(通常是 `/etc/ssh/sshd_config`),确保 SSH 服务的端口号正确设置为 22,并且没有其他限制或配置错误。 4. SSH 服务故障:如果以上步骤都没有解决问题,可能是由于 SSH 服务本身出现故障导致无法连接。你可以尝试重新启动 SSH 服务,或者在目标主机上进行一些故障排除操作,如查看系统日志等。 综上所述,如果你遇到 "ssh: connect to host XXX port 22: Connection refused" 错误,可以按照上述步骤逐一排查问题,并根据具体情况采取相应的解决措施。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

月上柳青

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值