Linux port related commands

Premise: First of all, you must know that the port does not exist independently, it is attached to the process. When a process is opened, its corresponding port is opened, and when the process is closed, the port is also closed. Next time if a process is opened again, the corresponding port will be opened again. Instead of purely understanding it as closing a port, you can disable a port.

1. You can check which ports are open by "netstat -anp".
(Note: adding the parameter '-n' will convert the application to port display, that is, the address in digital format, such as: nfs->2049, ftp->21, so you can open two terminals, one by one corresponding to the program
2. Then you can view the program using the port through "lsof -i:$PORT" ($PORT refers to the corresponding port number) . Or you can also look in the file /etc/services, from which you can find out the service corresponding to the port.
(Note: Some ports cannot be found through netstat, a more reliable method is "sudo nmap -sT -O localhost")
3. To close a port, you can:
1) Disable the port through the iptables tool, such as :
"sudo iptables -A INPUT -p tcp --dport $PORT -j DROP"
"sudo iptables -A OUTPUT -p tcp --dport $PORT -j DROP"   
2) Or close the corresponding application, the port will be Naturally closed, such as:
"kill -9 PID" (PID: process number)
such as: through "netstat -anp | grep ssh"

Then: "kill -9 7546"

(you can check the open status of system services through "chkconfig")

Sometimes after closing the software, the background process dies, causing the port to be occupied. The following takes JBoss port 8083 as an example to list the detailed solution process.

Solution:

1. Find the occupied port

netstat -tln 
netstat -tln | grep 8083 
netstat -tln to check the port usage, while netstat -tln | grep 8083 only check the usage of port 8083



2. Check which program the port belongs to ? Which process is the port occupied by

lsof -i :8083 


3. Kill the process occupying the port

kill -9 The process id is 
transferred from: http://taoistwar.iteye.com/blog/701704

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326706446&siteId=291194637
Recommended