首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >一分钟上手Rinetd——端口转发工具 原

一分钟上手Rinetd——端口转发工具 原

作者头像
wuweixiang
发布2018-12-24 10:19:19
1.4K0
发布2018-12-24 10:19:19
举报
文章被收录于专栏:吴伟祥吴伟祥

前言

iptables 的功能当然强大,但理解与设置却有点抽象,便通过google认识了rinetd。

简介

Rinetd是为在一个Unix和Linux操作系统中为重定向传输控制协议(TCP)连接的一个工具。Rinetd是单一过程的服务器,它处理任何数量的连接到在配置文件etc/rinetd中指定的地址/端口对。尽管rinetd使用非闭锁I/O运行作为一个单一过程,它可能重定向很多连接而不对这台机器增加额外的负担。

Rinetd官网  https://boutell.com/rinetd/

安装

cat >> rinetd-installer.sh <<'EOF'
#!/bin/bash
wget http://www.boutell.com/rinetd/http/rinetd.tar.gz
tar zxvf rinetd.tar.gz
cd rinetd
mkdir -p /usr/man/man8
# make编译提示:make cc Command not found 解决办法 -> yum安装gcc
yum install gcc
make && make install
EOF
chmod +x rinetd-installer.sh
./rinetd-installer.sh

配置

配置端口转发的配置文件在/etc/rinetd.conf

配置文件格式

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

在每一单独的行中指定每个要转发的端口。源地址和目的地址都可以是主机名或IP地址,IP 地址0.0.0.0将rinetd绑定到任何可用的本地IP地址上。例如:0.0.0.0 8080 wuweixiang.cn 80

rm -f /etc/rinetd.conf
cat >> /etc/rinetd.conf <<EOF
# 设置允许访问的ip地址信息
# allow 192.168.2.*

# 设置拒绝访问的ip地址信息
# deny 192.168.1.*

# 设置日志文件路径
logfile /var/log/rinetd.log

# 例子: 将本机 8080 端口重定向至 188.131.152.100 的 8080 端口
# 0.0.0.0 8090 188.131.152.100 8080
EOF

创建启动脚本

cat >> /etc/init.d/rinetd <<'EOF'
#!/bin/bash

EXEC=/usr/sbin/rinetd
CONF=/etc/rinetd.conf
PID_FILE=/var/run/rinetd.pid
NAME=Rinetd
DESC="Rinetd Server"

case "$1" in
    start)
        if [ -x "$PID_FILE" ]; then
            echo "$NAME is running ..."
            exit 0
        fi

        $EXEC -c $CONF

        echo -e "\e[1;32m$NAME is running\e[0m"
    ;;
    stop)
        if [ -f "$PID_FILE" ]; then
            kill `cat $PID_FILE`

            while [ -x "$PID_FILE" ]
            do
                echo "Waiting for $NAME to shutdown..."  
                sleep 1
            done

            rm -f $PID_FILE
        fi

        echo -e "\e[1;31m$NAME stopped.\e[0m"
    ;;
    restart)
        $0 stop
        $0 start
    ;;
    status)
        if [ -f $PID_FILE ]; then
            echo "$NAME is running ..."
        else
            echo "$NAME stopped."
        fi
    ;;
    *)
        echo $"Usage: $0 {start|stop|restart|status}"
        exit 2
    ;;
esac

exit 0
EOF

启动服务

/etc/init.d/rinetd start

开机启动

在/etc/rc.local 文件中,添加/usr/sbin/rinetd 或者 /usr/sbin/rinetd -c /etc/rinetd.conf 启动命令即可。

需要注意

rinetd.conf中绑定的本机端口必须没有被其它程序占用

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 前言
  • 简介
  • 安装
  • 配置
    • 配置文件格式
    • 创建启动脚本
      • 启动服务
      • 开机启动
      • 需要注意
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档