📅  最后修改于: 2023-12-03 14:44:05.739000             🧑  作者: Mango
As a programmer, it is essential to have a good understanding of the ports being used on your Mac system. Ports are used for communication between different software applications, services, and networks. In this guide, we will explore how you can list the used ports on your Mac and gather valuable information about their associated processes.
To list the used ports on your Mac, you can utilize various terminal commands. Markdown provides a convenient way to present these commands in a code block. Here are a few examples:
$ sudo lsof -i -P | grep LISTEN
The command lsof
stands for "list open files" and the options -i -P
indicate that we want to list network files and display port numbers instead of service names. The grep
command filters the output to only display the lines containing the word "LISTEN", representing active listening ports.
$ sudo lsof -i :<port_number>
Replace <port_number>
with the port number you want to investigate. This command lists all processes using the specified port, providing useful information such as the associated process ID (PID), process name, user, and more.
$ sudo lsof -i -n -P | grep LISTEN
Adding the -n
option prevents hostname resolution, and the -P
option avoids port name resolution. This combination ensures faster execution and prevents potential delays caused by DNS lookups.
After obtaining the list of used ports, you can analyze the results to gain insights into your system's network activity. Markdown allows presenting tabular data, making it easy to convey information concisely. Let's assume we obtained the following output:
| COMMAND | PID | USER | FD | TYPE | DEVICE | SIZE/OFF | NODE | NAME | |:-------:|:----:|:----:|:------:|:-----------:|:------:|:--------:|:----:|:----:| | Safari | 1234 | user | IPv6 | TCP *:8080 | * | * | * | * | | Chrome | 5678 | user | IPv4 | TCP *:3000 | * | * | * | * | | Zoom | 9876 | user | IPv4 | UDP *:5000 | * | * | * | * |
This table showcases important details, such as the command/process name, associated PID, user, IP version, protocol type, and the port number being used.
By using appropriate terminal commands, you can easily list the used ports on your Mac. This information can be invaluable when troubleshooting network-related issues or ensuring optimal utilization of resources. Markdown allows presenting this guide in an organized and visually appealing manner, making it easier for programmers to follow along and utilize the provided examples.