Linux 下使用 rinetd 进行端口转发

一、什么是 rinetd

rinetd 是一个轻量级的 TCP 端口转发工具,它可以将发往本机特定端口的请求转发到另一台服务器的指定端口。它是一个简单但强大的工具,适用于需要端口转发的各种场景,如:

  • 数据库远程访问
  • 内部服务暴露到公网
  • 负载均衡
  • 网络测试

二、安装 rinetd

1. 从源码编译安装

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 下载源码
wget http://www.boutell.com/rinetd/http/rinetd.tar.gz

# 解压并进入目录
tar -xvf rinetd.tar.gz
cd rinetd

# 修改端口范围(可选)
sed -i 's/65536/65535/g' rinetd.c

# 创建 man 目录并安装
mkdir -p /usr/man
make
make install

2. 包管理器安装

某些 Linux 发行版提供了 rinetd 的包管理安装方式:

1
2
3
4
5
# Ubuntu/Debian
apt-get install rinetd

# CentOS/RHEL
yum install rinetd

三、配置 rinetd

rinetd 的配置文件为 /etc/rinetd.conf,格式如下:

1
2
3
4
5
6
7
8
9
# 绑定地址  绑定端口  目标地址  目标端口
bindaddress bindport connectaddress connectport

# 日志文件配置
logfile /var/log/rinetd.log

# 允许/拒绝规则(可选)
allow 192.168.1.*
deny 10.0.0.1

配置示例

  1. 基本端口转发

    1
    2
    # 将本机 8080 端口的请求转发到 192.168.1.100 的 80 端口
    0.0.0.0 8080 192.168.1.100 80
  2. 多端口转发

    1
    2
    3
    4
    5
    6
    # HTTP 转发
    0.0.0.0 80 192.168.1.100 80
    # SSH 转发
    0.0.0.0 2222 192.168.1.101 22
    # MySQL 转发
    0.0.0.0 3306 192.168.1.102 3306
  3. 指定绑定地址

    1
    2
    # 只转发来自 192.168.1.1 的请求
    192.168.1.1 8080 192.168.1.100 80
  4. 使用 IPv6

    1
    2
    # IPv6 端口转发
    [::] 8080 [2001:db8::1] 80

四、启动和管理 rinetd

1. 启动 rinetd

1
2
3
4
5
# 启动 rinetd
rinetd -c /etc/rinetd.conf

# 检查是否启动成功
ps aux | grep rinetd

2. 停止 rinetd

1
2
3
4
5
# 停止 rinetd
pkill rinetd

# 或者使用 kill 命令
kill $(ps aux | grep rinetd | grep -v grep | awk '{print $2}')

3. 重启 rinetd

1
2
# 重启 rinetd
pkill rinetd && rinetd -c /etc/rinetd.conf

4. 设置开机自启

方法 1:使用 rc.local

1
2
echo "rinetd -c /etc/rinetd.conf" >> /etc/rc.local
chmod +x /etc/rc.local

方法 2:使用 systemd(推荐)

创建 systemd 服务文件 /etc/systemd/system/rinetd.service

1
2
3
4
5
6
7
8
9
10
11
12
[Unit]
Description=Rinetd Port Forwarder
After=network.target

[Service]
Type=forking
ExecStart=/usr/sbin/rinetd -c /etc/rinetd.conf
ExecStop=pkill rinetd
Restart=on-failure

[Install]
WantedBy=multi-user.target

启用并启动服务:

1
2
3
systemctl daemon-reload
systemctl enable rinetd.service
systemctl start rinetd.service

五、日志管理

rinetd 的日志默认存储在 /var/log/rinetd.log,可以通过配置文件修改:

1
logfile /var/log/rinetd.log

查看日志:

1
2
3
4
5
6
7
8
# 查看日志
cat /var/log/rinetd.log

# 实时查看日志
tail -f /var/log/rinetd.log

# 查看特定时间的日志
grep "2023-12-01" /var/log/rinetd.log

六、常见使用场景

1. 数据库远程访问

将本地数据库端口转发到公网,方便远程访问:

1
2
3
4
5
# MySQL 端口转发
0.0.0.0 3306 192.168.1.100 3306

# PostgreSQL 端口转发
0.0.0.0 5432 192.168.1.101 5432

2. Web 服务转发

