使用rinetd做端口转发

  最近项目上遇到一个需要代理做多个端口转发的需求,考虑到需求只是做tcp的网络转发没有业务需求用nginx比较繁琐,就使用了rinetd,整理记录一下。

一、安装rinetd软件
1、wget http://www.boutell.com/rinetd/http/rinetd.tar.gz #下载安装文件;
2、tar zvxf rinetd.tar.gz #解压下载到的文件;
3、cd rinetd/ #进入到解压目录;
4、sed -i 's/65536/65535/g' rinetd.c #将rinetd.c文件中65536修改为65535;
5、mkdir /usr/man #创建目录;
6、make&&make install #编译安装;
7、编译安装完成后就可以使用rinetd命令了。

二、修改配置文件配置端口映射的规则
1、修改配置文件,需要在/etc目录下创建rinetd.conf文件,并在文件中填写端口映射的规则:
配置文件映射规则格式如下
bindaddress bindport connectaddress connectport
绑定的地址 绑定的端口 连接的地址 连接的端口

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

2、配置规则举例,比如将10.10.10.10的80端口映射为20.20.20.20的90端口则规则为:
10.10.10.10 80 20.20.20.20 90

3、创建配置文件并填写规则:
vim /etc/rinetd.conf #然后输入如下规则,保存退出即可;
10.10.10.10 80 20.20.20.20 90

三、启动rinetd服务:
rinetd -c /etc/rinetd.conf

四、配置开机启动
1、简单方法:
把“rinetd -c /etc/rinetd.conf”这条命令加到/etc/rc.local里面就可以开机自动运行

2、配置服务脚本:
vim /etc/init.d/rinetdServer #然后输如以下内容;

#!/bin/bash
# The next lines are for chkconfig on RedHat systems.
# chkconfig: 2345 86 10
# description: Starts and stops xxx Server 

# The next lines are for chkconfig on SuSE systems.
# /etc/init.d/rinetdServer
#
### BEGIN INIT INFO
# Provides:xxxx
# Short-Description: Starts and stops rinetd Server 
# Description: Starts and stops rinetd Server 
### END INIT INFO

rinetd_pid=`ps -ef | grep rinetd.conf | grep -v grep | awk '{print $2}'`
proc_count=`ps -ef | grep rinetd.conf | grep -v grep | wc -l`
proc_run="rinetd server is running......"
proc_stop="rinetd server is stopped!"
rinetd_conf=/etc/rinetd.conf

case $1 in 
   start)  
        rinetd -c $rinetd_conf
           ;;
    stop)
        kill -9 $rinetd_pid
           ;;
   restart) 
          kill -9 $rinetd_pid
      rinetd -c $rinetd_conf
            ;;
     status)
           [ $proc_count -eq 1 ] && echo $proc_run || echo $proc_stop
             ;;
          *) echo "$0 {start|stop|restart|status}"
             exit 4
             ;;
esac

五、增加脚本与进程检查
1、vim /opt/shell/rinetdCheck  #添加如下代码,保存;

#!/bin/bash

source /etc/profile

proc_count=`ps -ef | grep rinetd.conf | grep -v grep | wc -l`
start_file=/etc/init.d/rinetdServer

[ $proc_count -ne 1 ] && $start_file start

2、crontab -e配置计划任务每5分钟运行一次脚本
/5 * /opt/shell/rinetdCheck >/dev/null 2>&1

转载于:https://blog.51cto.com/rongshu/2308789

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值