温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

linux端口映射转发工具rinetd

发布时间:2020-03-10 01:05:42 来源:网络 阅读:516 作者:wx5acc326ba509b 栏目:系统运维

linux下简单好用的工具rinetd,实现端口映射/转发/重定向
Linux下做地址NAT有很多种方法。比如haproxy、nginx的4层代理,linux自带的iptables等都能实现。haproxy、nginx就不说了,配置相对简单;iptables配置复杂,概念也比较多DNAT、SNAT、PREROUTING、POSTROUTING等等。其实,Linux下有一个叫rinetd的工具,安装简单,配置也不复杂。

1.下载安装

[root@centos-01 ~]# wget --no-check-certificate http://www.boutell.com/rinetd/http/rinetd.tar.gz //使用“不检查证书” 加入no-check-certificate 即可成功安装 [root@centos-01 ~]# tar zxvf rinetd.tar.gz [root@centos-01 ~]# cd rinetd && sed -i 's/65536/65535/g' rinetd.c [root@centos-01 ~]# mkdir /usr/man [root@centos-01 ~]# make && make install cc -DLINUX -g -c -o rinetd.o rinetd.c rinetd.c:176:6: 警告:与内建函数‘log’类型冲突 [默认启用] void log(int i, int coSe, int result); ^ cc -DLINUX -g -c -o match.o match.c gcc rinetd.o match.o -o rinetd [root@centos-01 ~]# install -m 700 rinetd /usr/sbin [root@centos-01 ~]# install -m 644 rinetd.8 /usr/man/man8

2.编辑配置文件

[root@centos-01 ~]# vim /etc/rinetd.conf 0.0.0.0 8080 172.19.94.3 8080 0.0.0.0 60022 192.168.0.103 60022 0.0.0.0 80 192.168.0.10 80 logfile /var/log/rinetd.log 说明(0.0.0.0表示本机绑定所有可用地址) 将所有发往本机8080端口的请求转发到172.19.94.3的8080端口 将所有发往本机60022端口的请求转发到192.168.0.103的60022端口 将所有发往本机的80端口请求转发到192.168.0.10的80端口  

命令格式是:
bindaddress bindport connectaddress connectport
绑定的地址 绑定的端口 连接的地址 连接的端口

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

3.启动与关闭程序

[root@centos-01 ~]# rinetd -c /etc/rinetd.conf [root@centos-01 ~]# pkill rinetd 

4.校验

[root@centos-01 ~]# netstat -tanulp|grep rinetd tcp 0 0 0.0.0.0:8080 0.0.0.0:* LISTEN 27683/rinetd tcp 0 0 0.0.0.0:60022 0.0.0.0:* LISTEN 27683/rinetd tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 27683/rinetd 

需要注意:
1.rinetd.conf中绑定的本机端口必须没有被其它程序占用
2.运行rinetd的系统防火墙应该打开绑定的本机端口

因为我们的跳板机都是转发的数据库等其他的端口,运行了一段时间后发现rinetd服务就挂了,没办法就执行写个脚本检查pid号,如果挂了就重启。

[root@centos-01 ~]# vim /data/scripts/rinetd_check.sh #!/bin/bash #rinetd pid check pid=`ps -ef |grep [r]inetd` if [ $? -ne 0 ] then rinetd -c /etc/rinetd.conf echo "Interrupt restart time:" `date` else echo rinetd pid is running... echo date is : `date` fi #加入定时任务(每两个小时监测一次) [root@centos-01 ~]# crontab -l 0 */2 * * * sh /data/scripts/rinetd_check.sh >> /data/logs/rinetd.log 2>&1
向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI