Linux netstat command to check whether the port is occupied

Source: Internet
Author: User

the netstat command is a useful tool for monitoring TCP/IP networks. It displays the route table, the actual network connection, and the status information of each network interface device, here I only use some of these features.

Common netstat parameters:-T,-u,-W, and-x indicate TCP, UDP, raw, and UNIX socket connections. -A flag will also display the socket waiting for connection (that is, in listening mode. -L display the listening (Listen) port.-N indicates that the port number is directly displayed instead of the port name by viewing/etc/service.-P indicates that the listening port is listed.Program

1) netstat-Tl
View the current TCP listening port
Active Internet connections (only servers)
PROTO Recv-Q send-Q local address foreign address State 
TCP 0 0 *: rrac *: * listen  
TCP 0 0 *: 34006 *: * listen  
......

2) netstat-TLP
To view the current TCP listening port, You need to display the listening program name. It is easy to use when it is unclear about the MySQL listening port.
Active Internet connections (only servers)
PROTO Recv-Q send-Q local address foreign address State PID/program name  
TCP 0 0 *: rrac *: * listen-  
TCP 0 0 *: 34006 *: * Listen 23425/mysqld  
......

3) netstat-Tl | grep 34006
Only view the MySQL listening port. The currently started MySQL port is 34006.

4) netstat-ta | grep 34006
TCP 0 0 *: 34006 *: * listen  
TCP 0 0 Linux. Local: 34006 Linux. Local: 41485 established  
TCP 0 0 Linux. Local: 34006 Linux. Local: 41486 established 
...
TCP 0 0 10.3.2.35: 41488 10.3.2.35: 34006 established  
TCP 0 0 10.3.2.35: 41489 10.3.2.35: 34006 established  
TCP 0 0 10.3.2.35: 41490 10.3.2.35: 34006 established 

Because the database and application programs are deployed on the same machine, the connection is displayed twice. You can use the-p parameter to display the PID and then grep the PID.

5) netstat-tap | grep 34006 | grep 23425
23425 is the current MySQL PID
TCP 0 0 *: 34006 *: * Listen 23425/mysqld  
TCP 0 0 Linux. Local: 34006 Linux. Local: 41510 established 23425/mysqld  
TCP 0 0 Linux. Local: 34006 Linux. Local: 41511 established 23425/mysqld  
TCP 0 0 Linux. Local: 34006 Linux. Local: 41516 established 23425/mysqld

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.