Sometimes we worked hard to install a service but found that unable to access, thinking that they did not install success, in fact, the solution is very simple, is to open the relevant port. For example, if I install Nginx or Apache, I need to release port 80 on the firewall. The relevant instance code is as follows.
/sbin/iptables-i input-p TCP--dport 80-j ACCEPT #开启80端口
/sbin/iptables-i input-p TCP--dport 22-j ACCEPT #开启22端口
/etc/rc.d/init.d/iptables Save #保存配置
/etc/rc.d/init.d/iptables Restart #重启服务
View open Ports
/etc/init.d/iptables status
The results are as follows
Table:filter
Chain INPUT (Policy ACCEPT)
Num Target prot opt source destination
2 ACCEPT TCP--0.0.0.0/0 0.0.0.0/0 TCP dpt:22
3 ACCEPT TCP--0.0.0.0/0 0.0.0.0/0 TCP dpt:80
Iptables is a firewall under Linux and is also the service name.
Service iptables Status View firewall state
Service iptables start to turn on the firewall
Service iptables Stop shutting down the firewall
Service iptables Restart Restart firewall
Firewalls Open specific ports:
① file/etc/sysconfig/iptables
② Add:
-A rh-firewall-1-input-m state--state new-m tcp-p TCP--dport 8080-j ACCEPT
★ The number 8080 stands for open 8080 ports, can also be changed to other ports ★
③ Restart the firewall
================================================================
Save settings for the firewall
Serivce iptables Save
View Iptables rules and numbers
IPTABLES-NL--line-number
Turn off all input FORWARD (forwards) output for all ports
Iptables-p INPUT DROP
Iptables-p FORWARD DROP
Iptables-p OUTPUT DROP
Open only 22 ports
Iptables-a input-p TCP--dport 22-j ACCEPT
Iptables-a output-p TCP--sport 22-j ACCEPT
Parameter explanation:
The –A parameter is seen as adding a rule
–P specifies what protocol we commonly use for the TCP protocol, and of course there are UDP, such as 53-port DNS
–dport is the destination port, when the data goes from outside to the server as the destination port
–sport data goes out of the server, it is used for the data source port
–J is designated as Accept-receive or DROP not receive
Disable an IP access
Iptables-a input-p tcp-s 192.168.1.2-j DROP
–s parameter is source (i.e. 192.168.1.2)
The back rejection is drop.
Delete Rule
Iptables-d INPUT 2
Delete the input chain number 2 rule
Linux firewall to open the relevant ports and view open ports