1

I'm just getting started with gcloud vm's and trying to secure them up a bit. If I change the ssh port, what switch/flag do I add to the gcloud command when using the gcloud command like this

gcloud beta compute ssh --zone "us-east4-c" "base" --project "testproject"

Thanks!

Jin Lee
  • 3,194
  • 12
  • 46
  • 86

1 Answers1

4

After checking this GCP doc, you can see that you'll be able to set a custom port by adding a flag called --ssh-flag.

For example:

gcloud compute ssh example-instance --zone=us-central1-a --project=project-id --ssh-flag="-p 8000"

It is also applicable for gcloud beta:

gcloud beta compute ssh example-instance --zone=us-central1-a --project=project-id --ssh-flag="-p 8000"

The sample commands will SSH to your Compute Engine instance on port 8000.

Note: Before connecting, make sure you have an ingress Firewall Rule that accepts TCP on the port you've chosen.


UPDATE: If above is not working and you are getting connection refused, it means you need to configure your VM to listen to the port you wanted. Here are the steps:

  1. Go to sshd configuration file : sudo vi /etc/ssh/sshd_config

  2. Add your chosen port for example: enter image description here

  3. Save the file.

  4. Restart sshd service : sudo systemctl reload sshd.service

Donnald Cucharo
  • 3,866
  • 1
  • 10
  • 17