基本的iptables NAT端口转发

我有三台机器:一台本地PC(公共IP 1.2.3.4),一个数据中心的Ubuntu 10服务器盒(5.6.7.8公共IP上的eth0)以及一个托pipe我networking之外的网站的第三方服务器Slashdot在216.34.181.45)。

  • 使用iptables,如何使用5.6.7.8:8080从本地机器访问Slashdot?
  • 如果Slashdot与我的Ubuntu盒子在同一个LAN上,这个过程会不同吗?
  • 这可以通过NAT PREROUTING / POSTROUTING完成,还是需要MASQUERADE?

PC ----- Ubuntu 10 Server ----- Slashdot (1.2.3.4) (5.6.7.8) (216.34.181.45) 
  1. 在Ubuntu上启用IP转发:

     echo 1 > /proc/sys/net/ipv4/ip_forward 

    并添加以下规则:

     iptables -t nat -A PREROUTING -i eth0 -p tcp -m tcp --dport 8080 -j DNAT \ --to-destination 216.34.181.45:80 iptables -t nat -A POSTROUTING -o eth0 -j SNAT --to-source 5.6.7.8 
  2. 没有。

  3. 如果Ubuntu有一个dynamicIP,你应该使用MASQUERADE:

     iptables -t nat -A POSTROUTING -j MASQUERADE 

在这种情况下,您也可以使用SSH本地端口转发,方法是在Ubuntu上执行以下命令:

 $ ssh -L 5.6.7.8:8080:216.34.181.45:80 -N [email protected] 

还有另一种(或更多)的方式来做到这一点。 看看rinetd :

 Name : rinetd Arch : i386 Version : 0.62 Release : 6.el5.art Size : 41 k Repo : installed Summary : TCP redirection server URL : http://www.boutell.com/rinetd License : GPL Description: rinetd is a daemon which redirects TCP connections from one IP address : and port to another IP address and port. This daemon is often used to : access services behind a firewall. 

configuration非常简单。 将下面的行添加到/etc/rinetd.conf

 5.6.7.8 8080 216.34.181.45 80 

并开始:

 # /etc/init.d/rinetd start Starting rinetd: [ OK ] 

它会为你做所有的事情。