rhel 5的nat功能配置

来源:百度文库 编辑:神马文学网 时间:2024/05/01 13:18:22
linux装系统设IP,这应该是系统管理员的基本功,可是不同的网络结构有不同的ip设法,您知道吗?
1.一块网卡的情况
这个没啥好说的,估计地球人都知道:address,netmask,gateway然后netwok restart,搞定.
2.两块网卡的情况
1).一个网关的情况
这种情况也简单,一个网卡设一个ip,其中一个网卡不设置gateway就ok了.这样上网的时候走带网关的那边,这台机器还能和不带网关的那个网络通讯.这种情况在利用linux当路由器或者代理网关的时候比较常见.
这个可是给了我很大的帮助啊,我现在想用一台redhat作为路由,提供nat服务,始终搞不定两个网卡的网关冲突问题,现在终于知道了,那就是我只要把外网的那张网卡设置有网关,内网那张不设置,这样内网网卡就可以使用外网的网关,并且还能和外网网卡通信了。
当然,前提是
  1、先注释/etc/sysconfig/network下: (这步可要,可不要!)  

   NETWORKING=yes

   HOSTNAME=linuxman

   #GATEWAY=X.X.X.X
把这个gateway给注释掉,这个是全局的。
然后可以为
2).两个网关的情况
这种情况相对复杂,复杂到两块网卡都不设置默认网关:)这种情况发生在,这台linux连接的两个网段都不是一个网段!就是通过连接的两个网段还可以访问其它的不同的网段.这种情况下,无论把gateway设到哪边,都会影响到另一个网段所连接的网段不能正常使用.这就是我今天最想表述的问题.在这种情况,如果你想连接多个网段,首先要在正确的网卡上设置正确的ip,剩下的工作就交给route来做了.利用route命令把能上网或者想通过那边上网的网关设置成默认网关,这样就解决了一个网段了.另一个段和它所连接的所有网段,就要一条一个的加路由了.示例
默认网关:
route add default gw 224.224.224.224 eth0
加路由:
route add -net 192.168.115.0/24 gw 192.168.1.254 eth1
没有学习过路由知识的同学可能不大理解加路由为啥去115段的连接,要走1.254.这是因为,加路由的时候,指定下一跳,只指定和本机连接的那个网关:)
3).三个网卡的情况
和两个卡类似.如果你的需求是,一个网段通过两个不同的网络上网,中间又夹着这么一个linux的话.个人觉得除了用iptables根据不同的ip地址划分不同的上网网络,似乎没有其它的解决办法:)
发散思维一下:知其然还要知其所以然嘛.为啥两个网卡一个网关的时候,只设置一个网卡有网关呢?因为linux会自动帮你设置路由!它会把你设置的网关设置成默认路由.这时候如果你设置两个网关.linux帮你随机选的默认路由可能是不能上网的那个或者说不是你想要的那个哟~再拓展一下,其实linux就是一个完整的路由器!只是它很低调默认取消的路由功能罢了.如果你正在学习路由方面的相关知识,没有那么多路由器,来做一些高级实验,像ospf,pgp等,不妨多装几个linux你会有意外的惊喜.写到这里在觉得linux很黄很暴力的同时,突然想展示它更多的内容.但是不想跑题,就留给下一篇吧:实战linux路由,我的一次客户现场经历!

我把我自己的配置nat的经验给总结一下吧:
一台机器安装rhel5,有两块网卡,eth0和eth1,但是eth0直接连到外网,eth1连到局域网,局域网内有个网关提供dhcp服务,但是我不想用这个网关提供的上网服务。我想用nat服务,就是局域网内的其它机器可以通过eth1作为跳板,然后通过eth0来上网。首先把eth1设置成静态ip,不要dhcp获取,因为这样就可以避免dhcp服务器把eth1的网关设置成dhcp服务器的了。静态设置ip的时候,不要设置内网的网关。然后外网的那个网卡的配置随便,dhcp获取地址或者静态ip都可以,但一定要设置网关。

