前言
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中绑定的本机端口必须没有被其它程序占用

转自:https://my.oschina.net/wuweixiang/blog/2983280
作者:吴伟祥

Last modification:March 27th, 2021 at 12:42 pm
如果觉得我的文章对你有用,请随意赞赏