防火墙
关闭firewalld启用iptables防火墙
yum -y install iptables-services #安装服务
[root@nianhua ~]# systemctl enable iptables.service
Created symlink from /etc/systemd/system/basic.target.wants/iptables.service to /usr/lib/systemd/system/iptables.service. #开启iptables服务
[root@nianhua ~]# systemctl start iptables.service #启动iptables服务
filter表用于过滤包,最常用的表,有INPUT、FORWARD、OUTPUT三个链
nat表用于网络地址转换,有PREROUTING、OUTPUT、POSTROUTING三个链
managle表用于给数据包做标记,几乎用不到
raw表可以实现不追踪某些数据包
security表在centos6中并没有,用于强制访问控制(MAC)的网络规则,
参考文章 http://www.cnblogs.com/metoy/p/4320813.html
TABLES
当前有三个表(哪个表是当前表取决于内核配置选项和当前模块)。
-t table
这个选项指定命令要操作的匹配包的表。如果内核被配置为自动加载模块,这时
若模块没有加载,(系统)将尝试(为该表)加载适合的模块。
这些表如下:
filter ,这是默认的表,包含了内建的链INPUT(处理进入的包)、FORWORD(处理通 过的包)和OUT‐
PUT(处理本地生成的包)。
nat 这个表被查询时表示遇到了产生新的连接的包,由三个内建的链构成:PREROUTING
(修改到来的包)、OUTPUT(修改路由之前本地的包)、POSTROUTING
(修改准备出去的包)。
mangle
这个表用来对指定的包进行修改。它有两个内建规则:PREROUTING(修改路由之
前进入的包)和OUTPUT(修改路由之前本地的包)。
iptables语法
iptables -nvL 默认规则
iptables -F 清空规则
iptables -t filter(表名) -nvL 指定查看表
iptables -Z 清零
iptables -I(i大写)新增插入规则
iptables -A 新增规则
iptables -D 删除规则
iptables -vnL --line-number 显示规则编号
iptables -D INPUT 7(编号) 指定编号删除规则
iptables -P 修改默认规则
service iptables save 保存规则
service iptables restart 重启规则
iptables -nvL
查看默认规则
[root@nianhua ~]# iptables -nvL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
220 18397 ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED
0 0 ACCEPT icmp -- * * 0.0.0.0/0 0.0.0.0/0
3 180 ACCEPT all -- lo * 0.0.0.0/0 0.0.0.0/0
0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:22
231 18590 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
0 0 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited
Chain OUTPUT (policy ACCEPT 190 packets, 49026 bytes)
pkts bytes target prot opt in out source destination
[root@nianhua ~]# cat /etc/sysconfig/iptables
# sample configuration for iptables service
# you can edit this manually or use system-config-firewall
# please do not ask us to add additional ports/services to this default configuration
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT
[root@nianhua ~]# iptables -F #清空规则
[root@nianhua ~]# iptables -nvL #查看规则
Chain INPUT (policy ACCEPT 20 packets, 1484 bytes)
pkts bytes target prot opt in out source destination
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain OUTPUT (policy ACCEPT 12 packets, 1728 bytes)
pkts bytes target prot opt in out source destination
[root@nianhua ~]# cat /etc/sysconfig/iptables #查看规则配置文件
# sample configuration for iptables service
# you can edit this manually or use system-config-firewall
# please do not ask us to add additional ports/services to this default configuration
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT
[root@nianhua ~]# iptables -t filter -nvL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
5 388 ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED
0 0 ACCEPT icmp -- * * 0.0.0.0/0 0.0.0.0/0
0 0 ACCEPT all -- lo * 0.0.0.0/0 0.0.0.0/0
0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:22
0 0 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
0 0 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited
Chain OUTPUT (policy ACCEPT 4 packets, 528 bytes)
pkts bytes target prot opt in out source destination
[root@nianhua ~]# iptables -t nat -nvL
Chain PREROUTING (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain POSTROUTING (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
[root@nianhua ~]# iptables -Z; iptables -vnL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
0 0 ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED
0 0 ACCEPT icmp -- * * 0.0.0.0/0 0.0.0.0/0
0 0 ACCEPT all -- lo * 0.0.0.0/0 0.0.0.0/0
0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:22
0 0 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
0 0 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited
Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
iptables -A INPUT -s 192.168.188.1 -p tcp --sport 1234 -d 192.168.188.128 --dport 80 -j DROP
#-A=add 新增 #-s=source 来源ip #-p=protocol 协议 #-d 目标ip #-j 指定处理方式
#在INPUT链中新增一条规则 指定来源ip为192.168.188.1,协议为tcp,来源端口为1234,目标ip为192.168.188.128,目标端口为80 对接受的数据包处理方式为丢弃。
#DROP 丢弃,对请求的数据包直接丢弃不过处理
#REJECT 拒绝,对请求的数据包进行处理时发现数据包不符合规则再进行拒绝。
#ACCEPT 放行,放行规则允许的数据包
[root@nianhua ~]# iptables -I INPUT -p tcp --dport 80 -j DROP
-A则将规则放在最后面,-I则将规则插入在最前面。
如果两条规则相同,数据包匹配了第一条规则,则直接执行第一条规则,后面的规则则不执行。
[root@nianhua ~]# iptables -D INPUT -p tcp --dport 80 -j DROP
[root@nianhua ~]# iptables -vnL --line-number #显示规则编号
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
num pkts bytes target prot opt in out source destination
1 0 0 DROP tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:80
2 415 35267 ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED
3 0 0 ACCEPT icmp -- * * 0.0.0.0/0 0.0.0.0/0
4 4 240 ACCEPT all -- lo * 0.0.0.0/0 0.0.0.0/0
5 0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:22
6 339 26925 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited
7 0 0 DROP tcp -- * * 192.168.188.1 192.168.188.128 tcp spt:1234 dpt:80
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
num pkts bytes target prot opt in out source destination
1 0 0 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited
Chain OUTPUT (policy ACCEPT 25 packets, 2528 bytes)
num pkts bytes target prot opt in out source destination
[root@nianhua ~]# iptables -D INPUT 7 #指定需要删除的编号删除规则
[root@nianhua ~]# iptables -P OUTPUT DROP 修改所有默认规则为DROP