To remotely access a virtual machine instance on Google Cloud through SSH, you need to follow the following steps:
-
Launch Google Cloud Shell
Google Cloud Shell is a command-line tool built into the Google Cloud Console that provides full access to Google Cloud resources.
You can start it by clicking the "Activate Cloud Shell" button in the top right corner (usually located on the right side of the top navigation bar). If this is your first time using Cloud Shell, a small virtual machine instance will be created for you.
-
Create SSH Keys
In Cloud Shell, you can run the following command to generate SSH key pairs:
ssh-keygen -t rsa -f ~/.ssh/[KEY_FILENAME] -C [USERNAME]
Replace
[KEY_FILENAME]
with your key file name and[USERNAME]
with your username on the remote instance. When prompted, you can choose to enter a passphrase for the key pair or skip it.Note: By default, if you do not specify the key type (
-t
option) when running thessh-keygen
command, it will generate RSA keys. -
Add the Public Key to Google Cloud
You need to add the public key you just generated to the "Metadata" page in Google Cloud. On the "Metadata" page, select the "SSH Keys" option, then click the "Edit" button, and finally click the "Add Item" button.
You can use the
cat
command to view your public key:cat ~/.ssh/[KEY_FILENAME].pub
Replace
[KEY_FILENAME]
with your key file name. Copy the displayed content and paste it into a new item on the "SSH Keys" page. -
Connect to the Remote Instance via SSH
You can now connect to your Google Cloud VM instance using the following command:
ssh -i ~/.ssh/[KEY_FILENAME] [USERNAME]@[IP_ADDRESS]
Replace
[KEY_FILENAME]
with your key file name,[USERNAME]
with your username on the remote instance, and[IP_ADDRESS]
with the public IP address of your VM instance.
The above steps will help you connect to a virtual machine instance on Google Cloud through SSH. If you encounter any issues during the process, feel free to ask for further assistance.