Home Google CloudHow to Configure VPC Networks and Firewall Rules on Google Cloud

How to Configure VPC Networks and Firewall Rules on Google Cloud

by Knowledge Base Linux
How to Configure VPC Networks and — How to Configure VPC Networks and Firewall Rules on Google Cloud

This article covers How to Configure VPC Networks and in detail. Understanding gcp firewall rules is essential if you run virtual machines on Google Cloud. Unlike host firewalls such as UFW or CSF, which protect a single server from inside the operating system, gcp firewall rules operate at the network level. They are enforced by Google’s virtual network fabric before traffic ever reaches your VM, they apply even if the guest OS is misconfigured, and they can be managed centrally for hundreds of instances at once. In this guide you will create a custom VPC, add subnets, and configure secure gcp firewall rules for SSH, HTTP and HTTPS following the principle of least privilege.

If you already manage a standalone Linux server, compare this approach with our guides on how to install and configure UFW on Ubuntu and how to install CSF on Ubuntu. The concepts overlap, but the enforcement point is completely different.

Prerequisites for GCP Firewall Rules – How to Configure VPC Networks and

  • A Google Cloud account with billing enabled and a project created
  • The gcloud CLI installed and authenticated, or access to Cloud Shell
  • Owner or Editor permissions on the project, or at least Compute Network Admin
  • Your public IP address, so SSH can be restricted to you only
gcloud auth login
gcloud config set project YOUR_PROJECT_ID
gcloud services enable compute.googleapis.com

Understanding the Default Network – How to Configure VPC Networks and

Every new Google Cloud project starts with a default network. It is an auto-mode VPC with one subnet per region and several pre-created rules, including one that allows SSH from anywhere. That convenience is also a risk. For production work you should use a custom-mode VPC where you control the subnets, the CIDR ranges and every rule. Auto-mode networks use the same IP ranges everywhere, which makes future VPC peering and hybrid connectivity harder. A custom network avoids those collisions from day one.

Creating a Custom-Mode VPC Network

Create the network with subnet creation turned off, then add only the subnets you actually need. Good gcp firewall rules start with good network design: small, planned subnets are easier to secure than one large flat range.

gcloud compute networks create prod-vpc --subnet-mode=custom --bgp-routing-mode=regional

gcloud compute networks subnets create prod-subnet-us \
  --network=prod-vpc \
  --region=us-central1 \
  --range=10.10.1.0/24

gcloud compute networks subnets create prod-subnet-eu \
  --network=prod-vpc \
  --region=europe-west1 \
  --range=10.10.2.0/24

Choose private, non-overlapping CIDR ranges from the RFC 1918 space. Document each range, because overlapping ranges will block VPC peering, VPN connections and migrations later.

gcp firewall rules - custom VPC subnets configuration

How GCP Firewall Rules Work

Every rule has a direction, a priority, an action and a target. Ingress rules control traffic arriving at a VM, egress rules control traffic leaving it. Priority is a number from 0 to 65535; lower numbers win. The action is allow or deny. Targets can be all instances in the network, instances with a specific network tag, or instances running as a specific service account. Source ranges define where ingress traffic may come from, and destination ranges do the same for egress.

Two implied rules always exist at the lowest priority: an implied deny for ingress and an implied allow for egress. That means any incoming connection you do not explicitly allow is blocked by default, which is exactly the secure baseline you want.

Creating Secure GCP Firewall Rules for SSH and Web Traffic

Allow SSH only from your IP

Never expose SSH to the whole internet. Create a rule that allows TCP port 22 only from your own public IP, with a /32 range, and apply it only to instances carrying the ssh-access tag.

gcloud compute firewall-rules create allow-ssh-restricted \
  --network=prod-vpc \
  --direction=INGRESS \
  --priority=1000 \
  --action=ALLOW \
  --rules=tcp:22 \
  --source-ranges=YOUR_PUBLIC_IP/32 \
  --target-tags=ssh-access

Allow HTTP and HTTPS

Public web servers do need open ports 80 and 443, but only the web servers should get them. Use a network tag so databases and internal services stay closed.

gcloud compute firewall-rules create allow-web \
  --network=prod-vpc \
  --direction=INGRESS \
  --priority=1000 \
  --action=ALLOW \
  --rules=tcp:80,tcp:443 \
  --source-ranges=0.0.0.0/0 \
  --target-tags=web-server

When you create a VM, add the tags so the right rules attach:

gcloud compute instances create web-1 \
  --zone=us-central1-a \
  --network=prod-vpc \
  --subnet=prod-subnet-us \
  --tags=web-server,ssh-access \
  --machine-type=e2-medium

For larger estates, prefer service accounts as targets instead of tags. Any user who can edit an instance can change its tags, while service account assignment is more tightly controlled, so your gcp firewall rules become harder to bypass by accident.

Logging, VPC Flow Logs and Best Practices

Enable firewall rules logging on important rules so you can audit which connections were allowed or denied. The logs go to Cloud Logging and are invaluable during incident response.

gcloud compute firewall-rules update allow-ssh-restricted --enable-logging

gcloud compute networks subnets update prod-subnet-us \
  --region=us-central1 \
  --enable-flow-logs

VPC Flow Logs record a sample of the traffic between VMs and to the internet. Together with firewall logging they answer the two questions you will ask in any incident: what talked to what, and which rule decided its fate.

Follow these best practices: apply least privilege everywhere, never use 0.0.0.0/0 as the source for SSH or database ports, use Identity-Aware Proxy TCP forwarding instead of opening SSH at all where possible, review rules quarterly, and delete the default wide-open rules in any project you inherit. With IAP, administrators connect through Google’s identity layer and no public SSH rule is required:

gcloud compute ssh web-1 --zone=us-central1-a --tunnel-through-iap

Troubleshooting GCP Firewall Rules

VM is unreachable

List the rules and check for a higher-priority deny rule or a priority conflict. Remember that the lowest number wins, so a deny at priority 900 beats your allow at 1000.

gcloud compute firewall-rules list --filter="network=prod-vpc"
gcloud compute firewall-rules describe allow-ssh-restricted

Rule is not applying

The most common cause is a network tag mismatch: the tag on the rule and the tag on the instance must match exactly, including spelling. Also confirm the instance is actually in the same VPC network as the rule, and that your source IP has not changed, which happens often on home connections.

For the authoritative reference, see the official documentation at Google Cloud VPC firewall rules documentation.

Conclusion

You now know how gcp firewall rules protect a custom VPC: plan subnets carefully, allow only the traffic each workload needs, target rules with tags or service accounts, turn on logging, and prefer IAP over public SSH. Treat your gcp firewall rules as code, review them regularly, and your network perimeter will stay tight as your Google Cloud footprint grows.

You may also like

KbLinux

Typically replies within a day

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