Home Google CloudHow to Create and Harden a Compute Engine VM on Google Cloud

How to Create and Harden a Compute Engine VM on Google Cloud

by Knowledge Base Linux
How to Create and Harden a — How to Create and Harden a Compute Engine VM on Google Cloud

Creating a compute engine vm on Google Cloud is easy, but creating one that is secure, reliable, and ready for production takes a few extra steps. A poorly configured compute engine vm can expose SSH to the internet, leak credentials, or run outdated software that attackers can exploit within minutes. In this guide, you will learn how to create a compute engine vm step by step and harden it properly from day one.

Google Cloud Compute Engine is an Infrastructure as a Service (IaaS) offering that lets you run virtual machines on Google’s global infrastructure. You control the operating system, networking, and security settings, which also means you are responsible for securing them. Hardening is not optional for any server that handles real traffic or data.

Why Hardening Your Compute Engine VM Matters – How to Create and Harden a

Every public IP address is scanned by bots within minutes of being created. If your compute engine vm allows password authentication, uses a default user, or has no firewall restrictions, it will be probed constantly. Hardening reduces the attack surface, enforces key-based authentication, and enables platform features like Shielded VM that protect against boot-level malware and rootkits.

A hardened instance also makes compliance easier and reduces operational surprises. Automatic updates, integrity monitoring, and OS Login give you an auditable and manageable server instead of a one-off machine nobody wants to touch.

compute engine vm hardening with Shielded VM and SSH keys

Prerequisites – How to Create and Harden a

  • A Google Cloud account with billing enabled
  • A project owner or editor role, or permissions to create Compute Engine instances
  • Basic familiarity with Linux and SSH — see our guide on how to SSH into a Linux server
  • Optionally, the gcloud CLI installed locally — or you can use Cloud Shell in the Console
  • An SSH key pair — learn how to generate SSH keys on Linux if you do not have one yet

For official documentation, refer to the Google Cloud Compute Engine documentation.

Step 1: Create a Project and Enable the Compute Engine API

In the Google Cloud Console, create a new project or select an existing one. Then navigate to APIs & Services and enable the Compute Engine API. From the terminal, you can do the same:

gcloud projects create my-secure-project --name="My Secure Project"
gcloud config set project my-secure-project
gcloud services enable compute.googleapis.com

Wait a minute for the API to become fully active before creating instances.

Step 2: Create Your Compute Engine VM

Create via the Console

Go to Compute Engine > VM instances > Create instance. Choose a name, region, and zone close to your users. Select machine type e2-medium (2 vCPU, 4 GB memory) as a balanced starting point. Under Boot disk, choose Debian 12 or Ubuntu 22.04 LTS. Review firewall options and do not open unnecessary ports.

Create via gcloud

The command-line method is reproducible and ideal for automation:

gcloud compute instances create secure-vm-1 \
  --zone=us-central1-a \
  --machine-type=e2-medium \
  --image-family=debian-12 \
  --image-project=debian-cloud \
  --boot-disk-size=20GB \
  --boot-disk-type=pd-balanced \
  --shielded-secure-boot \
  --shielded-vtpm \
  --shielded-integrity-monitoring

This single command creates a compute engine vm with Shielded VM features already enabled.

Step 3: Reserve a Static External IP

Ephemeral IPs change when an instance stops. For a production server, reserve a static address:

gcloud compute addresses create secure-vm-ip --region=us-central1
gcloud compute addresses list

Then assign it during instance creation with --address=secure-vm-ip or attach it from the Console under VPC network > External IP addresses. A static IP makes DNS, firewall allowlists, and SSL certificates far easier to manage.

Step 4: Configure SSH Keys and OS Login for Your Compute Engine VM

OS Login is the recommended way to manage SSH access on Google Cloud. It ties SSH access to Google identities and IAM, supports two-factor authentication, and removes the need to manually distribute keys.

gcloud compute instances add-metadata secure-vm-1 \
  --zone=us-central1-a \
  --metadata enable-oslogin=TRUE

If you prefer manual keys, add your public key in instance metadata under ssh-keys. Never share private keys and never reuse a personal key across production systems.

Step 5: Hardening Your Compute Engine VM

Disable Root and Password Login

Connect to the instance and edit the SSH daemon configuration:

sudo nano /etc/ssh/sshd_config

Set the following values:

PermitRootLogin no
PasswordAuthentication no
PubkeyAuthentication yes

Restart SSH:

sudo systemctl restart ssh || sudo systemctl restart sshd

Enable Shielded VM

Shielded VM provides Secure Boot, a virtual Trusted Platform Module (vTPM), and integrity monitoring. If you did not enable it at creation, stop the instance and enable it from the Console or with gcloud, then start it again. Integrity monitoring alerts you if the boot sequence changes unexpectedly.

Block Project-Wide SSH Keys

Project-wide keys grant access to all instances in a project. For sensitive machines, block them so only instance-specific keys or OS Login work:

gcloud compute instances add-metadata secure-vm-1 \
  --zone=us-central1-a \
  --metadata block-project-ssh-keys=TRUE

Enable Automatic Updates

On Debian and Ubuntu, install unattended upgrades:

sudo apt update && sudo apt install -y unattended-upgrades
sudo dpkg-reconfigure -plow unattended-upgrades

You can also use OS Patch Management in Google Cloud to schedule patching across a fleet.

Step 6: Verify the Configuration

Confirm the instance details and security settings:

gcloud compute instances describe secure-vm-1 --zone=us-central1-a
gcloud compute instances list

Check that Shielded VM is on, the external IP is correct, and you can log in with your key while password login is rejected. Test from a fresh terminal to be certain you have not locked yourself out.

Troubleshooting

Cannot SSH into the Instance

Check that the VPC firewall allows TCP port 22 from your IP, that the instance is running, and that OS Login or metadata keys are correctly set. Use the SSH troubleshooter in the Console and try connecting via the serial console if needed.

Permission Denied (publickey)

This usually means the key on the instance does not match your private key, the username is wrong, or project-wide keys are blocked while you are relying on them. Verify the user, re-add the key, and check IAM roles for OS Login — you need roles/compute.osLogin.

Conclusion

You now know how to create and harden a compute engine vm on Google Cloud: create the project, enable the API, launch the instance with the right machine type and image, reserve a static IP, configure OS Login, and apply hardening with Shielded VM, disabled password login, and automatic updates. Build this into a template or startup script and every new compute engine vm you launch will start secure by default.

You may also like

KbLinux

Typically replies within a day

Hello, Welcome to the site. Please click below button for chatting me through Telegram.