乐闻世界logo
搜索文章和话题

How to check which ports are listening on a linux server?

1个答案

1

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:

bash
netstat -tulnp
  • -t displays TCP connections.
  • -u displays UDP connections.
  • -l shows only sockets in the listening state.
  • -n displays addresses and port numbers in numerical form.
  • -p shows 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:

bash
ss -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:

bash
lsof -i -n | grep LISTEN
  • -i makes lsof display information related to network connections.
  • -n prevents lsof from 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:

bash
ss -tuln | grep 3306

If you see output similar to the following, it means the MySQL service is listening on port 3306:

shell
tcp 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.

2024年8月14日 13:08 回复

你的答案