±z»Ýn«Ø¥ß¤@¨Ç NAT ³W«h¡M¨Ó§i¶D®Ö¤ßþ¨Ç³s½un§ïÅÜ¡M¦P®É¦p¦ó¥h§ïÅÜ¥¦Ì¡Cn°µ¨ì³oÂI¡M§ÚÌ»Ýn¤@Ó«D±`¦h¥Î³ ªº iptables
¤u¨ã¡M¦P®É«ü©w `-t nat' ¿ï¶µ§i¶D¥¦¥hקï NAT ªí®æ¡C
NAT ³W«hªºªí®æ§t¦³¤TÓ¦Cªí¥s°µ`chains' ¡R¨C¤@±ø³W«h³£«ö¶¶§ÇÀˬd¡Mª½¨ì§ä¨ì¤@Ӭ۲Ūº¤ñ¹ï¡C¸Ó¤TÓÃì´N¥s°µ PREROUTING (¹ï Destination NAT ¨Ó»¡¡M¦]¬°«Ê¥]º¥ý¬O¶Ç¤Jªº)¡NPOSTROUTING (¹ï Source NAT ¨Ó»¡¡M¦]¬°«Ê¥]¬OÂ÷¶}ªº)¡N¥H¤Î OUTPUT (¹ï Destination NAT ¨Ó»¡¡M¬O«ü¨º¨Ç¥Ñ¥»¾÷²£¥Íªº«Ê¥])¡C
°²¦p§Ú°÷ÃÀ³N¤Ñ¥÷ªº¸Ü¡M¤U±ªº¹Ï¥Ü±N·Ç½T¼ÒÀÀ¥X¤W±©Ò»¡ªº·§©À¡C
_____ _____
/ \ / \
PREROUTING -->[Routing ]----------------->POSTROUTING----->
\D-NAT/ [Decision] \S-NAT/
| ^
| __|__
| / \
| | OUTPUT|
| \D-NAT/
| ^
| |
--------> Local Process ------
©ó«ezªº¨C¤@ÂI¡M·í¤@Ó«Ê¥]³q¹L§ÚÌn¬d¬Ýªº¬ÛÃö³s½u¤§®É¡M¦pªG¥¦¬O¤@Ó·s«Ø³s½u¡M§Ú̬d¬Ý¥¦¦b NAT ªí®æ¸Ì¹ïÀ³ªºÃì¡M¬Ý¬Ý¯à¹ï¤§°µ¨Ç¤°»ò°Ê§@¡C¦Ó¥Ñ¦¹Àò±oªºµª®×´NÀ³¥Î©ó¸Ó³s½u±N¨Óªº©Ò¦³«Ê¥]¡C
iptables
¨ã¦³¦p«á©Ò¦Cªº³\¦h¼Ð·Ç¿ï¶µ¡C©Ò¦³¨º¨Ç±aÂù´î¸¹ªº¿ï¶µ³£¬O¥i¥HÁY¼gªº¡M¥un iptables
¤´¥i±N¤§»P¨ä¥¦¥i¯àªº¿ï¶µ°Ï¤À¶}¨Ó´N¦æ¡C¦pªG±zªº®Ö¤ß¥H¼Ò²Õ§Î¦¡¨Ó¤ä´© iptables ¡M±z´N»Ýnº¥ý¸ü¤J ip_tables.o ¡R `insmod ip_tables'¡C
³o¸Ì¡M³Ì«nªº¤@ӿﶵ¬Oªí®æ¿ï¾Ü¿ï¶µ¡R `-t' ¡C¹ï©ó©Ò¦³ªº NAT ¾Þ§@¡M±z·|·Q¥Î `-t nat' ¨Óªí¥Ü NAT ªí®æ¡C²Ä¤GÓ«nªº¿ï¶µ¬O¥H `-A' ¼W¥[¤@±ø·s³W«h¦ÜÃ쪺¥½ºÝ (¦p¡R`-A POSTROUTING')¡M©Î¥H `-I' ´¡¤J¦Ü«eºÝ(¦p¡R`-I PREROUTING')¡C
±z¥i¥H«ü©w±zn°µ NAT ªº«Ê¥]¨Ó·½¦a§} (`-s' ©Î `--source') »P¥Øªº¦a (`-d' or `--destination')¡C³o¨âӿﶵ«á±¥i¥H«á±µ¤@Ó³æ¤@ªº IP ¦a§} (¦p¡R192.168.1.1)¡M©Î¤@Ó¦WºÙ (¦p¡R www.gnumonks.org)¡M©Î¤@Óºô¸ô¦a§} (¦p¡R192.168.1.0/24 ©Î 192.168.1.0/255.255.255.0)¡C
±z¤]¥i¥H«ü©wn¤ñ¹ïªº¶Ç¤J (`-i' ©Î `--in-interface') ©M¶Ç¥X (`-o' or `--out-interface') ¬É±¡M¦ýþ¤@Ӭɱ¥i¥H«ü©w«h¨ú¨M©ó±zn±N³W«h¼g¤Jþ¤@ÓÃì¥h¡R¹ï©ó PREROUTING ¡M±z¥i¥H¿ï¾Ü¶Ç¤J¬É±¡M¦ý¹ï©ó POSTROUTING (¥H¤Î OUTPUT)¡M±z¥i¥H¿ï¾Ü¶Ç¥X¬É±¡C¦pªG±z¤£¤p¤ß¥Î¿ù¤F¡M iptables
´N·|µ¹±z¤@Ó¿ù» ¡C
§Ú«e±¤w¸g»¡¹L¡M±z¥i¥H«ü©w¨Ó·½©M¥Øªº¦a¦a§}¡C¦pªG±z¬Ù²¤¨Ó·½¦a§}ªº¿ï¶µ¡M¨º»ò´Nªx«ü¥ô¦ó¨Ó·½¡C¦pªG±z¬Ù²¤¥Øªº¦a¦a§}¡M«hªx«ü©Ò¦³¥Øªº¦a¦a§}¡C
±zÁÙ¥i¥H«ü©w¤@Ó¯S©w¨ó©w (`-p' or `--protocol')©O¡M¨Ò¦p TCP ©Î UDP¡R¥u¦³³o¨Ç¨ó©wªº«Ê¥]¤ ²Å¦X¸Ó³W«h¡C¨ä¥Dnì¦]¬O¡M«ü©w tcp ©Î udp ¨ó©w¥i¥H¤¹³\§ó¦h¿ï¶µ¡R¤×¨ä¬O `--source-port' »P `--destination-port' ¿ï¶µ (ÁY¼g¬° `--sport' »P `--dport' )¡C
³o¨Ç¿ï¶µ¥i¥HÅý±z«ü©w¥u¦³þ¨Ç¯S©w¨Ó·½©M¥Øªº¦a°ð¤fªº«Ê¥]¤ ²Å¦X¸Ó³W«h¡C³o¦b±zn«¾É web ½Ð¨D (TCP port 80 ©Î 8080) ¦ý¤S©È¼vÅT¨ä¥¦«Ê¥]ªº®ÉÔ¡M´N«Ü¦n¥Î¤F¡C
³o¨Ç¿ï¶µ¥²¶·±µ¦b `-p' ¿ï¶µªº«á±(³o·|¦b¬°¸Ó¨ó©w¸ü¤J¦@¨É¨ç¦¡®w®É¦³°Æ§@¥Î)¡C±z¥i¥H¨Ï¥Î°ð¤f¸¹½X¡M©ÎªÌ¬O¦b /etc/services Àɤ¤ªº¦WºÙ¡C
©Ò¦³³o¨Ç±z¯à¿ï¾Üªº«Ê¥]¤§¤£¦P« ½è¡M³£¸Ô²Ó¦C¦b¨ºÓ¸Ô²Ó±o¦³ÂI®£©Æªº manual page ¤¤¤F(man iptables
)¡C