若您的 JUSTG 雲伺服器已新增額外 IP,或您為 JUSTG 獨立伺服器申請了 /24 IP 段,必須先在 Linux 新增 IP 地址,該 IP 才能響應流量。本文說明如何用 ip addr add 臨時綁定額外 IPv4、如何在 Ubuntu 用 netplan、Debian 用 /etc/network/interfaces、CentOS/Rocky/AlmaLinux 用 nmcli 永久設定、IP 位於不同子網時如何設定來源路由,以及如何用迴圈綁定整段 /24。示例使用文件專用地址,請替換成客戶中心顯示的 IP、字首與閘道器。
步驟 1:確認網路介面名稱與目前的 IP
ip 的精簡模式會列出每個介面與其地址。請記下主 IP 所在的介面(常見為 eth0、ens3 或 enp1s0)以及預設閘道器。
ip -br a
ip route show default步驟 2:用 ip addr add 臨時新增 IP(測試用)
測試新地址最快的方法是臨時新增。重啟後就會消失,因此適合用來初步確認。
# add the extra IPv4 (not persistent, lost after reboot)
ip addr add 203.0.113.11/24 dev eth0
# confirm and test outgoing traffic from the new address
ip -br a show eth0
ping -c 3 -I 203.0.113.11 1.1.1.1
# remove it again if needed
ip addr del 203.0.113.11/24 dev eth0如果以新來源地址 ping 成功,代表該 IP 已正確路由到您的伺服器,可以改為永久設定。
步驟 3:在 Ubuntu 用 netplan 永久新增額外 IP
Ubuntu 的網路設定存放在 /etc/netplan/ 下的 YAML 檔案。把新地址加入該介面的 addresses 列表,縮排只能使用空格。
# /etc/netplan/50-cloud-init.yaml (the file name may differ)
network:
version: 2
ethernets:
eth0:
addresses:
- 203.0.113.10/24
- 203.0.113.11/24
routes:
- to: default
via: 203.0.113.1
nameservers:
addresses: [1.1.1.1, 8.8.8.8]netplan try # rolls back automatically if you lose access and do not confirm
netplan apply若伺服器由 cloud-init 部署,重啟後設定被覆蓋,可停用 cloud-init 的網路管理:
echo "network: {config: disabled}" > /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg步驟 4:在 Debian 用 /etc/network/interfaces 新增 IP
使用 ifupdown 的 Debian 允許同一介面有多個 iface 段,每多一個段就多一個地址。
# /etc/network/interfaces
auto eth0
iface eth0 inet static
address 203.0.113.10/24
gateway 203.0.113.1
iface eth0 inet static
address 203.0.113.11/24建議在 VNC 控制檯中應用,或在維護時段直接重啟:
systemctl restart networking步驟 5:在 CentOS、Rocky Linux、AlmaLinux 用 nmcli 新增 IP
這些系統使用 NetworkManager。連線名稱可能是 System eth0、eth0 或 Wired connection 1,請先列出確認。+ 代表附加地址,而不是取代主 IP。
nmcli con show # find the connection name
nmcli con mod "System eth0" +ipv4.addresses 203.0.113.11/24
nmcli con up "System eth0"
nmcli -g ipv4.addresses con show "System eth0"完成後執行 ip -br a,確認介面上兩個地址都存在。
步驟 6:IP 位於不同子網時設定來源路由
如果額外 IP 屬於另一個有獨立閘道器的子網,響應資料包可能以錯誤的來源從主閘道器送出而被丟棄。策略路由可讓來自額外 IP 的流量走它自己的閘道器:
# extra IP 198.51.100.10 with its own gateway 198.51.100.1
ip addr add 198.51.100.10/24 dev eth0
ip route add default via 198.51.100.1 dev eth0 table 100
ip rule add from 198.51.100.10 table 100
ip rule show要在重啟後保留,請使用對應的永久設定:
# netplan: add under eth0
routes:
- to: default
via: 203.0.113.1
- to: default
via: 198.51.100.1
table: 100
routing-policy:
- from: 198.51.100.10
table: 100
# nmcli (CentOS / Rocky / AlmaLinux)
nmcli con mod "System eth0" +ipv4.routes "0.0.0.0/0 198.51.100.1 table=100" \
+ipv4.routing-rules "priority 100 from 198.51.100.10 table 100"
nmcli con up "System eth0"步驟 7:在 JUSTG 獨立伺服器上綁定整段 /24
一段 /24 共有 256 個地址。請依分配資訊確認閘道器與可用範圍;以下示例中 .1 為閘道器,.2 到 .254 可用。用迴圈一次新增以便測試:
# example: 198.51.100.0/24, gateway 198.51.100.1, usable .2 - .254
for i in $(seq 2 254); do
ip addr add 198.51.100.$i/24 dev eth0
done
ip -4 -o addr show dev eth0 | wc -l永久設定時,用命令生成配置內容,不必手動輸入 253 行:
# Ubuntu (netplan): print the address lines, then paste them under "addresses:"
for i in $(seq 2 254); do echo " - 198.51.100.$i/24"; done
# Debian (/etc/network/interfaces): one line inside the eth0 stanza
up for i in $(seq 2 254); do ip addr add 198.51.100.$i/24 dev eth0; done
# CentOS / Rocky / AlmaLinux (NetworkManager)
ADDRS=$(for i in $(seq 2 254); do printf "198.51.100.%s/24," "$i"; done)
nmcli con mod "System eth0" +ipv4.addresses "${ADDRS%,}"
nmcli con up "System eth0"重啟後執行 ip -4 -o addr show dev eth0 | wc -l 計算數量,確認所有地址都已恢復。
常見問題
如何為 VPS 獲取更多 IPv4?
每臺 JUSTG 雲伺服器含 1 個 IPv4。如需新增 IPv4,請提交工單聯絡技術支援;獨立伺服器另有 /24 IP 段可選。所有云伺服器也都免費提供 IPv6。
新 IP 已出現在 ip -br a,但從外部連不到,該檢查什麼?
對照分配資訊確認字首與閘道器、確認防火牆允許該地址上的服務,並用 ping -I 測試。若仍失敗,可能該 IP 尚未路由到您的伺服器。
伺服器對外連線時使用哪個地址?
預設使用該子網的主要(第一個)地址。應用程式可以綁定特定 IP,也可以在路由上設定 src 引數。
如仍無法解決,請提交工單聯絡 JUSTG 技術支援,並附上 IP 分配資訊與 ip -br a 的輸出。