11

When using google-cloud-sdk for windows, gcloud compute ssh will launch a built in Putty. Is it possible to specify a different default ssh client for gcloud compute ssh command?

2
  • Depends. Is putty registered to handle the ssh:// protocol shortcut?
    – ivanivan
    Commented Jan 3, 2018 at 0:13
  • Now in 2024 gcloud on Windows still launches Putty... OpenSSH has been standard in Windows for years now. Haven't yet been able to find out if there is now a way to change this. Even if there is though, kinda wild Putty is the default still... Commented Jan 11 at 4:54

3 Answers 3

7

Recent releases of Cloud SDK for Windows come with prepackaged putty executables (dir/bin/sdk folder) By looking at the source code of gcloud it seems that the use of PuTTY is hardcoded and it's currently not possible to choose another SSH client.

0
3

You can login to the GCP VM using MobaXterm, Windows Terminal or any other command-prompt application which supports ssh. All you need is the private key and the internal/external IP of your GCP instance(aka GCP VM).

Private key is usually C:\Users\<username>\.ssh\google_compute_engine

If you don't find that file you can know it's path by dry-running the gcloud login command.

$ gcloud compute ssh example-instance --zone=us-east1-b --dry-run

(--zone flag is optional.)

From the output, you'll notice the private key used in the command will end with a .ppk extension (ppk stands for a putty-private-key).

If you browse the File Explorer in the same path, you'll find another file without the .ppk extension, that's the private key in the standard format.

Now you need the external/internal IP of your instance (You can use the internal IP if you're on VPN).

You can find out your VM's external IP using 2 ways.

  1. log in to your VM normally and find out its external IP using the command

    $ curl ifconfig.co

(I would suggest you not to use the external IP as it's a dynamic IP and will be changed at some point of time and moreover, you'll be able to use it only if the GCP instance is allowed to login via external IP by enabling the ssh port)

  1. You can also find both internal/external IP in the GCP Console page (possible only if you've admin rights).

    • Link to image showing this point

Once you've got the internal or external IP of your VM, you can run the below command to log in from your favourite shell.

$ ssh -i <path to private key without the (.ppk) extension> user@your_instance_IP

1

You can create a new command and save it in your Microsoft.PowerShell_profile.ps1:

function g-ml-vm
{
    # get default command as string(--dry-run)
    $SrcCommand = (gcloud beta compute ssh username@ml-ubuntu-18 --dry-run `
        --zone "your-zone" --project "your-project-id") | Out-String

    # remove path to putty.exe
    $PuttyExe = "putty.exe"
    $SrcCommand = $SrcCommand.SubString($SrcCommand.IndexOf($PuttyExe) + $PuttyExe.length)

    # use ssh, remove .ppk file extension from the key name
    $NewCommand = "ssh" + $SrcCommand.Replace(".ppk", "")

    # run
    iex $NewCommand
}

You only have to change the first command of this function. Replace username@ml-ubuntu-18, your-zone and your-project-id with your values.

To use the new profile script restart the terminal or run . $profile. After that you can use your this command.

This way I connect to gcloud VM without leaving my fancy Windows Terminal window :)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .