Baeldung Pro – Linux – NPI EA (cat = Baeldung on Linux)
announcement - icon

Learn through the super-clean Baeldung Pro experience:

>> Membership and Baeldung Pro.

No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with.

Partner – Orkes – NPI EA (tag=Kubernetes)
announcement - icon

Modern software architecture is often broken. Slow delivery leads to missed opportunities, innovation is stalled due to architectural complexities, and engineering resources are exceedingly expensive.

Orkes is the leading workflow orchestration platform built to enable teams to transform the way they develop, connect, and deploy applications, microservices, AI agents, and more.

With Orkes Conductor managed through Orkes Cloud, developers can focus on building mission critical applications without worrying about infrastructure maintenance to meet goals and, simply put, taking new products live faster and reducing total cost of ownership.

Try a 14-Day Free Trial of Orkes Conductor today.

1. Overview

Apache Tomcat (Tomcat for short) is an application that allows us to run Java code on an HTTP web server.

In this tutorial, we’ll learn how to ensure the Tomcat TCP port responds correctly to local and remote user requests.

Firstly, we’ll look at how to check the Tomcat server IP address and the TCP port. Secondly, we’ll find out if there is a firewall rule to make the Tomcat connection available from other machines.

2. Check Tomcat IP Address

Sometimes, the Tomcat server doesn’t respond because we use the incorrect IP address.

To resolve that, we can utilize the netstat command. It allows us to get the Tomcat IP address and TCP port information:

$ netstat -ltnp | grep java
tcp        0      0   0.0.0.0:8080    0.0.0.0:*          LISTEN      2842/java    
tcp        0      0   ::1:8080        :::*               LISTEN      2842/java   

As can be seen, we have two active Tomcat TCP sockets.

The first one uses the IPv4 address 0.0.0.0 and the TCP port 8080, while the second one has the IPv6 address.

Let’s have a look at the netstat command options in more detail:

  • the option -l shows the listening sockets
  • -t filters for the TCP sockets
  • -n gives the numbering values for IP addresses and ports
  • -p displays PID name for sockets
  • the grep filter for Java allows us to get the Java-related Tomcat sockets

Now, we should have enough information to access the Tomcat web page.

3. Access From Another Machine

Sometimes, there is a problem accessing Tomcat from another Linux machine.

There are a couple of steps we need to follow, to ensure the Tomcat is available remotely.

3.1. Check Remote IP Address

First, let’s check the Tomcat IP address using the same command as above:

$ netstat -ltnp | grep java
tcp        0      0   10.0.3.1:8080    0.0.0.0:*          LISTEN      1626/java 

As we can see, the Tomcat remote IP address is 10.0.3.1.

If using this IP address doesn’t help to resolve the problem, then we need to check the firewall setup.

3.2. Configure the Firewall

To make Tomcat available from the other machines, our firewall should allow public connections for the TCP port 8080.

For that, we’ll use the ufw (Uncomplicated Firewall).

If the ufw isn’t enabled yet, we enable it:

$ sudo ufw enable
Firewall is active and enabled on system startup

As can be seen, the firewall is now enabled.

Then, we add a rule to make the TCP port 8080 public:

$ sudo ufw allow 8080/tcp
Rule added
Rule added (v6)

The rule has been added.

Let’s double-check that the TCP port 8080 is available with the ufw status command:

$ sudo ufw status
Status: active

To                         Action      From
--                         ------      ----             
8080/tcp                   ALLOW IN    Anywhere                           
8080/tcp (v6)              ALLOW IN    Anywhere (v6)   

As we can see, the port 8080 is in the ufw table. Thus, the Tomcat server should now be accessible from the other machines.

4. Conclusion

In this article, we’ve learned how to fix Tomcat connection problems.

Firstly, we looked at the netstat command to get the Tomcat IP address and the port number. Secondly, we checked how to configure the firewall when trying to access Tomcat from a remote machine.