Netstat command how to see if the port is occupied

Source: Internet
Author: User

Turn from: http://www.ahlinux.com/start/cmd/527.html

The netstat command is a very useful tool for monitoring TCP IP networks, displaying routing tables, actual network connections, and status information for each network interface device, where I only use its
The netstat command is a very useful tool for monitoring the TCP/IP network, which shows the routing table, the actual network connection, and the status information for each network interface device, where I use only some of the features.
Common parameters for Netstat:-T,-u,-W and-X represent TCP, UDP, raw, and UNIX socket connections, respectively. -A tag and also displays a socket waiting for a connection (that is, in listening mode).
-L Displays the port being monitored (listen), and-n indicates that the port number is displayed directly instead of by viewing the/etc/service to the port name, and the-P option indicates the program that lists the listener.


For detailed usage of the netstat command, refer to:


Windows port is occupied Netstat command view population use Linux netstat command instance linux command Instance tutorial netstat command Linux netstat command usage details netstat and awk command Statistics network connection number method Netstat view the port on which the computer is open use Netstat to locate the process that occupies the port
1) Code example:
Netstat-tl
View current TCP listening ports
Active Internet connections (only servers)
Proto recv-q send-q Local address Foreign
TCP 0 0 *:rrac *:* LISTEN
TCP 0 0 *:34006 *:* LISTEN
......
2) Code example:
Netstat-tlp
To view the current TCP listening port, you need to display the listener's name, which is easier to use when you are not aware of MySQL's 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) Code example:
Netstat-tl | grep 34006
Only view the MySQL listening port, the current boot of the MySQL port is 34006, clearly know the MySQL listening port to use.


4) Code example:
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
Since the database and the application are all placed on the same machine, the connection here is shown two times.
You can use the-p parameter to display the PID and then grep the PID.


5) Code Example:
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
Open the terminal, and execute the following command to see how the processes are consuming ports:

code example:
# Ps-ef|wc-l//View the total number of processes running in the background
# Ps-fu CSVN//view CSVN process

# NETSTAT-LNTP//See which ports are open
# Netstat-r//This option to display information about the routing table
# netstat-a//This option displays a list of all valid connection information
# netstat-an|grep 8080
# Netstat-na|grep-i Listen//Can see the port number on which the current system listens
# Netstat-antup//View the established connection process, the port occupied.
Netstat-anp|grep 1487
lsof-i:1487


See which processes are opening the specified port 1487

Closing the port is actually shutting down its corresponding service, such as 80 port is httpd shutdown 80 port can be achieved by shutting down the httpd service
Each port has a daemon, kill the daemon.
Windows usage:
Goal: In a Windows environment, use the netstat command to see if a port number is occupied and for which process.
Operation: The operation is divided into two steps: (1) to see that the port is occupied by that PID; method one: Targeted viewing port, using command
Netstat–ano|findstr "< port number >", as shown in the last column of PID. The port number in the figure is 1068, and the corresponding PID is 3840.

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.