How to solve the problem of port occupation in windows system and linux system

  • 2020-05-14 05:35:51
  • OfStack

1. In the windows operating system, the program to query port occupation and clear port occupation

Use: netstat-b after the permission is promoted

Or use the

1. Query the process ID occupied by the port

Click "start "-- > Enter "cmd" and click "ok" to enter the DOS window. Then run the following commands respectively:

Each port of netstat-a-n is occupied
netstat-ano various port occupiers and process PID
netstat -aon | findstr "80"
C:\Users\admin > netstat -aon|findstr "80"
TCP 127.0.0.1:9080 0.0.0.0:0 LISTENING 4632

2. View the application corresponding to the port number

tasklist | findstr " 4632"
Additional: tasklist /fi "pid eq 4632"
GROOVE.EXE 4632 Console 1 84,880 K
Obviously, GROOVE.EXE takes up port 80, GROOVE.EXE 1 IE ACTIVEX control.

3. Terminate the process

taskkill /pid 4632 /F

Or taskkill /f /t /im GROOVE.exe

Query again for successful termination

tasklist | findstr "4632"

If this command is successfully terminated, it should return null., indicating success, the process with PID 4632 is terminated.

2. In the Linux operating system, the program to query port occupation and clear port occupation

1, query the port number occupation, according to the port to view the process information

[root@server2 ~]# lsof -i:80
COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME
httpd 5014 root 3u IPv4 14346 TCP server2:http (LISTEN)

2, according to the process number to see the corresponding executable program

ps-f-p process number
# ps -f -p 5014
UID PID PPID C STIME TTY TIME CMD
root 5014 1 0 17:26 ? 00:00:00 /usr/local/apache/bin/httpd -k

3. View dynamic links according to the executable

ldd executable file name
ldd /usr/local/apache/bin/httpd
linux-vdso.so.1 = > (0x00007fff9dc90000)
libaprutil-0.so.0 = > /usr/local/apache/lib/libaprutil-0.so.0 (0x00002af026fcd000)

4. Get the details of the process it occupies according to the port number

netstat -tlnp|grep 80
tcp 0 0 192.168.33.10:80 0.0.0.0:* LISTEN 5014/httpd
tcp 0 0 0.0.0.0:48054 0.0.0.0:* LISTEN 5386/java

This step 1 is equal to the 12 steps above

5. Query the occupied process ID according to the port number

netstat -tlnp|grep 80|awk '{print 7}'|awk -F '/' '{print 1}'
5014

Or use the following command

netstat -pan|grep 80
NETSTAT [-a] [-b] [-e] [-n] [-o] [-p proto] [-r] [-s] [-v] [interval]
-a displays all connections and listening ports.
- b show
Show the executable component included in creating each connection or listening port. In some cases, executable components are known
Has multiple independent components, and in these cases the sequence of components included in creating a connection or listening port is displayed. In this case, the executable component name is at the bottom of the []
At the top is the component it calls, and so on, up to the TCP/IP section. Note that this option may take a long time and may fail if there are not enough permissions.
-e displays Ethernet statistics. This option can be used in combination with the -s option.
-n displays the address and port number in numeric form.
-o displays the ID process associated with each connection.
-p
proto displays the connection to the protocol specified by proto; proto may be one of the following agreements: TCP, UDP, TCPv6 or
UDPv6. If used with -s option 1 to display by protocol statistics, proto can be one of the following protocols:
IP, IPv6, ICMP, ICMPv6, TCP, TCPv6, UDP or UDPv6.
-r displays the routing table.
-s displays by protocol statistics. By default, display statistics for IP, IPv6, ICMP, ICMPv6, TCP, TCPv6, UDP, and UDPv6;
The -p option is used to specify a subset of the default.
-v with the -b option 1 will display the components included in creating connections or listening ports for all executable components.
interval redisplays the selected statistics, pausing in seconds between displays. Press CTRL+C to stop redisplaying statistics. If omitted, netstat displays the current configuration information (only once)

6, a one-time cleaning up of the 80 port procedures

lsof -i :80|grep -v "PID"|awk '{print "kill -9",$2}'|sh

7. Manually terminate the process

kill 5014

If the termination is not possible, it can be enforced

kill -9 5014
lsof -i:80

Summary: solution to the problem of port occupation under Linux

1. Confirm whether the port is occupied: netstat-tln 9704(occupied port number)
2. Check which process the port is occupied by: lsof-i 9704(occupied port number)
3. Make sure there are no problems, kill! kill-9 pid(process occupying this port id)

The introduction of this paper is also relatively detailed, text description is more, you can first understand, in case of a problem at a loss, hope to share this article to help you, thank you. !


Related articles: