CentOS configure firewall + allow designated ip access port

First, configure the firewall

Open configuration file 

[root@localhost ~]# vi /etc/sysconfig/iptables

The correct configuration file 

Copy code
# Firewall configuration written by system-config-firewall 
# Manual customization of this file is not recommended. 
*filter 
:INPUT ACCEPT [0:0] 
:FORWARD ACCEPT [0:0] 
:OUTPUT ACCEPT [0:0] 
-A INPUT -m state –state ESTABLISHED,RELATED -j ACCEPT 
-A INPUT -p icmp -j ACCEPT 
-A INPUT -i lo -j ACCEPT 
-A INPUT -m state –state NEW -m tcp -p tcp –dport 22 -j ACCEPT 
-A INPUT -m state –state NEW -m tcp -p tcp –dport 80 -j ACCEPT
-A INPUT -j REJECT –reject-with icmp-host-prohibited 
-A FORWARD -j REJECT –reject-with icmp-host-prohibited 
COMMIT
Copy code
-A INPUT -m state –state NEW -m tcp -p tcp –dport * -j ACCEPT

Note: The newly opened port must be behind port 22 

I found a problem when configuring pgAdmin to remotely access the virtual machine postgresql. It took a long time to find out that it was because the order of firewall port opening files was not right! sweat!
Restart the firewall to make the configuration take effect 

[root@localhost ~]# service iptables restart

other 

View open ports 

[root@localhost ~]# /etc/init.d/iptables status

 Turn off the firewall 

[root@localhost ~]# /etc/init.d/iptables stop

 Second , configure the firewall to allow the specified IP access port

The meaning of the following three lines:

Close all 80 ports first

Open the 80 port of the 192.168.1.0 / 24 end of the ip segment

Open the 80 port of the ip segment 211.123.16.123 / 24 end ip segment

# iptables -I INPUT -p tcp --dport 80 -j DROP 
# iptables -I INPUT -s 192.168.1.0/24 -p tcp --dport 80 -j ACCEPT
# iptables -I INPUT -s 211.123.16.123/24 -p tcp --dport 80 -j ACCEPT

Open some ports of an IP, others are closed

Copy code
iptables -A Filter -p tcp --dport 80 -s 192.168.100.200 -d www.pconline.com.cn -j ACCEPT
iptables -A Filter -p tcp --dport 25 -s 192.168.100.200 -j ACCEPT
iptables -A Filter -p tcp --dport 109 -s 192.168.100.200 -j ACCEPT
iptables -A Filter -p tcp --dport 110 -s 192.168.100.200 -j ACCEPT
iptables -A Filter -p tcp --dport 53 -j ACCEPT
iptables -A Filter -p udp --dport 53 -j ACCEPT
iptables -A Filter -j DROP
Copy code

Multiple ports

iptables -A Filter -p tcp -m multiport --destination-port 22,53,80,110 -s 192.168.20.3 -j REJECT

Internet access at designated time

iptables -A Filter -s 10.10.10.253 -m time --timestart 6:00 --timestop 11:00 --days Mon,Tue,Wed,Thu,Fri,Sat,Sun -j DROP
iptables -A Filter -m time --timestart 12:00 --timestop 13:00 --days Mon,Tue,Wed,Thu,Fri,Sat,Sun -j ACCEPT
iptables -A Filter -m time --timestart 17:30 --timestop 8:30 --days Mon,Tue,Wed,Thu,Fri,Sat,Sun -j ACCEPT

 The most used one works: 2017-8-16

1
2
3
4
5
6
7
8
ptables 限制ip访问
通过iptables限制9889端口的访问(只允许192.168.1.201、192.168.1.202、192.168.1.203),其他ip都禁止访问
iptables -I INPUT -p tcp --dport 9889 -j DROP
iptables -I INPUT -s 192.168.1.201 -p tcp --dport 9889 -j ACCEPT
iptables -I INPUT -s 192.168.1.202 -p tcp --dport 9889 -j ACCEPT
iptables -I INPUT -s 192.168.1.203 -p tcp --dport 9889 -j ACCEPT
 
注意命令的顺序不能反了。

Guess you like

Origin www.cnblogs.com/0daybug/p/12758733.html