Article Details

Google Cloud Account Resetting Root Password on Google Cloud Linux VM

GCP Account2026-05-16 16:59:35OrbitCloud

Introduction

So, you're staring at your Google Cloud Linux VM and realize you've lost access. Maybe you forgot your SSH key, or someone changed the password without telling you. Maybe you tried to enable root login and now it's locked up. Either way, this situation is frustrating but not irrecoverable. Unlike traditional servers where you might physically access the machine, Google Cloud provides powerful tools to reset credentials remotely. This guide walks you through two solid methods to regain control of your VM—whether you're a cloud novice or a seasoned sysadmin. Let's dive in!

Understanding Google Cloud's Default Security Setup

Before jumping into the reset process, it's crucial to understand why you're locked out in the first place. Google Cloud's default setup prioritizes security. By default, root login is disabled via SSH, and user accounts rely on SSH key-based authentication rather than passwords. This is a great security practice—it prevents brute-force attacks and password guessing. But it also means if you lose your SSH keys or accidentally disable access, you're locked out. Many people assume they can reset a root password like on a regular server, but that's not how GCP works out of the box. Let's break down what's happening behind the scenes.

Why Default Settings Cause Lockouts

When you create a new VM instance on Google Cloud, the platform automatically configures it to use SSH keys for authentication. The root account is intentionally disabled—you can't log in as root directly. Instead, you're given a standard user account (like "username" or "ubuntu" depending on the OS) that has sudo privileges. If you forget your SSH key or accidentally delete it from the instance's metadata, you lose access. But here's the kicker: Google Cloud doesn't store passwords for user accounts by default. So there's no "root password" to reset because it wasn't set up that way. This is where confusion often arises. People expect to reset a password like on a local Linux machine, but GCP's infrastructure handles authentication differently. The key takeaway here is that "resetting root password" usually isn't necessary; instead, you need to regain access through SSH keys or other methods.

Google Cloud Account SSH Keys vs. Password Authentication

SSH keys are a more secure way to authenticate than passwords. They're unique cryptographic tokens that are harder to crack than even the strongest passwords. In Google Cloud, when you create a VM, the platform generates a pair of SSH keys and adds the public key to the instance's metadata. You keep the private key securely on your local machine. Whenever you connect, your SSH client uses this private key to authenticate. This setup is great for security, but it's also why losing your private key is such a problem. Unlike password-based systems where you can reset the password via the console, you can't simply "reset" an SSH key. If you lose the key, you have to either recover it or reset the entire authentication mechanism. For most users, the solution involves using the serial console to reset the SSH keys or enabling password authentication temporarily. But for root specifically, since it's disabled by default, resetting a root password isn't typically the right approach. Instead, you might need to reset the password for a standard user or re-add SSH keys.

Method 1: Using the Serial Console

The serial console method is the most straightforward way to regain access to your VM if you're locked out. This approach lets you interact with your VM at a low level, bypassing SSH entirely. Let's walk through the steps.

Step 1: Preparing the VM

First, make sure your VM is stopped. While you can sometimes access the serial console while the VM is running, stopping it ensures you have full control during the boot process. Go to the Google Cloud Console, navigate to "Compute Engine" > "VM instances", and stop the instance. Note: If your VM has automatic restarts enabled, make sure to disable them before stopping to prevent it from restarting automatically. Next, check the serial console settings. By default, Google Cloud enables the serial console for all instances. To verify, go to the VM instance details page, click "Edit", and look for the "Serial port" section. Ensure "Enable connecting to serial ports" is checked. If it's not, enable it and save changes. This step is critical—you won't be able to access the serial console without it.

Step 2: Accessing the Serial Console

Now, start your VM. Once it's running, go back to the VM instance details page in the Console. Look for the "Connect to serial port" option—usually a button or link under the "Remote access" section. Click it to open the serial console in your browser. This is where you'll interact with the VM during boot. As the VM starts up, you'll see the boot logs scrolling by. You need to time it right—just before the OS loads. When you see the GRUB menu (the boot loader screen), press Enter to stop the countdown. If you miss it, you'll need to restart the VM and try again. The GRUB menu is where you'll modify the boot parameters to get a root shell.

Step 3: Interrupting Boot Process

Once you're in the GRUB menu, select the first boot option (usually the latest kernel) and press 'e' to edit the boot parameters. This will open an editable line showing the kernel configuration. Find the line starting with "linux" or "linux16" (depending on your OS). At the end of this line, add the text "init=/bin/bash" (without quotes). This tells the kernel to start a bash shell instead of the usual init system. Press Ctrl+X or F10 to boot with these parameters. Your VM should now boot into a root shell. If you're seeing a shell prompt, you've succeeded. Now, the filesystem is usually mounted as read-only, so you'll need to remount it to make changes.

