<?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>五四陈科学院-坚信科学，分享技术 &#187; iptables</title>
	<atom:link href="http://www.54chen.com/tag/iptables/feed" rel="self" type="application/rss+xml" />
	<link>http://www.54chen.com</link>
	<description>PHP、JAVA、缓存、架构、经验、分享</description>
	<lastBuildDate>Sat, 11 Feb 2012 17:09:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>用iptables做两机之间的端口转发</title>
		<link>http://www.54chen.com/_linux_/%e7%94%a8iptables%e5%81%9a%e4%b8%a4%e6%9c%ba%e4%b9%8b%e9%97%b4%e7%9a%84%e7%ab%af%e5%8f%a3%e8%bd%ac%e5%8f%91.html</link>
		<comments>http://www.54chen.com/_linux_/%e7%94%a8iptables%e5%81%9a%e4%b8%a4%e6%9c%ba%e4%b9%8b%e9%97%b4%e7%9a%84%e7%ab%af%e5%8f%a3%e8%bd%ac%e5%8f%91.html#comments</comments>
		<pubDate>Thu, 19 Feb 2009 00:00:12 +0000</pubDate>
		<dc:creator>cc0cc</dc:creator>
				<category><![CDATA[linux]]></category>
		<category><![CDATA[iptables]]></category>
		<category><![CDATA[端口转发]]></category>

		<guid isPermaLink="false">http://www.54chen.com/_linux_/%e7%94%a8iptables%e5%81%9a%e4%b8%a4%e6%9c%ba%e4%b9%8b%e9%97%b4%e7%9a%84%e7%ab%af%e5%8f%a3%e8%bd%ac%e5%8f%91.html</guid>
		<description><![CDATA[<a href="http://www.54chen.com"><img border="0" src="http://www.54chen.com/wp-content/themes/54chen2011/images/54chen-logo.gif"></a><br>以下内容由<a href="http://www.54chen.com">[五四陈科学院]</a>提供<br> [文章作者：陈臻 本文版本：v1.0 最后修改：2009.2.19 转载请注明原文链接：http://www.54chen.com/c/264] 以前发过一篇利用iptables做本机的端口转发的文章，利用简单的地址转换的原理，可以将两台互相可访问的机器利用iptables转发，这样很容易实现类似透明代理的功能，当然，这个代理不会有缓存的。：） 假如我希望把对 1.2.4.5:8080的访问都转向 1.2.6.9:80： IP包来到之后，修改目的地址，使之转向目标机器的目标端口，在.5这个机器上： iptables -t nat -A PREROUTING -d 1.2.4.5 -p tcp --dport 8080 -j DNAT --to 1.2.6.9:80 IP包返回的时候，修改源地址（源端口），使之符合IP协议，正确返回，还是在.5这个机器上： iptables -t nat -A POSTROUTING -d 1.2.6.9 -p tcp --dport 80 -j SNAT --to 1.2.4.5:8080 &#8230;<br><br>想快点找到作者也可以到Twitter上留言: <a href="https://www.twitter.com/54chen" target="_blank">@54chen</a><br>或者你懒得带梯子上墙，请到新浪微博：<a href="http://t.sina.com.cn/54chen" target="_blank">@54chen</a>]]></description>
			<content:encoded><![CDATA[<a href="http://www.54chen.com"><img border="0" src="http://www.54chen.com/wp-content/themes/54chen2011/images/54chen-logo.gif"></a><br>以下内容由<a href="http://www.54chen.com">[五四陈科学院]</a>提供<br><p> [文章作者：陈臻 本文版本：v1.0 最后修改：2009.2.19 转载请注明原文链接：<a href="http://www.54chen.com/c/264">http://www.54chen.com/c/264</a>]</p>
<p>以前发过一篇利用<a href="http://www.54chen.com/c/262">iptables做本机的端口转发的文章</a>，利用简单的地址转换的原理，可以将两台互相可访问的机器利用iptables转发，这样很容易实现类似透明代理的功能，当然，这个代理不会有缓存的。：）</p>
<p>假如我希望把对 1.2.4.5:8080的访问都转向 1.2.6.9:80：</p>
<p>IP包来到之后，修改目的地址，使之转向目标机器的目标<a href="http://www.54chen.com/c/262">端口</a>，在.5这个机器上：</p>
<p><span style="font-family: 'Courier New'; line-height: 18px; white-space: pre; "> </span> iptables -t nat -A PREROUTING -d 1.2.4.5 -p tcp --dport 8080 -j DNAT --to 1.2.6.9:80</p>
<p>IP包返回的时候，修改源地址（源端口），使之符合IP协议，正确返回，还是在.5这个机器上：</p>
<p><span style="font-family: 'Courier New'; line-height: 18px; white-space: pre; "> </span> iptables -t nat -A POSTROUTING -d 1.2.6.9 -p tcp --dport 80 -j SNAT --to 1.2.4.5:8080</p>
<p>然后我们需要在.5操作系统上打开IP转发：</p>
<p>echo '1' &gt; /proc/sys/net/ipv4/ip_forward</p>
<p>最后再设置.5的连接状态</p>
<pre>  iptables -A FORWARD -o eth0 -d 1.2.4.5 -p tcp --dport 8080 -j ACCEPT

  iptables -A FORWARD -i eth0 -s 1.2.6.9 -p tcp --sport 80 -m state --state ESTABLISHED -j ACCEPT</pre>
<p> </p>
<p>附全部<a href="http://www.54chen.com/c/379">脚本</a>（不关.9的事，全是在.5上）</p>
<pre>iptables -t nat -A POSTROUTING -d 1.2.4.5 -p tcp --dport 8080 -j DNAT --to 1.2.6.9:80

iptables -t nat -A PREROUTING -d 1.2.4.5 -p tcp --dport 8080 -j DNAT --to 1.2.6.9:80

iptables -t nat -A POSTROUTING -d 1.2.6.9 -p tcp --dport 80 -j SNAT --to 1.2.4.5:8080

iptables -A FORWARD -i eth0 -s 1.2.6.9 -p tcp --sport 80 -m state --state ESTABLISHED -j ACCEPT

iptables -A FORWARD -o eth0 -d 1.2.4.5 -p tcp --dport 8080 -j ACCEPT</pre>
<br><br>想快点找到作者也可以到Twitter上留言: <a href="https://www.twitter.com/54chen" target="_blank">@54chen</a><br>或者你懒得带梯子上墙，请到新浪微博：<a href="http://t.sina.com.cn/54chen" target="_blank">@54chen</a>]]></content:encoded>
			<wfw:commentRss>http://www.54chen.com/_linux_/%e7%94%a8iptables%e5%81%9a%e4%b8%a4%e6%9c%ba%e4%b9%8b%e9%97%b4%e7%9a%84%e7%ab%af%e5%8f%a3%e8%bd%ac%e5%8f%91.html/feed</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>用iptables做本机端口转发</title>
		<link>http://www.54chen.com/_linux_/%e7%94%a8iptables%e5%81%9a%e6%9c%ac%e6%9c%ba%e7%ab%af%e5%8f%a3%e8%bd%ac%e5%8f%91.html</link>
		<comments>http://www.54chen.com/_linux_/%e7%94%a8iptables%e5%81%9a%e6%9c%ac%e6%9c%ba%e7%ab%af%e5%8f%a3%e8%bd%ac%e5%8f%91.html#comments</comments>
		<pubDate>Fri, 23 Jan 2009 14:52:45 +0000</pubDate>
		<dc:creator>cc0cc</dc:creator>
				<category><![CDATA[linux]]></category>
		<category><![CDATA[iptables]]></category>

		<guid isPermaLink="false">http://www.54chen.com/_linux_/%e7%94%a8iptables%e5%81%9a%e6%9c%ac%e6%9c%ba%e7%ab%af%e5%8f%a3%e8%bd%ac%e5%8f%91.html</guid>
		<description><![CDATA[<a href="http://www.54chen.com"><img border="0" src="http://www.54chen.com/wp-content/themes/54chen2011/images/54chen-logo.gif"></a><br>以下内容由<a href="http://www.54chen.com">[五四陈科学院]</a>提供<br>代码如下： iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-ports 8080 估计适当增加其它的参数也可以做不同IP的端口转发。 如果需要本机也可以访问，则需要配置OUTPUT链： iptables -t nat -A OUTPUT -p tcp --dport 80 -j REDIRECT --to-ports 8080 原因： 外网访问需要经过PREROUTING链，但是localhost不经过该链，因此需要用OUTPUT，或者POSTROUTING。 POSTROUTING不行，需要看看。<br><br>想快点找到作者也可以到Twitter上留言: <a href="https://www.twitter.com/54chen" target="_blank">@54chen</a><br>或者你懒得带梯子上墙，请到新浪微博：<a href="http://t.sina.com.cn/54chen" target="_blank">@54chen</a>]]></description>
			<content:encoded><![CDATA[<a href="http://www.54chen.com"><img border="0" src="http://www.54chen.com/wp-content/themes/54chen2011/images/54chen-logo.gif"></a><br>以下内容由<a href="http://www.54chen.com">[五四陈科学院]</a>提供<br><p>代码如下：</p>
<pre>  iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-ports 8080</pre>
<p>估计适当增加其它的参数也可以做不同IP的<a href="http://www.54chen.com/c/381">端口</a>转发。</p>
<p>如果需要本机也可以访问，则需要<a href="http://www.54chen.com/c/406">配置</a>OUTPUT链：</p>
<pre>  iptables -t nat -A OUTPUT -p tcp --dport 80 -j REDIRECT --to-ports 8080</pre>
<p>原因：</p>
<p>外网访问需要经过PREROUTING链，但是localhost不经过该链，因此需要用OUTPUT，或者POSTROUTING。</p>
<p>POSTROUTING不行，需要看看。</p>
<p><!-- Saved in parser cache with key pewiki:pcache:idhash:222-0!1!0!!zh-cn!2 and timestamp 20081210025542 --></p>
<p><!-- end content --></p>
<br><br>想快点找到作者也可以到Twitter上留言: <a href="https://www.twitter.com/54chen" target="_blank">@54chen</a><br>或者你懒得带梯子上墙，请到新浪微博：<a href="http://t.sina.com.cn/54chen" target="_blank">@54chen</a>]]></content:encoded>
			<wfw:commentRss>http://www.54chen.com/_linux_/%e7%94%a8iptables%e5%81%9a%e6%9c%ac%e6%9c%ba%e7%ab%af%e5%8f%a3%e8%bd%ac%e5%8f%91.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

