Software & AppsOperating SystemLinux

How To Fix “Connection Timed Out” Error When SSH-ing into Ubuntu Machine

Ubuntu 1

In this guide, we will walk you through the steps to troubleshoot and fix the “Connection Timed Out” error that you may encounter when trying to SSH into an Ubuntu machine. This error typically occurs when the SSH client is unable to establish a connection to the server within the designated time frame.

Understanding the Issue

The “Connection Timed Out” error generally means that your SSH client is not able to connect to the server. This could be due to a variety of reasons such as network connectivity issues, incorrect IP addresses, firewall settings, or the SSH service not running on the server.

Step 1: Check if SSH Service is Running

The first step is to verify if the SSH service is running on the server. You can do this by running the following command:

sudo service ssh status

The sudo command is used to execute the command as the root user. service is a utility in Ubuntu that starts, stops, and manages services. ssh is the name of the service we want to check and status is the command to get the current status of the service.

If the service is not running, you can start it using:

sudo service ssh start

Step 2: Verify Name Resolution

Next, you need to verify the name resolution on the server. Run the following command to check the hostname:

cat /etc/hostname

This command reads the file /etc/hostname which contains the hostname of the server. Make sure the hostname matches the name specified in the /etc/hosts file. If it doesn’t, you may need to update the hostname.

Step 3: Check Firewall Settings

Firewall settings could also be the culprit. Run the following command to check the status of the firewall:

sudo ufw status

If the firewall is active, ensure that port 22 is allowed. You can do this by running:

sudo ufw allow 22

Step 4: Verify Port 22 is Listening

Verify that port 22 is listening on the server by running:

sudo ss -lnp | grep sshd

The ss command is used to dump socket statistics. The -l option tells it to display listening sockets, -n shows numerical addresses instead of trying to determine symbolic host, network or service names, and -p displays the process using the socket.

Step 5: Check Network Connectivity

Check the network connectivity between the client and the server. Run ping server_IP from the client and ping client_IP from the server to ensure they can reach each other.

Step 6: Perform a Telnet Test

Perform a telnet test from the client to the server by running:

telnet server_IP 22

If it times out or fails to connect, it could indicate a problem with the SSH service, name resolution, or firewall settings on the server.

Step 7: Double-Check IP Addresses

Finally, double-check the IP addresses of both the client and the server. Run ifconfig or ip addr on both machines to ensure they have the correct IP addresses assigned.

Conclusion

The “Connection Timed Out” error when SSH-ing into an Ubuntu machine can be caused by a variety of issues. By following the steps outlined in this guide, you should be able to troubleshoot and resolve the problem. If none of the steps resolve the issue, it may be helpful to provide more information such as the IP addresses of both machines, the exact error message received, and any relevant log files (e.g., /var/log/auth.log) for further troubleshooting.

How do I know if the SSH service is running on my Ubuntu machine?

To check if the SSH service is running, you can use the command sudo service ssh status. If the service is running, it will display the status as "active (running)".

How can I start the SSH service if it is not running?

If the SSH service is not running, you can start it using the command sudo service ssh start. This will initiate the SSH service on your Ubuntu machine.

How do I check the hostname of my Ubuntu machine?

To check the hostname of your Ubuntu machine, you can use the command cat /etc/hostname. This will display the hostname configured on your machine.

What should I do if the hostname does not match the name specified in the /etc/hosts file?

If the hostname does not match the name specified in the /etc/hosts file, you may need to update the hostname. You can do this by editing the /etc/hostname file and replacing the existing hostname with the correct one.

How can I check the status of the firewall on my Ubuntu machine?

To check the status of the firewall on your Ubuntu machine, you can use the command sudo ufw status. This will display the current status of the firewall, including whether it is active or inactive.

How do I allow port 22 through the firewall?

To allow port 22 (used for SSH) through the firewall, you can use the command sudo ufw allow 22. This will add a rule to the firewall configuration to allow incoming connections on port 22.

How can I verify if port 22 is listening on the server?

You can verify if port 22 is listening on the server by using the command sudo ss -lnp | grep sshd. This will display the processes using the SSH port (port 22) on the server.

How can I check the network connectivity between the client and server?

To check the network connectivity between the client and server, you can use the ping command. From the client, run ping server_IP and from the server, run ping client_IP. If both pings are successful, it indicates that the client and server can reach each other.

What should I do if the telnet test fails to connect or times out?

If the telnet test fails to connect or times out, it could indicate a problem with the SSH service, name resolution, or firewall settings on the server. You can revisit the previous steps to ensure that the SSH service is running, the hostname is correct, and the firewall allows connections on port 22.

How can I check the IP addresses of the client and server?

To check the IP addresses of the client and server, you can use the commands ifconfig or ip addr. Running these commands on both machines will display the assigned IP addresses. Make sure that the IP addresses are correct and match the intended configuration.

Leave a Comment

Your email address will not be published. Required fields are marked *