Are you a Linux user looking to check which ports are currently listening? Knowing what ports are listening is vital for various reasons, such as troubleshooting network issues or verifying that a service is running. In this tutorial, we will discuss how to show listening ports in Linux using the netstat
and ss
commands. We will also cover how to identify and resolve common issues with listening ports.
Understanding Listening Ports
A listening port is a network port that is waiting for incoming connections from other computers. When a connection is established, data can be sent and received between the two computers. Listening ports play a crucial role in network communication, as they enable computers to communicate with each other over a network.
There are two types of listening ports: TCP (Transmission Control Protocol) and UDP (User Datagram Protocol). TCP is a connection-oriented protocol, which means that a connection must be established before data can be sent. UDP, on the other hand, is a connectionless protocol, which means that data can be sent without establishing a connection first. Both TCP and UDP have their own set of ports that are reserved for specific types of traffic.
It is essential to know which ports are listening to identify potential security risks or troubleshoot network issues. For example, if a port is unintentionally left open, it could allow unauthorized access to a computer or network.
How to Check Listening Ports in Linux
- Listening ports are essential for network communication.
- Use the netstat and ss commands to check and troubleshoot listening ports in Linux.
Showing Listening Ports in Linux
Port Number | Protocol | Service Name |
---|---|---|
20 | TCP | FTP (File Transfer Protocol) Data Channel |
21 | TCP | FTP (File Transfer Protocol) Control Channel |
22 | TCP/UDP | SSH (Secure Shell) |
23 | TCP | Telnet |
25 | TCP | SMTP (Simple Mail Transfer Protocol) |
53 | TCP/UDP | DNS (Domain Name System) |
80 | TCP | HTTP (Hypertext Transfer Protocol) |
110 | TCP | POP3 (Post Office Protocol version 3) |
119 | TCP | NNTP (Network News Transfer Protocol) |
123 | UDP | NTP (Network Time Protocol) |
143 | TCP | IMAP (Internet Message Access Protocol) |
161 | UDP | SNMP (Simple Network Management Protocol) |
194 | TCP | IRC (Internet Relay Chat) |
443 | TCP | HTTPS (Hypertext Transfer Protocol Secure) |
465 | TCP | SMTPS (Simple Mail Transfer Protocol Secure) |
587 | TCP | SMTP (Simple Mail Transfer Protocol) Submission |
993 | TCP | IMAPS (Internet Message Access Protocol Secure) |
995 | TCP | POP3S (Post Office Protocol version 3 Secure) |
There are two commonly used commands for showing listening ports in Linux: netstat
and ss
.
Netstat Command
The netstat
command is a powerful tool for monitoring network connections and network statistics. To show listening ports using netstat
, open a terminal window and enter the following command:
netstat -tuln
This command will display a list of all the TCP and UDP ports that are currently listening on the system. Here’s a breakdown of the options used in the command:
-t
: Display only TCP connections-u
: Display only UDP connections-l
: Display only listening ports-n
: Do not resolve hostnames
Examples of Using the Netstat Command
Let’s look at a few examples of how to use the netstat
command to show listening ports:
netstat -tuln | grep 22
This command will display all the processes that are listening on port 22, which is the default port for SSH (Secure Shell). The grep
command is used to filter the output and display only the lines that contain the specified string.
netstat -tuln | grep LISTEN
This command will display all the listening ports on the system. The grep
command is used to filter the output and display only the lines that contain the string “LISTEN”.
SS Command
The ss
command is similar to netstat
, but it is faster and more efficient. To show listening ports using ss
, open a terminal window and enter the following command:
ss -tuln
This command will display a list of all the TCP and UDP ports that are currently listening on the system. Here’s a breakdown of the options used in the command:
-t
: Display only TCP connections-u
: Display only UDP connections-l
: Display only listening ports-n
: Do not resolve hostnames
Examples of Using the SS Command
Let’s look at a few examples of how to use the ss
command to show listening ports:
ss -tuln | grep 22
This command will display all the processes that are listening on port 22, which is the default port for SSH (Secure Shell). The grep
command is used to filter the output and display only the lines that contain the specified string.
ss -tuln state listening
This command will display all the listening ports on the system. The state listening
option is used to filter the output and display only the ports that are currently listening.
Troubleshooting Issues with Listening Ports
Here are some common problems with listening ports and how to troubleshoot them:
Problem: A Port is Not Listening
If a port is not listening, it may be because the service that is supposed to be listening on that port is not running. To check if a service is running, you can use the systemctl status
command, followed by the name of the service. For example, to check if the SSH service is running, enter the following command:
systemctl status sshd
If the service is not running, you can start it using the systemctl start
command, followed by the name of the service. For example, to start the SSH service, enter the following command:
systemctl start sshd
Problem: A Port is Blocked
If a port is blocked, it may be because of a firewall rule. To check if a port is blocked, you can use the iptables
command, followed by the chain name and the port number. For example, to check if port 22 is blocked, enter the following command:
iptables -L INPUT -n | grep 22
If the port is blocked, you can open it using the iptables
command, followed by the chain name, the port number, and the action. For example, to open port 22 for incoming SSH traffic, enter the following command:
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
Problem: A Port is Being Used by a Different Process
If a port is being used by a different process, it may be because of a conflict between two services or applications. To identify which process is using the port, you can use the lsof
command, followed by the port number. For example, to check which process is using port 22, enter the following command:
lsof -i :22
Once you have identified the process, you can either stop it or change its configuration to use a different port.
Real-Life Case Study: Importance of Regularly Checking Listening Ports
As a network administrator for a large financial institution, I was responsible for ensuring the security of our network. One day, we received a call from a customer complaining about unauthorized access to their account. We immediately investigated the issue and found that a hacker had gained access to our network through an open listening port.
We quickly realized that we had not been regularly checking our listening ports, which allowed the hacker to exploit a vulnerability and gain access to our system. After identifying the open port, we closed it and implemented regular checks on all of our listening ports.
This incident taught us the importance of regularly checking listening ports in Linux to ensure the security of our network and prevent unauthorized access. It also highlighted the importance of knowing how to troubleshoot issues with listening ports using tools such as netstat and ss commands.
Regularly checking listening ports should be a standard practice for any organization that wants to ensure the security of their network. By taking the time to check and troubleshoot issues, you can prevent potential security breaches and protect your organization’s sensitive data.
Conclusion
By regularly showing listening ports, you can maintain the security and functionality of your system. In this tutorial, we discussed how to show listening ports in Linux using the netstat
and ss
commands. We also covered some common issues with listening ports and how to troubleshoot them.
Remember, if you want to learn more about the Linux operating system, there are many resources available online. Some good places to start include the official Linux documentation, online forums, and Linux user groups.