5

I have a running VM1 (internal IP is: xxx.xxx.0.7). I will create another new VM2 and I want to change the IP of VM1 to any other IP and set the IP of VM2 to "xxx.xxx.0.7". OS of both VM is windows server.

I have found some way to change the IP of a running VM. But I am not sure if that works. Is there any straightforward way to do this?

Thanks in advance!

hasnayn
  • 356
  • 4
  • 22
  • Your new edit significantly changes your question. Do not do that after you have received answers. Your modified question is too broad and presents multiple problems - only present one problem per post. Your goal has several big problems - Google does not support resizing disks smaller and your objective cannot be achieved in 10 minutes. – John Hanley Feb 08 '22 at 16:48
  • I will try to follow your suggestions while questioning again. Thanks for your reply. – hasnayn Feb 09 '22 at 07:57
  • You should correct this question. – John Hanley Feb 09 '22 at 17:04
  • I have removed the edit and will post another question for that. Thanks! – hasnayn Feb 10 '22 at 05:23

3 Answers3

2

You can assign a static internal IP address when the instance is created and not once the VM is running.

To accomplish your goal of creating a new instance using an existing internal IP address:

  1. Backup the existing virtual machine (create an image, snaphot, etc.)
  2. Promote the internal ephemeral IP address.
  3. Delete the virtual machine.
  4. Create a new virtual machine specifying the static internal IP address.
  5. Recreate the original virtual machine from the image or snapshot.

References:

John Hanley
  • 74,467
  • 6
  • 95
  • 159
  • 1
    Havent thought of that - simple and easy :)) Thanks John for submitting another idea :) – Wojtek_B Feb 08 '22 at 08:08
  • I actually reproduced this and it works. It's weird however why you can delete the VM with static internal IP and the address is preserved, but if I want to change the IP drom static to automatic on the same VM - the address is released and no longer is static... – Wojtek_B Feb 08 '22 at 09:40
  • John, thanks for your fantastic reply. I have updated my question, plz take a look. – hasnayn Feb 08 '22 at 10:33
1

First - for this operation you will need to stop your VM1. But I believe this is not a concern since you are planning to make VM2 work as VM1.

Second - It's impossible to attach new or existing static internal IP to an existing VM.

If you want to reserve internal IP address for VM2 then the only way I know is the first method I described.

First method - switch disks. Simple but may require some configuration after the switch.

  • create VM2 (may be identical but you may add processors or RAM if you need)
  • configure VM2 with all you need to do the job of VM1
  • clone VM2's boot disk (lets call it disk22)
  • stop VM1
  • detach a boot disk from VM1
  • attach disk22 to VM2 (just like you would switch disks on a regular PC)
  • start VM1 (but running off of a VM2's cloned disk)

Done. Very simple and elegant. You can then Create another VM using VM1's old disk (which is detached at the moment).

I don't know if there will be any issues with booting Windows or any other running services (that may have depend on the old VM'2 IP) but this is your part.

Second method - switch addresses. More complicated but you may loose your original IP - but if you're the only person having administration rights to this VPC (or a project) then you should be OK.

Ephemeral internal IP addresses: these addresses are available to VM instances and forwarding rules. Ephemeral internal IP addresses remain attached to VM instances and forwarding rules until the instance or forwarding rule is deleted.

Procedure is as fallows (for this example my VPC network name was default - if yours is names otherwise please make proper changes).

  • create new VM2 with all needed configuration
  • stop VM2
  • create new VPC and call it lan1 (or whatever you like)
  • change Network on VM2's primary network interface to lan1 (select ephemeral internal IP address):

enter image description here

  • stop VM1
  • note VM1's IP - lets call it ip1
  • change Network on VM1's primary network interface to lan1 and select "Ephemeral (automatic) as Primary Internal IP:

enter image description here

  • change Network on VM2's primary network interface to default and select as "Ephemeral (custom)" Primary Internal IP and type the noted earlied IP in the "address" field. enter image description here
  • start VM2

Why going through lan1 ? For some reason GCP console refuses to change even ephemeral (custom) address to different one: enter image description here

This way you end up with VM2 having address you wanted.

If you want it to be a static one you need to promote it.


Both methods minimise the downtime to a couple of minutes.

If you're OK with a longer downtime and can create&configure you new VM quick then you can turn off VM1, change it's IP, reserve it as a new static internal IP and then create VM2 with that IP.

Wojtek_B
  • 4,245
  • 1
  • 7
  • 21
  • Thanks for your reply. The first method won't work for me as the disk size of both VM is not the same. I will try the second method. – hasnayn Feb 08 '22 at 10:01
  • I believe this shouldn't be an issue here, and even so you can [resize](https://cloud.google.com/sdk/gcloud/reference/compute/disks/resize) (increase disk size) both to the same amount and then switch them. – Wojtek_B Feb 08 '22 at 10:14
  • I have to decrease the disk size actually :P, that has made my life harder. I have updated my question. Please take a look. – hasnayn Feb 08 '22 at 10:31
  • 1
    Theres no issue with attachin a boot disk of a different size to a VM. Just tested method one and it works. – Wojtek_B Feb 08 '22 at 10:56
  • Have you tested with windows VMs? – hasnayn Feb 08 '22 at 12:08
  • I just did - switched disk betwen VM's and Windows booted up right away - I was able to login via RDP. – Wojtek_B Feb 08 '22 at 13:22
0

This should be possible, while you'd have to use a temporary third one IP address, in order not to produce a duplicate address conflict among one of the other two addresses, while changing them. They MUST be unique at any time, else you'll have a routing problem. The most straightforward might be to use both server's serial consoles, because these would not care about temporary IP address conflicts. The crux is, that one cannot change both at precisely the same time, and as soon as having produced the duplicate IP address, one may want to resort to the serial console anyway.

Martin Zeitler
  • 1
  • 19
  • 155
  • 216
  • Martin, Thanks for your reply. From where I can change the IP of a VM? In the network settings inside the VM, the IP is configured as automatic, should I change it from there or there is any way to change it from GCP console? Thanks again! – hasnayn Feb 07 '22 at 09:02