linux跨IP端口转发iptables与rinetd

作者: Darren 分类: 收藏 发布时间: 2019-01-10 11:54

iptables

永久性配置如下:

vi /etc/sysctl.conf

# 找到下面的值并将0改成1
net.ipv4.ip_forward = 1

sysctl –p(使之立即生效)

%title插图%num

跨IP段配置端口转发

# iptables -t nat -A PREROUTING  -p tcp -d 1.1.1.1 --dport 80 -j DNAT --to-destination 2.2.2.2:8080
# iptables -t nat -A POSTROUTING -p tcp -s 2.2.2.2 --sport 8080 -j SNAT --to-source 1.1.1.1
# service iptables save

当然也可以直接写到iptables文件中:

-A PREROUTING  -p tcp -d 1.1.1.1 --dport 80 -j DNAT --to-destination 2.2.2.2:8080
-A POSTROUTING -p tcp -s 2.2.2.2 --sport 8080 -j SNAT --to-source 1.1.1.1


注:
如果你用的是一个没有开启的端口,比如小与1024的端口,假如说是800,而不是8080,那么要记着打开这些端口。

方式如下:

-A INPUT -p tcp -m state --state NEW -m tcp --dport 800 -j ACCEPT

配置完成后,记着重启防火墙

# service iptables restart

然后就可以使用了。

遇到的问题

端口映射设置好是成功的,可是过一会就失效。发现了一个问题

系统的iptables服务没有启动,比如可以使用如下命令查看:

# chkconfig
auditd          0:off   1:off   2:on    3:on    4:on    5:on    6:off
blk-availability        0:off   1:on    2:on    3:on    4:on    5:on    6:off
crond           0:off   1:off   2:on    3:on    4:on    5:on    6:off
ip6tables       0:off   1:off   2:off   3:off   4:off   5:off   6:off
iptables        0:off   1:off   2:off   3:off   4:off   5:off   6:off
lvm2-monitor    0:off   1:on    2:on    3:on    4:on    5:on    6:off
mcollective     0:off   1:off   2:on    3:on    4:on    5:on    6:off
netconsole      0:off   1:off   2:off   3:off   4:off   5:off   6:off
netfs           0:off   1:off   2:off   3:on    4:on    5:on    6:off
network         0:off   1:off   2:on    3:on    4:on    5:on    6:off
ntpd            0:off   1:off   2:on    3:on    4:on    5:on    6:off
ntpdate         0:off   1:off   2:off   3:off   4:off   5:off   6:off
puppet          0:off   1:off   2:on    3:on    4:on    5:on    6:off
rdisc           0:off   1:off   2:off   3:off   4:off   5:off   6:off
restorecond     0:off   1:off   2:off   3:off   4:off   5:off   6:off
rhnsd           0:off   1:off   2:on    3:on    4:on    5:on    6:off
rsyslog         0:off   1:off   2:on    3:on    4:on    5:on    6:off
saslauthd       0:off   1:off   2:off   3:off   4:off   5:off   6:off
sendmail        0:off   1:off   2:on    3:on    4:on    5:on    6:off
snmpd           0:off   1:off   2:on    3:on    4:on    5:on    6:off
snmptrapd       0:off   1:off   2:off   3:off   4:off   5:off   6:off
sshd            0:off   1:off   2:on    3:on    4:on    5:on    6:off
udev-post       0:off   1:on    2:on    3:on    4:on    5:on    6:off

找到第6行,发现iptables对应的level开关都是off,这个时候要把off改成on即可。

# chkconfig --help
chkconfig version 1.3.49.3 - Copyright (C) 1997-2000 Red Hat, Inc.
This may be freely redistributed under the terms of the GNU Public License.

usage:   chkconfig [--list] [--type <type>] [name]
         chkconfig --add <name>
         chkconfig --del <name>
         chkconfig --override <name>
         chkconfig [--level <levels>] [--type <type>] <name> <on|off|reset|resetpriorities>


使用命令:

# chkconfig --level 012345 iptables on


重新查看:

# chkconfig
auditd          0:off   1:off   2:on    3:on    4:on    5:on    6:off
blk-availability        0:off   1:on    2:on    3:on    4:on    5:on    6:off
crond           0:off   1:off   2:on    3:on    4:on    5:on    6:off
ip6tables       0:off   1:off   2:off   3:off   4:off   5:off   6:off
iptables        0:on    1:on    2:on    3:on    4:on    5:on    6:on
lvm2-monitor    0:off   1:on    2:on    3:on    4:on    5:on    6:off
mcollective     0:off   1:off   2:on    3:on    4:on    5:on    6:off
netconsole      0:off   1:off   2:off   3:off   4:off   5:off   6:off
netfs           0:off   1:off   2:off   3:on    4:on    5:on    6:off
network         0:off   1:off   2:on    3:on    4:on    5:on    6:off
ntpd            0:off   1:off   2:on    3:on    4:on    5:on    6:off
ntpdate         0:off   1:off   2:off   3:off   4:off   5:off   6:off
puppet          0:off   1:off   2:on    3:on    4:on    5:on    6:off
rdisc           0:off   1:off   2:off   3:off   4:off   5:off   6:off
restorecond     0:off   1:off   2:off   3:off   4:off   5:off   6:off
rhnsd           0:off   1:off   2:on    3:on    4:on    5:on    6:off
rsyslog         0:off   1:off   2:on    3:on    4:on    5:on    6:off
saslauthd       0:off   1:off   2:off   3:off   4:off   5:off   6:off
sendmail        0:off   1:off   2:on    3:on    4:on    5:on    6:off
snmpd           0:off   1:off   2:on    3:on    4:on    5:on    6:off
snmptrapd       0:off   1:off   2:off   3:off   4:off   5:off   6:off
sshd            0:off   1:off   2:on    3:on    4:on    5:on    6:off
udev-post       0:off   1:on    2:on    3:on    4:on    5:on    6:off

这个时候就解决问题了。

 

 

rinetd

linux下简单好用的工具rinetd,实现端口映射/转发/重定向
官网地址http://www.boutell.com/rinetd

软件下载
wget http://www.321dz.com/shell/rinetd.tar.gz

解压安装
tar zxvf rinetd.tar.gz
make
make install

编辑配置
vi /etc/rinetd.conf
0.0.0.0 8080 172.19.94.3 8080
0.0.0.0 2222 192.168.0.103 3389
1.2.3.4 80     192.168.0.10 80

说明一下(0.0.0.0表示本机绑定所有可用地址)
将所有发往本机8080端口的请求转发到172.19.94.3的8080端口
将所有发往本机2222端口的请求转发到192.168.0.103的3389端口
将所有发往1.2.3.4的80端口请求转发到192.168.0.10的80端口

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

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

启动程序
pkill rinetd  ##关闭进程
rinetd -c /etc/rinetd.conf  ##启动转发
把这条命令加到/etc/rc.local里面就可以开机自动运行

查看状态
netstat -antup

需要注意
1.rinetd.conf中绑定的本机端口必须没有被其它程序占用
2.运行rinetd的系统防火墙应该打开绑定的本机端口
例如:
-A RH-Firewall-1-INPUT -m state –state NEW -m tcp -p tcp –dport 1111 -j ACCEPT
-A RH-Firewall-1-INPUT -m state –state NEW -m tcp -p tcp –dport 2222 -j ACCEPT