在有外网的机器上用iptables给内网的ftp server打洞

工作需求搭一个ftp服务器
但是这个服务器只有私(内)网地址
于是就从网通、电信和教育网各打了个洞过来(用iptables)
于是在服务器上装一个proftpd
具体怎么装不解释,这里主要讲打洞的外网机器怎么配
比如
打洞的机器的外网ip是a.b.c.d
内网ip是192.168.100.100
ftp server的ip是192.168.100.200
ftp服务开在端口21上
那么我们只需要在打洞的机器上这么操作
modprobe ip_conntrack_ftp ports=21
modprobe ip_nat_ftp ports=21

注意:如果ftp服务开在别的端口
比如2121,那么要做相应改动
modprobe ip_conntrack_ftp ports=2121
modprobe ip_nat_ftp ports=2121

或者
modprobe ip_conntrack_ftp ports=21,2121
modprobe ip_nat_ftp ports=21,2121

然后再
iptables -t nat -A PREROUTING -p tcp -d a.b.c.d –dport 21 -j DNAT –to 192.168.100.200:21
iptables -t nat -A POSTROUTING -p tcp -d 192.168.100.200 –dport 21 -j SNAT –to 192.168.100.100

最后,启用ipv4的包转发
sysctl -w net.ipv4.ip_forward=1
看到这里
也许大家会奇怪
我只处理了一个数据控制端口21
那ftp还有数据传输端口呢
还有主动模式、被动模式呢
呵呵
这些东西都由前面modprobe进去的ip_conntrack_ftp模块儿搞定了

One Response to “在有外网的机器上用iptables给内网的ftp server打洞”

  1. 这里补充一点:
    如果你要在/etc/rc.d/rc.local里加3句这样启动的话:
    modprobe ip_conntrack_ftp ports=21
    modprobe ip_nat_ftp ports=21
    /etc/rc.d/init.d/iptables start
    这样不行!!!
    因为你用/etc/rc.d/init.d/iptables start的时候
    好像会把一些mod重新初始化
    所以可以这样
    /etc/rc.d/init.d/iptables start
    modprobe ip_conntrack_ftp ports=21
    modprobe ip_nat_ftp ports=21

Discussion Area - Leave a Comment




*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word