You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

安装OpenSSH后SSH连接localhost遇连接重置问题如何解决?

Troubleshooting "Connection reset by 127.0.0.1 port 22" when connecting to localhost via SSH

Let’s work through this issue step by step—this error typically indicates the SSH server isn’t accessible for one of a few common reasons. Here’s what to check:

  • Verify the SSH server is running
    First, make sure the sshd service is up and running. Run this command to check its status:
    systemctl status sshd
    (If you’re on an older system without systemd, use service ssh status instead.)
    If it shows "inactive" or "failed", start the service with:
    systemctl start sshd
    To ensure it starts automatically on boot, run:
    systemctl enable sshd

  • Check if port 22 is allowed through the firewall
    Your firewall might be blocking incoming connections on port 22.
    For ufw (common on Ubuntu/Debian), check the status with:
    ufw status
    If "OpenSSH" isn’t listed as allowed, add the rule:
    ufw allow ssh
    For iptables, add a rule to allow TCP traffic on port 22:
    iptables -A INPUT -p tcp --dport 22 -j ACCEPT
    Don’t forget to save the iptables rules afterward (try iptables-save > /etc/iptables/rules.v4 on Debian/Ubuntu).

  • Inspect the SSH server configuration
    A misconfigured sshd_config file can cause connection resets. Open the file with your editor:
    sudo nano /etc/ssh/sshd_config
    Check these key settings:

    • ListenAddress: Should be set to 0.0.0.0 (to listen on all interfaces) or explicitly include 127.0.0.1
    • PasswordAuthentication: Ensure this is set to yes (unless you’re using SSH keys and have that configured properly)
    • PermitRootLogin: If you’re trying to connect as root, make sure this isn’t set to no (though it’s safer to use a regular user)
    • AllowUsers: If this line exists, confirm your user is listed here (e.g., AllowUsers your_username)
      After making changes, restart the SSH server:
      systemctl restart sshd
  • Check if port 22 is being used by another process
    Sometimes another application might be occupying port 22. Use this command to see what’s listening on port 22:
    ss -tulpn | grep 22
    If you see a process other than sshd using the port, either stop that process or change the SSH port in sshd_config (set Port to something like 2222, restart the service, then connect with ssh user@localhost -p 2222).

  • Check SELinux (if you’re using it)
    SELinux can block SSH connections even if the firewall is open. First, check its status:
    getenforce
    If it returns "Enforcing", try temporarily disabling it to test:
    setenforce 0
    If the connection works after this, you’ll need to update SELinux rules to allow SSH. For example, to ensure port 22 is labeled correctly:
    semanage port -a -t ssh_port_t -p tcp 22
    Then re-enable SELinux with setenforce 1.

  • Look at SSH server logs for specific errors
    The logs will give you detailed info about why the connection is being reset. Run this command to tail the auth logs:
    tail -f /var/log/auth.log
    (On RHEL/CentOS, the log is /var/log/secure.)
    Try connecting again while watching the logs—you’ll see exactly what’s failing (e.g., authentication issues, configuration errors).

内容的提问来源于stack exchange,提问作者Shehan Ekanayake

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 03:16:12