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.
Last updated: March 18, 2024
As Linux users, there will be times when we need to check the number of connections to our services on the server. Knowing such information is useful for planning system scaling and being able to determine whether our services are under DOS attack or not. We use the netstat command to check connection numbers on the server.
In this tutorial, we’ll learn about HTTP and how to use netstat to check the number of open HTTP connections.
HTTP, short for Hypertext Transfer Protocol, is an application layer for exchanging documents over the web. It runs on the TCP/IP suite of protocols, which is a set of rules that enable devices to communicate on a network. Moreover, HTTP was designed for communication between web browsers and servers, giving users a way to interact with web resources by sending and receiving hypertext messages.
Additionally, HTTP is a stateless protocol. This means that although multiple requests are over the same HTTP connection to the server, the current request is not aware of the activities of the previous requests. The server considers these multiple requests as separate requests, and each one must have a full response.
This design was intentional to minimize the time spent on re-establishing a connection for each request. By default, a web server listens on port 80 for HTTP connections. However, other system administrators change the default port to another for various reasons.
netstat is a combination of two words: network and statistics. It’s a command line tool that we use for monitoring network connections, both incoming and outgoing. It also displays routing tables, per-protocol statistics, interface statistics, and other information.
netstat is available on most Unix-like operating systems and Windows operating systems. It’s a useful tool for network troubleshooting and performance. In particular, having knowledge about incoming and outgoing connections helps fight against disproportionate traffic and malicious network connections.
We use the -a option to display all ports and connections regardless of their state or protocol:
$ netstat -a
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State
udp 0 0 192.168.238.129:bootpc 192.168.238.254:bootps ESTABLISHED
raw6 0 0 [::]:ipv6-icmp [::]:* 7
Active UNIX domain sockets (servers and established)
Proto RefCnt Flags Type State I-Node Path
unix 3 [ ] DGRAM CONNECTED 15330 /run/systemd/notify
unix 2 [ ACC ] STREAM LISTENING 15333 /run/systemd/private
Firstly, the Active Internet connections section lists the connected external connections and local sockets listening for remote connection requests. Let’s have a closer look at what each column represents:
Secondly, the Active UNIX domain sockets section shows the connected and listening internal connections. In other words, these are the connections established within the computer between different processes and applications. Let’s examine the meaning of each of these columns:
We can use the netstat command with grep to list all the open HTTP connections. The -n option shows the numerical addresses and port numbers instead of trying to determine the symbolic host, port, or username:
$ netstat -an | grep :80
tcp 0 0 127.0.0.1:51730 127.0.0.1:80 TIME_WAIT
tcp 0 0 127.0.0.1:51714 127.0.0.1:80 TIME_WAIT
tcp6 0 0 :::80 :::* LISTEN
The grep command searches for the given string pattern, :80, in the netstat output and displays the lines that match the pattern.
Alternatively, we can grep for the HTTP protocol in the case that the web server is running on a different port other than 80:
$ netstat -a | grep http
tcp 0 0 localhost:47996 localhost:http TIME_WAIT
tcp 0 0 localhost:47982 localhost:http TIME_WAIT
tcp 0 0 192.168.238.129:60532 93.184.220.29:http TIME_WAIT
tcp 0 0 192.168.238.129:36772 mba01s09-in-f10.1:https TIME_WAIT
tcp 0 0 192.168.238.129:60546 93.184.220.29:http TIME_WAIT
In this article, we learned about the HTTP protocol and the netstat tool. We also looked at checking the number of open HTTP connections using netstat.