linux端口映射转发工具rinetd

linux下简单好用的工具rinetd,实现端口映射/转发/重定向
Linux下做地址NAT有很多种方法。比如haproxy、nginx的4层代理,linux自带的iptables等都能实现。haproxy、nginx就不说了,配置相对简单;iptables配置复杂,概念也比较多DNAT、SNAT、PREROUTING、POSTROUTING等等。其实,Linux下有一个叫rinetd的工具,安装简单,配置也不复杂。

1.下载安装

[root@centos-01 ~]# wget --no-check-certificate http://www.boutell.com/rinetd/http/rinetd.tar.gz  //使用“不检查证书” 加入no-check-certificate 即可成功安装
[root@centos-01 ~]# tar zxvf rinetd.tar.gz
[root@centos-01 ~]# cd rinetd && sed -i 's/65536/65535/g' rinetd.c
[root@centos-01 ~]# mkdir /usr/man
[root@centos-01 ~]# make && make install
cc -DLINUX -g   -c -o rinetd.o rinetd.c

rinetd.c:176:6: 警告:与内建函数‘log’类型冲突 [默认启用]
 void log(int i, int coSe, int result);
      ^
cc -DLINUX -g   -c -o match.o match.c
gcc rinetd.o match.o -o rinetd
[root@centos-01 ~]# install -m 700 rinetd /usr/sbin
[root@centos-01 ~]# install -m 644 rinetd.8 /usr/man/man8

2.编辑配置文件

[root@centos-01 ~]# vim /etc/rinetd.conf
0.0.0.0 8080    172.19.94.3 8080
0.0.0.0 60022  192.168.0.103 60022
0.0.0.0 80        192.168.0.10 80
logfile  /var/log/rinetd.log 

说明(0.0.0.0表示本机绑定所有可用地址)
将所有发往本机8080端口的请求转发到172.19.94.3的8080端口
将所有发往本机60022端口的请求转发到192.168.0.103的60022端口
将所有发往本机的80端口请求转发到192.168.0.10的80端口  

命令格式是:
bindaddress bindport connectaddress connectport
绑定的地址 绑定的端口 连接的地址 连接的端口

[Source Address] [Source Port] [Destination Address] [Destination Port]
源地址 源端口 目的地址 目的端口

3.启动与关闭程序

[root@centos-01 ~]# rinetd -c /etc/rinetd.conf
[root@centos-01 ~]# pkill rinetd  

4.校验

[root@centos-01 ~]# netstat -tanulp|grep rinetd
tcp        0      0 0.0.0.0:8080                0.0.0.0:*                   LISTEN      27683/rinetd  
tcp        0      0 0.0.0.0:60022               0.0.0.0:*                   LISTEN      27683/rinetd  
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      27683/rinetd 

需要注意:
1.rinetd.conf中绑定的本机端口必须没有被其它程序占用
2.运行rinetd的系统防火墙应该打开绑定的本机端口

因为我们的跳板机都是转发的数据库等其他的端口,运行了一段时间后发现rinetd服务就挂了,没办法就执行写个脚本检查pid号,如果挂了就重启。

[root@centos-01 ~]# vim /data/scripts/rinetd_check.sh
#!/bin/bash
#rinetd pid check

pid=`ps -ef |grep [r]inetd`

if [ $? -ne 0 ]
then
  rinetd -c /etc/rinetd.conf
  echo "Interrupt restart time:" `date`
else
  echo rinetd pid is running...
  echo date is : `date`
fi

#加入定时任务(每两个小时监测一次)
[root@centos-01 ~]# crontab -l
0 */2 * * * sh /data/scripts/rinetd_check.sh >> /data/logs/rinetd.log 2>&1

猜你喜欢

转载自blog.51cto.com/13696145/2467864