Setting up OS Login

This topic covers the basic steps for setting up OS Login.

OS Login lets you use Compute Engine IAM roles to grant or revoke SSH access to your Linux instances. OS Login is an alternative to managing instance access by adding and removing SSH keys in metadata. To learn more about the benefits of using this feature, see OS Login.

If you want to enable OS Login with a layer of security by using two-factor authentication, see Setting up OS Login with 2-step verification. To review all the options for managing access to your VMs, see Choosing an access method.

To configure OS Login and connect to your instances, complete the following steps:

  1. Install or update the guest environment.
  2. (Optional) If you are an organization administrator, review Managing OS Login in an organization.
  3. Enable the OS Login feature on your project or on individual instances.
  4. Grant the necessary IAM roles to yourself, your project members, or your organization members.
  5. (Optional) Add custom SSH keys to user accounts for yourself, your project member, or organization members. Alternatively, Compute Engine can automatically generate these keys for you when you connect to instances.
  6. Connect to instances.

Before you begin

Limitations

  • OS Login is not currently supported in Google Kubernetes Engine (GKE). GKE cluster nodes continue to use metadata SSH keys when OS Login is enabled.

  • Currently, Fedora CoreOS images do not support OS Login. To manage instance access to VMs created using these images, use the Fedora CoreOS ignition system.

  • Windows Server and SQL Server images do not support OS Login.

Step 1: Install or update the guest environment

Your instance must have the latest version of the guest environment installed. Most public images already have the latest version installed.

If you have instances that run custom images that you imported, install the guest environment on those VMs.

If you don't have the latest guest environment, update your guest environment.

Step 2: (Optional) Review managing OS Login in an organization

If you are organization admin, you can set some configurations such as enabling OS Login at the organization level. See Managing OS Login in an organization.

Step 3: Enabling or disabling OS Login

You can enable or disable OS Login by setting metadata values at the instance or project level. To set these values, you can use either the Google Cloud Console or the gcloud command-line tool.

Console

You can apply the metadata values on your projects or VMs using one of the following options:

  • Option 1: Set enable-oslogin in project-wide metadata so that it applies to all of the instances in your project.

    1. In the Google Cloud Console, go to the Metadata page.

      Go to the Metadata page

    2. Click Edit.
    3. Add a metadata entry where the key is enable-oslogin and the value is TRUE. Alternatively, set the value to FALSE to disable the feature.
    4. Click Save to apply the changes.

    For VMs that are not running CoreOS, this change is applied instantaneously; you do not need to restart your instance. For CoreOS distributions, reboot or restart the instance for the change to take effect. To restart, perform a stop and then start operation on your instances.

  • Option 2: Set enable-oslogin in the metadata of an existing instance.

    1. In the Google Cloud Console, go to the VM instances page.

      Go to the VM instances page

    2. Click the name of the instance on which you want to set the metadata value.
    3. At the top of the instance details page, click Edit to edit the instance settings.
    4. Under Custom metadata, add a metadata entry where the key is enable-oslogin and the value is TRUE. Alternatively, set the value to FALSE to exclude the instance from the feature.
    5. At the bottom of the instance details page, click Save to apply your changes to the instance.

    For all operating systems except CoreOS, this change is applied instantaneously; you do not need to restart your instance. For CoreOS distributions, reboot or restart the instance for the change to take effect. To restart, perform a stop and then start operation on your instances.

  • Option 3: Set enable-oslogin in instance metadata when you create an instance.

    1. In the Cloud Console, go to the VM Instances page.

      Go to the VM Instances page

    2. Click Create instance.
    3. On the Create a new instance page, fill in the properties for your instance.
    4. In the Metadata section, add a metadata entry where the key is enable-oslogin and the value is TRUE. Alternatively, set the value to FALSE to exclude the instance from the feature.
    5. Click Create to create the instance.

gcloud