如果eth0为192.168.10.123/255.255.255.0,eth1为192.168.20.231/255.255.255.0,则命令格式如下:

  #route add -net 192.168.10.0 netmask 255.255.255.0 dev eth0

  #route add -net 192.168.20.0 netmask 255.255.255.0 dev eth1

  上面的命令把发送给192.168.10.0网段的IP包交给eth0转发,把192.168.20.0网段的IP包交给eth1转发。如果还有可能有发送给其他目的IP的包,那么你肯能希望设置一个“默认网关”:

  #route add default gw 192.168.10.1

 上面的命令把所有发送给其他目的IP的包都转发给192.168.10.1,而如何转发给192.168.10.1这个地址的规则已经在刚才的第一条命令中定义了(从eth0转发)。一般情况下,默认网关已经自动设置好了,不用重复设置。可以用route命令加-n参数进行检查。

  如果要删除某一条,命令格式为:

  #route del -net 192.168.10.0 netmask 255.255.255.0

  配置时的一种思路是把192.168.10.0网段路由至eth0,192.168.20.0网段路由至eth1,再设置默认路由。另一种思路是,只指定其中一个,然后把默认的0.0.0.0路由至另一个。其实效果一样,就是两种风格。

这一段也很经典,可以好好研读。

确定使用多张网卡中的一张上网- -

                                      

对于两张网卡的问题,网上基本都是再说,一台机器两张网卡:其中一张链接内网,一张链接外网,该如何指定路由表。没有提到过如何在一台有两张网卡的机器上,如何使用其中一张网卡上网的方法。


问题的原因,是我在一台有两张网卡的服务器上操作:每张网卡各对应一个独立ip和一个webserver,一个ip A链接着国际网,一个ipB只链接着国内网。所以当在服务器上需要链接到国外网络的时候就狠不方便。因为似乎是B地址小于A的缘故,总是默认使用B去上网。

现在说明解决的办法:
  1. 使用arp -a查询两张网卡所对应的interface值
  2. 使用route -p add 0.0.0.0 mask 0.0.0.0 (网关ip) if (A的interface值),设置默认的路由路径
这样就解决问题了。


You might also want to check that IP forwarding is enabled in /etc/sysctl.conf by executing

sysctl -w net.ipv4.ip_forward=1
当然,这一步也可以直接编辑/etc/sysctl.conf这个文件,直接修改也是可以的。
用上面那个命令也是可以直接写进去的。

要使用nat功能,这个命令是至少要执行的。
iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE


Quick-Tip: Linux NAT in Four Steps using iptables

By Frank Wiles

In everyone's life a little rain must fall. My main Linux workstation at homesuffered a hard drive failure the day after Christmas. I can only guess Iwas bad last year and Santa turned my hard drive into a lump of coal aspunishment.

Unfortunately, at some point over the year I introduced a typoin a script I used to backup my personal website and some other data onthat particular computer. Along with some data that wasn't very important,I lost my handy little script I used to setup iptables to NAT myinternal network to the Internet at large.

After an hour of not being able to find a quick and easy tutorial on how todo this seemingly basic task on Google, I promised myself I would writethis Quick-Tip.

If you are running a recent 2.6 Linux Kernel this four step processshould work for you. This has been specifically tested on Fedora Core 3, 4,5, and 6, but should work on any modern Linux distribution.All of these commands must be executed as the root user.First you need to tell your kernel that you want to allow IP forwarding.

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

Then you'll need to configure iptables to forward the packets fromyour internal network, on /dev/eth1, to your external network on /dev/eth0.You do this will the following commands:

/sbin/iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
/sbin/iptables -A FORWARD -i eth0 -o eth1 -m state --state RELATED,ESTABLISHED -j ACCEPT
/sbin/iptables -A FORWARD -i eth1 -o eth0 -j ACCEPT

You should now be NATing. You can test this by pinging an external addressfrom one of your internal hosts. The last step is to ensure that thissetup survives over a reboot. Obviously you should only do these last twosteps if your test is a success.

You will need to edit /etc/sysctl.conf and change the line that saysnet.ipv4.ip_forward = 0 tonet.ipv4.ip_forward = 1. Notice how this is similar tostep number one? This essentially tells your kernel to do step one on boot.

Ok last step for Fedora/RHEL users. In order for your system to save theiptables rules we setup in step two you have to configure iptablescorrectly. You will need to edit /etc/sysconfig/iptables-configand make sure IPTABLES_MODULES_UNLOAD,IPTABLES_SAVE_ON_STOP, andIPTABLES_SAVE_ON_RESTART are all set to 'yes'.

For non-Fedora/RHEL users you can simply setup an init script for this orsimply append these commands to the existing rc.local script so they areexecuted on boot. Or if you want to get even more fancy, you can use thecommands iptables-save and iptables-restore to save/restore the current stateof your iptables rules.

After all that is done, you should probably do a test reboot to ensurethat you've done everything correctly. If you find any errors on this pageor this does not work for you please feel free to E-mail me directlyat frank@revsys.com.