<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>wd and cc &#187; iptables</title>
	<atom:link href="http://wdicc.com/tag/iptables/feed/" rel="self" type="application/rss+xml" />
	<link>http://wdicc.com</link>
	<description>Happy every day...</description>
	<lastBuildDate>Wed, 01 Feb 2012 03:27:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>端口映射</title>
		<link>http://wdicc.com/portmap/</link>
		<comments>http://wdicc.com/portmap/#comments</comments>
		<pubDate>Tue, 09 May 2006 11:21:49 +0000</pubDate>
		<dc:creator>wd</dc:creator>
				<category><![CDATA[Other]]></category>
		<category><![CDATA[iptables]]></category>

		<guid isPermaLink="false">http://blog.wdicc.com/?p=153</guid>
		<description><![CDATA[★ 需求 1 只有一个ip，怎么让多台电脑上网？ 只讲一下主机是双网卡的时候怎么解决。windows下面可以使用winroute、wingate或者系统自带的共享网络连接的功能，都可以实现共享上网。linux下面使用iptables一句话就可以实现。 至于主机是单网卡怎么解决，自己研究研究吧。我只在win下面尝试过用wingate，可是及其不稳定。 2 怎么将某个端口映射给内网？ 内网某台电脑提供了服务，怎么让外网的用户可以访问呢？某些对外的服务可能会在内网的多台电脑上面提供，这时就需要做端口映射来让这些服务器让外网可以访问。 ★ 解决方法 1 共享上网 1) 按照下图连接电脑。 &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160;isp &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#124; &#160; &#160; &#160; &#160; &#160; &#160;gw(用来做网关的电脑，双网卡) &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; [...]]]></description>
			<content:encoded><![CDATA[<p>★ 需求</p>
<p>1 只有一个ip，怎么让多台电脑上网？</p>
<p>只讲一下主机是双网卡的时候怎么解决。windows下面可以使用winroute、wingate或者系统自带的共享网络连接的功能，都可以实现共享上网。linux下面使用iptables一句话就可以实现。</p>
<p>至于主机是单网卡怎么解决，自己研究研究吧。我只在win下面尝试过用wingate，可是及其不稳定。</p>
<p>2 怎么将某个端口映射给内网？</p>
<p>内网某台电脑提供了服务，怎么让外网的用户可以访问呢？某些对外的服务可能会在内网的多台电脑上面提供，这时就需要做端口映射来让这些服务器让外网可以访问。</p>
<p><span id="more-153"></span>★ 解决方法</p>
<p>1 共享上网</p>
<p>1) 按照下图连接电脑。<br />
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;isp<br />
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; |<br />
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;gw(用来做网关的电脑，双网卡)<br />
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; |<br />
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;hub(or switch)<br />
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; |<br />
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br />
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;| &nbsp; &nbsp;&nbsp; | &#8230;&#8230;.. | &nbsp; &nbsp; &nbsp; |<br />
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;pc1 &nbsp; pc2 &#8230;&#8230;&nbsp; pcx &nbsp;&nbsp; pcy</p>
<p>gw一个网卡接isp，ip设置为isp告诉你的。另外一个网卡接hub，ip设置为一个内网ip，例如192.168.0.1。</p>
<p>2) 在gw上面执行如下脚本。<br />
#&#8212;&#8212;&#8212;&#8212;script start&#8212;&#8212;&#8211;<br />
#!/bin/sh</p>
<p>IPT=”/sbin/iptables”</p>
<p># Internet Interface<br />
INET_IFACE=”ethx”<br />
INET_ADDRESS=”x.x.x.x”</p>
<p># Local Interface Information<br />
LOCAL_IFACE=”ethy”<br />
LOCAL_IP=”192.168.0.1&#8243;<br />
LOCAL_NET=”192.168.0.0/24&#8243;<br />
LOCAL_BCAST=”192.168.0.255&#8243;</p>
<p>#修改从外网网卡出去的包的源地址，好让外网返回的包能正确回来。<br />
$IPT -t nat -A POSTROUTING -o $INET_IFACE -j SNAT &#8211;to-source $INET_ADDRESS</p>
<p>#打开系统的转发功能<br />
echo “1&#8243; &gt; /proc/sys/net/ipv4/ip_forward</p>
<p>#&#8212;&#8212;&#8212;-script end&#8212;&#8212;&#8212;-</p>
<p>这样内网的电脑做如下设置后就应该可以上网了。</p>
<p>ip：&nbsp; 192.168.0.x<br />
网关: 192.168.0.1<br />
dns:&nbsp; 和gw的设置一样</p>
<p>这时的服务器已经具备一个网关的功能，不过内网的电脑想要上网，都必须手动设置ip，想做到自动获取，可以</p>
<p>2 端口映射</p>
<p>端口映射需要做的其实就是修改对方发过来包的目的地址和端口，然后帮忙转发一下，并且转发的同时修改一下源地址，好让被转发的电脑认为是gw发的包，处理完毕之后好能正常返回gw，然后由gw在负责转发给外网的ip。端口转发需要用到iptables的nat表，上面这些操作是在PREROUTING和POSTROUTING链之间完成的。</p>
<p>1) 映射到外网ip<br />
整个过程可以参考下图。<br />
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;eth0(外网网卡) &nbsp; &nbsp;<br />
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;________________________<br />
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; | &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;|<br />
 &nbsp; &nbsp;&nbsp; gw的80端口 &nbsp; &nbsp; &nbsp;&nbsp; 修改目标地址(DNAT) &nbsp; &nbsp; &nbsp; 修改源地址(SNAT)<br />
