port 22: Connection refused

本文记录了解决SSH连接失败的情况,通过检查22端口是否开启,发现未开启后安装并启动openssh-server服务,最终成功开启SSH服务并实现免密登录。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

设置免密登录时提示连接失败

[lj@localhost .ssh]$ ssh-copy-id lj@192.168.145.129
/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed

/bin/ssh-copy-id: ERROR: ssh: connect to host 192.168.145.129 port 22: Connection refused

查看22号端口是否开启ss -lnt ,结果是未开启

lj@ubuntu .ssh % ss -lnt                                        [0]
State  Recv-Q Send-Q Local Address:Port   Peer Address:Port Process 
LISTEN 0      4096   127.0.0.53%lo:53          0.0.0.0:*            
LISTEN 0      5          127.0.0.1:631         0.0.0.0:*            
LISTEN 0      5              [::1]:631            [::]:*            
lj@ubuntu .ssh % lsof -i:22                                     [0]

察觉到可能是SSH服务未安装因此进行下载安装sudo apt-get install openssh-server openssh-client

lj@ubuntu .ssh % sudo apt-get install openssh-server openssh-client   
[sudo] lj 的密码: 
正在读取软件包列表... 完成
正在分析软件包的依赖关系树       
正在读取状态信息... 完成       
openssh-client 已经是最新版 (1:8.2p1-4ubuntu0.5)。
openssh-client 已设置为手动安装。
下列软件包是自动安装的并且现在不需要了:
  libfprint-2-tod1 libfwupdplugin1 libllvm10
使用'sudo apt autoremove'来卸载它(它们)。
将会同时安装下列软件:
  ncurses-term openssh-sftp-server ssh-import-id
建议安装:
  molly-guard monkeysphere ssh-askpass
下列【新】软件包将被安装:
  ncurses-term openssh-server openssh-sftp-server ssh-import-id
升级了 0 个软件包,新安装了 4 个软件包,要卸载 0 个软件包,有 9 个软件包未被升级。
需要下载 688 kB 的归档。
解压缩后会消耗 6,010 kB 的额外空间。
您希望继续执行吗? [Y/n] y
获取:1 http://ftp.sjtu.edu.cn/ubuntu focal/main amd64 ncurses-term all 6.2-0ubuntu2 [249 kB]
获取:2 http://ftp.sjtu.edu.cn/ubuntu focal-updates/main amd64 openssh-sftp-server amd64 1:8.2p1-4ubuntu0.5 [51.5 kB]
获取:3 http://ftp.sjtu.edu.cn/ubuntu focal-updates/main amd64 openssh-server amd64 1:8.2p1-4ubuntu0.5 [377 kB]
获取:4 http://ftp.sjtu.edu.cn/ubuntu focal/main amd64 ssh-import-id all 5.10-0ubuntu1 [10.0 kB]
已下载 688 kB,耗时 10(65.5 kB/s)
正在预设定软件包 ...
正在选中未选择的软件包 ncurses-term。
(正在读取数据库 ... 系统当前共安装有 170245 个文件和目录。)
准备解压 .../ncurses-term_6.2-0ubuntu2_all.deb  ...
正在解压 ncurses-term (6.2-0ubuntu2) ...
正在选中未选择的软件包 openssh-sftp-server。
准备解压 .../openssh-sftp-server_1%3a8.2p1-4ubuntu0.5_amd64.deb  ...
正在解压 openssh-sftp-server (1:8.2p1-4ubuntu0.5) ...
正在选中未选择的软件包 openssh-server。
准备解压 .../openssh-server_1%3a8.2p1-4ubuntu0.5_amd64.deb  ...
正在解压 openssh-server (1:8.2p1-4ubuntu0.5) ...
正在选中未选择的软件包 ssh-import-id。
准备解压 .../ssh-import-id_5.10-0ubuntu1_all.deb  ...
正在解压 ssh-import-id (5.10-0ubuntu1) ...
正在设置 openssh-sftp-server (1:8.2p1-4ubuntu0.5) ...
正在设置 openssh-server (1:8.2p1-4ubuntu0.5) ...

