Query the process based on the port number in linux

Source: Internet
Author: User


In linux, we often use netstat-lnp to query the corresponding port and process based on the port number. However, some ports do not display the corresponding process pid in netstat-lnp, if we use a port that is already in use, we need to find the process that occupies the port to close it. At this time, we can use lsof to query the corresponding process pid Number of the port, www.2cto.com [html] [wei @ vm-cbu-qa-168-13 web-deploy] $ netstat-lnp (Not all processes cocould be identified, non-owned process info will not be shown, you wowould have to be root to see it all .) active Internet connections (only servers) Proto Recv-Q Send-Q L Ocal Address Foreign Address State PID/Program name tcp 0 0.0.0.0: 7008 0.0.0.0: * LISTEN 27709/java tcp 0 0 0.0.0.0: 6208 0.0.0.0: * LISTEN-tcp 0 0 0.0.0.0: 800 0.0.0.0: * LISTEN-tcp 0 0.0.0.0: 7009 0.0.0.0: * LISTEN 27709/java tcp 0 0 127.0.0.1: 15777 0.0.0.0: * LISTEN-www.2cto.com tcp 0 0 0.0.0.0: 2049 0.0.0.0: * LISTEN-tcp 0 0 127.0.0.1: 15778 0.0.0.0: * LISTEN-tcp 0 0 0.0.0.0: 35559 0.0.0.0 :* LISTEN 27709/java tcp 0 0.0.0.0: 55336 0.0.0.0: * LISTEN 27709/java tcp 0 0 0.0.0.0: 4200 0.0.0.0: * LISTEN-tcp 0 0 0.0.0.0: 58666 0.0.0.0: * LISTEN-tcp 0 0.0.0.0: 845 0.0.0.0: * LISTEN-tcp 0 0 0.0.0.0: 4110 0.0.0.0: * LISTEN-tcp 0 0 0.0.0.0: 4207 0.0.0.0: * LISTEN-tcp 0 0.0.0.0: 111 0.0.0.0: * LISTEN-tcp 0 0 0.0.0.0: 20880 0.0.0.0: * LISTEN-tcp 0 0 0.0.0.0: 4208 0.0.0.0: * LISTEN-tcp 0 0 0.0. 0.0: 18000 0.0.0.0: * LISTEN-tcp 0 0.0.0.0: 18001 0.0.0.0: * LISTEN-tcp 0 0 0.0.0.0: 18002 0.0.0.0: * LISTEN-tcp 0 0 0.0.0.0: 42483 0.0.0.0: * LISTEN-tcp 0 0.0.0.0: 48116 0.0.0.0: * LISTEN-tcp 0 0 10.20.168.13: 8022 0.0.0.0: * LISTEN-www.2cto.com. For example, we want to query which process occupies port 20880, netstat cannot be queried [html] [wei @ vm-cbu-qa-168-13 web-deploy] $ netstat-lnp | grep 20880 (Not all processes cocould be identified, non-own Ed process info will not be shown, you wowould have to be root to see it all .) tcp 0 0 0.0.0.0: 20880 0.0.0.0: * LISTEN-we use lsof to query the [html] [wei @ vm-cbu-qa-168-13 web-deploy] $ lsof-Pnl + M-i4 | grep 20880 java 15239 710 260u IPv4 87019792 TCP 10.20.168.13: 35822-> 10.20.172.31: 20880 (ESTABLISHED) java 15239 710 285u IPv4 87038933 TCP 10.20.168.13: 43058-> 10.20.172.93: 20880 (ESTABLISHED) java 25755 712 54 4u IPv4 87265535 TCP *: 20880 (LISTEN) java 25755 712 562u IPv4 87265755 TCP 10.20.168.13: 40151-> 10.20.168.16: 20880 (ESTABLISHED) java 25755 712 563u IPv4 87265695 TCP 10.20.168.13: 59947-> 10.20.151.21: 20880 (ESTABLISHED) java 25755 712 564u IPv4 87265696 TCP 10.20.168.13: 47042-> 10.20.142.80: 20880 (ESTABLISHED) java 25755 712 565u IPv4 87265703 TCP 10.20.168.13: 40939-> 10.20.141.52: 20880 (ESTABLISHED) Java 25755 712 570u IPv4 87265751 TCP 10.20.168.13: 43361-> 10.20.141.141: 20880 (ESTABLISHED) java 25755 712 571u IPv4 87265753 TCP 10.20.168.13: 40960-> 10.20.143.111: 20880 (ESTABLISHED) java 25755 712 572u IPv4 87265752 TCP 10.20.168.13: 45368-> 10.20.141.63: 20880 (ESTABLISHED) www.2cto.com java 25755 712 573u IPv4 87265754 TCP 10.20.168.13: 34315-> 10.20.168.17: 20880 (ESTABLISHED) java 27709 713 339u IPv4 87271862 TCP 10.20.168.13: 50212-> 10.20.172.11: 20880 (ESTABLISHED) java 27709 713 341u IPv4 87271859 TCP 10.20.168.13: 41128-> 10.20.172.88: 20880 (ESTABLISHED) java 27709 713 347u IPv4 87271865 TCP 10.20.168.13: 46782-> 10.20.172.1: 20880 (ESTABLISHED) you can see that the process pid listening to port 20880 is 25755, user is 712i4 is to query ipv4 port [html] [wei @ vm-cbu-qa-168-13 web-deploy] $ lsof-Pnl + M-i4 COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME portmap 1616 32 3u IPv4 3488 UDP *: 111 [portmapper] portmap 1616 32 4u IPv4 3489 TCP *: 111 [portmapper] (LISTEN) DragoonAg 4153 0 6u IPv4 28872832 TCP 10.20.168.13: 54978-> 10.20.149.114: 13888 (ESTABLISHED) DragoonAg 4153 0 8u IPv4 28872826 TCP 127.0.0.1: 15777 (LISTEN) DragoonAg 4153 0 10u IPv4 28872828 TCP 127.0.0.1: 15778 (LISTEN) dragoonAg 4153 0 12u IPv4 28872836 UDP 10.20.168.13: 13777-> 10.20. 149.114: 60569 DragoonAg 4153 0 13u IPv4 86898035 TCP 10.20.168.13: 38027-> 10.20.159.108: 15888 (ESTABLISHED) DragoonAg 4153 0 15u IPv4 86648332 TCP 127.0.0.1: 15777-> 127.0.0.1: 41653 (ESTABLISHED) python 10488 0 4u IPv4 292127 TCP 10.20.168.13: 8022 (LISTEN) java 15239 710 5u IPv4 86647757 TCP *: 4208 (LISTEN) java 15239 710 15u IPv4 86647829 TCP *: 35485 (LISTEN) java 15239 710 18u IPv4 86647831 TCP *: 4207 (LISTEN) java 15239 710 20u IPv4 86647833 TCP *: 42483 (LISTEN) java 15239 710 22u IPv4 87286934 TCP 10.20.168.13: 44190-> 110.75.194.17: 80 (CLOSE_WAIT) java 15239 710 246u IPv4 86648330 TCP 127.0.0.1: 41653-> 127.0.0.1: 15777 (ESTABLISHED) java 15239 710 253u IPv4 86648976 TCP *: 4110 (LISTEN) java 15239 710 255u IPv4 87010313 TCP 10.20.168.13: 4110-> 10.20.172.31: 38036 [html] [wei @ vm-cbu-qa-168-13 w Eb-deploy] $ lsof-Pnl + M-i6 command pid user fd type device size node name cfservd 5513 0 4u IPv6 17813 TCP *: 5308 (LISTEN) java 13334 0 64u IPv6 85851503 TCP 10.20.168.13: 46591-> 10.20.145.12: 57126 (ESTABLISHED) java 13334 0 65u IPv6 86276005 TCP 10.20.168.13: 34506-> 10.20.172.85: 80 (CLOSE_WAIT) java 13334 0 66u IPv6 86313523 TCP 10.20.168.13: 36320-> 10.20.172.85: 80 (CLOSE_WAIT) java 13334 0 67u IP V6 86382763 TCP 10.20.168.13: 52680-> 10.20.172.85: 80 (CLOSE_WAIT) java 13334 0 71u IPv6 85851508 TCP *: 58126 (LISTEN) www.2cto.com lsof command parameter explanation 1)-P: this option restricts the conversion from the port number of the network file to the port name. Constraint conversion can make lsof run faster. This is useful when port name search cannot take effect. 2)-n: This option restricts the conversion from the port number of the network file to the host name. Constraint conversion can make lsof run faster. It is useful when the host name cannot be found. 3)-l: This option restricts the conversion from user ID to login name. This option is useful when the login name is found incorrectly or slowly. 4) + M: This option supports registration reports of local TCP and UDP port ing programs. 5)-i4: only lists the ports under the IPv4 protocol. 6)-i6: only list the ports under the IPv6 protocol.
 

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.