If you use OS Login to manage access to your instances, you can add an extra layer of security by using two-factor authentication, also known as 2FA.
OS Login supports the following 2FA methods or challenge types:
- Google Authenticator
- Text message or phone call verification
- Phone prompts
To use 2FA authentication on your instances, complete the following steps:
- Enable 2FA for your Google account or domain
- Enable 2FA on your project or instance
- Connect to your instances
After setting up OS Login 2FA, you can use audit logs to monitor your authentication sessions.
Supported operating systems
OS Login two-factor authentication require operating system images created after the following dates:
| Operating System | Dates |
|---|---|
| CentOS 6 and 7 | March 26, 2019 |
| Debian 9 | March 26, 2019 |
| RHEL 6 and 7 | March 26, 2019 |
| SUSE 12 and 15 | June 17, 2019 |
| Ubuntu 16.04 LTS, 18.04 LTS, 18.10, and 19.04 | June 28, 2019 |
Enabling 2FA for your Google account or domain
Before you can enable two-factor authentication for your project or instance, you must first enable 2FA on your Google account or domain. Ensure that you enable 2FA on the domain that contains the project or instances, or for the user that owns the project or instances.
A G Suite administrator can enable two-factor authentication for a domain or an individual Google user can enable two-factor authentication at the user account level.
Domain
Two-factor authentication for a domain must be enabled by a G Suite administrator.
To enable 2FA for a domain, see Protect your business with 2-Step Verification in the G Suite Admin guide.
User account
If your user accounts are not managed by a G Suite administrator, you can configure 2FA for individual Google accounts.
To configure 2FA for an individual Google account, see Google 2-Step Verification.
Enabling 2FA on your project or instance
After you enable two-factor authentication at the domain or user account level, you can then enable individual instances or project to use OS Login 2FA.
An instance or project must have OS Login enabled in order to use OS Login 2FA.
You can configure both OS Login and OS Login 2FA during instance creation or project set up. You can also configure OS Login 2FA on an existing instance or project that already has OS Login enabled.
To configure your project or instance to use OS Login two-factor authentication,
set "enable-oslogin-2fa=TRUE" in the project or instance metadata.
Console
Set enable-oslogin-2fa in instance metadata when you create an instance:
- In the GCP Console, go to the VM Instances page.
- Click Create instance.
- On the Create a new instance page, fill in the desired properties for your instance.
In the Metadata section, add the following metadata entries:
enable-osloginand the value isTRUE.enable-oslogin-2faand the value isTRUE.
Click Create to create the instance.
Set enable-oslogin-2fa in project-wide metadata so that it applies to all of the instances in your project:
- Go to the Metadata page.
- Click Edit.
- Add a metadata entry where the key is
enable-oslogin-2faand the value isTRUE. Alternatively, set the value toFALSEto disable the feature. - 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.
Set enable-oslogin-2fa in metadata of an existing instance:
- Go to the VM instances page.
- Click the name of the instance on which you want to set the metadata value.
- At the top of the instance details page, click Edit to edit the instance settings.
- Under Custom metadata, add a metadata entry where the key is
enable-oslogin-2faand the value isTRUE. Alternatively, set the value toFALSEto exclude the instance from the feature. You should also verify thatenable-osloginis set toTrue. - 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.
gcloud
Set enable-oslogin-2fa in instance metadata when you create an instance:
gcloud compute instances create [INSTANCE_NAME] \
--metadata enable-oslogin=True,enable-oslogin-2fa=True
Set enable-oslogin-2fa in project-wide metadata so that it applies to all of the instances in your project:
gcloud compute project-info add-metadata \
--metadata enable-oslogin=True,enable-oslogin-2fa=True
Set enable-oslogin-2fa in metadata of an existing instance:
gcloud compute instances add-metadata \
--metadata enable-oslogin=True,enable-oslogin-2fa=True [INSTANCE_NAME]
where [INSTANCE_NAME] is the name of the instance.
Connecting to instances
After you configure the necessary roles, connect to an instance using Compute Engine tools. Compute Engine automatically generates SSH keys and associates them with your user account. Alternatively, if you create your own SSH keys and add the public keys to your user account, you can connect to instances using third-party tools. The instance obtains your public key from your user account and allows you to connect to the instance if you provide the correct user name and matching private SSH key.
When you connect to your instance, you will get a message based on your selected 2FA method or challenge type.
For Google authenticator, you will see the following message:
"Enter your one-time password:"
For text message or phone call verification, you will see the following message:
"A security code has been sent to your phone. Enter code to continue:"
For phone prompt, you will see the following message:
A login prompt has been sent to your enrolled device:"
For the phone prompt method, accept the prompts on your phone or tablet to continue. For other methods, enter your security code or one-time password.
After you connect to your instance, review the expected login behaviors.
Viewing OS Login 2FA audit logs
Compute Engine provides audit logs to track two-factor authentication requests. Two-factor authentication has two request types:
StartSession- Starts a new authentication session. In aStartSessioncall, a client declares its capabilities to the server and obtains information about the first challenge. AStartSessioncall returns the following:- A session ID - this session ID is passed to all subsequent
ContinueSessioncalls. - Information about the challenge or 2FA method used in this new authentication session.
- A session ID - this session ID is passed to all subsequent
ContinueSession- Continues an existing authentication session. By using the provided session ID, theContinueSessionAPI can perform one of the following two actions:- Accept the response to a challenge or method and then either authenticate, reject, or require additional challenges from the user.
- Switch to a different type of challenge than the one initially
proposed by the server on the previous round of API calls. If a client
chooses to complete a different challenge type
(for example, Google authenticator instead of phone prompt), the client
can request a different challenge type in a call to the server by using a
request.challengeIdof the desired type.
To view logs, you must have permissions for the Logs Viewer or be a project viewer or editor.
- Go to the Logs page in the GCP Console.
- Expand the drop-down menu and select
Audited Resource. - In the search bar, type
oslogin.googleapis.comand hit Enter. A list of audit logs describing the two-factor authentication requests displays. Expand any of the entries to get more information:
For any of the audit logs, you can:
Expand the
protoPayloadproperty.
Look for
methodNameto see activity this log applies to (either aStartSessionorContinueSessionrequest). For example, if this log tracks aStartSessionrequest, the method name would say"google.cloud.oslogin.OsLoginService.v1.StartSession". Similarly, aContinueSessionlog would say"google.cloud.oslogin.OsLoginService.v1.ContinueSession". An audit log entry is recorded for every start and continue session requests.
There are different audit log properties for different log types. For example,
audit logs relating to StartSession have properties that are
specific to starting sessions, while audit logs for ContinueSession have
their own set of properties. There are certain audit log properties that
are also shared between both log types.
All two-factor authentication audit logs
| Property | Value |
|---|---|
serviceName |
oslogin.googleapis.com |
resourceName |
A string containing the project number. This project number indicates which
login request the audit log belongs to.
For example, projects/myproject12345.
|
severity |
The severity level of the log message. For example, INFO
or WARNING. |
request.email |
The email address of the user that the API call is authenticating. |
request.numericProjectId |
The project number of the Google Cloud Platform project. |
response.@type |
type.googleapis.com/google.cloud.oslogin.OsLoginService.v1.StartOrContinueSessionResponse
|
response.sessionId |
An ID string uniquely identifying the session. This session ID is passed to the next API call in the sequence. |
response.authenticationStatus |
Status of the session. For example, Authenticated,
Challenge required or Challenge pending. |
response.challenges |
The set of challenges that you can attempt to pass this round of
authentication. At most one of these challenges is started and has a status of
READY. The others are provided as options that the user can
specify as an alternative to the proposed primary challenge. |
StartSession audit logs
| Property | Value |
|---|---|
methodName |
google.cloud.oslogin.OsLoginService.v1.StartSession |
request.@type |
type.googleapis.com/google.cloud.oslogin.OsLoginService.v1.StartSessionRequest
|
request.supportedChallengeTypes |
The list of challenge types or 2FA methods that you can choose from. |
ContinueSession audit logs
| Property | Value |
|---|---|
methodName |
google.cloud.oslogin.OsLoginService.v1.ContinueSession |
request.sessionId |
An ID string uniquely identifying the previous session. This session ID is passed from the previous API call in the sequence. |
request.@type |
type.googleapis.com/google.cloud.oslogin.OsLoginService.v1.ContinueSessionRequest
|
request.challengeId |
An ID string identifying which challenge to start or execute. This ID must
belong to a challenge type returned from the response.challenges
call in a previous API response. |
request.action |
The action to take. |


