# 启用 IP 转发

IP 转发，也称为 IP 路由，是一种允许网络设备（如路由器或配置为路由器的计算机）将传入的 IP 数据包从一个网卡转发到另一个网卡的过程。这是网络环境中的一项基本功能，能够实现不同网络之间的通信，并确保数据包到达其预定目标。

默认情况下，IP 转发是启用的。如果希望您的实例作为路由的下一跳，该实例需要转发那些源地址与默认 IP 地址或范围不匹配的数据包。为了转发任意源地址的数据包，必须启用 IP 转发。

## 操作步骤

1. 在创建实例时勾选 **启用 IP 转发**，或者在 **操作** 中勾选 **启用 IP 转发**。
2. 手动在实例的操作系统中启用 IP 转发。\
   以下是在常见操作系统类型（Linux 和 Windows）上启用 IP 转发的步骤：

{% tabs %}
{% tab title="Linux" %}
**临时启用**

要临时启用 IP 转发直至下次重启，可以使用以下命令。

* 对于 IPv4

  ```sh
  sudo sysctl -w net.ipv4.ip_forward=1
  ```
* 对于 IPv6

  ```sh
  sudo sysctl -w net.ipv6.conf.all.forwarding=1
  ```

**永久启用**

1. 若要使更改永久生效，请编辑 `/etc/sysctl.conf` 文件。

   ```sh
   sudo nano /etc/sysctl.conf
   ```
2. 添加或取消注释以下行。
   * 对于 IPv4

     ```sh
     net.ipv4.ip_forward=1
     ```
   * 对于 IPv6

     ```sh
     net.ipv6.conf.all.forwarding=1
     ```
3. 保存文件并应用更改。

   ```sh
   sudo sysctl -p
   ```

{% endtab %}

{% tab title="Windows" %}
**通过命令提示符启用**

1. 以管理员身份打开命令提示符。
2. 输入以下命令。
   * 对于 IPv4

     ```sh
     netsh interface ipv4 set global forwarding=enabled
     ```
   * 对于 IPv6

     ```sh
     netsh interface ipv6 set global forwarding=enabled
     ```

**通过注册表编辑器启用**

通过在运行对话框（Win + R）中输入 `regedit` 打开注册表编辑器。

* 对于 IPv4
  1. 导航到以下键值。

     ```plaintext
     HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters
     ```
  2. 找到 `IPEnableRouter` 项。如果不存在，请创建一个名为 `IPEnableRouter` 的新 DWORD（32 位）值。
  3. 将 `IPEnableRouter` 的值设置为 `1`。
  4. 重启计算机以使更改生效。
* 对于 IPv6
  1. 导航到以下键值。

     ```plaintext
     HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip6\Parameters
     ```
  2. 找到 `DisabledComponents` 项。如果不存在，请创建一个名为 `DisabledComponents` 的新 DWORD（32 位）值。
  3. 将 `DisabledComponents` 的值设置为 `0`。
  4. 重启计算机以使更改生效。
     {% endtab %}
     {% endtabs %}


---

# 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-networking/enable-ip-forwarding.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.
