As per the gcloud documentation,
The Compute Engine DHCP server programs a default route only on the primary network interface of the VM(nic0). If we want to connect to the secondary interface using an external IP, we have to set a default route on that network interface through serial console.
If you want to ssh into the machine using the public IP assigned to nic1, you will have to change the default listening interface(which is by default nic0).
- ssh into the machine using serial console
gcloud compute --project=your_project connect-to-serial-port vm_name --zone=your_zone
- delete the default interface
ip route del default
- add the new interface as default
ip route add default required_ip dev required_interface
You will find your required ip
and required interface
by ssh(ing) into the vm using a jump server and running the command ip route
.
Important: Only use serial console and not any jump server to change the default route because if you will do this through a jump server, you will loose connection from the machine once you delete the default interface.
If ssh in serial console asks for a username and password and if you haven't set that, then you need to ssh into the vm using a jump server, run sudo passwd $USER
, complete the process and then try the above mentioned method to ssh through nic1.
Jump server refers to that machine which is present in the same network as my private machine (not having any external IP), such that first we can ssh into jump server and from there, ssh(jump) into the private vm.
I know the answer is a bit late but if you still need any help, comment it out.