将内部 Web 服务暴露到公网:

1
2
3
4
5
# HTTP 服务
0.0.0.0 80 192.168.1.102 80

# HTTPS 服务
0.0.0.0 443 192.168.1.102 443

3. SSH 访问转发

通过公网服务器访问内网机器:

1
2
# SSH 端口转发
0.0.0.0 2222 192.168.1.103 22

七、安全设置

1. 访问控制

使用 allow 和 deny 规则限制访问:

1
2
3
4
5
6
7
8
9
# 只允许特定 IP 访问
allow 192.168.1.*
allow 10.0.0.1

# 拒绝特定 IP
deny 172.16.0.1

# 转发规则
0.0.0.0 8080 192.168.1.100 80

2. 防火墙设置

配合防火墙使用,只开放需要的端口:

1
2
3
4
5
6
7
8
9
# 使用 iptables 开放 8080 端口
iptables -A INPUT -p tcp --dport 8080 -j ACCEPT

# 保存规则
iptables-save > /etc/iptables/rules.v4

# 或者使用 firewalld
firewall-cmd --add-port=8080/tcp --permanent
firewall-cmd --reload

八、故障排除

1. 端口转发不工作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 检查 rinetd 是否运行
ps aux | grep rinetd

# 检查配置文件语法
rinetd -c /etc/rinetd.conf -f # 前台运行,查看错误信息

# 检查端口是否被占用
netstat -tulpn | grep 8080

# 检查目标服务器是否可达
ping 192.168.1.100

# 检查目标端口是否开放
telnet 192.168.1.100 80

# 查看日志
cat /var/log/rinetd.log

2. 启动失败

1
2
3
4
5
6
# 检查配置文件格式
rinetd -c /etc/rinetd.conf -f

# 检查端口权限(低于 1024 的端口需要 root 权限)

# 检查目标地址是否正确

九、性能优化

  1. 限制连接数
    在 rinetd.c 中修改 MAXCONNECTIONS 宏定义,然后重新编译。

  2. 调整系统参数

    1
    2
    3
    4
    5
    6
    7
    # 增加文件描述符限制
    echo "* soft nofile 65536" >> /etc/security/limits.conf
    echo "* hard nofile 65536" >> /etc/security/limits.conf

    # 调整网络参数
    echo "net.core.somaxconn = 4096" >> /etc/sysctl.conf
    sysctl -p

十、替代方案

  1. iptables

    1
    2
    3
    # 使用 iptables 进行端口转发
    iptables -t nat -A PREROUTING -p tcp --dport 8080 -j DNAT --to-destination 192.168.1.100:80
    iptables -t nat -A POSTROUTING -d 192.168.1.100 -j MASQUERADE
  2. socat

    1
    2
    # 使用 socat 进行端口转发
    socat TCP-LISTEN:8080,fork TCP:192.168.1.100:80
  3. HAProxy
    更强大的负载均衡和端口转发工具,支持更多高级功能。

十一、实例演示

示例:阿里云数据库远程访问

  1. 安装 rinetd

    1
    2
    3
    4
    wget http://www.boutell.com/rinetd/http/rinetd.tar.gz
    tar -xvf rinetd.tar.gz
    cd rinetd
    make && make install
  2. 配置转发规则

    1
    2
    3
    4
    5
    cat > /etc/rinetd.conf << EOF
    # 绑定地址 绑定端口 目标地址 目标端口
    0.0.0.0 3306 rm-uf6xxxxxxxxxxxx.mysql.rds.aliyuncs.com 3306
    logfile /var/log/rinetd.log
    EOF
  3. 启动服务

    1
    rinetd -c /etc/rinetd.conf
  4. 测试连接

    1
    mysql -h localhost -P 3306 -u username -p

十二、总结

rinetd 是一个简单但功能强大的端口转发工具,它轻量级、配置简单,适用于各种端口转发场景。通过本文的介绍,你应该已经掌握了 rinetd 的安装、配置、使用和故障排除方法。

在使用 rinetd 时,请注意安全性,合理设置访问控制规则,并配合防火墙使用,以确保系统安全。同时,对于高流量场景,需要适当优化系统参数,以获得更好的性能。

参考资料

  1. rinetd 官方网站
  2. 阿里云帮助文档
  3. Linux 端口转发技术详解