<?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://xiaobin.net/tag/iptables/feed/" rel="self" type="application/rss+xml" />
	<link>http://xiaobin.net</link>
	<description>一个社交恐惧症患者（俗称：宅男）的互联网从业生活</description>
	<lastBuildDate>Mon, 05 Jul 2010 03:12:26 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>iptables应用实例</title>
		<link>http://xiaobin.net/200912/iptables-example/</link>
		<comments>http://xiaobin.net/200912/iptables-example/#comments</comments>
		<pubDate>Wed, 09 Dec 2009 09:23:12 +0000</pubDate>
		<dc:creator>肖斌</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[iptables]]></category>

		<guid isPermaLink="false">http://xiaobin.net/?p=160</guid>
		<description><![CDATA[本机端口转发 将本机80端口的请求转发到8080端口： iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-ports 8080 如果需要本机也可以访问，则需要配置OUTPUT链： iptables -t nat -A OUTPUT -p tcp -d 127.0.0.1 --dport 80 -j REDIRECT --to-ports 8080 原因：外网访问需要经过PREROUTING链，但是localhost不经过该链，因此需要用OUTPUT，或者POSTROUTING。 以上规则可以通过 iptables -t nat -F PREROUTING(或者是OUTPUT)清除 两机之间的端口转发 将本机的81端口的请求全部转发到192.168.1.1:80 首先要启用ipv4的转发功能： echo 1 &#62; /proc/sys/net/ipv4/ip_forward 或者是修改/etc/sysctl.conf (via)以便重启后也会启用转发，然后设定iptables(via)： iptables -t nat -A PREROUTING -p tcp --dport [...]]]></description>
			<content:encoded><![CDATA[<h3>本机端口转发</h3>
<p>将本机80端口的请求转发到8080端口：</p>
<pre>iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-ports 8080</pre>
<p><span id="more-160"></span>如果需要本机也可以访问，则需要配置OUTPUT链：</p>
<pre>iptables -t nat -A OUTPUT -p tcp -d 127.0.0.1 --dport 80 -j REDIRECT --to-ports 8080</pre>
<p>原因：外网访问需要经过PREROUTING链，但是localhost不经过该链，因此需要用OUTPUT，或者POSTROUTING。</p>
<p>以上规则可以通过 iptables -t nat -F PREROUTING<em><span style="color: #c0c0c0;">(或者是OUTPUT)</span></em>清除</p>
<h3>两机之间的端口转发</h3>
<p>将本机的81端口的请求全部转发到192.168.1.1:80</p>
<p>首先要启用ipv4的转发功能：</p>
<pre>echo 1 &gt; /proc/sys/net/ipv4/ip_forward</pre>
<p>或者是修改/etc/sysctl.conf (<a href="http://www.ducea.com/2006/08/01/how-to-enable-ip-forwarding-in-linux/" target="_blank">via</a>)以便重启后也会启用转发，然后设定iptables(<a href="http://serverfault.com/questions/27221/iptables-port-forwarding-on-debian" target="_blank">via</a>)：</p>
<pre>iptables -t nat -A PREROUTING -p tcp --dport <span style="color: #ff0000;">81</span> -j DNAT --to <span style="color: #ff0000;">192.168.1.1:80</span>
iptables -t nat -A POSTROUTING -j MASQUERADE</pre>
<p>如果开启了防火墙功能，注意要将80和81两个端口都打开。</p>
<h3>禁止Ping入</h3>
<p>允许内网(192.168.1.*)的ping入，允许ping出，禁止其它网段的ping入</p>
<pre>iptables -A INPUT -p icmp --icmp-type 8 -s 192.168.1.0/24 -j ACCEPT
iptables -A INPUT -p icmp --icmp-type 8 -j DROP</pre>
<hr />
<p><strong><a href="http://xiaobin.net/200912/iptables-example/">iptables应用实例</a></strong> | 暂无评论，<a href="http://xiaobin.net/200912/iptables-example/#comments">添加评论</a>
<br>本文网址：<a href="http://xiaobin.net/200912/iptables-example/">http://xiaobin.net/200912/iptables-example/</a>
<br><a href="http://xiaobin.net">将之典藏</a> - 厚积而薄发，© 2005-2009. 如无特别声明，适用<a href="http://creativecommons.org/licenses/by-nc-sa/3.0/deed.zh" target="_blank">署名-非商业性使用-相同方式共享 3.0</a>授权，你可以署名使用全部或者部分内容用于非商业性目的。</p>]]></content:encoded>
			<wfw:commentRss></wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
