5

Centos 实现端口转发:rinetd 部署笔记

 2 years ago
source link: https://exp-blog.com/net/rinetd-bu-shu-bi-ji/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

虽然Linux本身自带的iptables可以实现端口转发功能,但其配置相对复杂。因此本文介绍另一个端口转发工具rinetd,其安装和配置都更为简单。

rinetd部署环境

本文是基于Centos7系统部署rinetd端口转发工具。

rinetd安装

官网下载最新版(v0.62),得到安装包rinetd.tar.gz :

注:rinetd 后来迁移到 Github 了, Github 的版本与本文叙述的安装方式是不同的,其安装方法可参考这篇文章

上传到Linux,本文上传位置为:

/usr/local/

解压安装包:

tar -zxvf rinetd.tar.gz

由于rinetd需要编译安装,先安装gcc编译环境:

yum install gcc gcc-c++

若安装 gcc 失败,则需要先替换源并更新

wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
sed -i ‘s/$releasever/7/g’ /etc/yum.repos.d/CentOS-Base.repo
yum repolist

进入rinetd安装目录:

cd /usr/local/rinetd

检查安装配置文件:

vi Makefile

注意配置文件中涉及到两处安装路径,一般情况下保持默认值即可:

CFLAGS=-DLINUX -g

rinetd: rinetd.o match.o
        gcc rinetd.o match.o -o rinetd

install: rinetd
        install -m 700 rinetd /usr/sbin
        install -m 644 rinetd.8 /usr/man/man8

但是若 /usr/man/man8 目录不存在,需要先手建:

mkdir -p /usr/man/man8

如果你使用的是 Github 上的最新版本,这里还需要多执行两条命令去创建 makefile 文件:

./bootstrap
./configure

编译并安装:

make && make install

至此rinetd安装完成。

rinetd配置

配置端口转发规则(该文件可能不存在,直接创建即可):

vi /etc/rinetd.conf

该文件每行一个转发规则,配置格式为:

[source_address] [source_port] [destination_address] [destination_port]

[本机IP(若非多网卡直接设为0.0.0.0)] [转发端口] [服务IP] [服务端口]

0.0.0.0 9527 192.168.64.22 9527


官方完整的 rinetd.conf 配置文件模板参考如下:

#
# this is the configuration file for rinetd, the internet redirection server
#
# you may specify global allow and deny rules here
# only ip addresses are matched, hostnames cannot be specified here
# the wildcards you may use are * and ?
#
# allow 192.168.2.*
# deny 192.168.2.1?


#
# forwarding rules come here
#
# you may specify allow and deny rules after a specific forwarding rule
# to apply to only that forwarding rule
#
# bindadress  bindport  connectaddress  connectport  options...
# 0.0.0.0     80        192.168.1.2     80
# 127.0.0.1   4000      127.0.0.1       3000
# 127.0.0.1   4000/udp  127.0.0.1       22           [timeout=1200]
# 127.0.0.1   8000/udp  192.168.1.2     8000/udp     [src=192.168.1.2,timeout=1200]

# logging information
# logfile /var/log/rinetd.log

# uncomment the following line if you want web-server style logfile format
# logcommon

注:非必要不建议启用日志选项,会消耗大量磁盘空间

rinetd使用

默认情况下,rinetd的启动需要指定规则配置文件,而停止需要杀掉进程:

启动:rinetd -c /etc/rinetd.conf
停止:killall rinetd

查看端口转发状态:

netstat -tanulp|grep rinetd


为了便于使用,可以把rinetd托管到systemd

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

复制以下内容到 rinetd.service 文件内并保存:

[Unit]
Description=rinetd
After=network.target

[Service]
Type=forking
ExecStart=/usr/sbin/rinetd -c /etc/rinetd.conf

[Install]
WantedBy=multi-user.target

重载daemon使其生效:systemctl daemon-reload

尔后就可以通过systemd管理rinetd了:

# 启动
systemctl start rinetd

# 停止
systemctl stop rinetd

# 重启
systemctl restart rinetd

# 设置开机自启
systemctl enable rinetd

# 取消开机自启
systemctl disable rinetd

本文全文下载


Recommend

  • 46
    • www.freebuf.com 6 years ago
    • Cache

    内网渗透之端口转发与代理

    内网渗透之端口转发与代理

  • 29
    • www.tuicool.com 5 years ago
    • Cache

    SSH 端口转发教程

    SSH 端口转发教程 对于开发者来说,ssh 是一个必备的工具,用来登录服务器。 但是,它还有一大用途,就是作为加密通信的中介,充当两台服务器之间的通信加密跳板。这个功能称为端口转发(port forwarding),又称 ssh...

  • 52
    • www.tuicool.com 4 years ago
    • Cache

    charles 端口转发

    本文参考: charles 端口转发 端口转发 端口转发(Port forwarding),有时被叫做隧道,是安全壳(SSH) 为网络安全通信使用的一种方...

  • 46
    • www.tuicool.com 4 years ago
    • Cache

    ssh端口转发的三种方式

    ssh是我使用最频繁的两个命令行工具之一(另一个则必须是vim)。有了ssh,我可以远程处理各种可能出现的问题而无需肉身到现场。 这几天teamviewer被黑的事情影响挺大,于是由远程控制想到了内网穿透,自然而然的想到了ssh的端口转...

  • 34
    • www.tlanyan.me 4 years ago
    • Cache

    ssh端口转发的三种方式 - tlanyan

    ssh端口转发的三种方式ssh是我使用最频繁的两个命令行工具之一(另一个则必须是vim)。有了ssh,我可以远程处理各种可能出现的问题而无需肉身到现场。这几天teamviewer被黑的事情影响挺大,于是由远程控制想到了内网穿透,自...

  • 8
    • blog.star7th.com 3 years ago
    • Cache

    用node实现服务器端口转发

    用node实现服务器端口转发 我们很多场景需要端口转发,如内网穿透,服务器之间的IP隐藏,网络加速,甚至“科学用网”等等。...

  • 6
    • www.itpwd.com 2 years ago
    • Cache

    CentOS 6.10修改SSH端口笔记

    CentOS 6.10修改SSH端口笔记_Linux笔记_IT密码[root@localhost ~]# vim /etc/ssh/sshd_config #Port 22 Port 18022 #新增ssh端口 [root@localhost ~]# service sshd restart [root@localhost ~]# [root@localhost ~]# vi /etc/sysconfig/iptables -A...

  • 3
    • www.jansora.com 2 years ago
    • Cache

    rinetd 端口转发

    rinetd 端口转发闲文杂记rinetd 端口转发 导航前往主站应用

  • 4
    • holydogs.github.io 1 year ago
    • Cache

    【centos】防火墙做端口转发

    top查看内存(可以free -m)、cpu(多核按1)占用情况 防火墙可以直接做端口转发查看状态 systemctl status firewalld.service 启动防火墙 systemctl start firewalld.service 开启转发 firewall-cmd --add-for...

  • 4

    【笔记】OpenWRT防火墙端口转发 2023-04-05 ...

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK