This document describes how to prevent users from accessing virtual machine (VM) instances by removing and blocking SSH keys from VMs.
Select the tab for how you plan to use the samples on this page:
When you use the Google Cloud console to access Google Cloud services and APIs, you don't need to set up authentication.
Install the Google Cloud CLI. After installation, initialize the Google Cloud CLI by running the following command:
gcloud initIf you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
To use the REST API samples on this page in a local development environment, you use the credentials you provide to the gcloud CLI.
Install the Google Cloud CLI.
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
For more information, see Authenticate for using REST in the Google Cloud authentication documentation.
VMs that use OS Login accept SSH keys that are associated with your Google account. You can remove a public SSH key from your user account using the Google Cloud CLI or the OS Login API. If you're an administrator for your organization, you can remove SSH keys from user accounts using the Directory API. Compute Engine automatically removes expired keys from your Google Account.
To remove a public SSH key from your account, do the following:
If you don't know which key you want to remove, run the
gcloud compute os-login describe-profile command
to view all keys associated with your account:
gcloud compute os-login describe-profile
Copy the fingerprint value of the key you want to delete.
Remove the key from your account using the
gcloud compute os-login ssh-keys remove command:
gcloud compute os-login ssh-keys remove --key=KEY
Replace KEY with the public SSH key you want to
remove, or the OS Login fingerprint for the key you want to remove.
To remove a public SSH key from your account, do the following:
If you don't know which key you want to remove, use the
users.getLoginProfile method
to view all keys associated with your account:
GET https://oslogin.googleapis.com/v1/users/ACCOUNT_EMAIL/loginProfile
Replace ACCOUNT_EMAIL with the email address
associated with your account.
Copy the fingerprint value of the key you want to delete.
Remove the key from your account using the
users.sshPublicKeys.delete method:
DELETE https://oslogin.googleapis.com/v1/users/ACCOUNT_EMAIL/sshPublicKeys/FINGERPRINT
Replace the following:
ACCOUNT_EMAIL: the email address associated with
your accountFINGERPRINT: the SHA-256 fingerprint of the key to
removeYou can remove a public SSH key from project or instance metadata using the Google Cloud console, the gcloud CLI, or the Compute Engine API.
After you remove the last key from metadata for a particular user, or the last
key in metadata for a particular user expires, Compute Engine deletes the
user's ~/.ssh/authorized_keys file on the VM.
Remove a public SSH key from project metadata to remove access to all VMs in a project.
When you remove a key from metadata using the gcloud CLI and the Compute Engine API, you must retrieve the list of existing keys, edit the list of keys to remove the unwanted keys, and overwrite the old keys with the list of keys you want to keep, as explained in the following section.
To perform this task, you must have the following permissions:
compute.projects.setCommonInstanceMetadataTo remove a public SSH key from project metadata using the Google Cloud console, do the following:
In the Google Cloud console, go to the Metadata page.
Click the SSH keys tab.
Click Edit at the top of the page.
Navigate to the SSH key that you want to remove and click the delete button next to the SSH key.
Repeat this step for each SSH key that you want to remove.
Click Save.
To remove a public SSH key from project metadata using the gcloud CLI, do the following:
Run gcloud compute project-info describe command to get the metadata for the project:
gcloud compute project-info describe
The output is similar to the following:
...
metadata:
...
- key: ssh-keys
value: |-
cloudysanfrancisco:ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDAu5kKQCPF
baklavainthebalkans:ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQDQDx3FNVC8 google-ssh {"userName":"baklavainthebalkans","expireOn":"2021-06-14T16:59:03+0000"}
...
Copy the ssh-keys metadata value.
Create and open a new text file on your workstation.
In the file, paste the list of SSH keys that you just copied, then delete any keys you want to remove from project metadata.
Save and close the file.
Run the
gcloud compute project-info add-metadata command
to set the project-wide ssh-keys value:
gcloud compute project-info add-metadata --metadata-from-file=ssh-keys=KEY_FILE
Replace KEY_FILE with one of the following:
To remove a public SSH key from project metadata using the Compute Engine API, do the following:
Use the projects.get
method to get the fingerprint and ssh-keys values from metadata.
GET https://compute.googleapis.com/compute/v1/projects/PROJECT_ID
Replace PROJECT_ID with your project ID.
The response is similar to the following:
...
"fingerprint": "utgYE_XWtE8=",
"items": [
{
"key": "ssh-keys",
"value": "cloudysanfrancisco:ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDAu5kKQCPF\nbaklavainthebalkans:ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQDQDx3FNVC8 google-ssh {"userName":"baklavainthebalkans","expireOn":"2021-06-14T16:59:03+0000"}"
}
]
...
Copy the list of SSH key values and delete the keys you want to remove.
Use the
projects.setCommonInstanceMetadata
to remove the SSH keys.
POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/setCommonInstanceMetadata
{
"items": [
{
"key": "ssh-keys",
"value": "EXISTING_SSH_KEYS"
}
]
"fingerprint": "FINGERPRINT"
}
Replace the following:
PROJECT_ID: your project IDEXISTING_SSH_KEYS: the list of the SSH keys
you want to keepFINGERPRINT: the value of the fingerprint
from the response of the projects.get requestRemove a public SSH key from instance metadata to remove access to a single VM.
When you remove a key from metadata using the gcloud CLI and the Compute Engine API, you must retrieve the list of existing keys, edit the list of keys to remove the unwanted keys, and overwrite the old keys with the list of keys you want to keep, as explained in the following section.