Chapters

Admin access 2: OS Login and no external IPs

IAP decides who can reach a VM's SSH port. The VM still has to decide who may log in, and as which Linux user. This chapter moves that second decision to identities too, and then puts together everything a VM needs to work with no external IP at all.

Controlling access · chapter 9 of 26 · 7 min read

The older way: SSH keys in metadata

Builds on29. SSH keys

Without OS Login, a VM decides who may log in by a list of SSH public keys kept in metadata, either on the project (every VM) or on one VM. A small Google program on each VM, the guest agent, copies that list into the VM. Anyone who holds the matching private key can log in. Nothing asks whether that person should still have access.

project metadata · ssh-keysworks on every VMyou@laptopadded by gcloud, 2025teammateadded by gcloud, 2025freelancerleft last yearkade-api-1accepts all 3 keyskade-api-2accepts all 3 keyskade-dbaccepts all 3 keyskade-workeraccepts all 3 keysthe guest agent on each VM copies the list inFreelancerstill holds theprivate keylogs in to every VM, and the log only names the key:Accepted publickey … SHA256:9fQ2…
  • Kadé's VMs, each accepting every key
  • The key list in project metadata

One list, copied to every VM. The freelancer's key is still on it, so the freelancer can still log in everywhere, and the VM's log names only the key.

Wide by design - scroll it sideways.

The part most people miss is how the keys got there. When OS Login is off and you run gcloud compute ssh for the first time, gcloud creates a key pair on your laptop and adds your public key to the project's metadata, if you have permission to edit it. So every person who has ever SSHed in with gcloud has usually left a key behind that works on every VM.

#ProblemWhat it means
1Keys are not tied to peopleA key is just a key. Someone leaves, and their key keeps working until a human removes it
2Project keys spread everywhereA key in project metadata works on every VM in the project. Giving access to one VM usually gives access to all of them
3No central controlManaging access means editing metadata by hand, key by key, and hoping nobody forgets to clean up
4Weak recordLogs show which key logged in, not which person
A VM can ignore project-wide keys with the instance metadata block-project-ssh-keys=TRUE. That narrows the problem but does not fix it; OS Login does.

The better way: OS Login

OS Login is a Google service that ties SSH login to Google identities and IAM, instead of keys kept in metadata. Like IAP, it is nothing you install or run as a server. It has two halves:

  • At Google: OS Login keeps a profile for each person: their Linux username, their user ID and their public SSH keys, all tied to their Google account. IAM records who may log in and who may use sudo.
  • On each VM: small parts of Google's guest environment, already installed in Google's public images, connect Linux to that profile. They let sshd fetch a person's keys from Google, and let Linux know accounts such as you_kade_lk that appear in no local user file. Setting enable-oslogin=TRUE in metadata switches them on.

The difference from metadata keys is when access is decided. With metadata keys, it was decided once, when someone added a key, maybe a year ago. With OS Login, the VM asks at every login: whose key is this, and does IAM let that person in right now? It asks through its metadata server, 169.254.169.254, a Google service that every VM reaches on its own host. So the check needs no internet access and no external IP, which matters for the rest of this chapter.

internetYouyou@kade.lkgcloud compute sshFreelancerold key, only inmetadataGoogle's networkOS Login · run by Googleprofile you@kade.lk: your keyIAM: compute.osAdminLoginLinux user: you_kade_lk1 gcloud adds your key to your profilekade-vpckade-db · sshdenable-oslogin=TRUEmetadata keys: ignoredasks via 169.254.169.2542 ssh, via IAP3 whose key? allowed?4 ✓ logged inas you_kade_lk,with sudo✕no allowed owner:refused
  • Google's: OS Login, which holds your profile and asks IAM
  • kade-vpc and kade-db
  • Your login

Your key lives on your OS Login profile, tied to you@kade.lk, not in the VM's metadata. Each login is checked against IAM at that moment, so removing a role ends access on every VM at once.

Wide by design - scroll it sideways.

#PropertyDetail
1Controlled by IAM rolesroles/compute.osLogin for a normal user, roles/compute.osAdminLogin for a user with sudo
2Revocation is instant and centralRemove the role or disable the Google account, and access to every VM ends at once. No stray keys to hunt
3Logins map to real peopleThe Linux username comes from the Google identity, so logs show who logged in
4Metadata keys are ignoredOnce OS Login is on for a VM, keys in project or instance metadata no longer work there. That is how the freelancer's old key stops working
5Optional second factorOS Login can require 2-step verification on top of the key (enable-oslogin-2fa=TRUE)
6Turned on with metadataenable-oslogin=TRUE on the project (all VMs) or on one VM. An organization policy can require it everywhere (chapter 17)

Why logging in needs the VM's service account

Logging in to a VM gives you more than a shell. It also gives you the VM's own Google identity:

  • A VM runs as a service account. kade-db runs as sa-kade-db, and Google API calls from kade-db carry that service account's permissions.
  • Any program on the VM can use it. The metadata server, 169.254.169.254, hands the service account's token to whoever asks from inside the VM. It cannot tell the database apart from a person who has logged in.
  • So a login can borrow those permissions. That is called privilege escalation: getting access your own roles never gave you.
