Home Google CloudGoogle Cloud Firewall Rules: Secure Compute Engine VMs

Google Cloud Firewall Rules: Secure Compute Engine VMs

by Knowledge Base Linux
Google Cloud Firewall Rules filtering traffic to Compute Engine VMs

Every Compute Engine virtual machine sits behind a firewall, whether you configured one or not. The rules deciding which traffic reaches your VMs live at the VPC network level, not on the guest operating system, so a misconfigured rule can expose SSH to the whole internet or lock you out of a server you own. This guide explains how google cloud firewall rules work and how to build a tight ruleset keeping administration private and web services reachable.

Google Cloud Firewall Rules network tags controlling VM access

Google Cloud firewall rules are stateful: return traffic for an allowed connection is permitted automatically, with no separate egress rule needed for replies. Rules are evaluated by priority, lower numbers winning, and every VPC network carries two implied rules: ingress denied by default, egress allowed by default. The implied rules cannot be deleted, but you can override them with higher-priority rules, meaning lower priority numbers.

By the end of this guide, you will understand rule priority, tags, and service accounts, create a restricted SSH rule, allow HTTP and HTTPS through tags, avoid a common egress mistake, and audit the result with gcloud. The examples use the default network, but the same commands work on custom VPC networks.

Prerequisites

  • A Google Cloud project with billing enabled and the Compute Engine API activated.
  • The Google Cloud CLI installed and authenticated, or Cloud Console access with the Compute Network Admin role.
  • At least one Compute Engine VM and its VPC network name.
  • Your current public IP address (curl ifconfig.me from your workstation).

How google cloud firewall rules Work

In google cloud firewall rules, a firewall rule has a direction, ingress or egress, a priority from 0 to 65535, an action of allow or deny, and a target. The target decides which VMs it applies to: all instances in the network, instances with specific network tags, or instances running under specific service accounts. A rule with no target applies to every VM, which is why the broad default rules deserve attention.

For ingress rules, the source is an IP range or a source tag or service account. For egress rules, the destination is an IP range. A rule can allow or deny specific protocols and ports, such as tcp:22 or tcp:80 and tcp:443, or all protocols. When a packet arrives, Google Cloud evaluates matching rules in priority order and applies the first match. If none matches, the implied rule decides: deny for ingress, allow for egress.

Rule Direction Priority Action
Implied allow egress Egress 65535 Allow
Implied deny ingress Ingress 65535 Deny
Your custom rules Ingress or egress 0 to 65534 Allow or deny

One more behavior matters: firewall rules affect new connections only, so an established session survives a rule change. Close the session and reconnect to test a change. For the full reference, see the official Google Cloud firewall documentation.

Review the Default Network Before You Change Anything

The default network ships with several preconfigured rules. List them first:

gcloud compute firewall-rules list \
  --filter="network:default" \
  --format="table(name, direction, priority, sourceRanges, allowed[].map().join(','))"

You will typically see default-allow-internal, default-allow-icmp, default-allow-rdp, and default-allow-ssh. The last one is the problem: it allows TCP port 22 from 0.0.0.0/0, so anyone on the internet can reach the SSH port of every VM. Key authentication still protects the VM, but an exposed port invites brute-force attempts. Replace it with a rule restricted to your IP, and only after the replacement is tested.

Create a Restricted SSH Rule

Create an ingress rule allowing SSH only from your public IP. Replace 203.0.113.25 with your own address, keeping the /32 suffix so it matches a single host:

gcloud compute firewall-rules create allow-ssh-from-office \
  --network=default \
  --direction=INGRESS \
  --priority=1000 \
  --action=ALLOW \
  --rules=tcp:22 \
  --source-ranges=203.0.113.25/32 \
  --target-tags=ssh-restricted \
  --description="Allow SSH only from the office IP"

The target tag ssh-restricted limits the rule to VMs carrying that tag. Attach it to your VM:

gcloud compute instances add-tags my-vm \
  --tags=ssh-restricted \
  --zone=us-central1-a

In the console: Compute Engine, then VPC network, then Firewall, then Create Firewall Rule, with direction ingress, action allow, target specified target tags, source range your IP with /32, and tcp:22. Test SSH while the old default rule is still in place, and only disable it once the new rule works:

gcloud compute firewall-rules update default-allow-ssh --disabled

Disable rather than delete: it keeps a quick rollback path if you ever lose access.

Allow HTTP and HTTPS with Network Tags

Web traffic should be public but deliberate, and google cloud firewall rules let you scope it precisely. Create one rule for web ports, scoped with a tag so only your web VMs receive it:

