GCP Account with Pre-loaded Credits How to Whitelist IP Addresses in Google Cloud Firewall

GCP Account / 2026-05-16 18:04:08

Introduction: Why Whitelist IPs in Google Cloud?

So, you want to let only certain IP addresses access your Google Cloud resources? Smart move. Whether you're protecting a sensitive app or ensuring only your team's office network can hit the database, whitelisting is your first line of defense. Forget those "open all ports" days—modern security is all about precision. Google Cloud Firewall rules let you do exactly that, but it's easy to mess up. Don't worry—this guide breaks it down step by step, with real-world examples and zero fluff.

Prerequisites

What You'll Need

Before diving in, make sure you've got a few things ready. First, a Google Cloud account with the necessary permissions (like compute.admin or firewall.admin). Second, know the IP addresses you want to allow—these should be static IPs, not dynamic ones that change. Third, be aware of which ports and protocols (TCP, UDP, ICMP) your service uses. If you're unsure, check your app's documentation. Lastly, have a plan for your target instances—whether it's all VMs in a network or specific ones tagged for this rule. Skipping these steps will leave you scratching your head later.

Step-by-Step Guide Using the Google Cloud Console

Creating a New Firewall Rule

Head over to the Google Cloud Console. Navigate to VPC Network > Firewall. Click Create Firewall Rule. Now, give your rule a descriptive name—something like "allow-remote-admin-ips". Avoid generic names like "rule1" or "allow-all". Next, set the Direction to Ingress (since you're controlling incoming traffic). For the Source IP ranges, enter the exact IPs or CIDR blocks you want to allow. For example, 192.0.2.0/24 for a whole subnet. Don't use 0.0.0.0/0 unless you want the entire internet in—trust me, you don't.

Configuring Source IP Ranges

Here's where people often slip up. If you're whitelisting specific IPs, make sure they're exact. If you're using a dynamic IP (like a home internet connection that changes), consider using a dynamic DNS service or a VPN with a fixed endpoint. You can also use Google Cloud's "Service Directory" for more advanced setups, but for most, just list the IPs. Separate multiple entries with commas. And remember—CIDR blocks are your friend. Instead of listing 100 IPs individually, use a /24 or /20 range if applicable.

Specifying Target Instances

Under Targets, you can choose between all instances in the network or specific tags. If you have multiple VMs, assign a tag like "allow-admin" and then apply the rule to that tag. This way, only VMs with that tag will be affected. It's cleaner than applying it to the whole network. If you're unsure, start with "All instances in the network" but tighten it later. But don't leave it open—always limit targets to what's necessary.

Using gcloud CLI for Firewall Rules

Basic Command Structure

GCP Account with Pre-loaded Credits Prefer the command line? Here's the CLI way. First, open your terminal (or Cloud Shell). The basic command is gcloud compute firewall-rules create followed by flags. You'll need the rule name, source ranges, allowed protocols/ports, and targets. Here's the skeleton:

gcloud compute firewall-rules create RULE_NAME \
  --direction=INGRESS \
  --source-ranges=IP_RANGE \
  --allow=PROTOCOL:PORT \
  --target-tags=TAG_NAME

Example Command

For example, to allow SSH (port 22) from 203.0.113.5 and 198.51.100.0/24 to VMs tagged "admin-servers":

gcloud compute firewall-rules create allow-ssh-admin \
  --direction=INGRESS \
  --source-ranges=203.0.113.5,198.51.100.0/24 \
  --allow=tcp:22 \
  --target-tags=admin-servers

Notice the tcp:22 part? That's critical. If you forget to specify the protocol and port, the rule won't do anything useful. Also, --target-tags is optional but highly recommended for precision.

Common Mistakes to Avoid

Overly Permissive Rules

One of the biggest no-nos is setting source ranges to 0.0.0.0/0 for services that don't need public access. I've seen people do this for internal databases, thinking "it's behind a firewall," but Google's network is still exposed to threats. Always restrict to the smallest possible IP range. If your app only needs access from your office, whitelist only that office's IP. Even better, use a bastion host or jump server for admin access instead of opening direct access.

Forgetting to Specify Protocols

Another classic: creating a firewall rule that allows "all protocols" when you only need HTTP/HTTPS. While allow=tcp:80,tcp:443 is fine for a web server, allowing all protocols (like allow=tcp,udp,icmp) is overkill and risky. Each unnecessary open port is a potential attack vector. Be specific—only allow what's absolutely necessary.

Best Practices for IP Whitelisting

Limiting Access to Specific Ports

Whitelisting IPs is great, but pairing it with port restrictions is even better. For instance, if you're running a web app, allow only ports 80 and 443 for HTTP/HTTPS. For a database, restrict to the specific port (like 3306 for MySQL) and only from the application servers' IPs. This layered approach—IP whitelisting plus port restrictions—makes your setup more resilient. It's like locking your door and then adding a security camera.

Regular Audits of Rules

Firewall rules can become outdated quickly. Maybe a developer left a temporary rule for testing, or a client changed their IP. Set up a monthly audit to review all firewall rules. Google Cloud has tools like Security Command Center that can flag overly permissive rules, but nothing beats a manual check. Remove unused rules and update IP ranges as needed. It's boring but necessary—think of it as spring cleaning for your cloud security.

Troubleshooting Firewall Rule Issues

Rule Not Applied Correctly

Ever created a rule but still can't connect? Check the priority. Firewall rules are evaluated in priority order (lower number = higher priority). If a higher-priority rule denies traffic, your new rule might never get a chance. Use the Firewall Rules page to see rule order. Also, ensure your rule's target tags match the VMs' tags. If you're using a specific tag, the VM must have that exact tag (case-sensitive!). And don't forget: rules only apply to the VPC network they're created in.

Testing Connectivity

Before declaring victory, test your rule. Use tools like telnet or nc to check if the port is open from a whitelisted IP. Alternatively, use Google Cloud's VPC Network > Connectivity Tests to simulate traffic from a source IP to your VM. If it fails, double-check the source ranges, ports, and target tags. Sometimes it's the simplest thing—like a typo in the IP address. I once spent an hour debugging because I wrote 192.168.1.1 instead of 192.168.1.10. Don't be like me.

Conclusion

Whitelisting IPs in Google Cloud Firewall isn't magic—it's just smart planning. By following these steps, you'll lock down your resources without overcomplicating things. Remember: start small, be precise, and review often. Security isn't about perfection; it's about making it hard for attackers to guess your next move. Now go forth and secure that cloud like a pro. And if you ever accidentally open a rule to the whole internet… just delete it and pretend it never happened. We've all been there.

TelegramContact Us
CS ID
@cloudcup
TelegramSupport
CS ID
@yanhuacloud