dev2@kade.lkno Secret Manageraccess of their ownGoogle's networkkade-vpckade-db · runs as sa-kade-dbdev2 runs: gcloud secretsversions access …metadata server169.254.169.254, on the host1 token?2 its tokenSecret Managerkade-db-password3 as sa-kade-dbdev2 reads a secretnobody granted them
  • Google's: the metadata server and Secret Manager
  • kade-vpc and kade-db

dev2 has no Secret Manager access, but one command on kade-db runs as sa-kade-db and reads the database password.

Wide by design - scroll it sideways.

Google closes this gap with one firm rule: nobody may use a service account's permissions unless IAM says they may act as it, which is what roles/iam.serviceAccountUser grants. Google checks it at every point where a service account could be borrowed:

#A way to borrow sa-kade-dbWhere Google checks serviceAccountUser
1Create a new VM that runs as sa-kade-db, then log in to itWhen you create the VM
2Deploy a Cloud Run service that runs as sa-kade-dbWhen you deploy (chapter 10)
3Log in to kade-db, which already runs as sa-kade-dbWhen you log in, by OS Login
Once you are inside the VM, nothing can check who you are any more, so the check has to happen at the door. With metadata keys there was no such check: Google only knew that some key matched, not who was logging in. OS Login knows the person, so it can apply the rule.

Two habits follow:

  • Grant serviceAccountUser on each VM's own service account, not on the project. On the project, the person could act as every service account in it.
  • Keep each service account's permissions small, as chapter 5.1 did by giving each VM its own. Logging in to a VM always brings its permissions along.

Who can log in

Each tab is one person or key trying to log in. The four checks run in order, and the first one that fails decides:

you@kade.lkproject adminOS LOGIN CHECKS ON KADE-DB✓OS Login is on for this VMenable-oslogin=TRUE on the project✓A login rolecompute.osAdminLogin: with sudo✓May use the VM's service accountserviceAccountUser on sa-kade-db✓Inside kade.lk, or allowed inyou@kade.lk is in the organizationlogged in as you_kade_lksudo works

you@kade.lk: logged in as you_kade_lk, sudo works.

Every check passes. osAdminLogin is the role with sudo, so you can run admin commands. The Linux user comes from your identity, and that is the name the VM's logs show.

Show as config
you@kade.lk → roles/compute.osAdminLogin on project kade-prod
             roles/iam.serviceAccountUser on sa-kade-db

Two gates, both about identity

you@kade.lkgcloud sshGoogle's networkGate 1 · IAP (chapter 6.1)can you REACH port 22?· IAP API enabled· iap.tunnelResourceAccessor· firewall: 22 from IAPkade-vpc · on the VMGate 2 · OS Login (this chapter)can you LOG IN, and as whom?· enable-oslogin=TRUE· compute.osLogin or osAdminLogin· serviceAccountUser on the VM's SA→ shell as you_kade_lk
  • Google's network; gate 1 is IAP
  • kade-vpc, where the VM runs
  • Your connection
  • Gate 2: OS Login on the VM

IAP controls reaching the VM by identity. OS Login controls logging in by identity. Together, access is decided entirely by who you are, and one IAM change removes it everywhere.

Wide by design - scroll it sideways.

Try both gates together. Each setting matches one requirement:

IAP API
Your IAM role
Firewall for port 22
SSH server on the VM
OS Login on the VM
Your login role
serviceAccountUser on VM's SA

Shell open. Both gates passed on identity alone.

  1. gcloud logs you in and opens an HTTPS tunnel to IAP on port 443.
  2. IAP accepts you: API on, IAM role present.
  3. Firewall lets 35.235.240.0/20 reach port 22.
  4. The tunnel reaches the SSH server on the VM.
  5. OS Login accepts you as you_kade_lk, with sudo.

Switching to OS Login safely

Do this after chapter 6.1's IAP change is proven, not at the same time. Changing both the access path and the login method at once makes it hard to tell which one broke if something does.

#StepCheck
1Grant the roles first: compute.osAdminLogin for you, serviceAccountUser on each VM's service accountThe grants show in IAM
2Turn on OS Login for one VM: enable-oslogin=TRUE on kade-api-2IAP SSH to kade-api-2 logs you in as you_kade_lk, and sudo works
3Turn it on for the projectSSH works on every VM
4Remove the old ssh-keys from project metadataNothing changes for anyone, because OS Login already ignores them. This is cleanup, and it removes the fallback if OS Login is ever turned off
5Later: require OS Login with an organization policyChapter 17

Removing a VM's external IP: the full picture

Builds on10. NAT

Can a VM have no external IP and still do its job? Yes. Every kind of traffic a VM needs has its own path that does not use the VM's own public address. There are four, each from a different chapter:

