Step-By-Step Configuration of NAT with iptables
使用iptalbes一步一步设置NAT
This tutorial shows how to set up network-address-translation (NAT) on a Linux system with iptables rules so that the system can act as a gateway and provide internet access to multiple hosts on a local network using a single public IP address. This is achieved by rewriting the source and/or destination addresses of IP packets as they pass through the NAT system.
本教程展示,使用iptables规则在Linux系统设置网络地址转换(NAT), 使得这个系统成为一个网关,使用1个公用IP为局域网的多台机器提供互联网访问。
Requirements:
CPU - PII or more
OS - Any Linux distribution
Software - Iptables
Network Interface Cards: 2
要求:
CPU--奔II或更高
OS--任何Linux发布版
软件--iptables
网卡--2个
Here is my considerations:
Replace xx.xx.xx.xx with your WAN IP
Replace yy.yy.yy.yy with your LAN IP
(i.e. 192.168.0.0/16, 172.16.0.0/12, 10.0.0.0/8 as suggested by Mr. tzs)
WAN = eth0 with public IP xx.xx.xx.xx
LAN = eth1 with private IP yy.yy.yy.yy/ 255.255.0.0
这里是我的考虑:
用您的Wan IP替换 xx.xx.xx.xx
用您的Lan IP替换 yy.yy.yy.yy (如. 192.168.0.0/16, 172.16.0.0/12, 10.0.0.0/8 Mr. tzs建议)
WAN = eth0 (public IP xx.xx.xx.xx)
LAN = eth1 ( IP yy.yy.yy.yy/ 255.255.0.0)
Ps:若是TC (Tiny Core Linux ) 做无线路由,则是
WAN = ppp0
LAN = wlan0
Step by Step Procedure
Step #1. Add 2 Network cards to the Linux box
Step #2. Verify the Network cards, Wether they installed properly or not
ls /etc/sysconfig/network-scripts/ifcfg-eth* | wc -l
( The output should be "2")
Step #3. Configure eth0 for Internet with a Public ( IP External network or Internet)
步骤过程:
1、把2个网卡加入Linux盒子
2、检查网卡是否安装正确,命令:
ls /etc/sysconfig/network-scripts/ifcfg-eth* | wc -l
( 输出应该是 "2")
3、把公有IP配置给eth0(外网或内网)
cat /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
BOOTPROTO=none
BROADCAST=xx.xx.xx.255 # Optional Entry
HWADDR=00:50:BA:88:72:D4 # Optional Entry
IPADDR=xx.xx.xx.xx
NETMASK=255.255.255.0 # Provided by the ISP
NETWORK=xx.xx.xx.0 # Optional
ONBOOT=yes
TYPE=Ethernet
USERCTL=no
IPV6INIT=no
PEERDNS=yes
GATEWAY=xx.xx.xx.1 # Provided by the ISP
Ps:若是TC,用rp-pppoe拨号,不需做这一步; TC的网卡配置在/opt下,如eth0.sh
Step #4. Configure eth1 for LAN with a Private IP (Internal private network)
4、把私有IP配置到eth1(内部私有网络)
cat /etc/sysconfig/network-scripts/ifcfg-eth1
BOOTPROTO=none
PEERDNS=yes
HWADDR=00:50:8B:CF:9C:05 # Optional
TYPE=Ethernet
IPV6INIT=no
DEVICE=eth1
NETMASK=255.255.0.0 # Specify based on your requirement
BROADCAST=""
IPADDR=192.168.2.1 # Gateway of the LAN
NETWORK=192.168.0.0 # Optional
USERCTL=no
ONBOOT=yes
Ps:若是TC,配置在/opt下,如wlan0.sh
Step #5. Host Configuration (Optional)
5、主机配置(可选)
cat /etc/hosts
127.0.0.1 nat localhost.localdomain localhost
Step #6. Gateway Configuration
6、网关配置
Ps:TC没有这一步,它没有这个文件(我没找到?)
cat /etc/sysconfig/network
NETWORKING=yes
HOSTNAME=nat
GATEWAY=xx.xx.xx.1 # Internet Gateway, provided by the ISP
Step #7. DNS Configuration
7、DNS配置
cat /etc/resolv.conf
nameserver 203.145.184.13 # Primary DNS Server provided by the ISP
nameserver 202.56.250.5 # Secondary DNS Server provided by the ISP
Step #8. NAT configuration with IP Tables
8、使用IP Tables配置NAT
# Delete and flush. Default table is "filter". Others like "nat" must be explicitly stated.
#删除并擦净。缺省表是"filter". 其他的像"nat"必须显式说明
(Ps:以下命令,在编辑器中输入,并保存为i.sh,Chomod修改属性为775)
iptables --flush # Flush all the rules in filter and nat tables
iptables --table nat --flush
iptables --delete-chain
# Delete all chains that are not in default filter and nat table
iptables --table nat --delete-chain
# Set up IP FORWARDing and Masquerading
iptables --table nat --append POSTROUTING --out-interface eth0 -j MASQUERADE
Ps:TC无线路由, 将eth0更换为ppp0
iptables --append FORWARD --in-interface eth1 -j ACCEPT
Ps:TC无线路由, 将eth1更换为Wlan0
# Enables packet forwarding by kernel
echo 1 > /proc/sys/net/ipv4/ip_forward
#Apply the configuration
service iptables restart
Ps:TC没有这个服务,直接执行sh
Step #9. Testing
9、测试
# Ping the Gateway of the network from client system
#从客户机Ping网络的网关
ping 192.168.2.1
Try it on your client systems
在客户机尝试
ping google.com
Configuring PCs on the network (Clients)
• All PC's on the private office network should set their "gateway" to be the local private network IP address of the Linux gateway computer.
• The DNS should be set to that of the ISP on the internet.
Windows '95, 2000, XP, Configuration:
• Select "Start" + Settings" + "Control Panel"
• Select the "Network" icon
• Select the tab "Configuration" and double click the component "TCP/IP" for the ethernet card. (NOT the TCP/IP -> Dial-Up Adapter)
• Select the tabs:
o "Gateway": Use the internal network IP address of the Linux box. (192.168.2.1)
o "DNS Configuration": Use the IP addresses of the ISP Domain Name Servers. (Actual internet IP address)
o "IP Address": The IP address (192.168.XXX.XXX - static) and netmask (typically 255.255.0.0 for a small local office network) of the PC can also be set here.
配置网络上的PC(客户端)
。开始---〉控制面板
。选 网络 图标
。选择 配置 ,双击以太网卡组件 “TCP/IP”。(不是TCP/IP-〉拨号适配器)
。选择标签:
o 网关, 使用Linux盒子的内网IP(如 192.168.2.1)
o DNS 配置, 使用ISP的域名服务器(可以使用网关,如使用dnsmasq;或者使用google的8.8.8.8)
o IP 地址,设置IP地址和掩码,如192.168.XXX.XXX/255.255.255.0)
注:
TC系统,使用1个以太网接口,接ADSL猫,1个无线网卡通过Hostapd对外做AP服务,只需:
cat eth0.sh
pkill udhcpc
ifconfig eth0 up
cat wlan0.sh
pkill udhcpc
ifconfig wlan0 192.168.2.1 netmask 255.255.255.0 broadcast 192.168.2.255 up
eth0做为Wan口,是不用配置IP,也不需要在它上面做Dchp,以免route混乱。
成品无线路由,只有1个IP,从无线可以访问它。从4个Lan也可以,是因为4个Lan是通过交换芯片的。