How to SSH into GCP's Compute Engine. It was more complicated than an SSH connection to an EC2 instance on AWS.
・ Linux environment can be used by WSL and cygwin. -Gcloud is installed.
On Linux, you can create an SSH public / private key pair by using the following command. You can create it on your local PC or on a GCE instance.
$ ssh-keygen -t rsa -f ~/.ssh/your-key-name -C your-name
Enter passphrase (empty for no passphrase): #Input passphrase
Enter same passphrase again: #Input passphrase again
Your-key-name (private key) and your-key-name.pub (public key) are created in the ~ / .ssh folder
Go to the GCP metadata page (see here)
If the metadata does not exist like the image, click "Add Metadata" and click Enter the value of the key "enable-oslogin" as "True" and save it. After saving, it will be displayed as follows. OS login has been enabled.
By enabling os-login, you can add the created public key to your account. It is added by entering the following command on the local PC.
$ gcloud compute os-login ssh-keys add --key-file ~\.ssh\your-key-name.pub
Now you are ready to SSH into GCE from your local PC.
Enter the following command to verify your credentials before making a connection
$ gcloud compute os-login describe-profile
"Username" is the user name to reuse the SSH connection
Enter the following command
$ ssh -i ~\.ssh\your-key-name username@your-ip-host
Enter passphrase for key 'C:\Users\user\.ssh\your-key-name':
$ ssh -i ~\.ssh\your-key-name username@your-ip-host
Enter passphrase for key 'C:\Users\user\.ssh\your-key-name':
Linux api-server 4.9.0-12-amd64 #1 SMP Debian 4.9.210-1 (2020-01-20) x86_64
The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.
Connection completed !!
By creating a file named "config" in the ~ / .ssh folder and filling in the necessary information for connecting, it will be easier because you do not have to enter information such as the IP address and private key path one by one. .. Example:
Host hogehoge
HostName your-ip-address
User your-username
IdentityFile ~/.ssh/your-key-name
If you add it to the config file,
$ ssh hogehoge
You can connect with.
Recommended Posts