How to Configure Google Compute Engine SSH Agent Forwarding

Published
Length
3 min read

Luis Felipe Dias

All posts

You set up google compute engine ssh agent forwarding by loading keys into your local agent. Then you pass those agent arguments through your cloud tool when you log in. Engineering teams often use this setup to reach private servers through a single public VM. They also use it to pull code from private Git repos onto a cloud machine. Still, broad permissions on shared VMs expose forwarded keys to anyone with root privileges. How google compute engine ssh agent forwarding works In a standard

SSH Agent Forwarding Security + End-to-End Encryption: the complete guide

Architecture patterns, implementation strategies, and security best practices. Delivered to your inbox.

Free white paper. No spam. Unsubscribe anytime.

You set up google compute engine ssh agent forwarding by loading keys into your local agent. Then you pass those agent arguments through your cloud tool when you log in. Engineering teams often use this setup to reach private servers through a single public VM. They also use it to pull code from private Git repos onto a cloud machine. Still, broad permissions on shared VMs expose forwarded keys to anyone with root privileges.

How google compute engine ssh agent forwarding works

In a standard cloud network, most virtual machines do not have public IP addresses. Teams place one bastion host at the edge to accept incoming traffic from engineers. When an engineer needs to hop from that first VM to another, they need a key. Copying private keys onto the first cloud VM creates a major security risk. Anyone who breaches that machine can steal the key file from the disk.

Agent forwarding solves the disk storage problem by keeping the private key on the laptop. The engineer starts a local SSH agent and loads their private key into memory. When the cloud tool starts an SSH session, it passes arguments to OpenSSH underneath. OpenSSH creates a special Unix domain socket on the remote Google Compute Engine VM. This socket acts as a secure channel back to the engineer's local laptop.

When the engineer runs an SSH command on the first VM, it checks that socket. The request travels back through the encrypted tunnel to the local agent on the laptop. The local agent signs the challenge with the private key and sends the result back. The private key file is masked in responses before reaching the cloud disk during this whole process. Teams can also use OpenSSH certificates signed by a CA key to manage user identities. OpenSSH added this simpler certificate format for users and hosts in version 5.4.

What native agent forwarding solves and what it leaves open

Native agent forwarding stops engineers from scattering static private key files across cloud disks. It lets a user hop across multiple internal machines or clone code with one local key. Once the user closes their terminal, the remote Unix socket disappears from the server. This setup is much safer than leaving raw key files in home folders.

Still, agent forwarding leaves a dangerous operational gap open while the session is active. The remote Unix socket sits on the cloud VM file system during the session. Any user with root privileges on that VM can connect to that active socket. They cannot copy the key file, but they can ask the socket to sign requests. An attacker or rogue admin on a shared bastion can impersonate the engineer anywhere.

SSH Agent Forwarding Security + End-to-End Encryption: architecture patterns and best practices

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

Cloud IAM roles and OS Login permissions only decide who can open the initial session. They do not inspect or restrict what individual commands run after the login succeeds. Also, standard cloud logs show who logged into the VM, not what they ran inside. If someone hijacks an agent socket or runs a bad command, teams lack clear records. Also, if an engineer reads database rows containing emails or phone numbers, raw data prints out. Native SSH tools do not rewrite matched fields in results or require approval for risky commands.

Securing SSH workflows across cloud virtual machines

To reduce risk on shared VMs, teams should avoid forwarding keys whenever they can. Instead of hopping through a shared machine with a forwarded socket, route traffic directly. If engineers must pull code on a VM, use scoped deploy keys for that repo. Keep root permissions tight so other users cannot hijack active sockets on the host.

For stronger control, teams need to move past network tunnels and static key sockets. Modern operations require visibility into every command and control over sensitive data in flight.

Add command and file guardrails to SSH access

Host firewalls and certificate principals control who opens an SSH session. Yet an allowed user or agent can still run a bad one-shot command or pull sensitive files over sftp. In practice, hoop.dev sits inline on the host as an open-source SSH server with guardrails that uses OpenSSH certificates. For every one-shot command or sftp task an agent or human runs in production, hoop.dev decides to pass, mask, block, or escalate it for review.

Using hoop.dev SSH controls, guardrails check one-shot commands, env variable names, and sftp or scp paths before they run. Port forwards default to deny unless the target is listed. Because interactive shells have no statement boundary and carry no guardrails, teams can turn them off so every command is checked and logged. Check out the MIT-licensed project on GitHub.

Frequently asked questions

Why is SSH agent forwarding risky on shared Google Compute Engine VMs?

Agent forwarding creates a Unix socket on the remote VM while you are logged in. Anyone with root access on that machine can use your socket to log into other servers.

Does Google Compute Engine erase my forwarded key when I log out?

Your private key is masked in responses before reaching the remote VM disk at all during the session. When you close the SSH session, the temporary agent socket on the VM closes too.

How can I copy files between VMs without forwarding my SSH agent?

You can route traffic through a bastion from your workstation without exposing an agent socket. This lets your local machine log in directly to the target VM while keeping keys safe.

Save the open-source gateway.

hoop is MIT-licensed infrastructure for controlling how AI agents reach production data. Star hoophq/hoop so you can inspect it, deploy it, or share it when your team starts governing agent access.