You can apply the metadata values on your projects or VMs using one of the following options:

  • Option 1: Set enable-oslogin in project-wide metadata so that it applies to all of the instances in your project.

    Use the project-info add-metadata command in the gcloud command-line tool and set oslogin=TRUE to enable OS Login:

    gcloud compute project-info add-metadata \
        --metadata enable-oslogin=TRUE
    

    Alternatively, you can set enable-oslogin to FALSE to disable OS Login.

    For VMs that are not running CoreOS, this change is applied instantaneously; you do not need to restart your instance. For CoreOS distributions, reboot or restart the instance for the change to take effect.

  • Option 2: Set enable-oslogin in metadata of an existing instance.

    Use the instances add-metadata command in the gcloud command-line tool and set oslogin=TRUE to enable OS Login. Replace instance-name with the name of your instance.

    gcloud compute instances add-metadata instance-name \
        --metadata enable-oslogin=TRUE
    

    Alternatively, you can set enable-oslogin to FALSE to exclude your instance from using OS Login.

    For all operating systems except CoreOS, this change is applied instantaneously; you do not need to restart your instance. For CoreOS distributions, reboot or restart the instance for the change to take effect.

  • Option 3: Set enable-oslogin in instance metadata when you create an instance.

    Use the instances create command in the gcloud command-line tool and set oslogin=TRUE to enable OS Login. Replace instance-name with the name of your instance.

    gcloud compute instances create instance-name \
        --metadata enable-oslogin=TRUE
    

    Alternatively, you can set enable-oslogin to FALSE to exclude your instance from using OS Login.

After you enable OS Login on the instances in your project, grant users permission to connect to those instances.

Step 4: Configuring OS Login roles on user accounts

Granting OS Login IAM roles

After you enable OS Login on one or more instances in your project, those VMs accept connections only from user accounts that have the necessary IAM roles in your project or organization.

To allow OS Login access to these VMs, you need to grant the necessary roles to the user. To allow OS Login access, complete the following steps:

  1. Grant one of the following instance access roles.

    You can grant these instance access roles at the instance level by using the gcloud compute instances add-iam-policy-binding command.

  2. If your VM instance uses a service account, then each user must be configured to have the roles/iam.serviceAccountUser role on the service account. To learn how to add access for a user to a service account, see Managing service account impersonation.

  3. For users that are outside of your organization to access your VMs, in addition to granting an instance access role, grant the roles/compute.osLoginExternalUser role. This role must be granted at the organization level by an organization administrator. For more information, see Granting instance access to users outside of your organization.

Granting SSH access to a service account

You can use OS Login roles to allow service accounts to establish SSH connections to your instances. This is useful for the following tasks:

You can grant SSH access to your service accounts by using the following process:

  1. Create a service account.
  2. Grant the necessary OS Login roles to your service account. Service accounts require the same roles as user accounts. To learn how to configure roles and permissions for service accounts, see Granting roles to service accounts
  3. Provide Application Default Credentials to your service account so that it can authorize requests to the necessary APIs. Provide Application Default Credentials using one of the following options:

After you grant SSH access to your service accounts, you can configure your apps to create SSH keys and establish SSH connections to other instances on your VPC networks. Read the Connecting applications to instances using SSH tutorial to see an example app for service account SSH.

Revoking OS Login IAM roles

To revoke user access to instances that are enabled to use OS Login, remove the user roles from that user account. For information about removing an IAM role for a user, see Granting, changing, and revoking access to resources.

When a user's access is revoked, the user will still have public SSH keys that are associated with their account, but those keys no longer function on the VM instances.

Step 5: (Optional) Adding SSH keys to a user account

If you want connect to your VMs by using third party tools, you need to add your SSH keys to your user account. If you connect to your instances using other options such as gcloud command-line tool or SSH from the browser, you can skip this step as Compute Engine automatically generate these keys for you.

You can associate public SSH keys with the following user account types:

You can use the gcloud command-line tool, or the OS Login API to add SSH keys to your own account. Alternatively, if you are a domain admin for an organization, you can use the Directory API, to add SSH keys to the user account in your organization.

gcloud

The gcloud compute os-login commands are available only on Cloud SDK version 184 and later.

Use the gcloud command-line tool to associate public SSH keys with an account.

gcloud compute os-login ssh-keys add \
    --key-file key-file-path \
    --ttl expire-time

