Details of Linux forced release of occupied ports and Linux firewall port opening methods
- 2020-12-13 19:13:40
- OfStack
When installing nginx, mysql, tomcat, and so on, you may encounter a problem where the port you need to use is somehow occupied. Here's how to solve this problem.
When it comes to ports, I have to mention firewalls, and this article briefly describes how to configure firewall development ports.
Various ways for Linux to view port conditions
The mapping relationships for all ports are in the /etc/services file
The port number of Linux is from 0 to 65536. The purpose of each port number is as follows:
0-1023: Recognized ports, bound to common services (FTP, SSH)
1024-49151: Register ports for binding to 1 or more services
49152-65535: Dynamic or private port, available for any network connection
Ports are divided into TCP and UDP transport protocols.
Linux view port status command
The following commands can be used to view the port situation. Click the command to adjust the usage details of each command:
nmap
Command, port scan used
netstat
Detection development port
lsof
Check the port descriptor
# View the port that binds the native
nmap 127.0.0.1
# check 3306 port
netstat -anlp | grep 3306
# detection 3306 interface
lsof -i:3306
linux release the occupied port solution
The solution steps are as follows:
Find the process occupying the port
Kill the process
Just use the following command:
# can be written as 1 command
netstat -anp|grep 8080|awk '{print $7}'|awk -F '/' '{print $1}'|xargs kill -s 9
The meanings of the commands are as follows:
netstat -anp
Displays all network usage and shows the usage program
grep 8080
Records matching port 8080 (may contain 18080)
awk '{print $7}'
Output the process in column 7 as follows: 18989/nginx
awk -F '/' '{print $1}'
Intercepting process PID: 18989
xargs kill -s 9
Kill the process using the output of the previous command as an argument
Release the occupied port as a step by step solution
Query whether the port is occupied
For example, if you need to check whether port 8080 is occupied, you can use the following command
netstat -an | grep 8080
Query the process occupying the port
You can view it using the lsof command
lsof -i:8080
You can also view it using netstat and grep
netstat
0
The last line of this command is the process PID and its name occupying port 8080.
Kill the process occupying the port
You can use the kill command to directly kill the process you found in the previous step.
kill -9 19664
linux firewall release port
Linux Firewall up and down
The firewall described below is iptable and does not apply to firewalld.
Firewall enabled (permanent after restart) : chkconfig iptables on
Firewall turned off (permanent on restart) : chkconfig iptables off
Firewall on (effective immediately, expired after restart) : service iptables start
Close firewall (effective immediately, expired after restart) : service iptables stop
Restart firewall :service iptables restart
Linux to view firewall status
You can use the following command to see:
/etc/init.d/iptables status
# Or abbreviations
iptables status
iptables -L
# You can also view the configuration file directly
vim /etc/sysconfig/iptables
Linux opens a port on the firewall
For example, open port 8080, you can use the following command:
iptables -A INPUT -p tcp --dport 8080 -j ACCEPT
# You also need to restart the service
service iptables restart
# You can also edit configuration files directly
vim /etc/sysconfig/iptables
# Then add it at the end of the file 1 Okay, let's develop 8080-8181 Between all ports
iptables -A INPUT -p tcp --dport 8080:8181 -j ACCEPT
Among them
�A
The argument is just like adding 1 rule
�p
What protocol is it, the tcp protocol that we use, and of course udp
�dport
Is the target port, when data from the outside into the server for the target port
�sport
When the data goes out of the server, it is used for the data source port
�j
Specify ACCEPT - to receive or DROP not to receive
The above is about Linux forced release and Linux firewall port opening methods. To see more articles on Linux ports, click on the related article below