外网ip&#8212;&#8212;&#8212;-&gt;PREROUTING&#8212;&#8212;&#8212;&#8212;&gt;POSTROUTING&#8212;&#8212;&#8212;&#8212;&gt;外网ip</p>
<p>所以，例如我们要将对gw 80端口的访问都转发到外网的202.202.202.202这台电脑上面，只需要在上面的脚本中添加下面的语句即可。</p>
<p># 修改从外网进来的对80端口访问的数据包的目的地址<br />
$IPT -t nat -A PREROUTING -p tcp -i $INET_IFACE &#8211;destination-port 80 \<br />
 &nbsp; &nbsp; -j DNAT &#8211;to-destination 202.202.202.202</p>
<p>那么，你可能要问，按照上面的说法，这样只修改了目的地址，还要修改源地址啊，这样包才能正确返回到gw。问的好，不过其实我们已经做了这个设置了，聪明的你可能已经想到了，就是上面让内网ip可以上网的那个脚本里的一句语句已经实现了这个功能。</p>
<p>#修改从外网网卡出去的包的源地址，好让外网返回的包能正确回来。<br />
$IPT -t nat -A POSTROUTING -o $INET_IFACE -j SNAT &#8211;to-source $INET_ADDRESS</p>
<p>就是上面这句，他把所有从外网网卡出去的包的源地址都修改了，当然也包括去往202.202.202.202的80端口的包。</p>
<p>2) 映射到内网ip<br />
整个过程可以参考下图。<br />
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;eth0(外网网卡) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;eth0(内网网卡)<br />
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;________________________ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;_______________________<br />
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; | &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;| &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;| &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; |<br />
 &nbsp; &nbsp;&nbsp; gw的80端口 &nbsp; &nbsp; &nbsp;&nbsp; 修改目标地址(DNAT) &nbsp; &nbsp; &nbsp; 修改源地址(SNAT) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; 修改源地址(SNAT)<br />
外网ip&#8212;&#8212;&#8212;-&gt;PREROUTING&#8212;&#8212;&#8212;&#8212;&gt;POSTROUTING&#8212;&#8212;&#8212;&#8212;&gt;PREROUTING&#8212;&#8212;&#8212;&#8211;&gt;POSTROUTING&#8212;&#8212;&#8212;&#8211;&gt;内网ip</p>
<p>所以，例如我们要映射80端口到内网的192.168.1.80这台电脑上面，只需要在上面的脚本中添加下面的语句即可。</p>
<p># 修改从外网进来的对80端口访问的数据包的目的地址<br />
$IPT -t nat -A PREROUTING -p tcp -i $INET_IFACE &#8211;destination-port 80 \<br />
 &nbsp; &nbsp; -j DNAT &#8211;to-destination 192.168.1.80<br />
 &nbsp; &nbsp;<br />
# 修改从内网网卡出去的所有数据包源地址<br />
$IPT -t nat -A POSTROUTING -o $LOCAL_IFACE \<br />
 &nbsp; &nbsp; -j SNAT &#8211;to-source $LOCAL_IP</p>
<p>这个比上面多了一句，意思相信你已经明白了吧。因为访问内网ip的时候走了内网网卡，所以还需要对从内网网卡出去的包修改一下源地址。</p>
]]></content:encoded>
			<wfw:commentRss>http://wdicc.com/portmap/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>为远程服务器设置防火墙</title>
		<link>http://wdicc.com/iptables-on-remote-server/</link>
		<comments>http://wdicc.com/iptables-on-remote-server/#comments</comments>
		<pubDate>Thu, 20 Apr 2006 16:00:37 +0000</pubDate>
		<dc:creator>wd</dc:creator>
				<category><![CDATA[Other]]></category>
		<category><![CDATA[iptables]]></category>

		<guid isPermaLink="false">http://blog.wdicc.com/?p=149</guid>
		<description><![CDATA[如果服务器不在本地，又需要对防火墙做设置的时候，操作一定要注意，]]></description>
			<content:encoded><![CDATA[<p>如果服务器不在本地，又需要对防火墙做设置的时候，操作一定要注意，</p>
]]></content:encoded>
			<wfw:commentRss>http://wdicc.com/iptables-on-remote-server/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

