CentOS6.5 配置防火墙+容许指定ip访问端口

参考博文:html

iptables防火墙只容许指定ip链接指定端口、访问指定网站sql

1、配置防火墙tcp

打开配置文件 
post

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

正确的配置文件 
网站

# 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
-A INPUT -m state –state NEW -m tcp -p tcp –dport * -j ACCEPT

注意点:新开放的端口必定要在端口22后面 this

在配置pgAdmin远程访问虚拟机postgresql时发现出问题了,改了半天才发现是由于 防火墙端口开放文件中顺序没放对!汗!
重启防火墙使配置生效 
spa

[root@localhost ~]# service iptables restart

其它 .net

查看开放端口 
rest

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

 关闭防火墙 postgresql

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

 二、配置防火墙容许指定ip访问端口

下面三行的意思:

先关闭全部的80端口

开启ip段192.168.1.0/24端的80口

开启ip段211.123.16.123/24端ip段的80口

# 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

开放一个IP的一些端口,其它都封闭

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

多个端口

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

指定时间上网

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

 最经用的这个起做用:2017-8-16

ptables 限制ip访问
经过iptables限制9889端口的访问(只容许192.168.1.20一、192.168.1.20二、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

注意命令的顺序不能反了。
相关文章
相关标签/搜索