Google Cloud Account Step 4: Modifying the Password

Type "mount -o remount,rw /" to remount the root filesystem as read-write. Now you can modify system files. For root password, type "passwd" and follow the prompts to set a new password. But wait—remember that root login is disabled by default on GCP VMs. So even if you set a root password, you won't be able to log in as root via SSH unless you've explicitly enabled it. If you want to reset a standard user's password (like "ubuntu" or "username"), use "passwd username" instead. For example, "passwd ubuntu". After setting the password, you need to make sure to re-enable the SSH key setup if you were using keys. Otherwise, you'll be stuck with password authentication, which is less secure. To do this, you can edit the /etc/ssh/sshd_config file and ensure "PermitRootLogin" is set to "no" (or "prohibit-password" for root, and "yes" for password if you want to keep it). But generally, the best practice is to re-add your SSH key to the instance's metadata. So after resetting the password, you can use it to log in, then add your public key back via gcloud compute ssh or by editing the metadata. Once done, type "exec /sbin/init" to continue booting normally.

Step 5: Rebooting Safely

After entering "exec /sbin/init", the VM will finish booting. Wait a minute or two for everything to start up properly, then close the serial console window. Now, try connecting to your VM via SSH. If you reset a user's password, use that password to log in. But remember—this is a temporary solution. For long-term security, you should switch back to SSH key authentication. To do this, log in using the password, then add your public key to the authorized_keys file or via GCP metadata. Then disable password authentication in /etc/ssh/sshd_config and restart the SSH service with "systemctl restart sshd" or "service ssh restart". Congratulations—you're back in!

Method 2: Attaching the Disk to Another VM

What if the serial console method doesn't work for some reason? Maybe the VM's boot process is corrupted, or you're working with a distribution that doesn't use GRUB. In that case, the disk-attach method is your go-to. This involves taking the boot disk of the locked VM, attaching it to a new temporary VM, and making the necessary changes there. It's a bit more involved but works in almost all scenarios.

Step 1: Creating a Snapshot

Before doing anything risky, take a snapshot of your VM's boot disk. This is your safety net—if something goes wrong, you can revert to the snapshot. Go to the "Disks" section in the Google Cloud Console, find the boot disk of your locked VM, and click "Create snapshot". Give it a meaningful name like "locked_vm_snapshot". Wait for the snapshot to complete—this can take a few minutes depending on disk size. Once done, you're ready to attach the disk to another VM. If your VM is still running, stop it first. Then, go to the "Disks" page, select the boot disk, and click "Edit". In the "Instance attached to" field, choose "Detach" and wait for the disk to be detached. Now you're ready to attach it to a new VM.

Step 2: Attaching the Disk to a Temporary VM

Create a new temporary VM instance. Make sure it's in the same region and zone as your locked VM's disk. For simplicity, use a standard Linux image like Debian or Ubuntu. Once the temporary VM is running, go back to the "Disks" page, select the locked VM's boot disk, and click "Edit". In the "Attach to instance" dropdown, choose your temporary VM. After attaching, SSH into the temporary VM. Now you need to identify the attached disk. Use the "lsblk" command to list block devices. You'll see a new disk (like /dev/sdb or /dev/nvme1n1) that's your locked VM's boot disk. Create a directory to mount it, e.g., "sudo mkdir /mnt/locked_vm", then mount the disk with "sudo mount /dev/sdb1 /mnt/locked_vm" (adjust the partition number as needed). For example, if the disk has one partition, it's usually /dev/sdb1. Check with "sudo fdisk -l /dev/sdb" to confirm the correct partition.

Step 3: Mounting and Modifying the Filesystem

Now that the disk is mounted, navigate to the /mnt/locked_vm directory. The critical files to modify are in /etc. For example, if you want to reset the root password, you'd edit /mnt/locked_vm/etc/shadow. This file contains encrypted passwords for all users. Open it with a text editor like nano: "sudo nano /mnt/locked_vm/etc/shadow". Find the line for the root user (it starts with "root:"). The password field is the second part, between the first and second colon. To reset the password, you can replace the encrypted string with a known hash. For example, to set the password to "password123", you can replace the current string with "$6$randomsalt$hash" (but it's easier to use "passwd" on the temporary VM to generate a hash). Alternatively, you can temporarily enable password login for root by setting the password field to an empty string (just remove everything between the colons), but this is insecure. A better approach is to reset the user password. For example, if the standard user is "ubuntu", find the "ubuntu" line in shadow and replace the password field with a known hash. To generate the hash, on the temporary VM, run "openssl passwd -6" and enter a new password. Copy the output. Then replace the root or user password field with that hash. After editing, save the file. Also, make sure to fix any permissions issues—like ensuring /etc/shadow has permissions 640 and owner root:shadow. But usually, if you're copying the file back, the permissions should be intact. If you want to re-enable SSH key authentication, check /mnt/locked_vm/etc/ssh/sshd_config to ensure "PasswordAuthentication" is set to "no" and "PermitRootLogin" is "no" or "prohibit-password". If you need to add SSH keys, you can also edit /mnt/locked_vm/home/username/.ssh/authorized_keys and add your public key. But the preferred way is to add the key to the instance's metadata in GCP Console after reattaching the disk. Once all changes are made, unmount the disk with "sudo umount /mnt/locked_vm" and detach it from the temporary VM in the Console.

