Linux中使用rinetd实现快速端口转发

Linux中一个很好用的NAT工具—rinetd,它是由C实现的一个短小、实用的NTA转发工具,它的官网是在:http://www.boutell.com/rinetd/

  [root@localhost src] # wget http://www.boutell.com/rinetd/http/rinetd.tar.gz
  [root@localhost src] # tar -zxvf rinetd.tar.gz
  [root@localhost src] # cd rinetd
  [root@localhost rinetd] # vim Makefile#修改下Makefile
  CFLAGS=-DLINUX -g
  
rinetd: rinetd.o match.o
        gcc rinetd.o match.o -o rinetd
  
install : rinetd
        install  -m 700 rinetd  /usr/sbin
        install  -m 644 rinetd.8  /usr/local/share/man/man8
  [root@localhost rinetd] # make && make install

这样rinetd就安装好了,使用也很简单可以指定配置文件,一般是放在/etc/rinetd.conf使用-c参数指定配置文件,rinetd是依赖于配置文件工作的

[root@localhost rinetd] # rinetd --help
Usage: rinetd [OPTION]
  -c, --conf- file  FILE    read  configuration from FILE
  -h, --help             display this help
  - v , --version          display version number
  
Most options are controlled through the
configuration  file . See the rinetd(8)
manpage  for  more  information.

更多的参数选项可以参看man rinetd在此就不做过多的说明,利用rinetd可以实现快速高效的端口转发,举一个简单的例子,在三层交换机中的2个vlan:192.168.1.0/24、192.168.2.0/24,2个vlan间内网是互通的但是192.168.1.0/24中没有做任何的策略路由,只有内网而192.168.1.0/24在外有一个公网ip做了内部服务器的NAT端口映射,即DZM区,而在外部需要访问192.168.2.0/24内部的某一主机上的相关资源,此时就需要做相应的端口转发,而在2个vlan中分别有192.168.2.22和192.168.1.240,2台主机用rinetd来转发,就在192.168.2.22的主机上的/etc/rinetd.conf文件中添加一行配置:

0.0.0.0 80 192.168.1.240 80
#source_address source_port destination_address destination_port

当然利用iptables的nat表转发也是一样,nat表如下:

*nat
:PREROUTING ACCEPT [0:0]
:POSTROUTING ACCEPT [3:226]
:OUTPUT ACCEPT [3:226]
-A PREROUTING -d 192.168.2.22 /32  -p tcp -m tcp --dport 80 -j DNAT --to-destination 192.168.1.240:80 
-A POSTROUTING -d 192.168.1.240 /32  -p tcp -m tcp --dport 80 -j SNAT --to- source  192.168.2.22 
COMMIT

二者之间是等效的,如此可见rinetd的工具在实际生产环境中是非常高效的,这里要注意两点:一、无论是使用rinetd或者是iptables的nat表都需要开启内核ip地址转发的功能,即net.ipv4.ip_forward = 1;二、是在filter表中开相应的端口,如果是使用iptables的nat表来转发还要开放FORWARD链以实现转发

猜你喜欢

转载自www.linuxidc.com/Linux/2017-05/144035.htm