iptables 防火墙(下)

1. 常见的隐含匹配条件:

1.1 端口匹配: --sport 源端口、--dport 目的端口

1.2 TCP标记匹配: -tcp-flags 检查范围被设置的标记

1.3 ICMP类型匹配: --icmp-type ICMP类型常见的icmp类型

  • 8 Echo request--->回显请求( Ping请求)
  • 8 0 Echo Reply--->回显应答(Ping 应答)
  • 8 3 错误回显

2. 常用的显示匹配条件:

2.1 多端口匹配:

  • -m multiport --sports源端口列表、
  • -m multiport --dports目的端口列表

2.2 IP范围匹配:

-m iprange --src-range IP范围

2.3 MAC地址匹配:

-m mac --mac-source MAC地址

2.4 状态匹配: -m state --state 连接状态

  • 常见的连接状态:
  • NEW: 新连接,与任何连接无关
  • ESTABLISHED: 响应请求或已建立连接的
  • RELATED: 与已连接有相关性的,如FTP数据连接

3.SNAT策略

3.1 SNAT作用:

局域网主机共享单个公网IP地址接入Internet

3.2 SNAT策略的原理

源地址转换,Source Network Address Translation修改数据包的源地址

3.3 固定的外网IP地址

[root@iptables02 ~]# iptables -t nat -A POSTROUTING -s 192.168.200.0/24 -o eth1 -j SNAT --to-source 192.168.100.100

3.4 非固定外网IP地址或ADSL

[root@iptables02 ~]# iptables -t nat -I POSTROUTING -s 192.168.200.0/24 -o eth0 -j MASQUERADE

4. DNAT策略

4.1 DNAT 原理

在Internet环境中,通过网关服务器中正确设置DNAT策略可实现企业所注册的网站或域名必须对应公网IP地址。

4.2 DNAT转发规则1:发内网Web服务

[root@iptables02 ~]# iptables -t nat -A PREROUTING -i eth1 -d 192.168.100.100 -p tcp --dport 80 -j DNAT --to-destination 192.168.200.99

4.3 (2) DNAT转换规则2:发布时修改目标端口

[root@iptables02 ~]# iptables -t nat -A PREROUTING -i eth1 -d 192.168.100.100 -p tcp --dport 80 -j DNAT --to-destination 192.168.200.99:8080

5. 项目实战

项目要求:三台主机,要求其中部署Nginx(iptables01),另外一台可以查看到网页内容

注:主机2与主机3均无网关

5.1 部署环境

主机名 主机IP(1) 主机IP(2) 网卡模式(1) 网卡模式(2) 主机网关
iptables01 192.168.200.99 NET8 192.168.200.100
iptables02 192.168.200.100 192.168.100.100 NET8 NET1(仅主机)
iptables03 192.168.100.110 NET8

5.2 部署网卡配置文件

5.2.1 iptables01的网卡配置文件

[root@iptables01 network-scripts]# pwd
/etc/sysconfig/network-scripts
[root@iptables01 network-scripts]# cat ifcfg-eth0
DEVICE=eth0
TYPE=Ethernet
ONBOOT=yes
NM_CONTROLLED=yes
BOOTPROTO=none
IPADDR=192.168.200.99 #本主机的IP
NETMASK=255.255.255.0
GATEWAY=192.168.200.100 #本主机的网关,iptables02第一个网卡的IP

5.2.2 iptables02的网卡配置文件

[root@iptables02 network-scripts]# pwd
/etc/sysconfig/network-scripts
[root@iptables02 network-scripts]# cat ifcfg-eth0
DEVICE=eth0
TYPE=Ethernet
ONBOOT=yes
NM_CONTROLLED=yes
BOOTPROTO=none
IPADDR=192.168.200.100 #本主机的IP,iptables01的网关
NETMASK=255.255.255.0 [root@iptables02 network-scripts]# cat ifcfg-eth1
DEVICE=eth1
TYPE=Ethernet
ONBOOT=yes
NM_CONTROLLED=yes
BOOTPROTO=none
IPADDR=192.168.100.100 #本主机的IP,iptables03的网关
NETMASK=255.255.255.0

5.2.3 iptables03的网卡配置文件

[root@iptables03 network-scripts]# pwd
/etc/sysconfig/network-scripts
[root@iptables03 network-scripts]# cat ifcfg-eth0
DEVICE=eth0
TYPE=Ethernet
ONBOOT=yes
NM_CONTROLLED=yes
BOOTPROTO=none
IPADDR=192.168.100.110 #本主机的IP
NETMASK=255.255.255.0

5.2.4 Nginx

Nginx部署略。。。

[root@iptables01 ~]# curl www.yangwenbo.com
192.168.200.99 welcome to yunjisuan Nginx网页内容

5.3 修改iptables02转发的配置

[root@iptables02 /]# sed -n '7p' /etc/sysctl.conf
net.ipv4.ip_forward = 1 #修改成1

5.4 对来往网卡IP做限制