Step 4: Reattaching and Cleaning Up

Now, go back to the "Disks" page, select the boot disk, and click "Edit". Attach it back to your original VM. Start the original VM. Once it's up, try connecting to your VM via SSH. If you reset a user's password, use that password to log in. But remember—this is a temporary fix. You should immediately restore SSH key authentication to maintain security. Log in using the password, then add your public key to the authorized_keys file or via GCP metadata. Then disable password authentication in /etc/ssh/sshd_config and restart SSH. Finally, clean up by deleting the temporary VM and snapshot. Don't forget to remove the snapshot to save costs!

Common Pitfalls and How to Avoid Them

Even if you follow these steps carefully, mistakes can happen. Here are some common issues and how to dodge them.

Filesystem Permissions Issues

When modifying files on the mounted disk, permissions are crucial. For example, if you edit /etc/shadow and set the permissions incorrectly, the system might refuse to use the password. Always check permissions: shadow should be 640 (rw-r-----), owned by root:shadow. Similarly, authorized_keys should be 600. On the temporary VM, you can use "chmod" and "chown" to fix these before unmounting. If you forget, you'll need to repeat the process, which is frustrating but easy to fix.

Boot Process Missteps

In the serial console method, timing is everything. If you miss the GRUB menu, you'll boot normally and have to restart. Practice this step a few times on a non-critical VM first. Also, when adding "init=/bin/bash", make sure you don't accidentally add extra spaces or typos. Even a single character error can cause a kernel panic. Always double-check the boot parameters before hitting Ctrl+X.

Security After Resetting

Perhaps the biggest pitfall is forgetting to restore SSH key authentication after resetting the password. If you leave password authentication enabled, your VM becomes vulnerable to brute-force attacks. Always re-enable keys and disable password logins after regaining access. Don't assume you'll remember—make it a checklist item. Also, consider using Identity-Aware Proxy (IAP) for SSH access instead of opening ports to the internet. It's more secure and adds an extra layer of protection.

Best Practices for Future Access

Once you've regained access, take steps to prevent future lockouts. Here are some solid habits to adopt.

Using SSH Keys Properly

Always generate and store SSH keys securely. Use a passphrase for your private key, but don't make it too complicated. Store keys in a password manager or secure location. For multiple users, use the Google Cloud Console's metadata to manage SSH keys centrally. Never rely on password-based authentication for production VMs—stick to keys. If you lose a key, rotate it immediately by replacing it in the metadata rather than resetting passwords.

Implementing IAM Policies

Google Cloud's IAM (Identity and Access Management) is your best friend for controlling access. Instead of sharing SSH keys, use IAM roles to grant users the "Compute OS Admin Login" role. This lets them authenticate via the Google Cloud Console's browser-based SSH, which doesn't require keys. It's more secure and easier to manage. Additionally, use policies to restrict who can modify metadata or start/stop VMs. This way, even if someone accidentally removes keys, others with the right permissions can fix it.

Regular Backups and Metadata Checks

Take regular snapshots of your VMs. This ensures you can roll back to a known good state if things go wrong. Also, check your instance metadata frequently to ensure SSH keys are correctly added and up-to-date. For critical VMs, set up monitoring alerts for SSH access failures. If you notice multiple failed login attempts, it's time to investigate. Finally, document your access procedures. What steps do you take if locked out? Having a clear playbook saves hours of panic when issues arise.

Conclusion

Locking yourself out of a Google Cloud VM is stressful, but with the right tools and knowledge, it's fixable. Whether you use the serial console or attach the disk to another VM, the process is straightforward once you know the steps. Always prioritize security—never leave password authentication enabled after fixing the issue. And remember: prevention is better than cure. By using SSH keys, IAM roles, and regular backups, you can avoid lockouts altogether. Now that you've got the keys to recovery (pun intended), go forth and manage your VMs with confidence!

TelegramContact Us
CS ID
@cloudcup
TelegramSupport
CS ID
@yanhuacloud