How can I access the console of a Google Compute Engine VM instance?
问题:
回答1:
To see the console output (read-only), you can use any of the following methods:
- Web UI via Developers Console – on the instance detail page, scroll to the bottom of the page and expand the console output view
- CLI via
gcloud compute instances get-serial-port-output
- API via
getSerialPortOutput
To get read/write (interactive) access, follow instructions on this page:
gcloud compute instances add-metadata [INSTANCE_NAME] \ --metadata=serial-port-enable=1
and then, per the same page, either connect via Google Cloud Console:
Go to the VM instances page.
- Go to the GCE VM instances page
- Click the instance you want to connect to.
- Scroll to the bottom of the page and look for the Serial port section.
- If you want to connect to a serial port other than the default serial port 1, click the down arrow next to the Connect to serial port button and change the port number accordingly.
- Click the Connect to serial port button to connect to port 1 by default. For Windows instances, pull down the dropdown menu next to the button and connect to Port 2 to access the serial console.
or, connect via gcloud
:
Use the
gcloud compute connect-to-serial-port
subcommand to connect using thegcloud
command-line tool. For example:gcloud compute connect-to-serial-port [INSTANCE_NAME]
where
[INSTANCE_NAME]
is the name of the instance for which you want to access the serial console.By default, the
connect-to-serial-port
command connects to port 1 of the serial console. If you are connecting to a Windows VM instance, connect to port 2 instead:gcloud compute connect-to-serial-port [INSTANCE_NAME] --port 2
To connect to any other port, provide a different port number using the
--port
flag. You can provide a port number from 1 through 4, inclusively. To learn more about port numbers, see Understanding serial port numbering.
回答2:
While this doesn't answer your direct question, if the reason you need physical console access is to troubleshoot why a system is inaccessible (i.e. it no longer boots or, because of a bad firewall configuration, you can no longer access it over SSH), your best bet is to:
- Update the disk configuration so it is not deleted when the instance is destroyed
- Delete the instance so the disk is no longer attached to a running instance
- Attach the disk to another instance which boots correctly
- Mount the disk to a temporary location within that instance, so you can read logs, view/edit configuration files, etc.
回答3:
You can login to your instance via serial console. If you don't have a user with a password, you can create one via a startup script and then use that to login and check the instance. Please follow the instructions below:
- 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.
- Under Custom metadata, click Add item.
- 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.
- Enable "Enable connecting to serial ports" by checking the box below the SSH button.
- Click Save and then click RESET on the top of the page. Wait for some time for the instance to reboot.
- Click on 'Connect to serial port' in the page. In the new window, you might need to wait a bit and press on Enter of your keyboard once; then, you should see the login prompt.
- Login using the USERNAME and PASSWORD you provided.
- Then restart ssh service
- After restarting, try if connecting via SSH Works.
Note that once you have created the user, please make sure to delete the startup script from the metadata so it won't change the password every reboot.