SSH keys for Compute Engine

5 minutes 5 Questions

SSH keys are essential for securely connecting to Google Compute Engine virtual machine instances. They provide cryptographic authentication, allowing users to access Linux-based VMs through the Secure Shell protocol. Google Cloud offers several methods for managing SSH keys: 1. **OS Login**: The recommended approach that uses IAM roles to manage SSH access. It links Linux user accounts to Google identities and provides centralized access management across your organization. 2. **Project-level metadata**: SSH public keys stored in project metadata are propagated to all VMs in that project. This method is suitable when you need consistent access across multiple instances. 3. **Instance-level metadata**: SSH keys can be added to individual VM instances, providing granular control over who can access specific machines. 4. **Temporary SSH keys**: When using the Cloud Console or gcloud command-line tool, Google can generate temporary SSH key pairs that expire after a short period. To add SSH keys, you can use the gcloud CLI, Cloud Console, or the Compute Engine API. The format for metadata-based keys follows: USERNAME:SSH_PUBLIC_KEY. Best practices include: - Using OS Login for enterprise environments as it integrates with IAM and supports two-factor authentication - Regularly rotating SSH keys to maintain security - Removing keys for users who no longer require access - Using instance-level keys when project-wide access is too permissive - Blocking project-wide SSH keys on sensitive instances When troubleshooting SSH connectivity issues, verify that firewall rules allow TCP port 22, the public key exists in metadata, and the private key matches the stored public key. SSH keys work alongside other security measures like VPC firewall rules and IAM permissions to create a comprehensive security posture for your Compute Engine resources.

SSH Keys for Compute Engine - Complete Guide

Why SSH Keys for Compute Engine Matter

SSH keys are fundamental to secure access management in Google Cloud Platform. They provide a cryptographic method for authenticating to Compute Engine virtual machine instances, eliminating the need for passwords. Understanding SSH key management is essential for the GCP Associate Cloud Engineer exam because it directly impacts security, access control, and operational efficiency in cloud environments.

What Are SSH Keys in Compute Engine?

SSH keys are pairs of cryptographic keys consisting of a public key and a private key. The public key is stored on the VM instance, while the private key remains with the user. When a connection is attempted, the keys are used to verify identity through asymmetric encryption.

GCP supports several methods for managing SSH keys:

• Project-level SSH keys - Apply to all VMs in a project unless blocked
• Instance-level SSH keys - Apply only to specific VM instances
• OS Login - Links SSH access to IAM permissions and Google identities
• Temporary SSH keys - Generated by Cloud Console or gcloud for one-time access

How SSH Key Management Works

Project-Level Keys:
Keys added at the project level in Compute Engine metadata are propagated to all VM instances. Any user with a matching private key can access VMs where their public key is deployed. This approach is convenient but offers less granular control.

Instance-Level Keys:
Keys can be added to individual instance metadata, providing more targeted access. You can also block project-level keys on specific instances by setting the block-project-ssh-keys metadata value to TRUE.

OS Login:
This is the recommended approach for organizations requiring centralized access management. OS Login uses IAM roles to manage SSH access:
• roles/compute.osLogin - Grants standard user access
• roles/compute.osAdminLogin - Grants sudo/administrator access

To enable OS Login, set the metadata key enable-oslogin to TRUE at the project or instance level.

Key Commands and Operations

Adding SSH keys via gcloud:
gcloud compute project-info add-metadata --metadata-from-file ssh-keys=KEY_FILE

Connecting via gcloud:
gcloud compute ssh INSTANCE_NAME --zone=ZONE

Enabling OS Login:
gcloud compute project-info add-metadata --metadata enable-oslogin=TRUE

Security Best Practices

• Use OS Login for enterprise environments with many users
• Block project-level SSH keys on sensitive instances
• Regularly rotate and audit SSH keys
• Use service accounts with appropriate scopes for automated access
• Enable two-factor authentication with OS Login when possible

Exam Tips: Answering Questions on SSH Keys for Compute Engine

1. Know the hierarchy: Remember that instance-level metadata takes precedence over project-level metadata for SSH keys.

2. OS Login scenarios: When questions mention centralized identity management, IAM integration, or LDAP/Active Directory environments, OS Login is typically the correct answer.

3. Blocking project keys: If a question asks about restricting access to a specific VM while maintaining project-wide keys for others, look for answers involving block-project-ssh-keys metadata.

4. IAM roles for OS Login: Memorize the two key roles - compute.osLogin for regular access and compute.osAdminLogin for administrative privileges.

5. Metadata locations: Questions may test whether you understand that SSH keys are stored in instance or project metadata, not in IAM policies themselves.

6. Temporary access: When the Cloud Console or gcloud SSH commands are used, temporary keys are created and managed automatically by GCP.

7. Service accounts: For VM-to-VM communication or automated scripts, questions often involve service accounts rather than user SSH keys.

8. Troubleshooting scenarios: If a user cannot SSH to an instance, consider whether OS Login is enabled, whether the correct IAM roles are assigned, or whether project-level keys are blocked.
Test mode:
More SSH keys for Compute Engine questions
29 questions (total)

Other Planning and Implementing a Cloud Solution concepts

AlloyDBAvailability policy for Compute EngineBigQueryBigtableChoosing and deploying data productsChoosing and deploying load balancersChoosing and deploying storage productsChoosing storage for Compute EngineCloud InterconnectCloud Next Generation Firewall (Cloud NGFW)Cloud RunCloud Run functionsCloud SQLCloud StorageCloud Storage classes (Standard, Nearline, Coldline, Archive)Cloud Storage object change notificationsCloud VPNCommand-line data uploadCompute EngineConfig ConnectorConfiguring OS LoginConfiguring VM ManagerCreating autoscaled managed instance groupsCreating a VPC with subnetsCustom machine typesCustom mode VPCDataflowDeploying containerized applications to GKEDeploying GKE clustersDeploying to serverless compute platformsEstablishing network connectivityEventarcFabric FASTFilestoreFirestoreFirewall ingress and egress rulesGKE AutopilotGoogle Cloud HyperdiskGoogle Cloud Managed Service for Apache KafkaGoogle Cloud NetApp VolumesGoogle Kubernetes Engine (GKE)Helm for KubernetesIaC versioning and state managementInfrastructure as code toolingInstalling kubectl CLIInstance templatesKnative servingLaunching a compute instanceLoading data into Google CloudMaintaining multi-region redundancyMemorystoreNetwork Service TiersPlanning and executing IaC deploymentsPrivate GKE clustersProcessing Pub/Sub eventsPub/SubRegional GKE clustersRegional Persistent DiskSecure TagsSelecting compute choices for workloadsService accounts in firewall rulesShared VPCSpannerSpot VM instancesStorage Transfer ServiceTerraform on Google CloudUsing Tags in Cloud NGFW policy rulesVPC Network PeeringZonal Persistent Disk