iptables nat实验

运维 系统运维
iptables 是与最新的 2.6.x 版本Linux 内核集成的 IP 信息包过滤系统。本文将讲述一个iptables 的nat实验过程。

iptablesnat实验过程如下:

  1、 需求:

  1. 设置iptables,使之成为网关,能通过NAT代理局域网客户机上internet;

  2. 允许局域网客户机192.168.1.2-100的地址能上网

  2、 基本的拓扑图

  

 

  3、 系统及其软件环境:

  1.操作系统: Red Hat Enterprise Linux AS release 4 (Nahant Update 2)

  2.所需rpm软件包:

  iptables-1.2.11-3.1.RHEL4

  iptables-ipv6-1.2.11-3.1.RHEL4

  iptables-devel-1.2.11-3.1.RHEL4

  4、 所需设置文件或命令:

  /sbin/iptables

  5、 试验步骤:

  说明:以下所有操作均服务器(192.168.1.1)上操作

  1. 使用vi 编写如下脚本

  [root@gdlc ~]# vi fw.sh

  #!/bin/bash

  # Set up a default search path.

  PATH="/sbin:/usr/sbin:/bin:/usr/bin:/usr/X11R6/bin"

  export PATH

  # Source function library.

  . /etc/rc.d/init.d/functions

  IPT="/sbin/iptables"

  localnet="192.168.1.0/24"

  ctlip="192.168.1."

  startip=2

  endip=100

  Upnic="eth1"

  Downnic="eth0"

  nat_func () {

  /sbin/modprobe ip_tables

  /sbin/modprobe ip_conntrack_ftp

  /sbin/modprobe ip_nat_ftp

  echo 1 > /proc/sys/net/ipv4/ip_forward

  $IPT -F -t filter

  $IPT -F -t nat

  $IPT -F -t mangle

  ###IP MASQUERADE #############

  $IPT -A FORWARD -i $Upnic -m state --state ESTABLISHED,RELATED -j ACCEPT

  $IPT -t nat -A POSTROUTING -o $Upnic -s $localnet -j MASQUERADE

  ###FORWARD CONTRL ############

  declare n1=$startip

  declare n2=$endip

  for ((i=n1;i -A FORWARD -s $ctlip$i -j ACCEPT ;done

  $IPT -A FORWARD -j DROP

  }

  stop (){

  echo 0 > /proc/sys/net/ipv4/ip_forward

  $IPT -F -t filter

  $IPT -F -t nat

  $IPT -F -t mangle

  }

  status (){

  $IPT -n -L

  }

  # See how we were called.

  case "$1" in

  start)

  nat_func && echo "IPNAT start : `echo_success`"

  ;;

  stop)

  stop && echo "IPNAT stop : `echo_success`"

  ;;

  status)

  status

  ;;

  *)

  echo $"Usage: $0 {start|stop|status}"

  exit 1

  ;;

  esac

  2. 运行脚本

  [root@gdlc ~]# sh fw.sh start

  IPNAT start : [ OK ]

  [root@gdlc ~]# sh fw.sh stop

  IPNAT stop : [ OK ]

通过上文一系类的说明,我们肯定知道了iptables 的nat实验过程是怎么样的,希望对大家有用!

【编辑推荐】

  1. iptables 简单学习笔记
  2. 在Red Hat上安装iptables
  3. 搭建基于netfilter/iptables的实验环境
  4. 用IPtables限制BT、电驴等网络流量
  5. 如何使用IPTables实现字符串模式匹配
  6. iptables相关脚本
  7. 如何使用 IPTables
  8. 如何用iptables实现NAT
责任编辑:赵鹏 来源: 互联网
相关推荐

2011-03-16 09:05:34

iptablesnat

2011-03-17 14:09:03

iptables na

2011-03-16 10:59:57

2011-03-15 14:26:23

iptablesNAT

2011-03-15 09:10:47

iptablesNAT

2011-03-16 09:05:29

iptablesNAT

2011-03-17 13:55:23

iptablesNAT端口映射

2011-03-17 15:44:21

2011-03-16 09:05:32

RedhatiptablesNAT

2011-03-16 09:05:53

NATiptables

2011-03-16 09:05:33

2011-03-17 13:28:49

iptables na

2011-03-17 17:40:52

iptables配置

2011-03-15 15:51:12

netfilteriptables

2011-03-17 13:02:07

iptables na

2011-09-06 16:31:58

iptablesNAT转发

2010-05-27 11:04:32

2011-03-17 11:27:11

Linux iptab

2011-03-16 11:06:55

Iptables防火墙

2011-03-17 17:19:24

iptables
点赞
收藏

51CTO技术栈公众号