This page explains how to enable [OS Login](https://docs.cloud.google.com/compute/docs/oslogin.md.txt) and
configure an organization policy to enforce OS Login for
GKE Standard mode clusters and nodes. You can use
OS Login to manage SSH access to your instances using IAM
without having to create and manage individual SSH keys.

OS Login isn't available for GKE
Autopilot mode clusters because GKE manages the nodes.

This page is for
Security specialists who
want to add OS Login policies on GKE Standard clusters
to ensure that all VM instances have OS Login by default.
To learn more about
common roles and example tasks that we reference in Google Cloud content, see
[Common GKE user roles and tasks](https://docs.cloud.google.com/kubernetes-engine/enterprise/docs/concepts/roles-tasks.md.txt).

Before reading this page, ensure that you're familiar with the [general overview of OS Login](https://docs.cloud.google.com/compute/docs/oslogin.md.txt).

## Overview

You can set up an [OS Login](https://docs.cloud.google.com/compute/docs/oslogin.md.txt) constraint in your
organization to ensure that all new projects, and the VM instances created in
these new projects, have OS Login enabled. OS Login has quickly become a
Google Cloud security best practice, recommending that you enforce its use
through an [organization policy](https://docs.cloud.google.com/compute/docs/oslogin/manage-oslogin-in-an-org.md.txt#set-org-policy).

The following instructions detail how to enable OS Login using an organization
policy in GKE.

## Before you begin


Before you start, make sure that you have performed the following tasks:

- Enable the Google Kubernetes Engine API.
[Enable Google Kubernetes Engine API](https://console.cloud.google.com/apis/enableflow?apiid=container.googleapis.com)
- To use the Google Cloud CLI for this task, [install](https://docs.cloud.google.com/sdk/docs/install) and then [initialize](https://docs.cloud.google.com/sdk/docs/initialize.md.txt) the gcloud CLI. If you previously installed the gcloud CLI, get the latest version by running the `gcloud components update` command. Earlier gcloud CLI versions might not support running the commands in this document.

  > [!NOTE]
  > **Note:** For existing gcloud CLI installations, make sure to set the `compute/region` [property](https://docs.cloud.google.com/sdk/docs/properties.md.txt#setting_properties). If you use primarily zonal clusters, set the `compute/zone` instead. By setting a default location, you can avoid errors in the gcloud CLI like the following: `One of [--zone, --region] must be supplied: Please specify location`. You might need to specify the location in certain commands if the location of your cluster differs from the default that you set.

## Update existing projects to use OS Login

Before setting the organization policy, migrate any existing clusters to use OS
Login.

- Enable OS Login on all existing and new VM instances by default by setting
  the `enable-oslogin` flag to `TRUE`. You don't need to reboot the node.

  ```bash
  gcloud compute project-info add-metadata --metadata enable-oslogin=TRUE
  ```

  > [!CAUTION]
  > **Caution:** Enabling OS Login on instances disables metadata-based SSH key configurations on those instances. Disabling OS Login restores SSH keys that you have [configured in project or instance metadata](https://docs.cloud.google.com/compute/docs/connect/add-ssh-keys.md.txt#metadata).

## Set the OS Login organization policy

To set the OS Login constraint at the organization level, perform the following:

1. Find your organization ID by running the following command:

   ```bash
   gcloud organizations list
   ```
2. Set the OS Login organization policy. Replace `ORGANIZATION_ID`
   with your organization ID.

   ```bash
   gcloud resource-manager org-policies enable-enforce \
       compute.requireOsLogin \
       --organization=ORGANIZATION_ID
   ```

After the organization policy is set, the following conditions are applied:

- `enable-oslogin` is set to `true` in the project metadata for all new projects.
- Update requests to set `enable-oslogin` to `false` in instance or project metadata are rejected.

## Manage node access

Once you have enabled the OS Login organization policy, you no longer need to
manage SSH keys to make authorization decisions. OS Login moves authorization
management to Identity and Access Management. To manage SSH access to nodes, use OS Login. For
more details, see [Setting up OS Login](https://docs.cloud.google.com/compute/docs/instances/managing-instance-access).

## What's next

- Learn about the [OS Login service](https://docs.cloud.google.com/compute/docs/oslogin.md.txt).
- Learn to [troubleshoot OS Login](https://docs.cloud.google.com/compute/docs/oslogin/troubleshoot-os-login).