# 绑定弹性 IPv4 地址

您可以将最多 4 个弹性 IPv4 地址绑定到一个内网 IP。

## 操作步骤

1. 单击实例名称，进入实例 **详情** > **信息** > **网卡**。
2. 选择您要绑定弹性 IPv4 的内网 IP，然后进入 **操作** > **绑定弹性 IPv4**。

   <figure><img src="/files/rShLXjK8QbDnKxtPhl26" alt=""><figcaption></figcaption></figure>
3. 选择一个所需的弹性 IPv4 地址并点击 **确认** 进行绑定。如果没有所需的弹性 IPv4 地址，点击 **新建弹性 IPv4** 创建一个新的 IPv4。\
   详细步骤请参见 [**创建弹性 IPv4**](/welcome/cn/elastic-compute/get-started/manage-networking/create-an-elastic-ipv4.md)。

   <div align="left"><figure><img src="/files/l1LBOKZr1u9pd7YuySo3" alt="" width="444"><figcaption></figcaption></figure></div>

{% hint style="info" %} <mark style="color:blue;">**注意**</mark>

<mark style="color:blue;">您也可以进入</mark> <mark style="color:blue;">**弹性 IPv4**</mark> <mark style="color:blue;">></mark> <mark style="color:blue;">**操作**</mark> <mark style="color:blue;">></mark> <mark style="color:blue;">**绑定内网 IP**</mark> <mark style="color:blue;">来绑定弹性 IPv4 地址。</mark>

<img src="/files/ryKGgZz8cZBVZWcMV64a" alt="" data-size="original">
{% endhint %}

## **Bypass 模式配置指南**

#### 1. 适用范围与前提条件

**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 模式）

#### 2. 操作步骤

{% tabs %}
{% tab title="Console" %}

1. 创建命令：

路径：

> 控制台 → 编排服务 → 命令 → 创建命令

{% code expandable="true" %}

```shellscript
#!/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

```

{% endcode %}

2. 运行命令：

路径：

> 控制台 → 云服务器 → 云服务器实例列表 → 操作 → 运维 → 执行命令 → 选择命令 → 确认

运行已创建的命令。

3. 重启实例：\
   执行完成后，必须手动重启实例。
4. 添加额外 IP（可选）：\
   如需绑定更多 IP，请重复以下步骤：

* 绑定 IP
* 执行命令
* 重启实例
  {% endtab %}

{% tab title="API" %}

1. 创建计算实例：\
   按照标准流程创建一个实例。
2. 创建并绑定 EIP：\
   创建一个弹性公网 IP（EIP）。\
   将其以 Bypass 模式绑定到实例上。
3. 通过 API 运行命令：\
   API 文档：\
   <https://docs.console.zenlayer.com/api-reference/security/zos/command/createcommand>\
   使用下方提供的脚本

{% code expandable="true" %}

```shellscript
#!/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

```

{% endcode %}

4. 重启实例\
   调用 API 重启实例以使配置生效。
5. 添加更多 IP（可选）\
   如需绑定更多 IP，重复以下步骤：\
   \- 绑定 IP\
   \- 运行命令\
   \- 重启实例
   {% endtab %}
   {% endtabs %}

#### 3. 故障排查

**3.1 配置未生效**

**原因：** 未重启实例\
**解决方案：** 重启实例

**3.2 配置失败**

* 确保所有 IP 均为 Bypass 模式
* 确认操作系统在支持列表内


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.console.zenlayer.com/welcome/cn/elastic-compute/get-started/manage-instances/bind-an-elastic-ipv4-address.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
