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:
- Installed the openssh-server package:
sudo apt install openssh-server - Checked the SSH service status — it’s active (running):
sudo systemctl status ssh - Restarted the SSH service
- Checked the firewall and confirmed that port 22 is open:
sudo ufw allow 22sudo ufw status
- 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?
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
- SSH Configuration Check
- Service Analysis
- Network Binding Configuration
- iptables and UFW Check
- Socket/Service Conflict Resolution
- Additional Checks
- Conclusion
Main Causes of the Problem
The most common reasons why SSH doesn’t work in Ubuntu 24.04 despite an active service:
- 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.
- Incorrect interface binding - The SSH server may be configured to listen only on a specific IP address or interface.
- iptables issues - Even with port 22 open in UFW, iptables rules may be blocking connections.
- 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:
sudo nano /etc/ssh/sshd_config
Ensure the following parameters are properly configured:
Port 22- standard port, can be changedListenAddress 0.0.0.0- listen on all interfacesPermitRootLogin yes- allow root login (ornofor 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 thePort 22line, and change the port number.
Service Analysis
Check which type of SSH service is running:
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:
bashsudo systemctl disable --now ssh.socket sudo systemctl enable --now ssh.service
Network Binding Configuration
Check on which interfaces and IP addresses SSH is listening:
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:
sudo nano /etc/ssh/sshd_config
Change or add:
ListenAddress 0.0.0.0
Restart SSH:
sudo systemctl restart ssh
iptables and UFW Check
Even if UFW allows port 22, check iptables rules:
sudo iptables -L -n -v
sudo iptables -L INPUT -n -v | grep 22
Add an iptables rule if necessary:
sudo iptables -A INPUT -p tcp --dport 22 -m conntrack --ctstate NEW,ESTABLISHED -j ACCEPT
Check UFW status:
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:
sudo systemctl disable --now ssh.socket
sudo systemctl enable --now ssh.service
sudo systemctl restart ssh
Check the status:
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
- Check SSH logs:
sudo tail -f /var/log/auth.log
sudo tail -f /var/log/syslog | grep ssh
- Check port availability:
nc -zv your_server_ip 22 telnet your_server_ip 22
- Check network settings:
ip addr show netstat -tlnp | grep 22
- Check SELinux/AppArmor (if installed):
sudo aa-status
sudo sestatus
- Check for duplicate IP addresses:
arp -a
A Medium article notes that when duplicate IP addresses are found, you need to change the SSH server’s IP address.
Conclusion
- Main issue - In Ubuntu 24.04, a conflict between
ssh.socketandssh.serviceoften occurs, even if the service appears active. - Solution - Disable the socket and enable the standard service using the commands
systemctl disable --now ssh.socketandsystemctl enable --now ssh.service. - Additional measures - Check interface binding, iptables and UFW configuration, and review SSH logs for diagnostics.
- Problem prevention - Always check service status with
netstatorss, not just withsystemctl 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
- 24.04 - SSH connection refused - Ask Ubuntu
- Enable SSH on Ubuntu 24.04 – Secure Remote Access - LinuxConfig
- How to Fix Connection Refused by Port 22 Debian/Ubuntu – Linux Hint
- Solving Error port 22: Connection refused in Linux | Baeldung on Linux
- How to fix: Connection refused by port 22 Debian/Ubuntu | Medium
- (Resolved) SSH Connection Refused on Ubuntu & Debian – TecAdmin
- networking - ssh connection refused - Ask Ubuntu