inushome 发表于 2019-2-18 08:42:35

linux系统iptables的使用

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

二,iptables语法
  语法:

iptables (选项) (参数)
  iptables命令选项输入顺序:

iptables -t 表名规则链名 [规则号]-p 协议名--sport 源端口--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工作流程
http://i2.运维网.com/images/blog/201804/16/93df330cda7580abeceb27ac7ffa0a6b.png
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

# cat /etc/redhat-release   
Red Hat Enterprise Linux Server release 6.7 (Santiago)
  首先是防火墙服务的启动停止:

service iptables save | stop | start | restart | status   #CentOS6保存,停止,启动,重启,查看状态,保存
chkconfig iptables on| off                                                #开机自起或关闭
systemctl stop | start | restart | status   firewalld          #CentOS7停止,启动,重启,查看状态
systemctl enable | disable firewalld                           #开机自起或关闭
  防火墙基本基本命令:

iptables -F   #清除预设表filter中的所有规则链的规则
iptables -X   #清除预设表filter中使用者自定链中的规则及 使用 iptables -N xxx 新增自定义的 chain
iptables -P INPUT DROP#定义过滤政策
iptables -D INPUT 1   #删除第一条
iptables -nL   --line    #显示防火墙规则表和序号
  1.主机型防火墙
http://i2.运维网.com/images/blog/201804/19/447eba8ffffa5acfeb785770c010ff46.png
每次配置完后需要保存配置:
/etc/init.d/iptablessave 或者 serveice iptables save或者 iptables-save
  1.1 在61主机上开启iptables服务。只允许192.168.4.254的主机访问自己的ssh服务。

# iptables -F
# iptables-tfilter-AINPUT-s192.168.4.254 -p tcp--dport 22-j ACCEPT
# iptables-tfilter   -PINPUTDROP
# serveice iptables save
# iptables-tfilter-nLINPUT
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条新的规则,允许网络中的所有主机访问本机的网站服务。

# iptables-tfilter-AINPUT    -p tcp--dport80-j   DROP
# serveice iptables save
# iptables-tfilter-nLINPUT
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主机访问本机的网站服务。

# iptables-tfilter-IINPUT2-s192.168.4.62-p tcp--dport80-jACCEPT
# serveice iptables save
# iptables-tfilter-nLINPUT
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主机。

# iptables-tfilter-AINPUT-picmp   --icmp-typeecho-reply-jACCEPT
# serveice iptables save
# iptables-tfilter-nLINPUT
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.网络型型防火墙
http://i2.运维网.com/images/blog/201804/19/a17ecbef2c26b0ffe1ac393532c1f086.png
注:主机65和67的网关是192.168.4.68
2.1 让局域网内所有的主机共享一个公网ip地址上网。

# iptables -F
# iptables -t nat -A POSTROUTING -s 192.168.4.0/24 -o eth1 -j SNAT --to-source 192.168.2.68
# iptables --flush
# 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 发布局域网内的网站服务。

# iptables -t nat -A PREROUTING -i eth1 -d 192.168.2.68 -p tcp --dport 80 -j DNAT --to-destination 192.168.4.67
# 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.iyunv.net/article/112698.htm



页: [1]
查看完整版本: linux系统iptables的使用