gcloud compute firewall-rules create allow-web-traffic \
  --network=default \
  --direction=INGRESS \
  --priority=1000 \
  --action=ALLOW \
  --rules=tcp:80,tcp:443 \
  --source-ranges=0.0.0.0/0 \
  --target-tags=web-server \
  --description="Allow public HTTP and HTTPS to web VMs"

Attach the tag to each web VM:

gcloud compute instances add-tags my-web-vm \
  --tags=web-server \
  --zone=us-central1-a

A 0.0.0.0/0 source range is correct here, because a public website must be reachable from anywhere. The tag keeps the rule from applying to your database or administration VMs. If you later place these VMs behind a load balancer, health checks arrive from Google’s probe ranges, so review our guide to setting up HTTPS load balancing on Google Cloud before tightening the source range. The console offers predefined http-server and https-server tags at VM creation, but one rule with a single tag is easier to audit than several overlapping rules.

Think Twice Before a Deny-All Egress Rule

Blocking all outbound traffic sounds secure, but it is the fastest way to break a Compute Engine VM. A deny-all egress rule overrides the implied allow egress rule, yet the guest environment depends on outbound access: the metadata server at 169.254.169.254 supplies startup scripts, SSH keys, and tokens, and DNS and package updates need outbound traffic too. Deny egress without exceptions, and VMs may boot without startup scripts.

If compliance demands egress control, build it as an allowlist: first add a high-priority rule permitting the metadata server and DNS, then add the deny-all rule at a priority still higher than the implied rule:

gcloud compute firewall-rules create allow-metadata-egress \
  --network=default \
  --direction=EGRESS \
  --priority=1000 \
  --action=ALLOW \
  --rules=tcp:80 \
  --destination-ranges=169.254.169.254/32 \
  --description="Allow egress to the metadata server"

gcloud compute firewall-rules create deny-all-egress \
  --network=default \
  --direction=EGRESS \
  --priority=2000 \
  --action=DENY \
  --rules=all \
  --destination-ranges=0.0.0.0/0 \
  --description="Deny all other egress traffic"

Then add further allow rules, at priorities between 1000 and 2000, for every destination the workload genuinely needs, such as package mirrors and APIs. Pulling container images from a private registry is one of those destinations, so plan egress for it in advance, and see our guide to Artifact Registry for Docker images for the registry side. Test egress changes from a non-production VM first, because a mistake here can cut off the SSH session you are using to fix it.

Test and Audit google cloud firewall rules

Verify SSH from your allowed IP, then confirm the restriction from a different network, such as a phone hotspot, where the connection should time out. Check the web rule with:

curl -I http://YOUR_VM_EXTERNAL_IP/

Audit the whole ruleset regularly, not just your own rules:

gcloud compute firewall-rules list \
  --format="table(name, disabled, direction, priority, sourceRanges, targetTags.list())"

Look for 0.0.0.0/0 sources on administrative ports, rules with no target tags applying to every VM, and rules you no longer recognize. For one rule, describe shows the full configuration:

gcloud compute firewall-rules describe allow-ssh-from-office

Enable firewall logging on sensitive rules with –enable-logging. Logged connections appear in Cloud Logging, where you can filter by rule name and see which sources are hitting your VMs.

Troubleshooting

The rule exists but traffic is still blocked

Check priority first. A deny rule with a lower priority number beats your allow rule, no matter which was created most recently. List rules sorted by priority and look for a higher-priority deny matching the same traffic. Also confirm the rule is enabled, since a disabled rule is silently ignored, and confirm the direction: an egress rule never fixes an ingress problem.

The rule exists but does not apply to the VM

This is almost always a tag mismatch: the rule targets ssh-restricted but the VM carries a different tag, or the tag went to the wrong instance. Check the tags section of gcloud compute instances describe. Tags and service accounts are separate mechanisms, so a rule targeted at a service account never matches a VM by tag alone.

SSH is still open to the whole internet

You probably created the restricted rule but forgot the default one. The default-allow-ssh rule still matches every VM, so your narrow rule changes nothing alone. Disable or delete default-allow-ssh after testing, then confirm no other rule allows tcp:22 from 0.0.0.0/0.

The VM breaks after an egress change

If startup scripts stop running or the guest agent reports errors after an egress deny, the metadata server or DNS is blocked. Re-add allow rules for 169.254.169.254 and DNS first, then add remaining destinations one at a time, working from the console or Cloud Shell rather than inside the affected VM.

Conclusion

A sound google cloud firewall rules posture is simple: deny by default, allow SSH only from addresses you control, scope web traffic with tags, and treat egress restrictions as an allowlist project rather than a single deny rule. Review the ruleset with gcloud regularly, remove stale rules, and log rules guarding administrative access. Done this way, google cloud firewall rules become a readable inventory of exactly what your network permits.

You may also like

KbLinux

Typically replies within a day

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