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实验过程是怎么样的,希望对大家有用!

相关内容