码迷,mamicode.com
首页 > 系统相关 > 详细

linux系统iptables的使用

时间:2018-04-19 21:58:13      阅读:280      评论:0      收藏:0      [点我收藏+]

标签:防火墙   iptables   firewalld   

一,iptables和firewalld比较

在CentOS 7系统中,firewalld防火墙取代了iptables防火墙。其实,iptables与firewalld都不是真正的防火墙,它们都只是用来定义防火墙策略的防火墙管理工具或者服务。
iptables服务会把配置好的防火墙策略交由内核层面的netfilter网络过滤器来处理,而firewalld服务则是把配置好的防火墙策略交由内核层面的nftables包过滤框架来处理。换句话说,当前在Linux系统中其实存在多个防火墙管理工具,旨在方便运维人员管理Linux系统中的防火墙策略,我们只需要配置妥当其中的一个就足够了。虽然这些工具各有优劣,但它们在防火墙策略的配置思路上是保持一致的。

二,iptables语法

语法:

iptables (选项) (参数)

iptables命令选项输入顺序:

iptables -t 表名 <-A/I/D/R> 规则链名 [规则号] <-i/o 网卡名> -p 协议名 <-s 源IP/源子网> --sport 源端口 <-d 目标IP/目标子网> --dport 目标端口 -j 触发动作(目标值)

注:
-t(table) 表 四表:filter,nat,mangle,raw (默认表是filter)
-A(append) 追加添加 -I(insert)插入 行号 -D(delete)删除 行号 -R(replace)替换
-i(in-interface)流入接口 -o(out-interface)流出接口
-p(protocol) 协议 tcp, udp, udplite, icmp等
-s(source) 源地址 --sport(source port)源端口
-d(destination)目标地址 --dport(destination port) 目标端口
-j(jump) 执行 ACCPET/DROP/REJECT/DNAT/SNAT/MASQUERADE/REDIRECT/LOG(必须大写)

三,iptables工作流程

技术分享图片
iptables过滤的规则顺序是:
由上至下,匹配即停止。
iptables有四表和五链:(链名必须大写)
四表:
filter(过滤规则表):(默认)INPUT,OUPUT,FORWARD
nat(地址转发规则表):PREROUTING,OUTPUT,POSTROUTING
mangle(修改数据位规则表):PREROUTING,INPUT,OUPUT,FORWARD,POSTROUTING
raw(跟踪数据表规则表):PRERONTING,OUTOUT

五链:PREROUTING(路由前过滤),INPUT(入站过滤),OUTPUT(出站过滤),FORWARD(转发过滤),POSTROUTING(路由后过滤)

四,iptables命令

以下配置的系统环境:redhat6.7

[root@host61 ~]# cat /etc/redhat-release    
Red Hat Enterprise Linux Server release 6.7 (Santiago)

首先是防火墙服务的启动停止:

service iptables save | stop | start | restart | status | save  //CentOS6保存,停止,启动,重启,查看状态,保存
chkconfig iptables on| off  //开机自起或关闭
systemctl stop | start | restart | status   firewalld  //CentOS7停止,启动,重启,查看状态
systemctl enable | disable firewalld   //开机自起或关闭

1.主机型防火墙
技术分享图片
每次配置完后需要保存配置:
/etc/init.d/iptables save 或者 serveice iptables save

1.1 在61主机上开启iptables服务。只允许192.168.4.254的主机访问自己的ssh服务。

[root@host61 ~]# iptables -F
[root@host61 ~]# iptables  -t  filter  -A  INPUT  -s  192.168.4.254 -p tcp  --dport 22  -j ACCEPT
[root@host61 ~]# iptables  -t  filter   -P  INPUT  DROP
[root@host61 ~]# serveice iptables save
[root@host61 ~]# iptables  -t  filter  -nL  INPUT
Chain INPUT (policy DROP)
target     prot opt source               destination         
ACCEPT     tcp  --  192.168.4.254        0.0.0.0/0           tcp dpt:22 

1.2 在61主机上添加1条新的规则,允许网络中的所有主机访问本机的网站服务。

[root@host61 ~]# iptables  -t  filter  -A  INPUT    -p tcp  --dport  80  -j   ACCEPT
[root@host61 ~]# serveice iptables save
[root@host61 ~]# iptables  -t  filter  -nL  INPUT
Chain INPUT (policy DROP)
target     prot opt source               destination         
ACCEPT     tcp  --  192.168.4.254        0.0.0.0/0           tcp dpt:22 
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:80

1.3 在61主机上添加1条新的规则,不允许192.168.4.62主机访问本机的网站服务。

[root@host61 ~]# iptables  -t  filter  -I  INPUT  2  -s  192.168.4.62  -p tcp  --dport  80  -j  ACCEPT
[root@host61 ~]# serveice iptables save
[root@host61 ~]# iptables  -t  filter  -nL  INPUT
Chain INPUT (policy DROP)
target     prot opt source               destination         
ACCEPT     tcp  --  192.168.4.254        0.0.0.0/0           tcp dpt:22 
ACCEPT     tcp  --  192.168.4.62         0.0.0.0/0           tcp dpt:80 
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:80 

1.4 在61主机上添加新的规则,61可以ping网络中的其他主机 ,但其他主机不可以ping61主机。

[root@host61 ~]# iptables  -t  filter  -A  INPUT  -p  icmp   --icmp-type  echo-reply  -j  ACCEPT
[root@host61 ~]# serveice iptables save
[root@host61 ~]# iptables  -t  filter  -nL  INPUT
Chain INPUT (policy DROP)
target     prot opt source               destination         
ACCEPT     tcp  --  192.168.4.254        0.0.0.0/0           tcp dpt:22 
ACCEPT     tcp  --  192.168.4.62         0.0.0.0/0           tcp dpt:80 
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:80 
ACCEPT     icmp --  0.0.0.0/0            0.0.0.0/0           icmp type 0

2.网络型型防火墙
技术分享图片
注:主机65和67的网关是192.168.4.68
2.1 让局域网内所有的主机共享一个公网ip地址上网。

[root@host68 ~]# iptables -F
[root@host68 ~]# iptables -t nat -A POSTROUTING -s 192.168.4.0/24 -o eth1 -j SNAT --to-source 192.168.2.68
[root@host68 ~]# iptables -t nat -nL POSTROUTING 
Chain POSTROUTING (policy ACCEPT)
target     prot opt source               destination         
SNAT       all  --  192.168.4.0/24       0.0.0.0/0           to:192.168.2.68

2.2 发布局域网内的网站服务。

[root@host68 ~]# iptables -t nat -A PREROUTING -i eth1 -d 192.168.2.68 -p tcp --dport 80 -j DNAT --to-destination 192.168.4.67
[root@host68 ~]# iptables -t nat -nL PREROUTING 
Chain PREROUTING (policy ACCEPT)
target     prot opt source               destination         
DNAT       tcp  --  0.0.0.0/0            192.168.2.68        tcp dpt:80 to:192.168.4.67

共勉:I hear and I forget , I see and I remember, I do and I understand!

参考资料:
http://man.linuxde.net/iptables
https://www.cnblogs.com/alimac/p/5848372.html
https://www.cnblogs.com/can-H/p/6726743.html
http://www.benet.wang/%E6%8A%80%E6%9C%AF%E9%9D%A2%E8%AF%95/174.html
https://www.cnblogs.com/wajika/p/6382853.html
http://www.jb51.net/article/112698.htm

linux系统iptables的使用

标签:防火墙   iptables   firewalld   

原文地址:http://blog.51cto.com/13667098/2105527

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!