# Reassigning a public IP address from one VM to another

To reassign a public IP address from one VM to another, convert the IP address to static, unassign it from the original VM, and assign it to the new one.


## Convert the public IP address of the original VM to static

You can convert a dynamic [public IP address](../../../vpc/concepts/address.md#public-addresses) to static. Static public IP addresses are reserved and remain attached to respective resources when VMs and network load balancers are stopped.

{% note info %}

Make sure to check out our [pricing policy](../../../vpc/pricing.md#prices-public-ip) for inactive static public IP addresses.

{% endnote %}

{% list tabs group=instructions %}

- Management console {#console}

   1. In the [management console](https://console.yandex.cloud), select the folder containing the address.
   1. Navigate to **Virtual Private Cloud**.
   1. In the left-hand panel, select ![image](../../../_assets/console-icons/map-pin.svg) **Public IP addresses**.
   1. Click ![image](../../../_assets/console-icons/ellipsis.svg) in the row with the IP address and select **Make static**.
   1. In the window that opens, click **Change**.

- CLI {#cli}

   If you do not have the Yandex Cloud CLI yet, [install and initialize it](../../../cli/quickstart.md#install).

   The folder used by default is the one specified when [creating](../../../cli/operations/profile/profile-create.md) the CLI profile. To change the default folder, use the `yc config set folder-id <folder_ID>` command. You can also specify a different folder for any command using `--folder-name` or `--folder-id`. If you access a resource by its name, the search will be limited to the default folder. If you access a resource by its ID, the search will be global, i.e., through all folders based on access permissions.

   1. See the description of the CLI commands for updating the address attributes:

      ```bash
      yc vpc address update --help
      ```

   1. Get a list of addresses in the default folder:

      ```bash
      yc vpc address list
      ```

      Result:

      ```text
      +----------------------+------+---------------+----------+------+
      |          ID          | NAME |    ADDRESS    | RESERVED | USED |
      +----------------------+------+---------------+----------+------+
      | e2l46k8conff******** |      | 84.201.177.41 | false    | true |
      +----------------------+------+---------------+----------+------+
      ```

      The `false` value of the `reserved` parameter for the IP address with the `e2l46k8conff********` ID shows that this address is dynamic.

   1. Convert it to static by using the `--reserved=true` parameter and the address ID:

      ```bash
      yc vpc address update --reserved=true e2l46k8conff********
      ```

      Result:

      ```text
      id: e2l46k8conff********
      folder_id: b1g7gvsi89m3********
      created_at: "2021-01-14T09:36:46Z"
      external_ipv4_address:
        address: 84.201.177.41
        zone_id: ru-central1-a
        requirements: {}
      reserved: true
      used: true
      ```

      Now that the `reserved` parameter is `true`, the IP address is static.

- API {#api}

  To change the type of a public IP address from dynamic to static, use the [update](../../../vpc/api-ref/Address/update.md) REST API method for the [Address](../../../vpc/api-ref/Address/index.md) resource or the [AddressService/Update](../../../vpc/api-ref/grpc/Address/update.md) gRPC API call, and provide the following in the request:

  * ID of the IP address you want to convert to static in the `addressId` parameter.

    To get the address ID, use the [list](../../../vpc/api-ref/Address/list.md) REST API method for the [Address](../../../vpc/api-ref/Address/index.md) resource or the [AddressService/List](../../../vpc/api-ref/grpc/Address/list.md) gRPC API call and provide the folder ID in the `folderId` request parameter.

    To learn how to find out the folder ID, see [Getting the folder ID](../../../resource-manager/operations/folder/get-id.md).

  * `true` in the `reserved` parameter.
  * Name of the `reserved` parameter in the `updateMask` parameter.

  {% note warning %}
  
  The API method will assign default values to all the parameters of the object you are modifying unless you explicitly provide them in your request. To avoid this, list the settings you want to change in the `updateMask` parameter as a single comma-separated string.
  
  {% endnote %}

{% endlist %}

The number of static public IP addresses is [limited](../../../vpc/concepts/limits.md#vpc-quotas). If the number allowed by the quota is not enough for you, contact [support](https://center.yandex.cloud/support) to have your quota increased.


## Unassign the public IP address from the original VM

{% list tabs group=instructions %}

- Management console {#console}

  1. In the [management console](https://console.yandex.cloud), select the folder this VM belongs to.
  1. Navigate to **Compute Cloud**.
  1. Select the VM.
  1. In the window that opens, under **Network**, click ![image](../../../_assets/console-icons/ellipsis.svg) in the top-right corner of the relevant network interface section and select **Disassociate public IP address**.
  1. In the window that opens, click **Delete**.

- CLI {#cli}

  If you do not have the Yandex Cloud CLI yet, [install and initialize it](../../../cli/quickstart.md#install).

  The folder used by default is the one specified when [creating](../../../cli/operations/profile/profile-create.md) the CLI profile. To change the default folder, use the `yc config set folder-id <folder_ID>` command. You can also specify a different folder for any command using `--folder-name` or `--folder-id`. If you access a resource by its name, the search will be limited to the default folder. If you access a resource by its ID, the search will be global, i.e., through all folders based on access permissions.

  To unassign a public IP address from a VM, run the following CLI command:

  ```bash
  yc compute instance remove-one-to-one-nat
    --id=<VM_ID>
    --network-interface-index=<VM_network_interface_number>
  ```

  Where:

  * `--id`: VM ID. You can get a list of available VM IDs in the folder using the `yc compute instance list` [CLI command](../../../cli/cli-ref/compute/cli-ref/instance/list.md).
  * `--network-interface-index`: VM network interface number. The default value is `0`. To get a list of VM network interfaces and their numbers, run `yc compute instance get <VM_ID>`.

  For more information about the `yc compute instance remove-one-to-one-nat` command, see the [CLI reference](../../../cli/cli-ref/compute/cli-ref/instance/remove-one-to-one-nat.md).

- API {#api}

  Use the [removeOneToOneNat](../../api-ref/Instance/removeOneToOneNat.md) REST API method for the [Instance](../../api-ref/Instance/index.md) resource or the [InstanceService/RemoveOneToOneNat](../../api-ref/grpc/Instance/removeOneToOneNat.md) gRPC API call.

{% endlist %}


## Create a new VM without a public IP address

{% list tabs group=instructions %}

- Management console {#console}

  When [creating the VM](../vm-create/create-linux-vm.md), select `No address` in the **Public IP address** field under **Network settings**.

- CLI {#cli}

  When [creating a VM](../vm-create/create-linux-vm.md), exclude the `--network-interface` parameter under `nat-ip-version=ipv4`.

- Terraform {#tf}

  When [creating a VM](../vm-create/create-linux-vm.md), specify `nat = false` under `network_interface`.

- API {#api}

  When [creating a VM](../vm-create/create-linux-vm.md), exclude the `primaryV4AddressSpec` section in the request body.

{% endlist %}


## Assign the IP address to the new VM

{% note info %}

If the new VM resides in a different folder, [move](../../../vpc/operations/address-move.md) the public IP address to that folder.

{% endnote %}

{% list tabs group=instructions %}

- Management console {#console}

  1. In the [management console](https://console.yandex.cloud), select the [folder](../../../resource-manager/concepts/resources-hierarchy.md#folder) the VM belongs to.
  1. Navigate to **Compute Cloud**.
  1. In the left-hand panel, select ![image](../../../_assets/console-icons/server.svg) **Virtual machines**.
  1. Select the VM.
  1. In the window that opens, under **Network**, click ![image](../../../_assets/console-icons/ellipsis.svg) in the top-right corner of the relevant network interface section and select **Add public IP address**. In the window that opens:
      * In the **Public address** field, select `Auto` to get an IP address automatically or `List` to choose a reserved address from the list.
      * Optionally, if you selected `Auto` in the **Public address** field, enable **DDoS protection**. For more information, see [Yandex DDoS Protection in Virtual Private Cloud](../../../vpc/ddos-protection/index.md).
      * If you selected `List` in the **Public address** field, choose the IP address you want to assign to your VM. The IP address and the VM must be in the same availability zone.
      * Click **Add**.

- CLI {#cli}

  If you do not have the Yandex Cloud CLI yet, [install and initialize it](../../../cli/quickstart.md#install).

  The folder used by default is the one specified when [creating](../../../cli/operations/profile/profile-create.md) the CLI profile. To change the default folder, use the `yc config set folder-id <folder_ID>` command. You can also specify a different folder for any command using `--folder-name` or `--folder-id`. If you access a resource by its name, the search will be limited to the default folder. If you access a resource by its ID, the search will be global, i.e., through all folders based on access permissions.

  To assign a public IP address to a VM, run the following [CLI](../../../cli/index.md) command:

  ```bash
  yc compute instance add-one-to-one-nat \
    --id=<VM_ID> \
    --network-interface-index=<VM_network_interface_number> \
    --nat-address=<IP_address>
  ```

  Where:
  * `--id`: VM ID. You can get a list of available VM IDs in the [folder](../../../resource-manager/concepts/resources-hierarchy.md#folder) using the `yc compute instance list` [CLI command](../../../cli/cli-ref/compute/cli-ref/instance/list.md).
  * `--network-interface-index`: VM network interface number. The default value is `0`. To get a list of VM network interfaces and their numbers, run `yc compute instance get <VM_ID>`.
  * `--nat-address`: Public IP address to assign to the VM. This is an optional setting. If you skip it, the VM will get a public IP address automatically.

    You can get a list of reserved public IP addresses available in the folder using the `yc vpc address list` [CLI command](../../../cli/cli-ref/vpc/cli-ref/address/list.md). The IP address and the VM must be in the same availability zone.

  Here is a possible use case:

  ```bash
  yc compute instance add-one-to-one-nat \
    --id=fhmsbag62taf******** \
    --network-interface-index=0 \
    --nat-address=51.250.*.***
  ```

  Result:

  ```text
  id: fhmsbag62taf********
  folder_id: b1gv87ssvu49********
  created_at: "2022-05-06T10:41:56Z"
  ...
  network_settings:
    type: STANDARD
  placement_policy: {}
  ```

  For more information about the `yc compute instance add-one-to-one-nat` command, see the [CLI reference](../../../cli/cli-ref/compute/cli-ref/instance/add-one-to-one-nat.md).

- Terraform {#tf}

  With [Terraform](https://www.terraform.io/), you can quickly create a cloud infrastructure in Yandex Cloud and manage it using configuration files. These files store the infrastructure description written in HashiCorp Configuration Language (HCL). If you change the configuration files, Terraform automatically detects which part of your configuration is already deployed, and what should be added or removed.
  
  Terraform is distributed under the [Business Source License](https://github.com/hashicorp/terraform/blob/main/LICENSE). The [Yandex Cloud provider for Terraform](https://github.com/yandex-cloud/terraform-provider-yandex) is distributed under the [MPL-2.0](https://www.mozilla.org/en-US/MPL/2.0/) license.
  
  For more information about the provider resources, see the relevant documentation on the [Terraform](https://www.terraform.io/docs/providers/yandex/index.html) website or [its mirror](../../../terraform/index.md).

  If you do not have Terraform yet, [install it and configure the Yandex Cloud provider](../../../tutorials/infrastructure-management/terraform-quickstart.md#install-terraform).
  
  
  To manage infrastructure using Terraform under a service account or user accounts (a Yandex account, a federated account, or a local user), [authenticate](../../../terraform/authentication.md) using the appropriate method.

  1. To create a public IP address and link it to a VM network interface, use the `yandex_vpc_address` resource and specify the address you get in the `nat_ip_address` field under `network_interface` for the network interface in question in the `yandex_compute_instance` resource configuration:

     ```hcl
     # Creating a static IP address

     resource "yandex_vpc_address" "addr" {
       name = "vm-adress"
       external_ipv4_address {
         zone_id = "<availability_zone>"
       }
     }

     # Creating a VM

     resource "yandex_compute_instance" "vm-1" {
       name        = "<VM_name>"
       platform_id = "standard-v3"
       resources {
         core_fraction = 20
         cores         = 2
         memory        = 1
       }
       ...

       ## Assigning a subnet and IP address to the VM network interface under `network_interface`

       network_interface {
         subnet_id      = "<VM_subnet_ID>"
         nat            = true
         nat_ip_address = yandex_vpc_address.addr.external_ipv4_address[0].address
       }
       ...

     }
     ```

     Where `nat_ip_address` is the public IP address to assign to the VM network interface. The `yandex_vpc_address` resource contains a list of items, where `[0]` is the list's first item that contains the IP address. If you already have a reserved public IP address to assign to your VM, specify it in the `nat_ip_address` field:

     ```hcl
     nat_ip_address = "<IP_address>"
     ```

     The IP address and the VM must be in the same availability zone.

     For more information about `yandex_compute_instance` properties, see [this provider guide](../../../terraform/resources/compute_instance.md).
  1. Create the resources:

     1. In the terminal, navigate to the configuration file directory.
     1. Make sure the configuration is correct using this command:
     
        ```bash
        terraform validate
        ```
     
        If the configuration is valid, you will get this message:
     
        ```bash
        Success! The configuration is valid.
        ```
     
     1. Run this command:
     
        ```bash
        terraform plan
        ```
     
        You will see a list of resources and their properties. No changes will be made at this step. Terraform will show any errors in the configuration.
     1. Apply the configuration changes:
     
        ```bash
        terraform apply
        ```
     
     1. Type `yes` and press **Enter** to confirm the changes.

     Terraform will create all the required resources. You can check the new resources in the [management console](https://console.yandex.cloud).

- API {#api}

  To assign a public IP address to a VM network interface, use the [addOneToOneNat](../../api-ref/Instance/addOneToOneNat.md) REST API method for the [Instance](../../api-ref/Instance/index.md) resource or the [InstanceService/AddOneToOneNat](../../api-ref/grpc/Instance/addOneToOneNat.md) gRPC API call.

{% endlist %}

Your VM network interface will have a public IP address assigned. You can use this IP address to [connect](../vm-connect/ssh.md#vm-connect) to the VM over SSH.