想在一台伺服器上用好253個住宅IP,做法是:把整段/24繫結到網絡卡上,再為每個服務指定出口IP——用Nginx proxy_bind/listen、curl --interface、Python的source_address、按使用者SNAT或策略路由,或在Docker中為容器單獨建網路——同時維護一張IP分配表,讓每個IP只有一個負責人、一個用途。本文以IMIDC原生住宅IP伺服器為例逐步講解,該產品附帶/24共253個原生家庭寬頻IPv4並提供root許可權,起價$2,188/月。
把整段地址都加到伺服器網絡卡上,先臨時測試,再寫入持久化配置。
交付資訊會註明閘道器、主IP以及IP段是否同網段直連;如與本例不同請以交付資訊為準,有疑問可諮詢IMIDC技術支援。
# 1) Check what is already configured (interface name may be eth0, ens3, enp1s0...)
ip -4 addr show dev eth0
ip route show default # expect: default via 203.0.113.1 dev eth0
# 2) Add the rest of the block at runtime (lost on reboot - good for a first test)
for i in $(seq 3 254); do
ip addr add 203.0.113.$i/24 dev eth0
done
# 3) Count: should print 253 (203.0.113.2 - 203.0.113.254)
ip -4 addr show dev eth0 | grep -c 'inet 203\.0\.113\.'
Ubuntu等使用netplan的系統,需要寫入永久配置。用netplan try應用,配置出錯會自動回滾,不至於把自己鎖在外面。
# /etc/netplan/50-cloud-init.yaml (excerpt) - keep your existing gateway/DNS lines
network:
version: 2
ethernets:
eth0:
addresses:
- 203.0.113.2/24
- 203.0.113.3/24
# ... one line per address ...
- 203.0.113.254/24
routes:
- to: default
via: 203.0.113.1
# Generate the 253 address lines instead of typing them:
for i in $(seq 2 254); do echo " - 203.0.113.$i/24"; done
# Apply with automatic rollback if you lose SSH:
netplan try
較老的Debian/CentOS可用/etc/network/interfaces或NetworkManager實現同樣效果(nmcli con mod eth0 +ipv4.addresses 203.0.113.3/24)。建議在維護視窗重啟一次,確認253個地址都能恢復。
253個地址繫結後,任何監聽0.0.0.0的服務都會在所有IP上暴露,因此要顯式繫結。
listen 203.0.113.20:443,而不是listen 443。# /etc/ssh/sshd_config - management only on the primary IP
ListenAddress 203.0.113.2
把IP分配給任何人之前,先確認流量確實從該IP發出,以及它的地理定位結果。
# Each IP should report itself as the source address
for i in 2 20 50 200; do
printf '203.0.113.%s -> ' "$i"
curl -s --max-time 10 --interface 203.0.113.$i https://ip.example.com; echo
done
把ip.example.com換成你信任的任意“查詢本機IP”介面。同時在目標平台可能使用的地理位置資料庫中查詢這些地址,記下顯示的運營商名稱。IMIDC交付的是註冊在當地運營商名下的IP,但沒有任何服務商能保證第三方資料庫或平台對每個地址的判定,請如實記錄檢測結果。
Linux預設使用主IP作為源地址,下面每種方法都能針對某個服務、使用者或容器覆蓋這一預設值。
| 方法 | 作用範圍 | 是否需要改程式碼 | 適用場景 |
|---|---|---|---|
Nginx listen / proxy_bind | 按站點或location | 只改配置 | 店鋪站點、反向代理、API閘道器 |
curl --interface | 按命令 | 否 | 定時任務、健康檢查、指令碼 |
Python source_address | 按連線/會話 | 是(改動很小) | 自有自動化程式和API客戶端 |
| iptables/nftables按UID做SNAT | 按Linux使用者 | 否 | 每位成員或每個任務一個IP |
策略路由(ip rule uidrange) | 按使用者設定預設源地址 | 否 | 效果同SNAT,但不用NAT規則 |
| Docker bridge + SNAT | 按容器網路 | 否 | 容器化應用固定出口IP |
| Docker ipvlan L2 | 按容器 | 否 | 需要在自己IP上接受入站的容器 |
# Inbound: each storefront answers only on its own residential IP
server {
listen 203.0.113.20:443 ssl;
server_name shop-tw.example.com;
# ssl_certificate ... ;
}
# Outbound: pick the source IP for upstream requests per site
map $host $egress_ip {
default 203.0.113.21;
shop-tw.example.com 203.0.113.21;
brand-b.example.com 203.0.113.22;
}
server {
listen 203.0.113.22:443 ssl;
server_name brand-b.example.com;
location /api/ {
proxy_pass https://partner-api.example.com;
proxy_bind $egress_ip;
}
}
# Shell / cron jobs
curl --interface 203.0.113.40 -s https://example.com/health
# Python standard library
import http.client
conn = http.client.HTTPSConnection("example.com", timeout=10,
source_address=("203.0.113.41", 0))
conn.request("GET", "/")
print(conn.getresponse().status)
# httpx
import httpx
transport = httpx.HTTPTransport(local_address="203.0.113.42")
with httpx.Client(transport=transport, timeout=10) as client:
print(client.get("https://example.com/").status_code)
# requests (pip install requests-toolbelt)
import requests
from requests_toolbelt.adapters.source import SourceAddressAdapter
s = requests.Session()
s.mount("https://", SourceAddressAdapter("203.0.113.43"))
print(s.get("https://example.com/").status_code)
為每位成員或每個任務建一個Linux使用者並固定到一個IP;除非你修改規則,該使用者執行的任何程式都不會從其他地址發出。
# One Linux user per team member or job, each pinned to one IP
useradd -m tw_ops01
iptables -t nat -A POSTROUTING -o eth0 -m owner --uid-owner tw_ops01 \
-j SNAT --to-source 203.0.113.50
# nftables equivalent
nft add rule ip nat postrouting oifname "eth0" meta skuid "tw_ops01" snat to 203.0.113.50
# Persist iptables rules (Debian/Ubuntu)
apt install -y iptables-persistent && netfilter-persistent save
# Alternative: policy routing by UID - sets the default source address
# for anything that user runs without an explicit bind
ip route add default via 203.0.113.1 dev eth0 src 203.0.113.60 table 160
ip rule add uidrange 1060-1060 lookup 160 priority 1060
# Check which source the kernel picks for that user
ip route get 198.51.100.10 uid 1060
方案A讓容器留在私有bridge網路中,把流量改寫為某個住宅IP;方案B直接把住宅IP分配給容器。
# A) Bridge network per IP, with your own SNAT rule
docker network create --subnet 172.30.70.0/24 \
-o com.docker.network.bridge.enable_ip_masquerade=false net_ip70
iptables -t nat -A POSTROUTING -s 172.30.70.0/24 -o eth0 \
-j SNAT --to-source 203.0.113.70
docker run -d --name app70 --network net_ip70 my-app:latest
# B) ipvlan L2: containers own residential IPs directly (shares the host MAC)
# Do NOT also bind these addresses on the host.
docker network create -d ipvlan -o parent=eth0 -o ipvlan_mode=l2 \
--subnet 203.0.113.0/24 --gateway 203.0.113.1 \
--ip-range 203.0.113.192/27 res_ipvlan
docker run -d --name web200 --network res_ipvlan --ip 203.0.113.200 nginx
ipvlan L2複用宿主機的MAC地址,上遊交換機一般都能接受;macvlan會為每個容器生成新的MAC,部分機房埠會限制,使用macvlan前請先與IMIDC技術支援確認。較新的Docker版本也提供了指定bridge SNAT源地址的選項,請查閱對應版本的文件。
按用途把整段劃分成若幹區間,讓任何人一眼就能看出某個地址是做什麼的。
| 區間 | 數量 | 用途 | 方法 |
|---|---|---|---|
| 203.0.113.2 | 1 | 伺服器主IP、SSH、管理 | 系統預設 |
| 203.0.113.10 – .29 | 20 | 入站網站和API | Nginx listen |
| 203.0.113.30 – .79 | 50 | 團隊成員的店鋪後臺 | 按UID SNAT |
| 203.0.113.80 – .159 | 80 | 應用與容器 | Docker SNAT / ipvlan |
| 203.0.113.160 – .239 | 80 | QA、自有網站的廣告與SEO核驗 | curl / Python |
| 203.0.113.240 – .254 | 15 | 備用與隔離 | 不啟用 |
堅持“一個IP一個負責人”的原則。如果每天把IP在不相關的任務之間來回挪,就浪費了穩定獨享IP段最大的價值。
像管理程式碼一樣管理IP規劃:一份納入版本控制的檔案,作為“誰在用哪個IP”的唯一依據。
# ip-allocation.csv (keep in git; review monthly)
ip,owner,purpose,method,since,notes
203.0.113.2,infra,server primary / SSH only,host,2026-10-01,never used for business traffic
203.0.113.20,web,shop-tw.example.com inbound,nginx listen,2026-10-02,
203.0.113.50,tw_ops01,store back-office,iptables SNAT uid,2026-10-03,
203.0.113.70,app70,price-check API (own SKUs),docker SNAT,2026-10-05,
203.0.113.241,-,quarantine,-,2026-10-09,removed from rotation after 403 spike
用一個小指令碼根據這份檔案生成防火牆和SNAT規則,這樣文件和線上配置就不會脫節。每月複查一次,有成員離職時也要及時更新。
定期檢查每個IP,一旦某個IP開始報錯或被列入黑名單,要快速處理。
#!/bin/sh
# Daily DNSBL check for the whole block (use your own recursive resolver;
# some blocklists refuse queries from large public resolvers)
for i in $(seq 2 254); do
ip=203.0.113.$i
rev=$(echo "$ip" | awk -F. '{print $4"."$3"."$2"."$1}')
for bl in zen.spamhaus.org bl.spamcop.net; do
host -W 2 "$rev.$bl" >/dev/null 2>&1 && echo "$(date +%F) $ip listed on $bl"
done
done
# Open connections per source IP (spot a runaway job)
ss -Htn state established | awk '{print $3}' | cut -d: -f1 | sort | uniq -c | sort -rn | head
按市場選國家;如果4核配置成為瓶頸,再單獨擴充套件算力。
先把地址繫結到網絡卡,再按服務設定源地址:Nginx用proxy_bind,curl用--interface,Python用source_address,或按Linux使用者設定SNAT/策略路由。如果都不設定,Linux會使用主IP。
可以。可以為每個容器網路設定一條SNAT規則指向一個住宅IP,也可以用ipvlan L2網路直接把住宅IP分配給容器。IMIDC原生住宅IP伺服器提供root許可權,兩種方式都能用。
做帳號運營、驗證、API呼叫和輕量網站通常夠用,因為每個IP的流量都不大。但它不適合大檔案下載或影片分發,如需其他配置可諮詢IMIDC銷售。
IMIDC原生住宅IP伺服器附帶/24共253個原生家庭寬頻IPv4,提供root許可權,六個國家均可線上下單,台北起價$2,188/月。
準備好做IP分配規劃了嗎?在原生住宅IP伺服器頁面選擇國家,在IP資源頁面對比其他方案;如需路由配置協助、定製IP數量或更高配置,請聯絡IMIDC銷售或提交工單。