
# 更换弹性公网IP
您现在可以自行更换CPH服务器的弹性公网IP。
您可以选择通过[控制台]或者[调用API]接口进行更换EIP操作。
![](https://support.huaweicloud.com/usermanual-cph/public_sys-resources/note_3.0-zh-cn.png)
服务器的网络类型需要是"自定义"网络。可以通过控制台进入云手机服务器详情页面查看网络类型。
 #### 在控制台上更换弹性公网IP
1. 解绑弹性公网IP
   1. 登录[CPH控制台](https://console.huaweicloud.com/cph/?#/servers/list)，进入服务器管理页面。
   
   2. 在服务器列表，选择一个已经绑定了EIP的服务器，单击操作列的"更多"，在弹出的列表中单击"解绑弹性公网IP"。
   
   3. 在弹出的"解绑弹性公网IP"页面中，选择要解绑的EIP，单击"确定"。
      图1解绑弹性公网IP   
      ![](https://support.huaweicloud.com/usermanual-cph/zh-cn_image_0000001938843981.png "点击放大") 
    

2. 绑定弹性公网IP
   1. 在服务器列表，选择一个需要绑定弹性公网IP的服务器，单击操作列的"更多"，在弹出的列表中单击"绑定弹性公网IP"。
   
   
   
   2. 在弹出的"绑定弹性公网IP"页面，选择要绑定的弹性公网IP，单击"确定"。
   
   
   图2绑定弹性公网IP   
   ![](https://support.huaweicloud.com/usermanual-cph/zh-cn_image_0000001899607224.png "点击放大") 
 
 #### 调用API更换弹性公网IP
1. 解绑弹性公网IP。
   1. 请在管理控制台或者调用[查询云手机服务器详情](https://support.huaweicloud.com/api-cph/cph_api_0540.html)接口，查询服务器的EIP的IP地址（例如 122.9.102.xxx）。
   
   2. 在弹性公网IP的管理控制台或者调用[全量查询弹性公网IP列表](https://support.huaweicloud.com/api-eip/ListPublicipsV3.html)接口，通过IP地址查询EIP的id。
   
   3. 调用解绑弹性公网IP的[解绑弹性公网IP](https://support.huaweicloud.com/api-eip/DisassociatePublicips.html)接口进行解绑。
      调用示例如下：
      ```
      curl -i -k -X POST https://{Endpoint}/v3/{project_id}/eip/publicips/{eip_id}/disassociate-instance -H "Content-Type: application/json" -H "X-Auth-Token: ${token}"
      ```
      
    
2. 绑定弹性公网IP。
   严格来讲弹性公网IP实际是绑定到port的，存在空闲port的机器可以进行绑定操作。
   1. 根据第[1]步的指导查询到要绑定的弹性公网IP的id，这个弹性公网IP需要是未绑定状态。
   
   2. 根据CPH的server_id调用[查询端口列表](https://support.huaweicloud.com/api-vpc/vpc_port01_0003.html)接口，查询出服务器的所有port。
      调用示例如下：
      ```
      curl -i -k -X GET https://{Endpoint}/v1/{project_id}/ports?instance_id={cph_server_id} -H "Content-Type: application/json" -H "X-Auth-Token: ${token}"
      ```
      
   
   
   
   3. 根据port_id调用[查询EIP列表](https://support.huaweicloud.com/api-eip/eip_api_0003.html)接口查询弹性公网IP，可以一次传入一个或多个port_id进行查询，如果哪个port查不到弹性公网IP，则说明它可以绑定弹性公网IP。
      调用示例如下：
      ```
      curl -i -k -X GET https://{Endpoint}/v1/{project_id}/publicips?port_id={port_id1}\&port_id={port_id2}\&port_id={port_id3} -H "Content-Type: application/json" -H "X-Auth-Token: ${token}"
      ```
      
   
   
   
   4. 对于没有绑定弹性公网IP的port，可以调用[绑定弹性公网IP](https://support.huaweicloud.com/api-eip/AssociatePublicips.html)接口，绑定弹性公网IP。
      调用示例如下：
      ```
      curl -i -k -X POST https://{Endpoint}/v3/{project_id}/eip/publicips/{eip_id}/associate-instance -H "Content-Type: application/json" -H "X-Auth-Token: ${token}" -d '{"publicip":{"associate_instance_id":"{port_id}","associate_instance_type":"PORT"}}'
      ```
      
     
 