internetYouadmin workShopperswww.kade.lk/mediaScannerlooking for a way inapt, PayGatesites on the internetGoogle's networkIAPchapter 6.1load balancer34.120.88.10Google APIsStorage, Loggingkade-vpckade-worker10.10.1.20 · no external IPkade-media VMs · ch. 12.2:8080 · no external IPCloud NAT · chapter 7a shared IP, out only1 admin in2 Private GoogleAccess · ch. 93 out to the internet4 public traffic into internal IPs,from LB ranges✕nothing to connect to:no external IP
  • Google's network: IAP, the load balancer and Google APIs
  • kade-vpc, its VMs and Cloud NAT
  • Traffic coming in

Wide by design - scroll it sideways.

#Without an external IP, the VM would be…Fixed byChapter
1Unreachable for admin workIAP TCP forwarding6.1
2Unable to reach Google APIs (Cloud Storage, Secret Manager, Logging)Private Google Access9
3Unable to reach the internet (apt, PayGate)Cloud NAT in its region7
4Unreachable for the public (a website or an API)An external load balancer11, 12

Path 4 is how a VM serves the public without a public address of its own, and it is the one most people ask about:

  • The public IP lives on the load balancer, not the VM. Shoppers connect to 34.120.88.10, which belongs to the load balancer's forwarding rule. The load balancer then connects to the VMs on their internal IPs, across Google's network (chapter 12.2 builds this for kade-media).
  • The firewall lets the app's port in from the load balancer's ranges only: 35.191.0.0/16 and 130.211.0.0/22, the same ranges its health checks use. Nobody else can reach port 8080.
  • Cloud NAT does not help here. NAT is outbound only: it lets a VM start connections out, and it never accepts a new connection in. Inbound public traffic always needs a load balancer, or an external IP.
  • The VM sees the load balancer's address, not the shopper's. The shopper's real IP arrives in a header (chapter 12.1).

With all four paths in place, a VM does its whole job while nothing on the internet can start a connection to it directly. A VM becomes reachable from the internet in only two ways: an external IP of its own, or a place behind an external load balancer. Take away the first, and the only way in is the one Kadé builds on purpose (chapters 11 to 13). That makes no external IP the strongest position a VM can be in.

Back to Kadé

Commands in this chapter

Commands 1 to 3 are read-only. Commands 4 to 7 change the project.

See SSH keys and OS Login settings in project metadata

  • What it shows: Project-wide metadata, including ssh-keys and enable-oslogin.
  • What to look for: Keys whose owner you cannot name, and whether enable-oslogin is TRUE.
gcloud compute project-info describe \  --format="yaml(commonInstanceMetadata.items)"

See one VM's own metadata

  • What it shows: Instance-level keys and settings, which override the project's.
  • What to look for: enable-oslogin=FALSE on a single VM, which would switch it back to metadata keys.
gcloud compute instances describe kade-db --zone=asia-southeast1-b \  --format="yaml(metadata.items)"

See your OS Login profile

  • What it shows: Your Linux username and the keys registered to your identity.
  • What to look for: The username, e.g. you_kade_lk, which is what shows in the VM's logs.
gcloud compute os-login describe-profile

Grant OS Login with sudo (changes the project)

  • What it shows: Lets you log in to every VM as an admin.
  • What to look for: Use roles/compute.osLogin instead for people who do not need sudo.
gcloud projects add-iam-policy-binding kade-prod \  --member=user:you@kade.lk --role=roles/compute.osAdminLogin

Allow using a VM's service account (changes the project)

  • What it shows: The second role trap: needed to log in to a VM that runs as sa-kade-db.
  • What to look for: Without it, OS Login refuses even with the login role.
gcloud iam service-accounts add-iam-policy-binding \  sa-kade-db@kade-prod.iam.gserviceaccount.com \  --member=user:you@kade.lk --role=roles/iam.serviceAccountUser

Turn on OS Login for one VM, then the project (changes the project)

  • What it shows: Steps 2 and 3 of section 04.
  • What to look for: After the first line, test SSH to kade-api-2 before running the second.
gcloud compute instances add-metadata kade-api-2 --zone=asia-southeast1-b \  --metadata=enable-oslogin=TRUEgcloud compute project-info add-metadata --metadata=enable-oslogin=TRUE

Remove the old keys from project metadata (changes the project)

  • What it shows: Step 4 of section 04: cleanup of every project-wide SSH key.
  • What to look for: ssh-keys gone from command 1. Save command 1's output first as a record.
gcloud compute project-info remove-metadata --keys=ssh-keys

Spotted a mistake, or something unclear? Suggest an edit on GitHub

See it broken

Harbour is a platform built wrong on purpose. These defects are this chapter's ideas, left undone: what each one breaks, how you would find it, and the change that closes it.

  1. D3a
    Bastion carries an external IP

    mediumcloses in phase 2, Secure access

    Goes with Removing a VM's external IP: the full picture

  2. D16
  3. D3b