0

I'm trying to access GCP VM instance but I messed it up and now I'm getting this error: sudo: /usr/bin/sudo must be owned by uid 0 and have the setuid bit set

but I don't have the root password. Is there a way I can access GCP VM instance as a rot user directly? Without using the command line? Because sudo commands are not working, to use su you need a password

  • If you've broken sudo, you may need to restore from a backup. Guessing you accidentally `chown`ed `/`? – ceejayoz Jun 06 '22 at 18:32
  • @ceejayoz. There is no other solution? – Allen Chen Jun 06 '22 at 18:35
  • If you did indeed entirely break `sudo`, I can't think of any. If you need to keep some data on this instance, you can likely mount its filesystem to a new server to pull data off it, but you're still gonna need a new server. – ceejayoz Jun 06 '22 at 18:38
  • You might be able to run commands via the VM startup scripts or serial console to recover, but that depends on what you did to break the VM. I recommend following @ceejayoz advice. Take a snapshot, create a new VM with the snapshot as the second disk. – John Hanley Jun 06 '22 at 19:45

1 Answers1

2

As @John Hanley suggest to run commands via VM startup script, you can reset the password for root access by using the serial console and following the steps below.

  1. Go to the VM instances page and click on the instance name of your VM.
  2. Click the Edit button at the top of the page.
  3. Under Custom metadata, click Add item.
  4. Set 'Key' to 'startup-script' and set 'Value' to this script:
#! /bin/bash
useradd -G sudo USERNAME
echo 'USERNAME:PASSWORD' | chpasswd

NOTE: Change the value of USERNAME and PASSWORD to the name and password of your choice.

  1. Enable "Enable connecting to serial ports" by checking the box below the SSH button.

  2. Click Save and then click RESET on the top of the page. Wait for some time for the instance to reboot.

  3. Click on 'Connect to serial port' in the page. In the new window, you might need to wait a bit and press Enter on your keyboard once, then you should see the login prompt.

  4. Login using the USERNAME and PASSWORD you provided.

かいぜん
  • 146
  • 5
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 26 '22 at 04:49