CentOS6.5 配置防火墙+允许指定ip访问端口
参考博文:
iptables防火墙只允许指定ip连接指定端口、访问指定网站
一、配置防火墙
打开配置文件
[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后面
在配置pgAdmin远程访问虚拟机postgresql时发现出问题了,改了半天才发现是因为 防火墙端口开放文件中顺序没放对!汗!
重启防火墙使配置生效
[root@localhost ~]# service iptables restart
其它
查看开放端口
[root@localhost ~]# /etc/init.d/iptables status
关闭防火墙
[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.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
注意命令的顺序不能反了
查看版本:cat /etc/centos-release
cat /etc/centos-release
CentOS release 6.6 (Final)
允许访问所有端口的配置,文件/etc/sysconfig/iptables的内容:
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
COMMIT
查看防火墙:iptables -L -n
# iptables -L -n
Chain INPUT (policy ACCEPT)
target prot opt source destination
Chain FORWARD (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
禁止外部访问TCP的40080端口:
-A INPUT -p tcp --dport 40080 -j DROP
禁止外部访问TCP的3000到4000端口(端口范围):
-A INPUT -p tcp --dport 3000:4000 -j DROP
禁止某个IP地址访问所有TCP端口:
-A INPUT -p tcp -s 192.168.1.2 -j DROP
禁止某个IP地址访问TCP的57025端口:
-A INPUT -p tcp -s 192.168.1.2 --dport 57025 -j DROP
文件/etc/sysconfig/iptables配置的所有内容:
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -p tcp --dport 40080 -j DROP
-A INPUT -p tcp --dport 3000:4000 -j DROP
-A INPUT -p tcp -s 192.168.1.103 -j DROP
-A INPUT -p tcp -s 192.168.1.2 --dport 57025 -j DROP
COMMIT
重新启动:service iptables restart
查看防火墙:iptables -L -n
# iptables -L -n
Chain INPUT (policy ACCEPT)
target prot opt source destination
DROP tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:40080
DROP tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpts:3000:4000
DROP tcp -- 192.168.1.103 0.0.0.0/0
DROP tcp -- 192.168.1.2 0.0.0.0/0 tcp dpt:57025
Chain FORWARD (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
1万+

被折叠的 条评论
为什么被折叠?



