<?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>เพื่อชีวิตที่ผ่อนคลายของคนใช้ลีนุกซ์</description>
	<lastBuildDate>Sat, 12 May 2012 10:38:31 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>แก้ไขคอนฟิก firewall บนลีนุกซ์ด้วย iptables</title>
		<link>http://spalinux.com/2012/04/configure-linux-firewall-by-iptables</link>
		<comments>http://spalinux.com/2012/04/configure-linux-firewall-by-iptables#comments</comments>
		<pubDate>Sun, 08 Apr 2012 10:28:19 +0000</pubDate>
		<dc:creator>editor</dc:creator>
				<category><![CDATA[CentOS 6]]></category>
		<category><![CDATA[Firewall]]></category>
		<category><![CDATA[Linux Installation]]></category>
		<category><![CDATA[Linux Shell]]></category>
		<category><![CDATA[Networking]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[iptables]]></category>

		<guid isPermaLink="false">http://spalinux.com/?p=2013</guid>
		<description><![CDATA[บนลีนุกซ์มีคุณสมบัติ firewall ติดตั้งมาให้สามารถเลือกอนุญาต หรือปฏิเสธการ รับ/ส่ง packet เข้า/ออกเครื่องได้ จุดมุ่งหมายหลักๆ ของ firewall ก็เพื่อเพิ่มความปลอดภัยให้กับเครื่องเซิร์ฟเวอร์ หลายคนมักจะปิดคุณสมบัตินี้ไป ด้วยเหตุผลบอกว่ายากต่อการทำความเข้าใจ และแก้ไข ในบทความนี้จะอธิบายการใช้คำสั่ง iptables เพื่อแสดงสถานะ firewall ที่เปิดใช้งาน การเพิ่ม rule เพื่ออนุญาตการเชื่อมต่อ การลบ rule ที่ไม่ได้ใช้แล้ว ลองทำตามดู ในหลายเซอร์วิส เพียงแค่แก้ไข rule นิดเดียวเท่านั้น ก็จะใช้งานได้แล้ว ไม่จำเป็นต้องปิดคุณสมบัติ firewall แต่อย่างได้ แสดง (list) rule ที่เปิดใช้งานอยู่ ใช้คำสั่ง iptables ออปชั่น &#8220;-L&#8221; เพื่อแสดงคอนฟิก firewall ที่เปิดใช้งานอยู่ ออปชั่นเพิ่มเติม ที่มักจะใช้ร่วมกับออปชั่น &#8220;-L&#8221; ออปชั่น &#8220;-v&#8221; เพื่อแสดงรายละเอียดเพิ่มเติมของ rule เช่นผลรวมสะสมของจำนวน packets (pkts) [...]]]></description>
			<content:encoded><![CDATA[<p>บนลีนุกซ์มีคุณสมบัติ firewall ติดตั้งมาให้สามารถเลือกอนุญาต หรือปฏิเสธการ รับ/ส่ง packet เข้า/ออกเครื่องได้</p>
<p>จุดมุ่งหมายหลักๆ ของ firewall ก็เพื่อเพิ่มความปลอดภัยให้กับเครื่องเซิร์ฟเวอร์</p>
<p>หลายคนมักจะปิดคุณสมบัตินี้ไป ด้วยเหตุผลบอกว่ายากต่อการทำความเข้าใจ และแก้ไข</p>
<p>ในบทความนี้จะอธิบายการใช้คำสั่ง iptables เพื่อแสดงสถานะ firewall ที่เปิดใช้งาน การเพิ่ม rule เพื่ออนุญาตการเชื่อมต่อ การลบ rule ที่ไม่ได้ใช้แล้ว</p>
<p>ลองทำตามดู ในหลายเซอร์วิส เพียงแค่แก้ไข rule นิดเดียวเท่านั้น ก็จะใช้งานได้แล้ว ไม่จำเป็นต้องปิดคุณสมบัติ firewall แต่อย่างได้</p>
<p><span id="more-2013"></span></p>
<h4>แสดง (list) rule ที่เปิดใช้งานอยู่</h4>
<p>ใช้คำสั่ง iptables ออปชั่น &#8220;-L&#8221; เพื่อแสดงคอนฟิก firewall ที่เปิดใช้งานอยู่</p>
<p>ออปชั่นเพิ่มเติม ที่มักจะใช้ร่วมกับออปชั่น &#8220;-L&#8221;</p>
<ul>
<li>ออปชั่น &#8220;<strong>-v</strong>&#8221; เพื่อแสดงรายละเอียดเพิ่มเติมของ rule เช่นผลรวมสะสมของจำนวน packets (pkts) ขนาด (bytes)</li>
<li>ออปชั่น &#8220;<strong>-n</strong>&#8221; เพื่อแสดง IP (source และ destination) และพอร์ตให้เป็นตัวเลข</li>
<li>ออปชั่น &#8220;<strong>&#8211;line-numbers</strong>&#8221; เพื่อแสดงตัวเลขกำกับ rule ในแต่ละบรรทัด เพื่อสะดวกต่อการแก้ไข rule</li>
</ul>
<p>ตัวอย่างการใช้คำสั่ง iptables ออปชั่น &#8220;-L&#8221; เพื่อแสดงค่าคอนฟิก firewall ที่ใช้งานอยู่</p>
<pre>[root@cent6 ~]# <strong>iptables -L -v -n --line-numbers</strong>
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination
1      630 49714 ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0           state RELATED,ESTABLISHED
2        0     0 ACCEPT     icmp --  *      *       0.0.0.0/0            0.0.0.0/0
3        0     0 ACCEPT     all  --  lo     *       0.0.0.0/0            0.0.0.0/0
4        2   128 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0           state NEW tcp dpt:22
5        0     0 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0           reject-with icmp-host-prohibited 

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination
1        0     0 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0           reject-with icmp-host-prohibited 

Chain OUTPUT (policy ACCEPT 349 packets, 55278 bytes)
num   pkts bytes target     prot opt in     out     source               destination</pre>
<pre></pre>
<pre></pre>
<p>คอนฟิก rule ที่แสดงนี้ เป็นคอนฟิกดีฟอลต์จากติดตั้ง CentOS 6 (หากไม่ได้มีการเลือกแก้ไข firewall ระหว่างการติดตั้ง)</p>
<p>ในที่นี้ขออธิบาย rule คร่าวๆ ในส่วนของ Chain INPUT ที่เปิดใช้งานอยู่ดังนี้</p>
<ul>
<li>num 1 : อนุญาต (ACCEPT) ให้ทุก packet ที่ผ่านขั้นตอนการเชื่อมต่อเรียบร้อยแล้ว (RELATED, ESTABLISHED) เข้าสู่เครื่องได้</li>
<li>num 2 : อนุญาต (ACCEPT) ให้ packet ประเภท (icmp) เช่น ping เข้าสู่เครื่องได้</li>
<li>num 3 : อนุญาต (ACCEPT) ให้ทุก packet ที่ส่งภายในของเครื่องเองได้ (lo หรือ loopback)</li>
<li>num 4 : อนุญาต (ACCEPT) ให้ packet แรก (NEW) ที่พยายามเชื่อมต่อเข้าพอร์ตปลายทาง tcp dpt:22 (secure shell) สามารถเข้าสู่เครื่องได้ หรือสรุปง่ายๆ คืออนุญาตให้สามารถ ssh เข้าสู่เครื่องนี้ได้นั่นเอง</li>
<li>num 5 : packet อื่นๆ นอกจากนี้ จะถูกปฏิเสธ (REJECT) การเข้าถึงเครื่อง และจะมีการตอบ icmp-host-prohibited กลับไปยังเครื่องต้นทางที่พยายามส่ง packet เข้ามา</li>
</ul>
<p>การทำงาน firewall ของลีนุกซ์จะตรวจสอบ (match) ตามลำดับ rule num ที่คอนฟิกไว้ เช่นเมื่อมี packet พยายามส่งเข้า (INPUT) ก็จะตรวจสอบเรียงตาม num 1, 2, 3, 4, 5</p>
<p>&nbsp;</p>
<h4>เพิ่ม (insert) rule ใหม่</h4>
<p>หากเราติดตั้งเซอร์วิสใหม่ คอนฟิกและรันได้เรียบร้อยแล้ว เช่นเว็บเซิร์ฟเวอร์ เปิดใช้งานพอร์ต tcp 80</p>
<p>ตัวอย่างการใช้คำสั่ง netstat -an เพื่อดูพอร์ตที่เปิดอยู่ ในที่นี้จะใช้คำสั่ง grep เพื่อเลือกแสดงเฉพาะบรรทัดที่มีตัวเลข 80</p>
<pre>[root@cent6 ~]# <strong>netstat -an | grep 80</strong>
tcp        0      0 :::80                       :::*                        LISTEN</pre>
<p>เราสามารถทดสอบจากเครื่องที่รันเว็บเซิร์ฟเวอร์เองได้ เพราะ rule ของ firewall โดยดีฟอลต์อนุญาตให้การส่งข้อมูลภายใน (lo) สามารถทำได้ (rule num 3)</p>
<p>ตัวอย่างการใช้คำสั่ง telnet เพื่อทดสอบการเชื่อมต่อเข้าพอร์ต 80 ของเว็บเซิร์ฟเวอร์ 192.168.5.62 (ทดสอบบนเครื่องเดียวกัน กับที่รันเว็บเซิรฟ์เวอร์)</p>
<pre>[root@cent6 ~]# <strong>telnet 192.168.5.62 80</strong>
Trying 192.168.5.62...
Connected to 192.168.5.62.
Escape character is '^]'.

<strong>HEAD / HTTP/1.0</strong>

HTTP/1.1 403 Forbidden
Date: Sun, 08 Apr 2012 08:53:11 GMT
Server: Apache/2.2.15 (CentOS)
Accept-Ranges: bytes
Content-Length: 5039
Connection: close
Content-Type: text/html; charset=UTF-8

Connection closed by foreign host.</pre>
<p>คำอธิบายการใช้คำสั่ง telnet เข้าพอร์ต</p>
<ul>
<li>หากใช้คำสั่ง telnet แล้วขึ้น &#8220;command not found&#8221; ต้องติดตั้งไฟล์ rpm ของโปรแกรม telnet เพิ่ม ซึ่งอยู่ในแผ่นดีวีดีติดตั้ง CentOS อยู่แล้ว</li>
</ul>
<pre>[root@cent6 Packages]# <strong>rpm -i telnet-0.17-47.el6.x86_64.rpm</strong></pre>
<ul>
<li>เราสามารถใช้คำสั่ง telnet เพื่อทดสอบเข้าพอร์ตเว็บ (tcp 80) ได้ โดยพิมพ์คำสั่ง telnet ตามด้วย IP ของเว็บเซิร์ฟเวอร์ แล้วตามด้วยพอร์ตที่รันเว็บ</li>
<li>ในคำสั่ง telnet เมื่อขึ้น Connected แปลว่าสามารถเชื่อมต่อเข้าพอร์ตที่ระบะไว้ได้</li>
<li>กดปุ่ม [Enter] หนึ่งครั้ง แล้วพิมพ์คำสั่ง HEAD / HTTP/1.0 เพื่อเป็นการส่งคำสั่งขอดูข้อมูลเบื้องต้นของเว็บเซิร์ฟเวอร์ได้</li>
<li>หากต้องการออกจากคำสั่ง telnet ให้กดปุ่ม [Ctrl] และ ] พร้อมกัน จะมีพร้อมพต์ telnet&gt; ขึ้นมาให้พิมพ์คำสั่ง quit เพื่อออจาก telnet</li>
</ul>
<pre>[root@cent6 ~]# <strong>telnet 192.168.5.62 80</strong>
Trying 192.168.5.62...
Connected to 192.168.5.62.
Escape character is '^]'.
<strong>^]</strong>
telnet&gt; <strong>quit</strong>
Connection closed.</pre>
<p>แต่ถ้าใช้ browser หรือทดสอบด้วยคำสั่ง telnet เข้าพอร์ต 80 จากเครื่องอื่นๆ เปิดเว็บเข้ามา จะเข้าไม่ได้</p>
<pre>$ <strong>telnet 192.168.5.62 80</strong>
 Trying 192.168.5.62...
 telnet: connect to address 192.168.5.62: No route to host</pre>
<p>ต้องเพิ่ม rule ใหม่ เข้าไปเพื่ออนุญาตการเชื่อมต่อเข้ามา โดย rule ที่เพิ่มใหม่ ต้องเพิ่มก่อน rule สุดท้ายที่มีการปฏิเสธการเข้าถึง (REJECT) ทุก packet</p>
<p>ใช้คำสั่ง iptables ออปชั่น &#8220;-L&#8221; เพื่อแสดงคอนฟิก firewall ที่ใช้งานอยู่</p>
<pre>[root@cent6 ~]# <strong>iptables -L -v -n --line-numbers</strong>
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination
1     2363  182K ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0           state RELATED,ESTABLISHED
2        2   168 ACCEPT     icmp --  *      *       0.0.0.0/0            0.0.0.0/0
3       11   660 ACCEPT     all  --  lo     *       0.0.0.0/0            0.0.0.0/0
4        4   252 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0           state NEW tcp dpt:22
5       10   620 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0           reject-with icmp-host-prohibited 

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination
1        0     0 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0           reject-with icmp-host-prohibited 

Chain OUTPUT (policy ACCEPT 1358 packets, 169K bytes)
num   pkts bytes target     prot opt in     out     source               destination</pre>
<pre></pre>
<p>ใช้คำสั่ง iptables ออปชั่น &#8220;-I&#8221; เพิ่มด้วยการแทรก (insert) rule ใหม่เข้าไปแทนที่ rule num 5 ระบุว่าอนุญาต (ACCEPT) ให้การเชื่อมต่อใหม่ (NEW) เข้า (INPUT) พอร์ต tcp 80 ได้</p>
<pre>[root@cent6 ~]# <strong>iptables -I INPUT 5 -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT</strong></pre>
<p>ตรวจสอบผลลัพธ์ที่ได้จาก iptables ออปชั่น &#8220;-L&#8221;</p>
<pre>[root@cent6 ~]# <strong>iptables -L -v -n --line-numbers</strong>
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination
1     2383  184K ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0           state RELATED,ESTABLISHED
2        2   168 ACCEPT     icmp --  *      *       0.0.0.0/0            0.0.0.0/0
3       11   660 ACCEPT     all  --  lo     *       0.0.0.0/0            0.0.0.0/0
4        4   252 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0           state NEW tcp dpt:22
<span style="color: #0000ff;">5 0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:80</span>
6       10   620 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0           reject-with icmp-host-prohibited 

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination
1        0     0 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0           reject-with icmp-host-prohibited 

Chain OUTPUT (policy ACCEPT 5 packets, 692 bytes)
num   pkts bytes target     prot opt in     out     source               destination</pre>
<pre></pre>
<p>ทดสอบจากเครื่องอื่นด้วยการ telnet เข้าพอร์ต 80 อีกครั้ง จะสามารถเชื่อมต่อได้แล้ว</p>
<pre>$ <strong>telnet 192.168.5.62 80</strong>
Trying 192.168.5.62...
Connected to 192.168.5.62.
Escape character is '^]'.

<strong>HEAD / HTTP/1.0</strong>

HTTP/1.1 403 Forbidden
Date: Sun, 08 Apr 2012 09:06:10 GMT
Server: Apache/2.2.15 (CentOS)
Accept-Ranges: bytes
Content-Length: 5039
Connection: close
Content-Type: text/html; charset=UTF-8

Connection closed by foreign host.</pre>
<pre></pre>
<p>&nbsp;</p>
<h4>ลบ (delete) rule</h4>
<p>หากต้องการยกเลิก rule ที่ใส่ไว้ เช่นไม่ต้องการให้เครื่องอื่นสามารถเชื่อมต่อเข้าพอร์ต 80 ได้อีกต่อไปแล้ว สามารถทำได้โดยใช้คำสั่ง iptables ออปชั่น -D ตามด้วย rule num ที่ต้องการลบ (delete) ใช้คำสั่ง iptables ออปชั่น &#8220;-L&#8221; เพื่อแสดง rule ที่เปิดใช้งานอยู่</p>
<pre>[root@cent6 ~]# <strong>iptables -L -v -n --line-numbers</strong>
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination
1     2417  186K ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0           state RELATED,ESTABLISHED
2        2   168 ACCEPT     icmp --  *      *       0.0.0.0/0            0.0.0.0/0
3       11   660 ACCEPT     all  --  lo     *       0.0.0.0/0            0.0.0.0/0
4        4   252 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0           state NEW tcp dpt:22
<span style="color: #0000ff;">5 1 60 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:80</span>
6       10   620 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0           reject-with icmp-host-prohibited 

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination
1        0     0 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0           reject-with icmp-host-prohibited 

Chain OUTPUT (policy ACCEPT 27 packets, 5466 bytes)
num   pkts bytes target     prot opt in     out     source               destination</pre>
<p>สมมติว่าต้องการลบ rule ที่อนุญาตการเชื่อมต่อเข้า (INPUT) พอร์ต 80 หรือ rule num 5 สามารถทำได้โดย</p>
<pre>[root@cent6 ~]# <strong>iptables -D INPUT 5</strong></pre>
<p>ใช้คำสั่ง iptables -L จะเห็นว่า rule num 5 ถูกลบไป</p>
<pre>[root@cent6 ~]# <strong>iptables -L -v -n --line-numbers</strong>
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination
1     2518  194K ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0           state RELATED,ESTABLISHED
2        2   168 ACCEPT     icmp --  *      *       0.0.0.0/0            0.0.0.0/0
3       11   660 ACCEPT     all  --  lo     *       0.0.0.0/0            0.0.0.0/0
4        4   252 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0           state NEW tcp dpt:22
5       10   620 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0           reject-with icmp-host-prohibited 

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination
1        0     0 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0           reject-with icmp-host-prohibited 

Chain OUTPUT (policy ACCEPT 6 packets, 728 bytes)
num   pkts bytes target     prot opt in     out     source               destination</pre>
<p>&nbsp;</p>
<p>ลองทดสอบ telnet เข้าพอร์ต 80 จากเครื่องอื่นดู ก็ไม่สามารถเชื่อมต่อได้แล้ว</p>
<pre>$ telnet 192.168.5.62 80
 Trying 192.168.5.62...
 telnet: connect to address 192.168.5.62: No route to host</pre>
<p>&nbsp;</p>
<h4>ข้อมูลอ้างอิง</h4>
<ul>
<li><a title="View Details: แก้ไขคอนฟิก firewall ด้วย system-config-firewall" href="http://spalinux.com/2012/04/configure-firewall-using-system-config-firewall" rel="bookmark">แก้ไขคอนฟิก firewall ด้วย system-config-firewall</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://spalinux.com/2012/04/configure-linux-firewall-by-iptables/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<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>