Creating config file /etc/ssh/sshd_config with new version
Creating SSH2 RSA key; this may take some time ...
3072 SHA256:1j46TJASEI//oOKLmgAcI8vQnJWxZHemllI/kqPXkEc root@ubuntu (RSA)
Creating SSH2 ECDSA key; this may take some time ...
256 SHA256:l6Stjl1ZVKPuTmvFA6uLMEeaaUHjR9Dx4xPDLWFqzVQ root@ubuntu (ECDSA)
Creating SSH2 ED25519 key; this may take some time ...
256 SHA256:7h0kKmUYhxTP65ZEIdrAWGCxZb7x5tWXPdFR4tWJ6/w root@ubuntu (ED25519)
Created symlink /etc/systemd/system/sshd.service → /lib/systemd/system/ssh.service.
Created symlink /etc/systemd/system/multi-user.target.wants/ssh.service → /lib/systemd/system/ssh.service.
rescue-ssh.target is a disabled or a static unit, not starting it.
正在设置 ssh-import-id (5.10-0ubuntu1) ...
Attempting to convert /etc/ssh/ssh_import_id
正在设置 ncurses-term (6.2-0ubuntu2) ...
正在处理用于 systemd (245.4-4ubuntu3.17) 的触发器 ...
正在处理用于 man-db (2.9.1-1) 的触发器 ...
正在处理用于 ufw (0.36-6ubuntu1) 的触发器 ...

再次进行查询,安装之后22端口已经打开

lj@ubuntu .ssh % lsof -i:22                                     [0]
COMMAND   PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
ssh     11870   lj    3u  IPv4 137835      0t0  TCP ubuntu:50830->ubuntu:ssh (ESTABLISHED)
lj@ubuntu .ssh % ss -lnt                                        [0]
State  Recv-Q Send-Q Local Address:Port   Peer Address:Port Process 
LISTEN 0      4096   127.0.0.53%lo:53          0.0.0.0:*            
LISTEN 0      128          0.0.0.0:22          0.0.0.0:*            
LISTEN 0      5          127.0.0.1:631         0.0.0.0:*            
LISTEN 0      128             [::]:22             [::]:*            
LISTEN 0      5              [::1]:631            [::]:*

再次运行,成功

[lj@localhost ~]$ ssh-copy-id lj@192.168.145.129
/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/lj/.ssh/id_rsa.pub"
/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed

/bin/ssh-copy-id: WARNING: All keys were skipped because they already exist on the remote system.
		(if you think this is a mistake, you may want to use -f option)


当遇到 `port 22: Connection refused` 错误时,通常表示客户端尝试通过 SSH(默认端口 22)连接到服务器时被拒绝。以下是可能的原因和解决方法: --- ### 可能的原因及解决方案: 1. **SSH 服务未运行** - 检查服务器是否安装并运行了 SSH 服务: ```bash sudo systemctl status sshd # Linux(如 Ubuntu/CentOS) ``` - 若未运行,启动服务: ```bash sudo systemctl start sshd ``` 2. **防火墙或安全组拦截** - 检查本地或服务器防火墙是否放行端口 22: ```bash sudo ufw status # Ubuntu 防火墙 sudo firewall-cmd --list-ports # CentOS ``` - 临时开放端口(测试用): ```bash sudo ufw allow 22/tcp ``` - 如果是云服务器(如 AWS/阿里云),需在安全组规则中添加 **入站规则** 允许 TCP 22。 3. **SSH 服务监听其他端口** - 查看 SSH 配置文件确认端口: ```bash sudo grep Port /etc/ssh/sshd_config ``` - 若端口非 22,连接时需显式指定: ```bash ssh user@host -p <端口号> ``` 4. **服务器未启动或网络问题** - 确认服务器已开机且网络可达: ```bash ping <服务器IP> ``` - 检查本地网络配置或 ISP 限制。 5. **SSH 服务配置错误** - 确保配置文件中无 `ListenAddress` 限制或 `AllowUsers` 规则阻止访问。 --- ### 其他建议: - 使用 `telnet` 或 `nc` 测试端口连通性: ```bash telnet <服务器IP> 22 ``` - 查看 SSH 日志定位问题: ```bash sudo tail -f /var/log/auth.log # Ubuntu sudo journalctl -u sshd # Systemd 系统 ``` ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

yitahutu79

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

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

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

打赏作者

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

抵扣说明:

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

余额充值