<?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>SpaLinux.com &#187; iptables</title>
	<atom:link href="http://spalinux.com/tag/iptables/feed" rel="self" type="application/rss+xml" />
	<link>http://spalinux.com</link>
	<description>Resources for Relaxing Linux System Administrators</description>
	<lastBuildDate>Sat, 21 Jan 2012 16:07:59 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>เก็บข้อมูลการใช้เว็บผ่าน Linux Router ด้วย Squid Transparent Proxy</title>
		<link>http://spalinux.com/2011/11/how-to-log-web-traffic-via-linux-router-using-squid-transparent-proxy</link>
		<comments>http://spalinux.com/2011/11/how-to-log-web-traffic-via-linux-router-using-squid-transparent-proxy#comments</comments>
		<pubDate>Fri, 25 Nov 2011 17:44:17 +0000</pubDate>
		<dc:creator>editor</dc:creator>
				<category><![CDATA[Networking]]></category>
		<category><![CDATA[Proxy]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[iptables]]></category>
		<category><![CDATA[router]]></category>
		<category><![CDATA[Squid]]></category>
		<category><![CDATA[Transparent Proxy]]></category>

		<guid isPermaLink="false">http://spalinux.com/?p=1519</guid>
		<description><![CDATA[ด้วยกฎหมาย พรบ. ที่เริ่มมีผลบังคับ ให้เก็บข้อมูลการใช้เว็บอินเตอร์เน็ต ว่าเป็นใครใช้ เข้าเว็บไหน เมื่อไร ถ้าเป็นเครื่องที่เราสามารถดูแลได้ วิธีหนึ่งที่ทำได้คือ คอนฟิก Proxy ใน ฺBrowser ของแต่ละเครื่องที่จะใช้อินเตอร์เน็ต ชี้ไปยัง Proxy Server ที่ทำหน้าที่เก็บข้อมูลการใช้งาน แต่ถ้าเราไม่สามารถควบคุมดูแลได้ หรือเครื่องที่ใช้งานมีการเปลี่ยนแปลงตลอดเวลา เช่นให้บริการอินเตอร์เน็ต สำหรับผู้มาติดต่องาน ถ้าเป็นแบบนี้ แนะนำให้คอนฟิก Transparent Proxy บน Linux Router เพื่อให้ทำหน้าที่เก็บข้อมูลการใช้เว็บโดยอัตโนมัติ ระบบทดสอบ Linux Router ติดตั้ง Squid มีพอร์ต LAN สองพอร์ต eth0, eth1 เครื่องผู้ใช้งานทั้งหมดต่อพอร์ต eth0 ของ Linux Router พอร์ต eth1 ของ Linux Router ต่อไปยังอินเตอร์เน็ต Redirect Web Traffic ให้เข้าไปใน Proxy [...]]]></description>
			<content:encoded><![CDATA[<p>ด้วยกฎหมาย พรบ. ที่เริ่มมีผลบังคับ ให้เก็บข้อมูลการใช้เว็บอินเตอร์เน็ต ว่าเป็นใครใช้ เข้าเว็บไหน เมื่อไร</p>
<p>ถ้าเป็นเครื่องที่เราสามารถดูแลได้ วิธีหนึ่งที่ทำได้คือ คอนฟิก Proxy ใน ฺBrowser ของแต่ละเครื่องที่จะใช้อินเตอร์เน็ต ชี้ไปยัง Proxy Server ที่ทำหน้าที่เก็บข้อมูลการใช้งาน</p>
<p>แต่ถ้าเราไม่สามารถควบคุมดูแลได้ หรือเครื่องที่ใช้งานมีการเปลี่ยนแปลงตลอดเวลา เช่นให้บริการอินเตอร์เน็ต สำหรับผู้มาติดต่องาน</p>
<p>ถ้าเป็นแบบนี้ แนะนำให้คอนฟิก Transparent Proxy บน Linux Router เพื่อให้ทำหน้าที่เก็บข้อมูลการใช้เว็บโดยอัตโนมัติ</p>
<p><span id="more-1519"></span></p>
<h4>ระบบทดสอบ</h4>
<ul>
<li>Linux Router ติดตั้ง Squid มีพอร์ต LAN สองพอร์ต eth0, eth1</li>
<li>เครื่องผู้ใช้งานทั้งหมดต่อพอร์ต eth0 ของ Linux Router</li>
<li>พอร์ต eth1 ของ Linux Router ต่อไปยังอินเตอร์เน็ต</li>
</ul>
<h4></h4>
<h4>Redirect Web Traffic ให้เข้าไปใน Proxy Server</h4>
<p>เริ่มแรกต้องบังคับให้การใช้เว็บอินเตอร์เน็ตทั้งหมดต้องผ่าน Linux Router เข้าพอร์ต eth0 แล้วออกอินเตอร์เน็ตทางพอร์ต eth1</p>
<p>ถ้าเราต้องการให้การใช้เว็บ (TCP Port 80) ทั้งหมด เข้าพอร์ต eth0 ต้องผ่านเข้าไปใน Proxy Server (TCP Port 3128) ก่อน เพื่อให้เก็บข้อมูลการใช้งาน สามารถใช้คำสั่ง iptables ตามตัวอย่าง</p>
<pre>
[root@router ~]# <b>iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 3128</b>
</pre>
<h4>คอนฟิก Squid ให้ทำหน้าที่เป็น Transparent Proxy</h4>
<p>อย่างที่สองคือต้องแก้ไขไฟล์คอนฟิก Squid ซึ่งอยู่ใน /etc/squid/squid.conf</p>
<p>การแก้ไขคอนฟิกขึ้นอยู่กับเวอร์ชั่นของ Squid ที่ติดตั้ง</p>
<p>คำแนะนำ : เพื่อคุณสมบัติและประสิทธิภาพที่ดีกว่า แนะนำให้ใช้เวอร์ชั่นใหม่ล่าสุดที่สามารถติดตั้งได้</p>
<p><span style="text-decoration: underline;"><strong>เวอร์ชั่นเก่ากว่า 2.6</strong></span></p>
<pre>
httpd_accel_host virtual
httpd_accel_port 80
httpd_accel_with_proxy on
httpd_accel_uses_host_header on
</pre>
<p><span style="text-decoration: underline;"><strong>เวอร์ชั่น 2.6 &#8211; 3.0</strong></span></p>
<pre>
http_port 3128 transparent
</pre>
<p><span style="text-decoration: underline;"><strong>ตั้งแต่เวอร์ชั่น 3.1</strong></span></p>
<pre>
http_port 3128 intercept
</pre>
<p>หรือ</p>
<pre>
http_port 3128 transparent
</pre>
<p>หลังการแก้ไขคอนฟิกไฟล์ของ squid ต้องรีสตาร์ตเซอร์วิส squid</p>
<pre>
[root@router ~]# <b>service squid restart</b>
</pre>
<p>สามารถดูข้อมูลการใช้งานเว็บได้ในไฟล์ /var/log/squid/access.log</p>
<h4>ข้อมูลอ้างอิง</h4>
<ul>
<li><a href="http://spalinux.com/2008/05/configure_squid_proxy_server">คอนฟิก Squid Proxy Server</a></li>
<li><a href="http://www.squid-cache.org/" target="_blank">squid : Optimising Web Delivery</a></li>
<li><a href="http://www.linuxhomenetworking.com/wiki/index.php/Quick_HOWTO_:_Ch32_:_Controlling_Web_Access_with_Squid" target="_blank">Quick HOWTO : Ch32 : Controlling Web Access with Squid</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://spalinux.com/2011/11/how-to-log-web-traffic-via-linux-router-using-squid-transparent-proxy/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>แก้ไขคอนฟิก iptables firewall rule</title>
		<link>http://spalinux.com/2009/07/customize_iptables_firewall_rules</link>
		<comments>http://spalinux.com/2009/07/customize_iptables_firewall_rules#comments</comments>
		<pubDate>Sat, 18 Jul 2009 06:40:45 +0000</pubDate>
		<dc:creator>editor</dc:creator>
				<category><![CDATA[Firewall]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[iptables]]></category>

		<guid isPermaLink="false">http://spalinux.com/?p=842</guid>
		<description><![CDATA[การติดตั้งลีนุกซ์ส่วนใหญ่รวมทั้ง fedora จะติดตั้ง และเปิดคุณสมบัติ firewall มาด้วย ซึ่งโดยดีฟอลต์แล้ว จะอนุญาตให้ packet ขาเข้า (INPUT) ได้เฉพาะ ping และ SSH เท่านั้น ลีนุกซ์จะใช้โปรแกรมที่ชื่อ iptables ทำหน้าที่เป็น firewall และใช้คำสั่งนี้ในการแสดงและแก้ไขคอนฟิก (rules) ของ firewall ตัวอย่างการใช้คำสั่ง iptables เพื่อแสดง rule [root@fc11-64min ~]# iptables -L -v Chain INPUT (policy ACCEPT 0 packets, 0 bytes)  pkts bytes target     prot opt in     out     source               destination    42  2148 ACCEPT     all  [...]]]></description>
			<content:encoded><![CDATA[<p>การติดตั้งลีนุกซ์ส่วนใหญ่รวมทั้ง fedora จะติดตั้ง และเปิดคุณสมบัติ firewall มาด้วย ซึ่งโดยดีฟอลต์แล้ว จะอนุญาตให้ packet ขาเข้า (INPUT) ได้เฉพาะ ping และ SSH เท่านั้น</p>
<p>ลีนุกซ์จะใช้โปรแกรมที่ชื่อ iptables ทำหน้าที่เป็น firewall และใช้คำสั่งนี้ในการแสดงและแก้ไขคอนฟิก (rules) ของ firewall</p>
<p><span id="more-842"></span></p>
<p>ตัวอย่างการใช้คำสั่ง iptables เพื่อแสดง rule</p>
<pre>[root@fc11-64min ~]# <strong>iptables -L -v
</strong>Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination
   42  2148 ACCEPT     all  --  any    any     anywhere             anywhere            state RELATED,ESTABLISHED
<span style="color: #0000ff;">    0     0 ACCEPT     icmp --  any    any     anywhere             anywhere
</span>    0     0 ACCEPT     all  --  lo     any     anywhere             anywhere
<span style="color: #0000ff;">    0     0 ACCEPT     tcp  --  any    any     anywhere             anywhere            state NEW tcp dpt:ssh
</span>    0     0 REJECT     all  --  any    any     anywhere             anywhere            reject-with icmp-host-prohibited</pre>
<pre>Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination
    0     0 REJECT     all  --  any    any     anywhere             anywhere            reject-with icmp-host-prohibited</pre>
<pre>Chain OUTPUT (policy ACCEPT 63 packets, 7412 bytes)
 pkts bytes target     prot opt in     out     source               destination</pre>
<p>ถ้าเราติดตั้งและคอนฟิกโปรแกรมเพิ่มเติมบนเครื่องนี้ เช่น ลงเว็บเซิร์ฟเวอร์ จะทำให้ไม่สามารถเรียกมาจากเครื่องอื่นๆ ได้ นอกจากต้องแก้ไขคอนฟิกของ firewalll ก่อน</p>
<p>บน Fedora จะมี script ชื่อ system-config-firewall-tui ช่วยในการคอนฟิกของ iptables เช่น ถ้าต้องการอนุญาตให้ เครื่องอื่นสามารถเรียกเว็บเซิร์ฟเวอร์ที่รันพอร์ตดีฟอลต์ http (TCP 80) ได้ ต้องแก้ไขดังนี้</p>
<pre>[root@fc11-64min ~]# <strong>system-config-firewall-tui</strong></pre>
<pre>                                      ┌────────────┤ Firewall Configuration ├────────────┐
                                      │                                                  │
                                      │ A firewall protects against unauthorized         │
                                      │ network intrusions. Enabling a firewall blocks   │
                                      │ all incoming connections. Disabling a firewall   │
                                      │ allows all connections and is not recommended.   │
                                      │                                                  │
                                      │              Firewall: [*] Enabled               │
                                      │                                                  │
                                      │    ┌────┐      ┌───────────┐      ┌────────┐     │
                                      │    │ OK │      │ <strong><span style="color: #0000ff;">Customize</span></strong> │      │ Cancel │     │
                                      │    └────┘      └───────────┘      └────────┘     │
                                      │                                                  │
                                      │                                                  │
                                      └──────────────────────────────────────────────────┘</pre>
<p>กด [tab] เพื่อเลื่อนไปที่ &#8220;Customize&#8221;</p>
<pre>                                 ┌───────────────────┤ Trusted Services ├────────────────────┐
                                 │                                                           │
                                 │ Here you can define which services are trusted. Trusted   │
                                 │ services are accessible from all hosts and networks.      │
                                 │                                                           │
                                 │           [ ] RADIUS                        ↑             │
                                 │           [*] SSH                           ▒             │
                                 │           [ ] Samba                         ▒             │
                                 │           [ ] Samba Client                  ▒             │
                                 │           [ ] Secure WWW (HTTPS)            ▮             │
                                 │           <strong><span style="color: #0000ff;">[*] WWW (HTTP)</span></strong>                    ↓             │
                                 │                                                           │
                                 │                                 ┌─────────┐   ┌───────┐   │
                                 │                                 │ Forward │   │ <strong><span style="color: #0000ff;">Close</span></strong> │   │
                                 │                                 └─────────┘   └───────┘   │
                                 │                                                           │
                                 │                                                           │
                                 └───────────────────────────────────────────────────────────┘</pre>
<p>ในหน้า &#8220;Trusted Services&#8221; กดปุ่มลูกศรเพื่อเลื่อนลงมาบรรทัด [x] WWW (HTTP) แล้วกดปุ่ม [space-bar] เพื่อเลือก แล้วกด &#8220;Close&#8221;</p>
<p>หน้าจอจะขึ้นถามให้ บันทึกคอนฟิกใหม่ เลือก [Yes]</p>
<pre>                                         ┌────────────────┤ Warning ├─────────────────┐
                                         │                                            │
                                         │ Clicking the 'Yes' button will override    │
                                         │ any existing firewall configuration. Are   │
                                         │ you sure that you want to do this?         │
                                         │                                            │
                                         │ Please remember to check if the services   │
                                         │ iptables and ip6tables are enabled.        │
                                         │                                            │
                                         │        ┌─────┐              ┌────┐         │
                                         │        │ <strong><span style="color: #0000ff;">Yes</span></strong> │              │ No │         │
                                         │        └─────┘              └────┘         │
                                         │                                            │
                                         │                                            │
                                         └────────────────────────────────────────────┘</pre>
<p>ใช้คำสั่ง iptables อีกครั้ง เพื่อแสดงคอนฟิก firewall ปัจจุบัน หลังการแก้ไข</p>
<pre>[root@fc11-64min ~]# <strong>iptables -L -v
</strong>Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination
   11   752 ACCEPT     all  --  any    any     anywhere             anywhere            state RELATED,ESTABLISHED
    0     0 ACCEPT     icmp --  any    any     anywhere             anywhere
    0     0 ACCEPT     all  --  lo     any     anywhere             anywhere
    0     0 ACCEPT     tcp  --  any    any     anywhere             anywhere            state NEW tcp dpt:ssh
<span style="color: #0000ff;"><strong>    0     0 ACCEPT     tcp  --  any    any     anywhere             anywhere            state NEW tcp dpt:http
</strong></span>    0     0 REJECT     all  --  any    any     anywhere             anywhere            reject-with icmp-host-prohibited</pre>
<pre>Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination
    0     0 REJECT     all  --  any    any     anywhere             anywhere            reject-with icmp-host-prohibited</pre>
<pre>Chain OUTPUT (policy ACCEPT 10 packets, 1072 bytes)
 pkts bytes target     prot opt in     out     source               destination</pre>
<p>หลังจากอนุญาต http แล้ว ตอนนี้เครื่องอื่น ก็สามารถเรียกเว็บเซิร์ฟเวอร์ที่รันบนเครื่องนี้ได้แล้ว</p>
]]></content:encoded>
			<wfw:commentRss>http://spalinux.com/2009/07/customize_iptables_firewall_rules/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>คอนฟิก NAT บนลีนุกซ์ด้วย iptables</title>
		<link>http://spalinux.com/2009/02/configure_nat_iptables</link>
		<comments>http://spalinux.com/2009/02/configure_nat_iptables#comments</comments>
		<pubDate>Sun, 08 Feb 2009 16:39:49 +0000</pubDate>
		<dc:creator>editor</dc:creator>
				<category><![CDATA[Networking]]></category>
		<category><![CDATA[iptables]]></category>
		<category><![CDATA[nat]]></category>

		<guid isPermaLink="false">http://spalinux.com/?p=452</guid>
		<description><![CDATA[ในบทความนี้จะอธิบายการคอนฟิก NAT (Network Address Translation) บนลีนุกซ์ ด้วย iptables โดยจะแยกเป็นข้อต่างๆ ตามลักษณะการใช้งาน ซึ่งในแต่ละข้อ จะเคลียร์คอนฟิก rule ทั้งหมดของ nat ออกก่อน ด้วยออปชั่น &#8216;-F&#8217; แล้วเริ่มคอนฟิกใหม่ ทั้งนี้เพื่อให้ผู้อ่านสามารถนำไปทดสอบดูผลลัพธ์ที่เกิดขึ้นได้  แล้วหลังจากเข้าใจ สามารถนำ rule ต่างๆ มารวมกันเพื่อคอนฟิก NAT ในหลายรูปแบบพร้อมๆ กันได้ เครื่องที่ทดสอบ ติดตั้งลีนุกซ์ kernel 2.6 พอร์ตแลนที่ 1 : eth0,  ip address: 192.168.1.1/24 ต่อเน็ตเวิร์กภายในองค์การ พอร์ตแลนที่ 2 : eth1,  ip address: 172.16.1.1/24 ต่อออกภายนอก ปิด iptables rule ที่ทำหน้าที่เป็น firewall คอนฟิกลีนุกซ์ทำหน้าที่เป็น Router ทุกเครื่องที่อยู่เน็ตเวิร์กภายใน [...]]]></description>
			<content:encoded><![CDATA[<p>ในบทความนี้จะอธิบายการคอนฟิก NAT (Network Address Translation) บนลีนุกซ์ ด้วย iptables โดยจะแยกเป็นข้อต่างๆ ตามลักษณะการใช้งาน ซึ่งในแต่ละข้อ จะเคลียร์คอนฟิก rule ทั้งหมดของ nat ออกก่อน ด้วยออปชั่น &#8216;-F&#8217; แล้วเริ่มคอนฟิกใหม่ ทั้งนี้เพื่อให้ผู้อ่านสามารถนำไปทดสอบดูผลลัพธ์ที่เกิดขึ้นได้  แล้วหลังจากเข้าใจ สามารถนำ rule ต่างๆ มารวมกันเพื่อคอนฟิก NAT ในหลายรูปแบบพร้อมๆ กันได้</p>
<p><span id="more-452"></span></p>
<p><strong>เครื่องที่ทดสอบ</strong></p>
<ul>
<li>ติดตั้งลีนุกซ์ kernel 2.6</li>
<li>พอร์ตแลนที่ 1 : eth0,  ip address: 192.168.1.1/24 ต่อเน็ตเวิร์กภายในองค์การ</li>
<li>พอร์ตแลนที่ 2 : eth1,  ip address: 172.16.1.1/24 ต่อออกภายนอก</li>
<li>ปิด iptables rule ที่ทำหน้าที่เป็น firewall</li>
<li><a href="http://spalinux.com/2009/02/configure_linux_to_be_router">คอนฟิกลีนุกซ์ทำหน้าที่เป็น Router</a></li>
<li>ทุกเครื่องที่อยู่เน็ตเวิร์กภายใน (192.168.1.0/24) ชี้ default gateway ที่ 192.168.1.1</li>
</ul>
<hr />
<h4>
1. เปลี่ยน Source IP Address แบบ MASQUERADE</h4>
<p><strong>ความต้องการ:</strong> เปลี่ยน Source IP Address ของ packet ที่ส่งจากเน็ตเวิร์ก 192.168.1.0/24 ไปยังเครื่องภายนอก เช่น 172.16.1.0/24</p>
<pre>[root@linux-nat ~]# <strong>iptables -t nat -F
</strong>[root@linux-nat ~]# <strong>iptables -t nat -A POSTROUTING -s 192.168.1.0/24 -o eth1 -j MASQUERADE</strong>
[root@linux-nat ~]# <strong>iptables -t nat -L -v -n
</strong>Chain PREROUTING (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination</pre>
<pre>Chain <span style="color: #0000ff;">POSTROUTING </span>(policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination
<span style="color: #0000ff;">    0     0 MASQUERADE  all  --  *      eth1    192.168.1.0/24       0.0.0.0/0</span></pre>
<pre>Chain OUTPUT (policy ACCEPT 31 packets, 2328 bytes)
 pkts bytes target     prot opt in     out     source               destination</pre>
<p><strong>การใช้งาน:</strong></p>
<ul>
<li>เป็นการทำ NAT เพื่อให้เครื่องที่อยู่ในองค์กรที่ใช้ Private IP Address สามารถใช้งานเน็ตเวิร์กภายนอกหรืออินเตอร์เน็ตได้พร้อมกัน</li>
<li>IP Address ของพอร์ต eth1 ที่ต่อกับเน็ตเวิ์กภายนอก มีการเปลี่ยนแปลงไปเรื่อยๆ เช่น ต่อเน็ตโดยใช้ ADSL, คุณสมบัติของ MASQUERADE จะเปลี่ยน Source IP Address เป็น IP ของพอร์ต eth1 โดยอัตโนมัติ</li>
</ul>
<hr />
<h4>
2. เปลี่ยน Source IP Address แบบ SNAT</h4>
<p><strong>ความต้องการ: </strong>เปลี่ยน Source IP Address ใน packet ที่ส่งจากเน็ตเวิร์ก 192.168.1.0/24 ไปยังเครื่องภายนอก เช่น 172.16.1.0/24 ให้กลายเป็น Source IP 172.16.1.1</p>
<pre>[root@linux-nat ~]# <strong>iptables -t nat -F
</strong>[root@linux-nat ~]# <strong>iptables -t nat -A POSTROUTING -s 192.168.1.0/24 -j SNAT --to-source 172.16.1.1</strong>
[root@linux-nat ~]# <strong>iptables -t nat -L -v -n
</strong>Chain PREROUTING (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination</pre>
<pre>Chain <span style="color: #0000ff;">POSTROUTING</span> (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination
<span style="color: #0000ff;">    0     0 SNAT       all  --  *      *       192.168.1.0/24       0.0.0.0/0           to:172.16.1.1</span></pre>
<pre>Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination</pre>
<p><strong>การใช้งาน:</strong></p>
<ul>
<li>เป็นการทำ NAT เพื่อให้เครื่องที่อยู่ในองค์กรที่ใช้ Private IP Address สามารถใช้งานเน็ตเวิร์กภายนอกหรืออินเตอร์เน็ตได้พร้อมกัน</li>
<li>IP Address ของพอร์ต eth1 ที่ต่อกับเน็ตเวิ์กภายนอก ไม่เปลี่ยนแปลง มีการ fix ไว้ ต้องระบุ IP ลงไปในคำสั่งเลย หลังออปชั่น &#8216;&#8211;to-source&#8217;</li>
<li>ถ้าเรารู้ IP ของพอร์ตขานอกแน่นอน ให้เราใช้เป็น SNAT แล้วระบุ &#8216;&#8211;to-source&#8217; เพื่อเพิ่มประสิทธิภาพการทำ NAT เพราะว่าในการทำงาน ลีนุกซ์ไม่ต้องเสียเวลาไปค้นหา IP Address จากพอร์ตอีกครั้ง</li>
</ul>
<hr />
<h4>
3. เปลี่ยน Destination IP Address แบบ DNAT</h4>
<p>ความต้องการ: เปลี่ยน Destination IP Address ใน packet เพื่อส่งต่อ (redirect) packet ไปยัง IP Address ภายในที่ต้องการได้</p>
<p>ตัวอย่างเช่น เมื่อเน็ตเวิร์กภายนอกเชื่อมต่อมาที่ IP 172.16.1.1 พอร์ต 80 ให้ส่งต่อ packet นี้ไปยังเครื่องภายในที่มี IP 192.168.1.2 พอร์ต 80</p>
<pre>[root@linux-nat ~]# <strong>iptables -t nat -F
</strong>[root@linux-nat ~]# <strong>iptables -t nat -A PREROUTING  -p tcp -d 172.16.1.1  --dport 80 -j DNAT --to-destination 192.168.1.2:80
</strong>[root@linux-nat ~]# <strong>iptables -t nat -A POSTROUTING -p tcp -s 192.168.1.2 --sport 80 -j SNAT --to-source 172.16.1.1:80</strong>
[root@linux-nat ~]# <strong>iptables -t nat -L -v -n
</strong>Chain <span style="color: #0000ff;">PREROUTING</span> (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination
<span style="color: #0000ff;">    0     0 DNAT       tcp  --  *      *       0.0.0.0/0            172.16.1.1          tcp dpt:80 to:192.168.1.2:80</span></pre>
<pre>Chain <span style="color: #0000ff;">POSTROUTING</span> (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination
<span style="color: #0000ff;">    0     0 SNAT       tcp  --  *      *       192.168.1.2          0.0.0.0/0           tcp spt:80 to:172.16.1.1:80</span></pre>
<pre>Chain OUTPUT (policy ACCEPT 31 packets, 2328 bytes)
 pkts bytes target     prot opt in     out     source               destination</pre>
<p><strong>การใช้งาน</strong>:</p>
<ul>
<li>ตั้ง web server ไว้เน็ตเวิร์กภายในองค์กร แต่ต้องการให้คนภายนอกเช่นจากอินเตอร์เน็ตสามารถเรียกใช้งานได้ เราต้องคอนฟิกให้ภายนอกเชื่อมต่อเข้ามาที่ ip ของพอร์ตภายนอก (172.16.1.1) แล้วคอนฟิก DNAT เพื่อส่งต่อ packet เข้ามายังเครื่องภายใน</li>
<li>การคอนฟิกตัวอย่างด้านบน เป็นการระบุที่พอร์ตเลย เราสามารถใช้หลายๆ พอร์ต พร้อมกัน คอนฟิกจากพอร์ตนึงไปเป็นอีกพอร์ต หรือแต่ละพอร์ตส่งต่อไปยังหลายๆ เครื่องได้</li>
</ul>
<hr />
<h4>
เพิ่มคอนฟิกในไฟล์ /etc/sysconfig/iptables</h4>
<p>สุดท้ายหลังจากทดสอบการทำ NAT แบบต่างๆ ตามที่ต้องการได้แล้ว ต้องนำ rule ที่ได้ เพิ่มเข้าไปในไฟล์ /etc/sysconfig/iptables ซึ่งเป็นไฟล์กำหนด rule ของ iptables ที่ถูกโหลดโดย service iptables ตอนบู๊ตเครื่อง</p>
<p>วิธีการเพิ่มแบบแรกคือ จากคำสั่งที่ใช้รันตัดคำว่า iptables -t nat ออกไป แล้วพิมพ์ส่วนที่เหลือต่อท้ายบรรทัดที่มีคำว่า COMMIT ของคอนฟิก table filter เช่นต้องการเพิ่มคอนฟิกทำ DNAT จากข้อ 3 สามารถทำได้โดย</p>
<pre>[root@linux-nat ~]# <strong>cat /etc/sysconfig/iptables</strong>
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
COMMIT</pre>
<pre><strong>*nat
 <img src='http://spalinux.com/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' /> REROUTING ACCEPT [0:0]
 <img src='http://spalinux.com/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' /> OSTROUTING ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A PREROUTING  -p tcp -d 172.16.1.1 --dport 80 -j DNAT --to-destination 192.168.1.2:80
-A POSTROUTING -p tcp -s 192.168.1.2 --sport 80 -j SNAT --to-source 172.16.1.1:80
COMMIT</strong></pre>
<p>อีกวิธีการหนึ่งในการบันทีก rule ที่ใช้งานอยู่ ลงในไฟล์ /etc/sysconfig/iptables คือ รันคำสั่ง service iptables save</p>
<pre>[root@linux-nat ~]# <strong>iptables -t nat -L -v -n
</strong>Chain PREROUTING (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination
    0     0 DNAT       tcp  --  *      *       0.0.0.0/0            172.16.1.1           tcp dpt:80 to:192.168.1.2:80</pre>
<pre>Chain POSTROUTING (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination
    0     0 SNAT       tcp  --  *      *       192.168.1.2            0.0.0.0/0           tcp spt:80 to:172.16.1.1:80</pre>
<pre>Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination</pre>
<pre>[root@linux-nat ~]# <strong>service iptables save</strong>
iptables: Saving firewall rules to /etc/sysconfig/iptables:[  OK  ]</pre>
<pre>[root@linux-nat ~]# <strong>cat /etc/sysconfig/iptables</strong>
# Generated by iptables-save v1.4.1.1 on Sun Feb  8 19:25:37 2009
<span style="color: #0000ff;">*nat
 <img src='http://spalinux.com/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' /> REROUTING ACCEPT [0:0]
 <img src='http://spalinux.com/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' /> OSTROUTING ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A PREROUTING -d 172.16.1.1/32 -p tcp -m tcp --dport 80 -j DNAT --to-destination 192.168.1.2:80
-A POSTROUTING -s 192.168.1.2/32 -p tcp -m tcp --sport 80 -j SNAT --to-source 172.16.1.1:80
COMMIT
</span># Completed on Sun Feb  8 19:25:37 2009
# Generated by iptables-save v1.4.1.1 on Sun Feb  8 19:25:37 2009
*filter
:INPUT ACCEPT [425:31352]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [393:41372]
COMMIT
# Completed on Sun Feb  8 19:25:37 2009</pre>
<h4>ทดสอบการรีโหลด iptables rules ด้วยคำสั่ง service iptables</h4>
<p>เราสามารถใช้คำสั่ง service iptables restart เพื่อตรวจสอบคอนฟิกไฟล์ /etc/sysconfig/iptables ถูกต้อง</p>
<pre>[root@linux-nat ~]# <strong>service iptables restart
</strong>iptables: Flushing firewall rules:                         [  OK  ]
iptables: Setting chains to policy ACCEPT: nat filter      [  OK  ]
iptables: Unloading modules:                               [  OK  ]
iptables: Applying firewall rules:                         [  OK  ]</pre>
<h4>
ข้อมูลอ้างอิง</h4>
<ul>
<li><a href="http://spalinux.com/2009/02/configure_linux_to_be_router">คอนฟิกลีนุกซ์ทำหน้าที่เป็น Router</a></li>
<li><a href=" http://en.wikipedia.org/wiki/Network_address_translation" target="_blank">Network address translation &#8211; Wikipedia</a><br />
  </li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://spalinux.com/2009/02/configure_nat_iptables/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

