SSH Connection Closed By Port 22: Causes and Fixes (2026)

SSH says Connection closed by IP port 22, UNKNOWN port 65535, or closed by remote host? Learn which stage failed and how to fix each one.

SSH Connection Closed By Port 22: Causes and Fixes (2026)

You run ssh and it ends with a single line and no explanation:

bash
Connection closed by 203.0.113.10 port 22

Or one of its relatives: Connection closed by UNKNOWN port 65535, Connection to example.com closed by remote host., or, in the server log, Connection closed by authenticating user deploy 198.51.100.7 port 51234 [preauth].

They look alike, but each one is printed at a different stage of the connection, and the stage tells you where to look. This guide maps every “connection closed by” variant to the point where the connection died, shows what we reproduced in a lab, and gives the fix for each case.

Quick Summary: What You’ll Learn

  • What Connection closed by <ip> port 22 means and why the client can’t tell you more
  • How to read the last lines of ssh -v and the server log to find the real cause
  • Why UNKNOWN port 65535 shows up whenever you use ProxyJump or ProxyCommand
  • The difference between “closed by remote host” and a plain “Connection to host closed.”
  • What the [preauth] lines in /var/log/auth.log or the journal actually mean

All messages below come from a lab running OpenSSH 9.6p1 on Ubuntu 24.04 (client and server), unless a section says otherwise.

What does “Connection closed by <ip> port 22” mean?

It means the server end of the TCP connection hung up while your ssh client was still waiting for the next protocol message. The client got an end-of-file instead of data, so it prints who closed (203.0.113.10 port 22) and exits with status 255.

That’s all the client knows. The server didn’t send a reason; if it had, you’d see a Received disconnect from ... line with text instead. So the fix is never on the client line itself. It’s in two places:

  • How far the handshake got, which ssh -v shows.
  • Why the server gave up, which only the server log knows.

Here’s every variant we hit and the stage it belongs to:

  • kex_exchange_identification: Connection closed by remote host: the connection died before key exchange even started. That’s a firewall, a connection limit or something other than sshd on the port, a different problem from the one covered here.
  • Connection closed by 203.0.113.10 port 22: key exchange finished, then the server closed the connection before or during authentication.
  • Connection closed by UNKNOWN port 65535: the same thing, seen through ProxyJump or ProxyCommand.
  • Received disconnect from 203.0.113.10 port 22:2: Too many authentication failures: the server did give a reason. Not the same problem.
  • Connection to example.com closed by remote host.: you were logged in, and the server side of your session went away.
  • Connection to example.com closed.: the session ended normally because the remote shell or command exited. If it happens right after login, the account’s shell is the problem.

If you get Connection refused or Connection timed out instead, you never reached sshd at all; see SSH connection refused vs timeout.

How do you find out where the connection died?

Start on the client with one -v:

bash
ssh -v [email protected]

Look at the lines just before the “Connection closed” message. In our lab, a server that dropped the connection mid-authentication ended like this:

yaml
debug1: Authentications that can continue: publickey
debug1: Next authentication method: publickey
debug1: Offering public key: id_lab ED25519 SHA256:/LDC... explicit
Connection closed by 127.0.0.1 port 2299

That tells you key exchange succeeded and the server hung up while it was checking a key. If the last line is a banner or key exchange message instead, you’re in kex territory.

Then read the server side. On Ubuntu and Debian the systemd unit is ssh, on RHEL, Fedora and their rebuilds it’s sshd:

bash
sudo journalctl -u ssh --since "10 min ago"
sudo grep sshd /var/log/auth.log | tail -n 50

If you can’t log in to read the log, get on the box another way first: a cloud provider’s serial or web console, an existing session, or a second account that still works.

Why does the server close the connection during login?

This is the case people search for most: a plain Connection closed by <ip> port 22 after the client has offered a key or is waiting at a prompt.

The login took longer than LoginGraceTime

LoginGraceTime is how long sshd waits for a client to finish logging in. The sshd_config(5) man page: “The server disconnects after this time if the user has not successfully logged in.” The default is 120 seconds.

Clients rarely sit at a prompt that long, but server-side steps can be slow: an AuthorizedKeysCommand that queries LDAP or an API, PAM modules that talk to a directory, or a slow DNS lookup. We set LoginGraceTime 3 and pointed AuthorizedKeysCommand at a script that sleeps for 10 seconds. The client offered its key and then printed:

