Use netstat to check the port
netstat is a command line tool that provides information about network connections.
netstat - atulnp will display all ports and all corresponding programs, and you can use the grep pipeline to filter out the desired fields
-a: all, means to list all connections, service monitoring, Socket information
-t: tcp, list the services of tcp protocol
-u: udp, list the services of udp protocol
-n: port number, use the port number to display
-l: listening, lists the current listening services
-p: program, lists the PID of the service program
Proto: network transmission protocol, mainly tcp and udpLocal Address: local ip:portForeign Address: remote host's ip:portState: connection status, mainly listening (LISTEN) and establishing (ESTABLISED) PID: process number of the service Program name :service name
netstat command example example
To display all open ports, use the following command:
netstat -anp
To list all TCP or UDP ports that are listening on, including services using port and socket status
Please use the following command:
netstat -tunlp
The options used in this command have the following meanings:
-t – Display TCP ports. -u – Display UDP ports. -n – Display numeric addresses instead of hostnames. -l – Show only listening ports. -p – Displays the PID and name of the process. This information only appears when you run the command as root or sudo.
Query the PID number of the service through the above two methods
Query the corresponding service through the ps command
ps -ef |grep 87254
Query the specified port and filter it through grep:
netstat -tnlp | grep :80
Note: Proto – the protocol used by the socket. Local Address – The IP address and port number the process listens on. PID/Program name – PID and process name.
Use lsof to check the port
lsof is a powerful command line utility that provides information about files opened by a process.
In Linux, everything is a file, and sockets can be thought of as files written to the network.
To get a list of all listening TCP ports using lsof: lsof -nP -iTCP -sTCP:LISTEN
The options used are: -n – Do not convert port numbers to port names. -p – Do not resolve hostnames, display numeric addresses. -iTCP -sTCP:LISTEN – Display only network files with TCP protocol status LISTEN.
Use lsof command
lsof -i: port number is used to check the usage of a certain port, such as checking the usage of port 9092
lsof -i:9095
You can see that port 9095 has been occupied by nginx
lsof -i:22220
View network
View network IPip a #View current network