บทความนี้อธิบายวิธีตั้งค่า Additional IP Address บน AlmaLinux 9 ด้วย NetworkManager Policy Routing เพื่อให้ Additional IP Address สามารถใช้งานผ่าน Gateway ของตนเองได้อย่างถูกต้อง
ก่อนดำเนินการตามคู่มือนี้ จำเป็นต้อง Assign Additional IP Address ให้กับ Cloud VPS จาก Cloud Portal ให้เรียบร้อยก่อน หากยังไม่ได้ดำเนินการ Additional IP จะไม่สามารถใช้งานบนระบบปฏิบัติการได้
วิธีการ Assign Additional IP Address ให้กับ Cloud VPS
วิธีตั้งค่า Additional IP บน AlmaLinux 9 ด้วย NetworkManager
ก่อนเริ่มดำเนินการ กรุณาตรวจสอบว่าท่านมีข้อมูลดังต่อไปนี้เรียบร้อยแล้ว
- Primary IP Address
- Additional IP Address
- Gateway ของ Additional IP Address
- สามารถเชื่อมต่อ SSH เข้าสู่เซิร์ฟเวอร์ได้
- ใช้งานบัญชี root หรือบัญชีที่สามารถใช้คำสั่ง sudo ได้
คู่มือนี้ใช้สำหรับ AlmaLinux 9 ที่บริหารจัดการ Network ด้วย NetworkManager (nmcli)
กรุณาดำเนินการตามขั้นตอนดังต่อไปนี้
Step 1: Backup Network Configuration
สำรอง Network Configuration ก่อนเริ่มแก้ไข
mkdir -p /root/network-backup-$(date +%F-%H%M)
cp -a /etc/NetworkManager/system-connections/* /root/network-backup-$(date +%F-%H%M)/ 2>/dev/nullStep 2: ตั้งค่า IPv4 ให้ Interface
กำหนด Additional IP Address ให้กับ Interface และป้องกันไม่ให้ Interface นี้ถูกใช้เป็น Default Route หลัก
nmcli con modify "Wired connection 1" \
ipv4.method manual \
ipv4.addresses "118.27.152.57/23" \
ipv4.never-default yesหาก Interface เดียวกันมี Additional IP Address มากกว่า 1 IP สามารถกำหนดหลาย IP ได้ เช่น ipv4.addresses "118.27.152.57/23,118.27.152.58/23,118.27.152.59/23" โดยทุก IP สามารถใช้งาน Gateway เดียวกันได้
Step 3: เพิ่ม Route
กำหนด Routing Table สำหรับ Additional IP Address
nmcli con modify "Wired connection 1" \
+ipv4.routes "118.27.152.0/23 table=102" \
+ipv4.routes "0.0.0.0/0 118.27.152.1 table=102"Step 4: เพิ่ม Routing Rule
กำหนด Routing Rule ให้ Traffic ที่มี Source IP ของ Additional IP Address ใช้งาน Routing Table 102
nmcli con modify "Wired connection 1" \
+ipv4.routing-rules "priority 102 from 118.27.152.57/32 table 102"กรณีมี Additional IP Address มากกว่า 1 IP และใช้งานผ่าน Interface และ Gateway เดียวกัน สามารถใช้ Routing Table เดียวกันได้ เช่น table 102 โดยเพิ่ม Routing Rule สำหรับแต่ละ Source IP และกำหนดค่า Priority ไม่ให้ซ้ำกัน ตัวอย่าง nmcli con modify "Wired connection 1" \ +ipv4.routing-rules "priority 102 from 118.27.152.57/32 table 102" \ +ipv4.routing-rules "priority 103 from 118.27.152.58/32 table 102" \ +ipv4.routing-rules "priority 104 from 118.27.152.59/32 table 102"
Step 5: Apply Network
ตรวจสอบชื่อ Connection ก่อนใช้งานคำสั่ง
nmcli connection showจากนั้นปิดและเปิด Connection ใหม่เพื่อให้ค่าที่ตั้งไว้มีผล
nmcli con down "Wired connection 1"
nmcli con up "Wired connection 1"ชื่อ Connection ของแต่ละเครื่องอาจแตกต่างกัน เช่น Wired connection 1, System eth1 หรือชื่ออื่น ๆ กรุณาตรวจสอบด้วยคำสั่ง nmcli connection show ก่อนใช้งาน
Step 6: ทดสอบการทำงาน
ทดสอบว่า Additional IP Address สามารถออก Internet ได้ถูกต้อง
curl --interface 118.27.152.57 https://ifconfig.meผลลัพธ์ควรแสดงเป็น
118.27.152.57ตรวจสอบหลังตั้งค่า
ตรวจสอบข้อมูล Network และ Policy Routing
ip a show eth1
ip route
ip rule show
ip route show table 102หลังตั้งค่าเรียบร้อย • Primary Interface จะยังคงเป็น Default Route หลักของระบบ • Additional IP Address จะใช้งานผ่าน Gateway ของตนเอง • หากมีหลาย Additional IP Address ที่ใช้ Gateway เดียวกัน สามารถใช้ Routing Table เดียวกันได้ โดยเพิ่ม Routing Rule แยกตามแต่ละ Source IP และกำหนด Priority ไม่ให้ซ้ำกัน
บทความที่เกี่ยวข้อง
- วิธีการ Assign Additional IP Address ให้กับ Cloud VPS
- วิธีเพิ่ม Additional IP Address เข้า Plesk (เฉพาะผู้ใช้งาน Plesk)
English Version (Click to expand)
This article explains how to configure an Additional IP Address on AlmaLinux 9 using NetworkManager Policy Routing, allowing the additional IP address to route traffic through its own gateway correctly.
How to Configure an Additional IP Address on AlmaLinux 9 with NetworkManager
Before you begin, make sure you have the following information available:
- Primary IP Address
- Additional IP Address
- Gateway for the Additional IP Address
- SSH access to the server
- Root privileges or a user account with sudo permission
This guide is intended for AlmaLinux 9 servers that manage networking using NetworkManager (nmcli).
Step 1: Backup the Network Configuration
Create a backup of the current network configuration before making any changes.
mkdir -p /root/network-backup-$(date +%F-%H%M)
cp -a /etc/NetworkManager/system-connections/* /root/network-backup-$(date +%F-%H%M)/ 2>/dev/nullStep 2: Configure IPv4 for the Interface
Assign the Additional IP Address to the network interface and prevent it from becoming the default route.
nmcli con modify "Wired connection 1" \
ipv4.method manual \
ipv4.addresses "118.27.152.57/23" \
ipv4.never-default yesIf the same interface contains multiple Additional IP Addresses, you can configure them together, for example: ipv4.addresses "118.27.152.57/23,118.27.152.58/23,118.27.152.59/23" All IP addresses can share the same gateway.
Step 3: Add Static Routes
Create a dedicated routing table for the Additional IP Address.
nmcli con modify "Wired connection 1" \
+ipv4.routes "118.27.152.0/23 table=102" \
+ipv4.routes "0.0.0.0/0 118.27.152.1 table=102"Step 4: Add Routing Rules
Create a routing rule so that traffic originating from the Additional IP Address uses routing table 102.
nmcli con modify "Wired connection 1" \
+ipv4.routing-rules "priority 102 from 118.27.152.57/32 table 102"If multiple Additional IP Addresses are assigned to the same interface and use the same gateway, they can share the same routing table (for example, table 102). Create one routing rule for each source IP and assign a unique priority value. Example: nmcli con modify "Wired connection 1" \ +ipv4.routing-rules "priority 102 from 118.27.152.57/32 table 102" \ +ipv4.routing-rules "priority 103 from 118.27.152.58/32 table 102" \ +ipv4.routing-rules "priority 104 from 118.27.152.59/32 table 102"
Step 5: Apply the Configuration
Verify the connection name before running the commands.
nmcli connection showRestart the connection to apply the new configuration.
nmcli con down "Wired connection 1"
nmcli con up "Wired connection 1"Connection names vary between systems, such as Wired connection 1, System eth1, or another custom name. Always verify the correct connection name using: nmcli connection show
Step 6: Verify the Additional IP Address
Confirm that the Additional IP Address can access the Internet successfully.
curl --interface 118.27.152.57 https://ifconfig.meThe expected output should be:
118.27.152.57Verify the Routing Configuration
ip a show eth1
ip route
ip rule show
ip route show table 102After the configuration is complete: • The primary interface remains the system's default route. • The Additional IP Address routes traffic through its own gateway. • Multiple Additional IP Addresses using the same gateway can share the same routing table by creating separate routing rules with unique priority values.