การทำ NAT เบื่องต้นในเครื่องลินุกซ์
Back to articles

การทำ NAT เบื่องต้นในเครื่องลินุกซ์

การทำ NAT เป็นสิ่งที่สำคัญเมื่อคุณมี Public IP แค่ไอพีเดียวแต่อยากเข้าถึงเครื่องภายในวงเน็ตจากภายนอก

Linux Updated: January 23, 2026

การตั้งค่า NAT Port Forwarding ด้วย iptables ใน Linux

1. ตั้งค่า PREROUTING

คำสั่งนี้จะรับข้อมูล TCP ที่เข้ามาที่อินเตอร์เฟซ ens3f1 บนพอร์ต 3390 แล้วนำไปสู่ IP ภายใน 192.168.122.154 บนพอร์ต 3389.

sudo iptables -t nat -A PREROUTING -p tcp -i ens3f1 --dport 3390 -j DNAT --to-destination 192.168.122.154:3389

2. ตั้งค่า FORWARD

คำสั่งนี้จะอนุญาตให้ส่งแพ็กเก็ต TCP ที่มีปลายทางเป็น IP 192.168.122.154 บนพอร์ต 3389 หากการเชื่อมต่อเป็นใหม่, ตั้งค่าแล้ว, หรือเกี่ยวข้อง.
sudo iptables -A FORWARD -p tcp -d 192.168.122.154 --dport 3389 -m state --state NEW,ESTABLISHED,RELATED -j ACCEPT

3. บันทึกการตั้งค่า iptables

คำสั่งนี้จะบันทึกการตั้งค่า iptables ปัจจุบันเพื่อไม่ให้กฎหายหลังจากการเริ่มระบบใหม่.
sudo iptables-save
หมายเหตุ: หากทำ NAT ในเซิฟเกมหรือเซอวิซที่มีการใช้ authenticate ด้วยไอพีอาจจำเป็นต้องใช้ คำสั่ง:
sudo iptables -t nat -A POSTROUTING -j MASQUERADE
เพื่อให้เปลี่ยนไอพีของไคเอนเป็น Public IP

D

DriteStudio | ไดรท์สตูดิโอ

Cloud, VPS, Hosting and Colocation provider in Thailand

Operated by Craft Intertech (Thailand) Co., Ltd.

Manage your cookie settings

We use different types of cookies to optimize your experience on our website. Click on the categories below to learn more and customize your preferences. Note that blocking some types of cookies may impact your experience.

Necessary Cookies

These cookies are essential for the website to function properly. They enable basic functions like page navigation and access to secure areas.

View cookies used
  • Session cookies (session management)
  • Security cookies (CSRF protection)
Always On

Functional Cookies

These cookies enable personalized features like language preferences and theme settings. Without these, some features may not work properly.

View cookies used
  • lang (language preference)
  • theme (dark/light mode)

Analytics Cookies

These cookies help us understand how visitors interact with our website by collecting and reporting information anonymously.

View cookies used
  • _ga (Google Analytics)
  • _gid (Google Analytics)

Marketing Cookies

These cookies are used to track visitors across websites to display relevant advertisements based on your interests.

View cookies used
  • Advertising cookies
  • Remarketing pixels

Privacy Policy