NeuroAgent

How to Fix SSH Connection Issues with Ubuntu 24.04

Fix SSH connection issues with Ubuntu 24.04: socket/service conflicts, interface configuration, iptables and UFW troubleshooting. Complete guide to resolving errors.

Question

How to fix SSH connection issues to a Linux Ubuntu server?

I’ve rented a VPS running Linux Ubuntu 24.04, but I can’t connect to it via SSH. Here’s what I’ve already done:

  1. Installed the openssh-server package: sudo apt install openssh-server
  2. Checked the SSH service status — it’s active (running): sudo systemctl status ssh
  3. Restarted the SSH service
  4. Checked the firewall and confirmed that port 22 is open:
    • sudo ufw allow 22
    • sudo ufw status
  5. Verified that port 22 is open: nc -zv 94.103.2.134 22

When attempting to connect from a client (PowerShell) via SSH:
ssh root@94.103.2.134

Pings are successful, but there’s no access. What could be the problem and how can I fix it?

NeuroAgent

SSH Connection to Ubuntu 24.04 May Not Work Even with Active Service and Open Port 22 Due to Socket/Service Conflict, Incorrect Interface Binding, iptables Issues, or Ubuntu 24.04 Specific Configuration Features

Contents

Main Causes of the Problem

The most common reasons why SSH doesn’t work in Ubuntu 24.04 despite an active service:

  1. Conflict between socket and service - In Ubuntu 24.04, SSH may be running as a socket but not as a service, leading to a situation where the service is active but ports are not listening.
  2. Incorrect interface binding - The SSH server may be configured to listen only on a specific IP address or interface.
  3. iptables issues - Even with port 22 open in UFW, iptables rules may be blocking connections.
  4. Ubuntu 24.04 specific configuration features - In newer Ubuntu versions, SSH settings may migrate to separate systemd configuration files.

According to research on AskUbuntu, in Ubuntu 22.10, 23.04, and 23.10 during updates, port settings or ListenAddress may migrate to /etc/systemd/system/ssh.socket.d/addresses.conf.

SSH Configuration Check

Check the main SSH server configuration parameters:

bash
sudo nano /etc/ssh/sshd_config

Ensure the following parameters are properly configured:

  • Port 22 - standard port, can be changed
  • ListenAddress 0.0.0.0 - listen on all interfaces
  • PermitRootLogin yes - allow root login (or no for security)
  • PasswordAuthentication yes - allow password authentication

According to the official Ubuntu 24.04 SSH guide, to change the port, you need to edit /etc/ssh/sshd_config, find the Port 22 line, and change the port number.

Service Analysis

Check which type of SSH service is running:

bash
systemctl list-units --type=socket | grep ssh
systemctl list-units --type=service | grep ssh

If you see an active ssh.socket but not an active ssh.service, this may be the cause of the problem.

As noted in an AskUbuntu answer, the issue is resolved by disabling the socket and enabling the service:

bash
sudo systemctl disable --now ssh.socket
sudo systemctl enable --now ssh.service

Network Binding Configuration

Check on which interfaces and IP addresses SSH is listening:

bash
sudo ss -tlnp | grep ssh
netstat -tlnp | grep ssh

If SSH is only listening on localhost (127.0.0.1) and not on an external IP, change the configuration:

bash
sudo nano /etc/ssh/sshd_config

Change or add:

ListenAddress 0.0.0.0

Restart SSH:

bash
sudo systemctl restart ssh

iptables and UFW Check

Even if UFW allows port 22, check iptables rules:

bash
sudo iptables -L -n -v
sudo iptables -L INPUT -n -v | grep 22

Add an iptables rule if necessary:

bash
sudo iptables -A INPUT -p tcp --dport 22 -m conntrack --ctstate NEW,ESTABLISHED -j ACCEPT

Check UFW status:

bash
sudo ufw status verbose

According to an article on Baeldung, you need to ensure that port 22 is actually open in UFW and iptables.

Socket/Service Conflict Resolution

This is the most likely cause of your problem in Ubuntu 24.04. Execute the following commands:

bash
sudo systemctl disable --now ssh.socket
sudo systemctl enable --now ssh.service
sudo systemctl restart ssh

Check the status:

bash
sudo systemctl status ssh.socket
sudo systemctl status ssh.service

The solution from AskUbuntu confirms that this approach fixes the issue in Ubuntu 24.04.

Additional Checks

  1. Check SSH logs:
bash
sudo tail -f /var/log/auth.log
sudo tail -f /var/log/syslog | grep ssh
  1. Check port availability:
bash
nc -zv your_server_ip 22
telnet your_server_ip 22
  1. Check network settings:
bash
ip addr show
netstat -tlnp | grep 22
  1. Check SELinux/AppArmor (if installed):
bash
sudo aa-status
sudo sestatus
  1. Check for duplicate IP addresses:
bash
arp -a

A Medium article notes that when duplicate IP addresses are found, you need to change the SSH server’s IP address.

Conclusion

  1. Main issue - In Ubuntu 24.04, a conflict between ssh.socket and ssh.service often occurs, even if the service appears active.
  2. Solution - Disable the socket and enable the standard service using the commands systemctl disable --now ssh.socket and systemctl enable --now ssh.service.
  3. Additional measures - Check interface binding, iptables and UFW configuration, and review SSH logs for diagnostics.
  4. Problem prevention - Always check service status with netstat or ss, not just with systemctl status.

By following these steps, you should resolve the SSH connection issue to your Ubuntu 24.04 VPS. If the problem persists, check the logs for errors and ensure your VPS provider is not blocking port 22 at the network level.

Sources

  1. 24.04 - SSH connection refused - Ask Ubuntu
  2. Enable SSH on Ubuntu 24.04 – Secure Remote Access - LinuxConfig
  3. How to Fix Connection Refused by Port 22 Debian/Ubuntu – Linux Hint
  4. Solving Error port 22: Connection refused in Linux | Baeldung on Linux
  5. How to fix: Connection refused by port 22 Debian/Ubuntu | Medium
  6. (Resolved) SSH Connection Refused on Ubuntu & Debian – TecAdmin
  7. networking - ssh connection refused - Ask Ubuntu