Replace the following:

  • key-file-path: The path to the public SSH key on your local workstation. Ensure that the public SSH key is properly formatted. If you use PuTTYgen on Linux systems to generate your public keys, you must use the public-openssh format.
  • expire-time: An optional flag to set an expiration time for the public SSH key. For example, you can specify 30m and the SSH key will expire after 30 minutes. This flag uses the following units:
    • s for seconds
    • m for minutes
    • h for hours
    • d for days Set the value to 0 to indicate no expiration time.

OS Login API

Use the OS Login API to associate public SSH keys with an account:

POST https://oslogin.googleapis.com/v1/users/account-email:importSshPublicKey

{
 "key": "ssh-key",
 "expirationTimeUsec": "expiration-timestamp"
}

Replace the following:

  • account-email: The email address that represents your managed user account.
  • ssh-key: The public key that you want to apply to the account. Ensure that the public SSH key is properly formatted. If you use PuTTYgen on Linux systems to generate your public keys, you must use the public-openssh format.
  • expiration-timestamp: The expiration time for the key, in microseconds since epoch.

Directory API

If you are a domain admin for an organization, you can use the Directory API reference to add SSH keys to the account of another user in your organization. For example, create a PUT request to the directory.users.update method with one or more SSH sshPublicKeys entries:

PUT https://www.googleapis.com/admin/directory/v1/users/user-id-key

{
 "sshPublicKeys": [
  {
   "key": "ssh-key",
   "expirationTimeUsec": "expiration-timestamp"
  },
  {
   "key": "ssh-key",
   "expirationTimeUsec": "expiration-timestamp"
  }
 ]
}

Replace the following:

  • user-id-key: An immutable ID for the user.
  • ssh-key: A public key that you want to apply to the account. Ensure that the public SSH key is properly formatted. If you use PuTTYgen on Linux systems to generate your public keys, you must use the public-openssh format.
  • expiration-timestamp: The expiration time for a key, in microseconds since epoch.

To remove all keys from an account, specify "sshPublicKeys": null as the body, replacing user-id-key with an immutable ID for the user:

PUT https://www.googleapis.com/admin/directory/v1/users/user-id-key

{
  "sshPublicKeys": null
}

After you add your keys to your account, you can connect to instances using third-party tools and the username associated with your account. Your organization admin can change this username.

You can find the current username for your account by running the gcloud compute os-login describe-profile command:

For example, your output might resemble the following:

name: '314159265358979323846'
posixAccounts:
- gid: '27182818'
  homeDirectory: /home/user_example_com
  ⋮
  uid: '27182818'
  username: user_example_com
⋮

Step 6: Connect to instances

When you connect to a VM, you have 3 main options:

If you connect to a VM by using either gcloud command-line tool or SSH from the browser, Compute Engine automatically generates SSH keys and associates them with your user account.

If you connect to an instance by using a third-party tool, you need to add the public keys to your user account. The VM obtains your public key from your user account and lets you connect to the instance if you provide the correct user name and matching private SSH key.

After you connect to your instance, review the expected login behaviors.

Review expected login behaviors

  • On some instances using OS Login, you might receive the following error message after the connection is established:

    /usr/bin/id: cannot find name for group ID 123456789

    Ignore this error message. This error does not affect your instances.

  • Cloud Identity administrators can configure POSIX information and set a username for organization members. If a username is not set by a Cloud Identity administrator, OS Login generates a default Linux username by combining the username and domain from the email associated with the user's Google profile. This naming convention ensures uniqueness. For example, if the user email associated with the Google profile is user@example.com, then their generated username is user_example_com.

    G Suite organizations can optionally change their default to remove the domain suffix for newly generated usernames. For example, if the user email associated with the Google profile is user@example.com, then their generated username is user. For more information, see Managing the OS Login API.

    If a user is from a separate G Suite organization, the generated username is prefixed with 'ext_'. For example, if user@example.com is accessing a VM in a different organization, then their generated username is ext_user_example_com.

  • When you log in to an instance by using the gcloud compute ssh command, the login message has the following format for a user user that belongs to the example.com domain:

    Using OS Login user user_example_com instead of default user user

    This message confirms that that the user is logging in with an OS Login profile.

What's next