How to check port availability
14:39, 06.08.2021
Ports are an essential part of communication of different devices, applications, and services between each other over a network. If you have to deal with administering a network, on many occasions it will be valuable to know how to check port availability, which is an essential practice for monitoring your network and troubleshooting in case of network issues. Let’s see how it’s done.
Network Ports Explained
Ports make up a complex communication system within a network, that allows the participating devices to exchange the necessary commands throughout the network and makes the functioning of the whole system possible. In modern network systems, ports are identified by numbers, so-called port numbers, ranging from – to 65535. Different ports allow routing the data to the right device, application, or service, easily distinguishing between them.
Network ports types
Network ports are distinguished in several types. Most common of them include:
- Well-known ports (0-1023) are ports that are ports implemented in the most used services and protocols, including HTTP (port 80), HTTPS (port 443), FTP (port 21), and SSH (port 22). Internet Assigned Numbers Authority (IANA) is the authority that typically assigns them to specific services.
- Registered ports (1024-49151) are ports that are registered by IANA and assigned for more specific services and applications. These are recognized less universally than well-known ports.
- Dynamic or private ports (49152-65535) are ports that are not registered and assigned by IANA, but instead are assigned dynamically by the OS. These are mostly used for ephemeral or temporary communication sessions in client applications.
How network ports work
When devices communicate over a network, they use a combination of an IP address and a port number, with which they can establish a connection with another device. This combination is commonly referred to as a socket.
For instance, for an HTTP request, a web server connects to the server’s IP address on port 80. After this, the server responds to the client’s request through the very same port, so the data is directed to the right destination application.
Why are network ports important?
Network ports play an important role in the overall functioning of networks. There are some key tasks they are in the first place responsible for:
- Identifying services: With the help of network ports, operating systems can direct incoming data to the correct applications. For instance, the data that arrives on port 80 is then for the most part processed by web server software.
- Network security: The possibility to either close or open ports gives additional control over the security of your network. Since cyber attackers often exploit open ports to perform their attacks, by closing unnecessary ports you can reduce their potential ways of attacking you.
- Troubleshooting: If there are any network issues, checking port status is an essential practice to make sure that all the services are working as they are supposed to work.
The netstat Command for Network Statistics
‘netstat’ command is a multi-purpose that allows users to monitor network connections and collect statistics. By using it, you can access a variety of information about the network, among which you can also find open ports and active connections.
Displaying Currently TCP and UDP Ports
TCP (Transmission Control Protocol) and UDP (User Datagram Protocol) are two of the core protocols of the Internet Protocol Suite. Their main function is to serve as transport protocols. They are responsible for managing how data is sent and received over the network.
To display currently active TCP and UDP ports, you can use the following command:
netstat -tuln
After entering this command, you’ll see all listening (open) TCP and UDP ports together with their statuses.
Listing All Active Connections
You can also list all active connections at once. You this command:
netstat -an
After entering the command, you’ll be able to see all the active connections, along with their protocol (TCP/UDP), local and foreign addresses, and their state.
Open Ports and Active TCP Connections
There is also a command for open ports and active TCP connections. Type:
netstat -tn
With the filter applied, you’ll only see TCP connections.
Open UDP Ports
Conversely, you can check open UDP ports only. To do this, enter:
netstat -un
In the output you’ll see exclusively open UDP connections.
The lsof Command (List Open Files)
lsof command allows you to see the list of open files and can be pretty helpful if you want to check which ports are used by which processes.
lsof -i
Enter this command and you’ll see all open Internet connections and the corresponding processes.
Active Network Connections
You can also check the combined input of ‘netstat’ and ‘lsof’ commands to get more comprehensive insights.
netstat -tulnp
With this command, you can overview all the listening ports with the associated PID and program name, thus getting a clear view of active network connections and the responsible processes.
Processes Using Specific Ports
You can also look from a different perspective and check which processes are using a specific port. Just enter:
lsof -i :<port_number>
Replace <port_number>with the port number that you’re currently interested in.
Active UDP and TCP Connections
You can also check all active UDP and TCP connections by entering:
netstat -tun
Conclusion
Checking port availability is one of the essential know-hows that you are likely to need for administering the network, particularly when it comes to troubleshooting network issues. With tools like ‘netstat’ and ‘lsof’ you can get detailed pictures of which ports are active and used by which processes, which will eventually let you figure out what’s the matter with your network and to make the necessary steps. This guide does not cover all operations regarding checking port availability, but it will surely give you some good approaches to start with. Good luck!