Instance: how to view the service and listening port in Linux

Source: Internet
Author: User
Article Title: instance: how to view services and listening ports in Linux. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.

Q: How can I find out which service is listening on a specific port? How can I find out which program is listening on a specific port?

A: In * NIX, you can use any of the following commands to obtain a list of listeners on a specific TCP port.

Lsof: Lists opened files, including listener ports.

Netstat: This command visually displays network-related data and information.

  Lsof command example

You can run the following command to view the IPv4 Port:

# Lsof-Pnl + M-i4

You can enter the following command to view the port list under the IPv6 protocol:

# Lsof-Pnl + M-i6

  Output in this example:



Here we may explain. The first column is command, which provides information about the program name. Pay attention to the title details. For example, the gweather * command in the second line obtains the weather report information from the NWS server (140.90.128.70), including the interactive Weather Information Network and other weather services. Here, we will explain the parameters of the command.

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 list the ports under the IPv4 protocol.

6.-i6: only list the ports under the IPv6 protocol.

Netstaty command example

Enter the following command:

# Netstat-tulpn

Or

# Netstat-npl

See the output result ::

  

Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name

Tcp 0 0 0.0.0.0: 6881 0.0.0.0: * LISTEN 6908/python

Tcp 0 0 127.0.0.1: 631 0.0.0.0: * LISTEN 5562/cupsd

Tcp 0 0 127.0.0.1: 3128 0.0.0.0: * LISTEN 6278/(squid)

Tcp 0 0 127.0.0.1: 25 0.0.0.0: * LISTEN 5854/exim4

Udp 0 0 0.0.0.0: 32769 0.0.0.0: * 6278/(squid)

Udp 0 0 0.0.0.0: 3130 0.0.0.0: * 6278/(squid)

Udp 0 0 0.0.0.0: 68 0.0.0.0: * 4583/dhclient3

Udp 0 0 0.0.0.0: 6881 0.0.0.0: * 6908/python

Note that the last column provides information about the program name and port. Here, we will explain the meaning of each parameter:

-T: indicates that the TCP port is displayed.

-U: indicates that the UDP port is displayed.

-L: display only the listening socket (the so-called socket is the program that enables the application to read and write and send and receive communication protocols (Protocols) and data)

-P: displays the process identifier and program name. Each socket/port belongs to a program.

-N: DNS polling is not performed (acceleration is supported)

About the/etc/services file

/Etc/services is a pure ASCII file that provides a friendly text name for Internet services, as well as its default allocated port number and protocol type. Every network program needs to enter this file to obtain the port number (and Protocol) of its service ). You can use the cat or less command to view the file:

  

$ Cat/etc/services

$ Grep 110/etc/services

$ Less/etc/services


Related Article

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.