[root@iptables02 ~]# iptables -t nat -A POSTROUTING -s 192.168.200.0/24 -o eth1 -j SNAT --to-source 192.168.100.100
[root@iptables02 ~]# iptables -t nat -A PREROUTING -i eth1 -d 192.168.100.100 -p tcp --dport 80 -j DNAT --to-destination 192.168.200.99:8080
[root@iptables02 ~]# iptables -t nat -L
Chain PREROUTING (policy ACCEPT)
target prot opt source destination
DNAT tcp -- anywhere 192.168.100.100 tcp dpt:http to:192.168.200.99:8080 Chain POSTROUTING (policy ACCEPT)
target prot opt source destination
SNAT all -- 192.168.200.0/24 anywhere to:192.168.100.100 Chain OUTPUT (policy ACCEPT)
target prot opt source destination

5.5 PC主机映射并查看

[root@iptables03 ~]# curl www.yangwenbo.com
192.168.200.99 welcome to yunjisuan

iptables 防火墙(下)的更多相关文章

  1. 在iptables防火墙下开启vsftpd的端口

    在开启vsftpd端口后发现用客户端工具能登陆,但无法浏览文件和新建文件.此时看了一下ftp的协议,发现ftp有主动模式和被动模式.在服务端开21端口是让客户端进来,并没有出去的端口,还在服务端开启出 ...

  2. Linux下iptables防火墙用法规则详解

    管理网络流量是系统管理员必需处理的最棘手工作之一,我们必需规定连接系统的用户满足防火墙的传入和传出要求,以最大限度保证系统免受×××.很多用户把 Linux 中的iptables当成一个防火墙,从严格 ...

  3. CentOS下配置iptables防火墙 linux NAT(iptables)配置

    CentOS下配置防火墙 配置nat转发服务CentOS下配置iptables防火墙 linux NAT(iptables)配置 CentOS下配置iptables 1,vim /etc/syscon ...

  4. (转载)Linux下IPTABLES防火墙的设定

    (转载)http://www.jefflei.com/post/1760.html 1.iptables防火墙启动和停止 启动iptables防火墙时命令行输入 #service iptables s ...

  5. centos6.5下系统编译定制iptables防火墙扩展layer7应用层访问控制功能及应用限制QQ2016上网

    iptables防火墙扩展之layer7应用层访问控制 概述: iptables防火墙是工作在网络层,针对TCP/IP数据包实施过滤和限制,属于典型的包过滤防火墙.以基于网络层的数据包过滤机制为主,同 ...

  6. Linux下 iptables防火墙 放开相关port 拒绝相关port 及查看已放开port

    我用的是fedora 14 1. 查看iptables 防火墙已经开启的port:/etc/init.d/iptables status [root@hzswtb2-mpc ~]#/etc/rc.d/ ...

  7. Centos7下安装iptables防火墙

    说明:centos7默认使用的firewalld防火墙,由于习惯使用iptables做防火墙,所以在安装好centos7系统后,会将默认的firewall关闭,并另安装iptables进行防火墙规则设 ...

  8. 编译内核实现iptables防火墙layer7应用层过滤 (三)

    在前面的两篇文章中我们主要讲解了Linux防火墙iptables的原理及配置规则,想博友们也都知道iptables防火墙是工作在网络层,针对TCP/IP数据包实施过滤和限制,属于典型的包过滤防火墙.以 ...

  9. [moka同学摘录]iptables防火墙规则的添加、删除、修改、保存

    文章来源:http://www.splaybow.com/post/iptables-rule-add-delete-modify-save.html 本文介绍iptables这个Linux下最强大的 ...

随机推荐

  1. 创建Uboot 环境变量 bin 文件

    As we know, the bootloader stores its configuration into an area of the flash called the environment ...

  2. day01 html介绍 文档声明头 head标签 body标签

    day01 html   初识html <!--文档的声明--> <!doctype html>   <html lang="en">    # ...

  3. Spark开发环境搭建和作业提交

    Spark高可用集群搭建 在所有节点上下载或上传spark文件,解压缩安装,建立软连接 配置所有节点spark安装目录下的spark-evn.sh文件 配置slaves 配置spark-default ...

  4. boost pointer container

    1. boost::ptr_vector #include <boost/ptr_container/ptr_vector.hpp> #include <iostream> i ...

  5. hdu 5885 XM Reserves (FFT建模)

    Problem Description As an eligible Ingress Resistance Agent you should know your power source, the E ...

  6. [CSP-S模拟测试58]题解

    以后题解还是单独放吧. A.Divisors 根号筛求所有数的因子,扫一遍去重统计即可. #include<cstdio> #include<iostream> #includ ...

  7. Eclipse Missing artifact jdk.tools:jdk.tools:jar:1.6

    Missing artifact jdk.tools:jdk.tools:jar:1.6 问题出在Eclipse Maven的支持上,在Eclipse下,java.home变量设置为用于启动Eclip ...

  8. centos 7 下升级自带 sqlite3

    问题 在 centos 7 上面运行 django 2.2 开发服务器时出现: django.core.exceptions.ImproperlyConfigured: SQLite 3.8.3 or ...

  9. 数据访问层的基类BaseDALSQL

    using System; using System.Text; using System.Collections; using System.Data; using System.Data.Comm ...

  10. Nginx网络架构实战学习笔记(五):大访问量优化整体思路、ab压力测试及nginx性能统计模块、nginx单机1w并发优化

    文章目录 大访问量优化整体思路 ab压力测试及nginx性能统计模块 ab压力测试及nginx性能统计模块 ab压力测试 nginx性能统计模块 nginx单机1w并发优化 整装待发: socket ...