To check which ports are listening on a Linux server, you can use various tools and commands to accomplish this. Here are several common methods:
1. netstat Command
netstat is a powerful networking tool that can be used to view network connections, routing tables, interface statistics, and more. To view the ports that are listening, you can use the following command:
bashnetstat -tulnp
-tdisplays TCP connections.-udisplays UDP connections.-lshows only sockets in the listening state.-ndisplays addresses and port numbers in numerical form.-pshows the process ID and name of the application listening on the port.
This command lists all TCP and UDP ports in the listening state, along with the program or service listening on those ports.
2. ss Command
ss is another useful tool for viewing socket statistics. It is considered a modern alternative to netstat with better performance. To view listening ports, you can use:
bashss -tuln
The parameters are similar to the netstat command, and the output includes the listening ports and corresponding service details.
3. lsof Command
The lsof command stands for "list open files." In Linux, almost everything is a file, including network connections. lsof can be used to view which processes have opened those files (including ports). To view listening ports, you can use:
bashlsof -i -n | grep LISTEN
-imakeslsofdisplay information related to network connections.-npreventslsoffrom converting IP addresses to hostnames, speeding up processing.
This command lists all ports in the listening state along with their associated process information.
Example Usage
Suppose you are a server administrator who needs to check if the MySQL database service is listening on the default port 3306. You can use the following command:
bashss -tuln | grep 3306
If you see output similar to the following, it means the MySQL service is listening on port 3306:
shelltcp 0 0 127.0.0.1:3306 0.0.0.0:* LISTEN 2398/mysqld
These are several methods to check listening ports on a Linux server. Depending on your specific needs and environment, choose the tool that best suits your requirements.