1 lsof view the occupation of the port
1.1 example of command use
#The command is lsof - I [[email protected] ~]# lsof -i #The command + process ID + user of the process, as well as the monitored protocol, status and other information will be displayed COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME dhclient 728 root 6u IPv4 11262 0t0 UDP *:bootpc ntpd 839 ntp 16u IPv4 13671 0t0 UDP *:ntp ntpd 839 ntp 18u IPv4 13677 0t0 UDP localhost:ntp
• note: if you are prompted that this command is not available, you need to install it as follows:
[[email protected] ~]# lsof -i -bash: lsof: command not found [[email protected] ~]# yum install -y lsof ...... Installed: lsof.x86_64 0:4.87-6.el7 Complete! #"Complete!" above appears, indicating successful installation.
1.2 check the occupancy of a port
#For example, to view the occupation of port 80, the command is:
[[email protected] ~]# lsof -i:22 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME sshd 3187 root 3u IPv4 16278 0t0 TCP *:ssh (LISTEN) sshd 9528 root 3u IPv4 4436480 0t0 TCP onepiece:ssh->120.253.xx.xx:30214 (ESTABLISHED)
It shows that port 22 is being used by sshd and the status is listen
1.3 kill all processes of a port
It is not recommended to solve the port conflict by killing!
In some rare cases, processes can be killed and ports can be released directly. For example, a Tomcat process fails to exit, resulting in a restart failure
#The command is as follows: [[email protected] ~]# killall sshd #In this way, all the sshd processes will be ended - I use SSH to remotely operate alicloud server. After killing, I will exit the connection, and the prompt is as follows: Connection to 47.52.xx.xx closed by remote host. Connection to 47.52.xx.xx closed. #At this time, you need to enter from the alicloud console through the terminal and start the sshd service. The command is as follows: service sshd start
In this way, all processes related to sshd will be terminated – be careful
2. Netstat to check the port usage
2.1 example of command use
#An example command is as follows: [[email protected] ~]# netstat -anp | grep 22 tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 9646/sshd tcp 0 36 172.31.xx.xx:22 120.253.xx.xx:30307 ESTABLISHED 9649/sshd: [email protected] unix 2 [ ] DGRAM 15722 476/dbus-daemon unix 3 [ ] STREAM CONNECTED 11122 476/dbus-daemon
You can see that port 22 is being monitored by process 9646
2.2 view the process occupying a port
#The command is fuser: [[email protected] ~]# fuser -v -n tcp 22 USER PID ACCESS COMMAND 22/tcp: root 9646 F.... sshd root 9649 F.... sshd
2.3 kill the occupation process of a port
[root @ onepiece ~] (kill - s 9 9646 (process number)
-9 parameter means to tell the operating system to kill the process directly, regardless of whether the process status can be killed or not;
This command only kills a process, which is safer than the kill command
How to remove port occupation in Linux
The above is the way Xiaobian introduced to you to check the occupation of Linux port and find out and kill the occupation process. I hope it can help you. If you have any questions, please leave me a message and Xiaobian will reply to you in time. Thank you very much for your support of the developepaer website!
If you think this article is helpful to you, welcome to reprint, please indicate the source, thank you!