How to open a port in centos?
How to open a port in centos:
1. iptables (centos 6 and before)
1. Turn on/off/restart the firewall
Turn on the firewall (it will take effect permanently after restarting):chkconfig iptables on
Turn off the firewall (it will take effect permanently after restarting):chkconfig iptables off
Turn on the firewall (effective immediately, invalid after restart): service iptables start
Turn off the firewall (effective immediately, invalid after restart): service iptables stop
Restart the firewall: service iptables restartd
2. Check the open ports
/etc/init.d/iptables status
3. Open a certain port (take 8080 as an example)
(1) Open the port
iptables -A INPUT -p tcp --dport 8080 -j ACCEPT
(2) Save and restart the firewall
/etc/rc.d/init.d/iptables save /etc/init.d/iptables restart
4. Open the port between 49152~65534
iptables -A INPUT -p tcp --dport 49152:65534 -j ACCEPT
Similarly, Here you need to save the settings and restart the firewall.
5. Other opening methods
We can also open the port by modifying the /etc/sysconfig/iptables
file, as follows
vi /etc/sysconfig/iptables
and then in Add a line to the file
-A RH-Firewall-1-INPUT -m state –state NEW -m tcp -p tcp –dport 8080 -j ACCEPT
Parameter description:
–A The parameter is regarded as adding a rule
–p specifies what protocol it is, our commonly used tcp protocol, of course There are also udp, for example, DNS
-dport of port 53 is the target port. When data enters the server from the outside, it is the target port
-sport. When data goes out of the server, it is the data source port.
–j means to specify ACCEPT - to receive or DROP not to receive
2. firewalld (centos7)
Centos7 has firewalld installed by default, if it is not installed , can be installed using yum install firewalld firewalld-config
.
1. Start the firewall
systemctl start firewalld
2.Disable the firewall
systemctl stop firewalld
3.Set the startup
systemctl enable firewalld
4.Stop and disable the startup
sytemctl disable firewalld
5. Restart the firewall
firewall-cmd --reload
6. Check the status
systemctl status firewalld或者 firewall-cmd --state
7. Check the version
firewall-cmd --version
8. Check the help
firewall-cmd --help
9. Check the area information
firewall-cmd --get-active-zones
10. Check the area information to which the specified interface belongs
firewall-cmd --get-zone-of-interface=eth0
11. Reject all packets
firewall-cmd --panic-on
12. Cancel the rejection status
firewall-cmd --panic-off
13 .Check whether it is rejected
firewall-cmd --query-panic
14.Add the interface to the zone (the default interfaces are all in public)
firewall-cmd --zone=public --add-interface=eth0(永久生效再加上 --permanent 然后reload防火墙)
15.Set the default interface zone
firewall-cmd --set-default-zone=public(立即生效,无需重启)
16.Update the firewall rules
firewall-cmd --reload或firewall-cmd --complete-reload(两者的区别就是第一个无需断开连接,就是firewalld特性之一动态 添加规则,第二个需要断开连接,类似重启服务)
17. View all open ports in the specified area
firewall-cmd --zone=public --list-ports
18. Open ports in the specified area (remember to restart the firewall)
firewall-cmd --zone=public --add-port=80/tcp(永久生效再加上 --permanent)
Recommended tutorial: "centos Tutorial》
The above is the detailed content of How to open a port in centos?. For more information, please follow other related articles on the PHP Chinese website!