版权说明:本文系博主通过各种渠道学习整理发表或者全文转载自其他平台,作为学习笔记,不能保证所有知识点是完全正确以及表达无误,用于生产环境配置时请斟酌。如有错误或建议请联系。转载请注明出处,侵删联系:linuxops@foxmail.com。感谢各位!
一、前言
有些服务为了安全仅供内网访问,比如在阿里云中redis仅提供了内网的地址,在开发过程中我们需要查看redis中的内容,这时候就无法在开发机上直接访问,rinetd解决了这个问题,它将redis端口映射出来,通过转发来实现访问redis。
在早期的版本中,rinetd只支持TCP协议,最新版本已经支持UDP协议了。
二、安装
下载 rinetd
源码地址:Github地址
克隆到服务器即可。
[root@Rinetd ~]# git clone https://github.com/samhocevar/rinetd.git
Cloning into 'rinetd'...
remote: Counting objects: 420, done.
remote: Total 420 (delta 0), reused 0 (delta 0), pack-reused 420
Receiving objects: 100% (420/420), 169.19 KiB | 145.00 KiB/s, done.
Resolving deltas: 100% (272/272), done.
安装
进入到rinetd文件夹,编译安装即可。
我们需要先运行 bootstrap
来创建配置文件,运行bootstrap
需要依赖automake
,请自行解决依赖。然后再执行编译安装等步骤。
[root@Rinetd rinetd]# ./bootstrap
+ test no = yes
+ aclocal -I .auto -I .
+ autoconf
+ test yes = yes
+ autoheader
+ test yes = yes
+ automake --foreign --add-missing --copy
configure.ac:7: installing '.auto/install-sh'
configure.ac:7: installing '.auto/missing'
src/Makefile.am: installing '.auto/depcomp'
+ rm -Rf autom4te.cache
[root@Rinetd rinetd]# ./configure --prefix=/usr/local/rinetd
.
.
.
[root@Rinetd rinetd]# make
.
.
.
[root@Rinetd rinetd]# make install
我们将rinetd安装在了/usr/local/rinetd目录中,看一下这个目录有什么东西
[root@Rinetd rinetd]# ls -la /usr/local/rinetd/
total 20
drwxr-xr-x 5 root root 4096 Jul 5 17:20 .
drwxr-xr-x. 15 root root 4096 Jul 5 17:20 ..
drwxr-xr-x 2 root root 4096 Jul 5 17:20 etc
drwxr-xr-x 2 root root 4096 Jul 5 17:20 sbin
drwxr-xr-x 3 root root 4096 Jul 5 17:20 share
[root@Rinetd rinetd]#
如上,看文件夹的名称大概也就知道每个文件是干什么用的,显然etc
目录是存放配置文件。sbin
目录存放命令,为了方便使用,也可以将命令加入到系统变量中。
三、配置
rinetd的配置及其简单,我们来看一下官方的示例配置文件,在示例配置文件中已经说明很清楚了。
[root@Rinetd etc]# cat 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提供了一个黑白名单,通过allow
和deny
可以限制或者允许某些IP的访问,IP值可以使用通配符,这极大方便了我们的使用,不需要写一串非常长的IP了。
配置端口转发只要按照按照[Source Address] [Source Port] [Destination Address] [Destination Port]
格式配置即可。
在端口的配置上也可以指定使用的协议
Destination Address
配置不仅仅可以使用IP,也可以使用域名或者主机名。
logfile
指定了日志文件,rinetd会记录每一次转发的日志
要确保监听的端口没有被使用
四、启动rinetd
你只需要通过运行 /usr/local/rinetd/sbin/rinetd -c /usr/local/rinetd/etc/rinetd.conf
即可启动。
你可以通过kill
命令杀死rinetd进程,然后再一次启动来实现重启的效果,你也可以托管到systemd中,通过systemctl来实现重启,有关于这部分请查看《systemd.service - 服务单元配置》一文
五、一些注意事项
rinetd配置了日志文件以后,会占用大量的磁盘空间,因为每一次转发rinetd都会记录到日志文件中。在我们实际的使用中,比如阿里云的redis,因为redis量巨大,RDM客户端工具每一次都需要遍历库中的信息,所以会导致rinetd日志巨大,可以不开启日志,或者见日志重定向到/dev/null
即可
在生产环境中,非常不建议是用rinetd,生产环境中应该要严格控制访问的权限,使用rinetd不仅仅打破了权限控制,还有可能出现其他的问题,比如rinetd并没有使用epoll,而是用了select,这将导致CPU的大量消耗,甚至导致业务异常。
rinetd在开发过程中是一个是一个好帮手,但是,再一次强调:非常非常不建议使用在生产环境中。