Adding an SSH key to Google Cloud allows you to securely connect to your virtual machine (VM) instances. Here's how to do it:
Steps to Add an SSH Key
- Navigate to the VM Instances Page:
- In the Google Cloud console, go to the VM instances page.
- Select the VM Instance:
- Click the name of the VM instance you want to add the SSH key to. This will take you to the VM instance details page.
- Edit the VM Instance:
- Click the Edit button at the top of the VM instance details page. This enables you to modify the VM's configuration.
- Add the SSH Key:
- Scroll down to the SSH Keys section.
- Click Add item.
- Paste the Public Key:
- Paste your public SSH key into the text box.
- Important: Ensure you're using the public key and not the private key. Public keys typically start with
ssh-rsa
,ssh-ed25519
, orecdsa-sha2-nistp256
. A complete example would resemble:ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC123EXAMPLEabc your_username@your_computer
- Important: Ensure you're using the public key and not the private key. Public keys typically start with
- Paste your public SSH key into the text box.
- Save the Changes:
- Click Save at the bottom of the page.
Understanding SSH Keys
SSH keys provide a more secure method of authentication compared to passwords. They use a cryptographic key pair: a private key (which you keep secret on your local machine) and a public key (which you add to the server).
Generating an SSH Key Pair (if you don't already have one)
If you don't already have an SSH key pair, you'll need to generate one. You can do this using the ssh-keygen
command in your terminal.
ssh-keygen -t rsa -b 2048 -f ~/.ssh/my-google-cloud-key
- This command will generate two files in the
.ssh
directory in your home directory:my-google-cloud-key
(the private key - keep this safe!)my-google-cloud-key.pub
(the public key - the one you paste into Google Cloud)
Important Security Considerations:
- Protect Your Private Key: Never share your private key with anyone. If someone gains access to your private key, they can access your VM instance.
- Use Strong Passphrases: When generating your SSH key pair, use a strong and unique passphrase to protect your private key.
- Regularly Rotate Keys: Consider rotating your SSH keys periodically for enhanced security.
Alternative Method: Using gcloud CLI
You can also add SSH keys using the Google Cloud SDK (gcloud CLI):
gcloud compute instances add-metadata <instance-name> --metadata "ssh-keys=<username>:<public-key>" --zone=<zone>
- Replace
<instance-name>
with the name of your VM instance. - Replace
<username>
with your desired username on the VM. - Replace
<public-key>
with the content of your public key file (e.g.,cat ~/.ssh/id_rsa.pub
). - Replace
<zone>
with the zone where your VM instance is located.
Example:
gcloud compute instances add-metadata my-vm --metadata "ssh-keys=myuser:ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC123EXAMPLEabc myuser@mycomputer" --zone=us-central1-a
Verifying the SSH Key Addition
After adding the SSH key, you should be able to connect to your VM instance using SSH without being prompted for a password. Use the following command:
ssh <username>@<external-ip-address>
- Replace
<username>
with the username you associated with the key when adding it (or your default username if added via the UI). - Replace
<external-ip-address>
with the external IP address of your VM instance.
Adding an SSH key to Google Cloud is a crucial step for securely managing your VM instances. By following these steps, you can ensure that only authorized users can access your servers.