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. 小程序中使用async函数 会报 regeneratorRuntime is not defined的问题

    async await比Promise更好的解决异步操作问题,但是在小程序中直接使用会出现以下的错误提示 是因为缺少了regeneratorRuntime这个模块,需要从外部引入 1.在新建的文件夹中 ...

  2. Droppable(放置组件)

    一.class加载方式 <div id="pop" class="easyui-droppable" style="width: 400px;h ...

  3. elasticsearch 父子关系

    ElasticSearch 中的Parent-Child关系和nested模型是相似的, 两个都可以用于复杂的数据结构中,区别是 nested 类型的文档是把所有的实体聚合到一个文档中而Parent- ...

  4. python3 线程调用与GIL 锁机制

    转载

  5. 【leetcode】316. Remove Duplicate Letters

    题目如下: Given a string which contains only lowercase letters, remove duplicate letters so that every l ...

  6. springboot关联Mybatis和Redis依赖

    <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot ...

  7. Python--基础之socket编程

    一 客户端/服务器架构 即C/S架构,包括 1.硬件C/S架构(打印机) 2.软件C/S架构(web服务) 美好的愿望: 最常用的软件服务器是 Web 服务器.一台机器里放一些网页或 Web 应用程序 ...

  8. UNP学习第六章select

    一.I/O复用典型的网络应用场合 当客户处理多个描述字时,必须使用I/O复用,这在前一段中已做了描述. 一个客户同时处理多个套接口时可能的,但很少出现. 如果一个TCP服务器既要处理监听套接口,又要处 ...

  9. 前端每日实战:58# 视频演示如何用纯 CSS 创作一只卡通鹦鹉

    效果预览 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/vrRmWy 可交互视频 此视频是可 ...

  10. JavaWeb解决中文乱码

    1.Get请求,方案有两种 A:修改Tomcat配置文件 server.xml   URIEncoding="UTF-8" 如:<Connector port="8 ...