bash
Connection closed by 127.0.0.1 port 2299

exactly 3.02 seconds after it started, with exit status 255. Note what the server logged at the default LogLevel INFO in our lab: nothing for that connection, not even at DEBUG1. That’s why this one is hard to spot from logs alone.

To check, print the effective value and time the slow parts by hand:

bash
sudo sshd -T | grep -i -e logingracetime -e authorizedkeyscommand
time sudo -u nobody /usr/local/bin/your-keys-command deploy

Fix the slow lookup rather than raising the grace time. A long grace time also means half-finished connections hold a slot for longer.

The server said no before you got a password prompt

Several “no” answers don’t produce a closed connection at all. They produce Permission denied, which is useful to rule things out. In our lab:

  • PermitRootLogin no with a valid root key: client got Permission denied (publickey), server logged ROOT LOGIN REFUSED FROM 127.0.0.1 port 56822.
  • DenyUsers root: client got Permission denied (publickey), server logged User root from 127.0.0.1 not allowed because listed in DenyUsers.
  • MaxAuthTries 1 with two keys offered: client got Received disconnect from 127.0.0.1 port 2299:2: Too many authentication failures, server logged maximum authentication attempts exceeded for root.

If you see any of these, the server is answering and the problem is the account or the key. Permission denied (publickey) walks through that side.

What does “Connection closed by UNKNOWN port 65535” mean?

It shows up when ssh doesn’t talk to the server over a TCP socket of its own, which is the case with ProxyJump (-J) and ProxyCommand. The client is then reading from a pipe, so it has no peer address to print. The OpenSSH source does exactly that: when the connection isn’t on a socket, the remote address is set to the string UNKNOWN and the port to 65535.

So UNKNOWN port 65535 is not a strange port and not a network scanner. It’s the normal “connection closed” message with the address left blank, and you have to work out which hop closed.

We tested both cases with a jump host in front of a target:

  • The jump host rejected our user. The client printed [email protected]: Permission denied (publickey). and then Connection closed by UNKNOWN port 65535.
  • The jump host accepted us, and the target hit its LoginGraceTime. The client printed only Connection closed by UNKNOWN port 65535.

Same last line, different hop. To split them:

  • Connect to the jump host on its own: ssh -v bastion true. If that fails, fix the bastion first.
  • Run the full chain with -v: ssh -v -J bastion app1. Error lines printed by the inner connection to the bastion show up before the final message, as in the first case above.
  • Read the log on whichever host the connection reached last.

For the config side of jump hosts, see bastion host ProxyJump setup.

What does “Connection to host closed by remote host” mean?

You were logged in, and then the server side of the session ended without you asking. In the lab we started sleep 20 over ssh and killed the server’s session process from another shell. The client printed:

bash
Connection to 127.0.0.1 closed by remote host.
Connection to 127.0.0.1 closed.

and exited with 255. Things that do this in practice:

  • Someone or something killed your session process: an admin, a reboot, a cleanup script, or the kernel’s OOM killer on a box that ran out of memory. Check last -x, journalctl -k | grep -i oom and the uptime.
  • The server’s client-alive check gave up. With ClientAliveInterval set, sshd sends probes, and “if this threshold is reached while client alive messages are being sent, sshd will disconnect the client” (ClientAliveCountMax, sshd_config(5)).

If the network dropped instead of the server ending the session, you usually get a broken pipe or a timeout message, not this one. Either way, long-running work belongs in tmux or screen so it survives the disconnect; see tmux for remote server sessions.

Why does ssh log in and close right away?

If you see Connection to example.com closed. (no “by remote host”) right after login, the session ended normally from ssh’s point of view: whatever ran on the server exited. The usual causes:

  • The account’s shell is /usr/sbin/nologin or /bin/false, or a ForceCommand runs something that exits. With ForceCommand /bin/false our lab client printed Connection to 127.0.0.1 closed. and exited with status 1, which is the remote command’s exit status passed through.
  • A broken ChrootDirectory. This one is silent. We made the chroot directory world-writable; key login succeeded, then the client exited with status 255 and printed nothing at all, not even a “closed” line. Only the server log had the reason: bad ownership or modes for chroot directory "/srv/jail". The man page explains why: sshd “checks that all components of the pathname are root-owned directories which are not writable by group or others.”

