Changing the VM internal IP address
After you create a VM, you can change the internal IP addresses of its network interfaces.
If you do not have the Yandex Cloud command line interface yet, install and initialize it.
The folder specified in the CLI profile is used by default. You can specify a different folder using the --folder-name
or --folder-id
parameter.
-
View the description of the CLI command for updating the internal IP address of a VM's network interface:
yc compute instance update-network-interface --help
-
Get a list of VMs in the default folder:
yc compute instance list
Result:
+----------------------+-----------------+---------------+---------+----------------------+ | ID | NAME | ZONE ID | STATUS | DESCRIPTION | +----------------------+-----------------+---------------+---------+----------------------+ | fhm0b28lgfp4******** | first-instance | ru-central1-a | RUNNING | my first vm via CLI | | fhm9gk85nj7g******** | second-instance | ru-central1-a | RUNNING | my second vm via CLI | +----------------------+-----------------+---------------+---------+----------------------+
-
Select the VM
ID
. -
Stop the selected VM.
-
Get a list of network interfaces for the selected VM by specifying its ID:
yc compute instance get <VM_ID>
Result:
... network_interfaces: - index: "0" mac_address: d0:0d:24:**:**:** subnet_id: e2lpp96bvvgp******** primary_v4_address: address: 192.168.2.23 one_to_one_nat: address: 158.160.**.*** ip_version: IPV4 - index: "1" mac_address: d0:1d:24:**:**:** subnet_id: e2lrucutusnd******** primary_v4_address: address: 192.168.1.32 - index: "2" mac_address: d0:2d:24:**:**:** subnet_id: e2lv9c6aek1d******** primary_v4_address: address: 192.168.4.26 ...
Save the
index
field value: number of the network interface you want to update the internal IP address for. -
Update the internal IP address of the selected VM's network interface:
yc compute instance update-network-interface \ --id <VM_ID> \ --ipv4-address <internal_IP_address> \ --network-interface-index <network_interface_number>
Where:
--id
: VM ID.--ipv4-address
: Internal IP address. Specify a new IP address or enterauto
to assign it automatically.--network-interface-index
: VM's network interface number you saved earlier.
With Terraform
Terraform is distributed under the Business Source License
For more information about the provider resources, see the documentation on the Terraform
If you don't have Terraform, install it and configure the Yandex Cloud provider.
-
To update the internal IP address of a VM's network interface, add the
ip_address
parameter to thenetwork_interface
section of the network interface you need in theyandex_compute_instance
resource configuration:resource "yandex_compute_instance" "vm-1" { ... network_interface { ... ip_address = "<internal_IP_address>" } }
For more information about the
yandex_compute_instance
resource parameters, see the provider documentation . -
Create resources:
-
In the terminal, change to the folder where you edited the configuration file.
-
Make sure the configuration file is correct using the command:
terraform validate
If the configuration is correct, the following message is returned:
Success! The configuration is valid.
-
Run the command:
terraform plan
The terminal will display a list of resources with parameters. No changes are made at this step. If the configuration contains errors, Terraform will point them out.
-
Apply the configuration changes:
terraform apply
-
Confirm the changes: type
yes
in the terminal and press Enter.
Terraform will create all the required resources. You can check the new resources using the management console
. -
To change the internal IP address of a VM's network interface, use the updateNetworkInterface REST API method for the Instance resource or the InstanceService/PrimaryAddress gRPC API call.