Use the netstat command to view the services and ports in use in the Linux system |
Common parameters
-a (all) displays all options, LISTEN related options are not displayed by default
-t (tcp) only displays tcp related options
-u (udp) only displays udp related options
-n refuses to display aliases, and all numbers that can be displayed are converted into numbers.
-l Only list the service status in Listen
-p Display the name of the program that establishes the related link
-r Display routing information, routing table
-e Display extended information, such as uid, etc.
-s Statistics according to each protocol
-c Every fixed time, execute the netstat command .
Tip: The status of LISTEN and LISTENING can only be seen with -a or -l
Examples of practical commands
1. List all ports (including listening and unlistened)
2. List all Sockets in the listening state
Only display the listening port netstat -l
Only list all listening tcp ports netstat -lt
Only list all listening udp ports netstat -lu
Only list all listening UNIX ports netstat -lx
3. Display statistics for each protocol
Display statistics information of all ports netstat -s
Display statistics information of TCP or UDP ports netstat -st or -su
4. Display PID and process name in netstat output netstat -p
5. Do not display host, port and user name (host, port or user)
When you don't want the host, port, and user name to be displayed, use netstat -n. Numbers will be used instead of those names.
6. Continuous output of netstat information
netstat will output network information every second. netstat -c
7. Display core routing information netstat -r
8. Find out which port the program is running on
Not all processes can be found. Those without permissions will not be displayed. Use root permissions to view all information.
netstat -ap | grep ssh
Commonly used:
netstat -nat lists all tcp ports