To see what a specific user gets, ask sshd for the effective settings after Match blocks apply, and check the shell:

bash
sudo sshd -T -C user=deploy,host=client.example.com,addr=198.51.100.7 | grep -i -e chrootdirectory -e forcecommand
getent passwd deploy

If the user is an SFTP-only chroot account, SFTP permission denied covers the directory ownership rules in more depth.

What do “Connection closed by authenticating user” lines in the server log mean?

These are the server’s side of the story, and on any internet-facing box most of them are scanners. What each one means, from our lab:

  • Connection closed by authenticating user root 127.0.0.1 port 49340 [preauth]: the user exists, the client tried to authenticate and then hung up. We got it by offering a key the server didn’t accept; the client printed Permission denied (publickey) and quit. Also logged after ROOT LOGIN REFUSED.
  • Connection closed by invalid user nosuchuser 127.0.0.1 port 52290 [preauth]: the user doesn’t exist, preceded by Invalid user nosuchuser from .... Watch for this one with real usernames: a user listed in DenyUsers is also logged as invalid user.
  • Connection closed by 127.0.0.1 port 52292 [preauth]: the client closed before sending a username. ssh-keyscan produces this.
  • Connection closed by 127.0.0.1 port 49560 (no [preauth]): a bare TCP check (nc -z) or a client that read the banner and left. Load balancer and uptime health checks look like this.

[preauth] means the line came from the part of sshd that runs before authentication finished.

When one of these lines has your own IP, read the line just above it. The real reason (ROOT LOGIN REFUSED, not allowed because, Could not open user ... authorized keys) is usually there. If the lines come from addresses you don’t know, they are noise from scanners; key-only login and tools like fail2ban are the answer, not chasing every line.

A checklist to run top to bottom

  • Note the exact last line on the client: which variant from the list above?
  • ssh -v and read the three lines before it: did key exchange finish?
  • Using a jump host? Test the bastion alone, then the chain with -v.
  • Read the server log for your IP and the line above the “closed” line.
  • Nothing logged and it dies at a fixed time? Check LoginGraceTime and time any AuthorizedKeysCommand or PAM lookup.
  • Logs in and closes at once? Check the user’s shell, ForceCommand and ChrootDirectory with sshd -T -C user=....
  • Closed mid-session? Check for OOM kills, reboots and ClientAlive* settings.

Where does termique fit?

termique is a free, native ssh manager we build, with end-to-end encrypted credentials. It doesn’t change what sshd does on the other end, so every cause above still applies to connections you open from it.

Where it helps is the jump host case. In termique you pick a jump host for a server under Connect via, and each hop authenticates with that host’s own saved credential. Chains can be up to 8 hops, and during a session the chain shows as Via in the session menu, so you can see which bastion a connection went through when you’re working out which hop closed it.

Saved credentials, passwords and private keys, are encrypted before they’re stored and sync to your other devices, including iOS, as ciphertext only. If you want to try it, download termique for macOS, Windows, Linux or iOS.

FAQ

What does “Connection closed by port 22” mean in SSH?

The server end of the connection hung up after key exchange, before or during login, without sending a reason. Run ssh -v to see the last step that worked, then read the server log for your IP; the cause is almost always on the line above the “closed” entry.

What is “Connection closed by UNKNOWN port 65535”?

It’s the same message printed when ssh connects through ProxyJump or ProxyCommand. The client is reading from a pipe, not a socket, so OpenSSH fills in UNKNOWN and 65535 as placeholders. Test the jump host on its own to find out which hop closed.

Why do I see “Connection closed by authenticating user” in auth.log?

A client with a valid username started to authenticate and disconnected before finishing. From unknown addresses it’s usually a scanner. From your own IP it means your client gave up, most often after its key was rejected; the line above it says why.

Why does SSH close immediately after login?

Whatever ran on the server exited at once: a nologin or /bin/false shell, a ForceCommand, or a ChrootDirectory with the wrong ownership, which fails without any message on the client. Check with getent passwd <user> and sshd -T -C user=<user>,....

Does restarting sshd fix “Connection closed by”?

Only if the running sshd is in a bad state. Most causes here are configuration or account problems, and a restart brings them straight back. Run sudo sshd -t before any restart so a config error doesn’t lock you out, and keep an existing session open while you test.

All articles