绑定弹性 IPv4 地址
最后更新于
您可以将最多 4 个弹性 IPv4 地址绑定到一个内网 IP。
单击实例名称,进入实例 详情 > 信息 > 网卡。
选择您要绑定弹性 IPv4 的内网 IP,然后进入 操作 > 绑定弹性 IPv4。

选择一个所需的弹性 IPv4 地址并点击 确认 进行绑定。如果没有所需的弹性 IPv4 地址,点击 新建弹性 IPv4 创建一个新的 IPv4。 详细步骤请参见 创建弹性 IPv4。

注意
您也可以进入 弹性 IPv4 > 操作 > 绑定内网 IP 来绑定弹性 IPv4 地址。

1.1 适用场景
仅支持单网卡(NIC)环境
支持多个 IP(最多 254 个)
所有 IP 必须为 Bypass 模式
1.2 支持的操作系统
Ubuntu:20.04 / 22.04 / 24.04
Debian:10 / 11 / 12
CentOS:7 / 9
Rocky Linux:8.10 / 9.6
1.3 注意事项
执行脚本后必须重启,否则配置不会生效
不支持混合模式(Bypass 模式 + NAT 模式)
创建命令:
路径:
控制台 → 编排服务 → 命令 → 创建命令
运行命令:
路径:
控制台 → 云服务器 → 云服务器实例列表 → 操作 → 运维 → 执行命令 → 选择命令 → 确认
运行已创建的命令。
重启实例: 执行完成后,必须手动重启实例。
添加额外 IP(可选): 如需绑定更多 IP,请重复以下步骤:
绑定 IP
执行命令
重启实例
创建计算实例: 按照标准流程创建一个实例。
创建并绑定 EIP: 创建一个弹性公网 IP(EIP)。 将其以 Bypass 模式绑定到实例上。
通过 API 运行命令: API 文档: https://docs.console.zenlayer.com/api-reference/security/zos/command/createcommand 使用下方提供的脚本
重启实例 调用 API 重启实例以使配置生效。
添加更多 IP(可选) 如需绑定更多 IP,重复以下步骤: - 绑定 IP - 运行命令 - 重启实例
3.1 配置未生效
原因: 未重启实例 解决方案: 重启实例
3.2 配置失败
确保所有 IP 均为 Bypass 模式
确认操作系统在支持列表内
最后更新于
#!/bin/sh -e
# Task Description:
# Used for initializing configuration in single NIC EIP full Bypass mode scenarios.
# Supports from 1 public IP up to 254 public IPs.
# Execute this task after external resource environment is ready.
# Adapted for ubuntu20.04, 22.04, 24.04, debian10, 11, 12, centos7, centos9, rocky9.6, rocky8.10 (10 systems)
# ===== Global Variable Definitions =====
TASK_NAME=single_nic_bypass_init
META_CURL="curl -sf"
META_URL="http://169.254.169.254/latest/meta-data/network/interfaces/macs"
NIC_NAME=eth0
PRI_GW4=
PRI_IP4=
PRI_PREFIX=
SYSTEM=
DEFAULT_PUB_EIP4=
ALL_PUB_EIP4s=
NAME_SERVER1=8.8.8.8
NAME_SERVER2=1.1.1.1
# =======================
CheckSupportedOS() {
if [ ! -f /etc/os-release ]; then
echo "Error: Unsupported system: /etc/os-release not found"
exit 1
fi
. /etc/os-release
if [ "$ID" = "ubuntu" ]; then
if [ "$VERSION_ID" = "20.04" ] || [ "$VERSION_ID" = "22.04" ] || [ "$VERSION_ID" = "24.04" ]; then
echo "Supported OS: Ubuntu $VERSION_ID"
SYSTEM=ubuntu
return 0
else
echo "Error: Unsupported Ubuntu version: $VERSION_ID"
exit 1
fi
elif [ "$ID" = "debian" ]; then
if [ "$VERSION_ID" = "11" ] || [ "$VERSION_ID" = "10" ] || [ "$VERSION_ID" = "12" ]; then
echo "Supported OS: Debian $VERSION_ID"
SYSTEM=debian
return 0
else
echo "Unsupported Debian version: $VERSION_ID"
exit 1
fi
elif [ "$ID" = "centos" ] || [ "$ID" = "rhel" ]; then
if [ "$VERSION_ID" = "7" ] && [ -f /etc/centos-release ]; then
echo "Supported OS: CentOS $VERSION_ID"
SYSTEM=centos
return 0
elif [ "$VERSION_ID" = "9" ] && [ -f /etc/centos-release ]; then
echo "Supported OS: CentOS $VERSION_ID"
SYSTEM=centos
return 0
else
echo "Unsupported CentOS/RHEL version: $VERSION_ID"
exit 1
fi
elif [ "$ID" = "rocky" ]; then
if [ "$VERSION_ID" = "9.6" ] || [ "$VERSION_ID" = "8.10" ]; then
echo "Supported OS: rocky $VERSION_ID"
SYSTEM=rocky
return 0
else
echo "Unsupported rocky version: $VERSION_ID"
exit 1
fi
else
echo "Unsupported OS: $ID $VERSION_ID"
exit 1
fi
}
ConfigUbuntuNetwork() {
cat > /etc/netplan/50-cloud-init.yaml << EOF
network:
version: 2
ethernets:
eth0:
addresses:
- ${PRI_IP4}/${PRI_PREFIX}
EOF
for EIP in $ALL_PUB_EIP4s; do
echo " - ${EIP}/32" >> /etc/netplan/50-cloud-init.yaml
done
cat >> /etc/netplan/50-cloud-init.yaml << EOF
nameservers:
addresses:
- ${NAME_SERVER1}
- ${NAME_SERVER2}
EOF
# Default route
cat >> /etc/netplan/50-cloud-init.yaml << EOF
routes:
- to: default
via: ${PRI_GW4}
from: ${DEFAULT_PUB_EIP4}
EOF
}
ConfigCentOSNetwork() {
rm -f /etc/sysconfig/network-scripts/ifcfg-eth0*
cat << EOF > /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
BOOTPROTO=static
ONBOOT=yes
IPADDR=${PRI_IP4}
PREFIX=${PRI_PREFIX}
EOF
index=0
for EIP in $ALL_PUB_EIP4s; do
cat << EOF > /etc/sysconfig/network-scripts/ifcfg-eth0:$index
DEVICE=eth0:$index
BOOTPROTO=static
ONBOOT=yes
IPADDR=${EIP}
PREFIX=32
EOF
index=$((index+1))
done
# Default route
cat << EOF > /etc/sysconfig/network-scripts/route-eth0
default via ${PRI_GW4} dev eth0 src ${DEFAULT_PUB_EIP4}
EOF
# Disable DHCP on eth2
cat << EOF > /etc/sysconfig/network-scripts/ifcfg-eth2
DEVICE=eth2
BOOTPROTO=none
ONBOOT=yes
EOF
}
ConfigDebianNetwork() {
cat > /etc/network/interfaces.d/50-cloud-init << EOF
auto lo
iface lo inet loopback
dns-nameservers ${NAME_SERVER1} ${NAME_SERVER2}
auto eth0
iface eth0 inet static
address ${PRI_IP4}/${PRI_PREFIX}
auto eth0:0
iface eth0:0 inet static
address ${DEFAULT_PUB_EIP4}/32
up ip route add default via ${PRI_GW4} dev eth0 src ${DEFAULT_PUB_EIP4}
down ip route del default via ${PRI_GW4} dev eth0 src ${DEFAULT_PUB_EIP4}
EOF
index=1
for EIP in $ALL_PUB_EIP4s; do
if [ "$EIP" = "$DEFAULT_PUB_EIP4" ]; then
continue
fi
cat >> /etc/network/interfaces.d/50-cloud-init << EOF
auto eth0:$index
iface eth0:$index inet static
address ${EIP}/32
EOF
index=$((index+1))
done
}
FetchMetaData() {
pri_macs=$($META_CURL $META_URL/ | sed 's/\///g')
nic_count=$(echo "$pri_macs" | wc -w | tr -d ' ')
if [ "$nic_count" -ne 1 ]; then
echo "[ERR] Expected exactly one nic, got $nic_count: $pri_macs" >&2
exit 4
fi
PRI_IP4=$($META_CURL $META_URL/$pri_macs/primary-ip-address)
pri_subnet_cidr=$($META_CURL $META_URL/$pri_macs/subnet-cidr-block)
PRI_PREFIX=$(echo "$pri_subnet_cidr" | cut -d'/' -f2)
PRI_GW4=$($META_CURL $META_URL/$pri_macs/gateway)
DEFAULT_PUB_EIP4=$($META_CURL $META_URL/$pri_macs/public-ipv4)
ALL_PUB_EIP4s=$($META_CURL $META_URL/$pri_macs/public-ipv4s | sed 's/\[//;s/\]//;s/"//g;s/,/ /g')
if [ -z "$pri_macs" ] || [ -z "$PRI_IP4" ] || [ -z "$pri_subnet_cidr" ] || [ -z "$PRI_PREFIX" ] || [ -z "$PRI_GW4" ] || [ -z "$DEFAULT_PUB_EIP4" ] || [ -z "$ALL_PUB_EIP4s" ]; then
echo "[ERR] Missing metadata, check EC2 metadata service." >&2
exit 3
fi
echo " Private MacAddr: $pri_macs"
echo " Private IPv4: $PRI_IP4"
echo " Private Prefix: $PRI_PREFIX"
echo " Private GWv4: $PRI_GW4"
echo " ALl Public IPv4: $ALL_PUB_EIP4s"
echo " Default Public IPv4: $DEFAULT_PUB_EIP4"
}
ConfigNetwork() {
# Disable RPF (Reverse Path Filtering)
if ! grep -q "net.ipv4.conf.all.rp_filter=0" /etc/sysctl.conf; then
echo "net.ipv4.conf.all.rp_filter=0" | tee -a /etc/sysctl.conf > /dev/null
sysctl -p /etc/sysctl.conf
fi
# Disable cloud-init network configuration
echo "network: {config: disabled}" > /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg
if [ "$SYSTEM" = "ubuntu" ]; then
ConfigUbuntuNetwork
elif [ "$SYSTEM" = "centos" ]; then
ConfigCentOSNetwork
elif [ "$SYSTEM" = "debian" ]; then
if [ "$VERSION_ID" = "11" ] || [ "$VERSION_ID" = "10" ]; then
ConfigDebianNetwork
elif [ "$VERSION_ID" = "12" ]; then
ConfigUbuntuNetwork
else
echo "Unsupported Debian version: $VERSION_ID"
exit 1
fi
elif [ "$SYSTEM" = "rocky" ]; then
ConfigCentOSNetwork
else
echo "Unsupported OS for network config: $SYSTEM"
exit 1
fi
}
echo "1. Check OS..."
CheckSupportedOS
echo
echo "2. FetchMetaData..."
FetchMetaData
echo
echo "3. Configuring networks..."
ConfigNetwork
echo
echo
echo "Done."
sync
#!/bin/sh -e
# Task Description:
# Used for initializing configuration in single NIC EIP full Bypass mode scenarios.
# Supports from 1 public IP up to 254 public IPs.
# Execute this task after external resource environment is ready.
# Adapted for ubuntu20.04, 22.04, 24.04, debian10, 11, 12, centos7, centos9, rocky9.6, rocky8.10 (10 systems)
# ===== Global Variable Definitions =====
TASK_NAME=single_nic_bypass_init
META_CURL="curl -sf"
META_URL="http://169.254.169.254/latest/meta-data/network/interfaces/macs"
NIC_NAME=eth0
PRI_GW4=
PRI_IP4=
PRI_PREFIX=
SYSTEM=
DEFAULT_PUB_EIP4=
ALL_PUB_EIP4s=
NAME_SERVER1=8.8.8.8
NAME_SERVER2=1.1.1.1
# =======================
CheckSupportedOS() {
if [ ! -f /etc/os-release ]; then
echo "Error: Unsupported system: /etc/os-release not found"
exit 1
fi
. /etc/os-release
if [ "$ID" = "ubuntu" ]; then
if [ "$VERSION_ID" = "20.04" ] || [ "$VERSION_ID" = "22.04" ] || [ "$VERSION_ID" = "24.04" ]; then
echo "Supported OS: Ubuntu $VERSION_ID"
SYSTEM=ubuntu
return 0
else
echo "Error: Unsupported Ubuntu version: $VERSION_ID"
exit 1
fi
elif [ "$ID" = "debian" ]; then
if [ "$VERSION_ID" = "11" ] || [ "$VERSION_ID" = "10" ] || [ "$VERSION_ID" = "12" ]; then
echo "Supported OS: Debian $VERSION_ID"
SYSTEM=debian
return 0
else
echo "Unsupported Debian version: $VERSION_ID"
exit 1
fi
elif [ "$ID" = "centos" ] || [ "$ID" = "rhel" ]; then
if [ "$VERSION_ID" = "7" ] && [ -f /etc/centos-release ]; then
echo "Supported OS: CentOS $VERSION_ID"
SYSTEM=centos
return 0
elif [ "$VERSION_ID" = "9" ] && [ -f /etc/centos-release ]; then
echo "Supported OS: CentOS $VERSION_ID"
SYSTEM=centos
return 0
else
echo "Unsupported CentOS/RHEL version: $VERSION_ID"
exit 1
fi
elif [ "$ID" = "rocky" ]; then
if [ "$VERSION_ID" = "9.6" ] || [ "$VERSION_ID" = "8.10" ]; then
echo "Supported OS: rocky $VERSION_ID"
SYSTEM=rocky
return 0
else
echo "Unsupported rocky version: $VERSION_ID"
exit 1
fi
else
echo "Unsupported OS: $ID $VERSION_ID"
exit 1
fi
}
ConfigUbuntuNetwork() {
cat > /etc/netplan/50-cloud-init.yaml << EOF
network:
version: 2
ethernets:
eth0:
addresses:
- ${PRI_IP4}/${PRI_PREFIX}
EOF
for EIP in $ALL_PUB_EIP4s; do
echo " - ${EIP}/32" >> /etc/netplan/50-cloud-init.yaml
done
cat >> /etc/netplan/50-cloud-init.yaml << EOF
nameservers:
addresses:
- ${NAME_SERVER1}
- ${NAME_SERVER2}
EOF
# Default route
cat >> /etc/netplan/50-cloud-init.yaml << EOF
routes:
- to: default
via: ${PRI_GW4}
from: ${DEFAULT_PUB_EIP4}
EOF
}
ConfigCentOSNetwork() {
rm -f /etc/sysconfig/network-scripts/ifcfg-eth0*
cat << EOF > /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
BOOTPROTO=static
ONBOOT=yes
IPADDR=${PRI_IP4}
PREFIX=${PRI_PREFIX}
EOF
index=0
for EIP in $ALL_PUB_EIP4s; do
cat << EOF > /etc/sysconfig/network-scripts/ifcfg-eth0:$index
DEVICE=eth0:$index
BOOTPROTO=static
ONBOOT=yes
IPADDR=${EIP}
PREFIX=32
EOF
index=$((index+1))
done
# Default route
cat << EOF > /etc/sysconfig/network-scripts/route-eth0
default via ${PRI_GW4} dev eth0 src ${DEFAULT_PUB_EIP4}
EOF
# Disable DHCP on eth2
cat << EOF > /etc/sysconfig/network-scripts/ifcfg-eth2
DEVICE=eth2
BOOTPROTO=none
ONBOOT=yes
EOF
}
ConfigDebianNetwork() {
cat > /etc/network/interfaces.d/50-cloud-init << EOF
auto lo
iface lo inet loopback
dns-nameservers ${NAME_SERVER1} ${NAME_SERVER2}
auto eth0
iface eth0 inet static
address ${PRI_IP4}/${PRI_PREFIX}
auto eth0:0
iface eth0:0 inet static
address ${DEFAULT_PUB_EIP4}/32
up ip route add default via ${PRI_GW4} dev eth0 src ${DEFAULT_PUB_EIP4}
down ip route del default via ${PRI_GW4} dev eth0 src ${DEFAULT_PUB_EIP4}
EOF
index=1
for EIP in $ALL_PUB_EIP4s; do
if [ "$EIP" = "$DEFAULT_PUB_EIP4" ]; then
continue
fi
cat >> /etc/network/interfaces.d/50-cloud-init << EOF
auto eth0:$index
iface eth0:$index inet static
address ${EIP}/32
EOF
index=$((index+1))
done
}
FetchMetaData() {
pri_macs=$($META_CURL $META_URL/ | sed 's/\///g')
nic_count=$(echo "$pri_macs" | wc -w | tr -d ' ')
if [ "$nic_count" -ne 1 ]; then
echo "[ERR] Expected exactly one nic, got $nic_count: $pri_macs" >&2
exit 4
fi
PRI_IP4=$($META_CURL $META_URL/$pri_macs/primary-ip-address)
pri_subnet_cidr=$($META_CURL $META_URL/$pri_macs/subnet-cidr-block)
PRI_PREFIX=$(echo "$pri_subnet_cidr" | cut -d'/' -f2)
PRI_GW4=$($META_CURL $META_URL/$pri_macs/gateway)
DEFAULT_PUB_EIP4=$($META_CURL $META_URL/$pri_macs/public-ipv4)
ALL_PUB_EIP4s=$($META_CURL $META_URL/$pri_macs/public-ipv4s | sed 's/\[//;s/\]//;s/"//g;s/,/ /g')
if [ -z "$pri_macs" ] || [ -z "$PRI_IP4" ] || [ -z "$pri_subnet_cidr" ] || [ -z "$PRI_PREFIX" ] || [ -z "$PRI_GW4" ] || [ -z "$DEFAULT_PUB_EIP4" ] || [ -z "$ALL_PUB_EIP4s" ]; then
echo "[ERR] Missing metadata, check EC2 metadata service." >&2
exit 3
fi
echo " Private MacAddr: $pri_macs"
echo " Private IPv4: $PRI_IP4"
echo " Private Prefix: $PRI_PREFIX"
echo " Private GWv4: $PRI_GW4"
echo " ALl Public IPv4: $ALL_PUB_EIP4s"
echo " Default Public IPv4: $DEFAULT_PUB_EIP4"
}
ConfigNetwork() {
# Disable RPF (Reverse Path Filtering)
if ! grep -q "net.ipv4.conf.all.rp_filter=0" /etc/sysctl.conf; then
echo "net.ipv4.conf.all.rp_filter=0" | tee -a /etc/sysctl.conf > /dev/null
sysctl -p /etc/sysctl.conf
fi
# Disable cloud-init network configuration
echo "network: {config: disabled}" > /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg
if [ "$SYSTEM" = "ubuntu" ]; then
ConfigUbuntuNetwork
elif [ "$SYSTEM" = "centos" ]; then
ConfigCentOSNetwork
elif [ "$SYSTEM" = "debian" ]; then
if [ "$VERSION_ID" = "11" ] || [ "$VERSION_ID" = "10" ]; then
ConfigDebianNetwork
elif [ "$VERSION_ID" = "12" ]; then
ConfigUbuntuNetwork
else
echo "Unsupported Debian version: $VERSION_ID"
exit 1
fi
elif [ "$SYSTEM" = "rocky" ]; then
ConfigCentOSNetwork
else
echo "Unsupported OS for network config: $SYSTEM"
exit 1
fi
}
echo "1. Check OS..."
CheckSupportedOS
echo
echo "2. FetchMetaData..."
FetchMetaData
echo
echo "3. Configuring networks..."
ConfigNetwork
echo
echo
echo "Done."
sync