[LINUX] Ssh connect to GCP from Windows

How to SSH into GCP's Compute Engine. It was more complicated than an SSH connection to an EC2 instance on AWS.

Prerequisites

・ Linux environment can be used by WSL and cygwin. -Gcloud is installed.

Creating an SSH key

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

Enable OS login function

Go to the GCP metadata page (see here)

image.png

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. image.png OS login has been enabled.

Add the created public key to your account

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.

Confirmation of authentication information

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

Make an SSH connection

Enter the following command

$ ssh -i ~\.ssh\your-key-name [email protected]
Enter passphrase for key 'C:\Users\user\.ssh\your-key-name':
$ ssh -i ~\.ssh\your-key-name [email protected]
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 !!

At the end

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

Ssh connect to GCP from Windows
Connect to sqlite from python
Connect to utf8mb4 database from python
Connect to centos6 on virtualbox with ssh connection from Mac
Connect to Packetix VPN from CentOS 7 minimal.
Connect to Docker's MySQL container from Flask
SSH connection from Windows via SSL VPN
Connect to coincheck's Websocket API from Python
Connect to mysql
I want to connect to PostgreSQL from various languages
GCP: Repeat from Pub / Sub to Cloud Functions, Cloud Functions to Pub / Sub
How to call Cloud API from GCP Cloud Functions
From ROS for Windows installation to operation check
Use Tor to connect from urllib2 [Python] [Mac]
SSH login to the target server from Windows with a click of a shortcut
Changes from Python 3.0 to Python 3.5
Transition from WSL1 to WSL2
Connect python to mysql
From editing to execution
[Linux] Copy data from Linux to Windows with a shell script
Ssh to an external server under http proxy [from Ubuntu 18.04]
[Postgresql] SSH connection to the external DB server from the client
How to connect to various DBs from Python (PEP 249) and SQLAlchemy
How to generate a public key from an SSH private key
Connect to KUINS-III (Kyoto University VPN) from Linux using PPTP
After all, the story of returning from Linux to Windows
I tried changing the python script from 2.7.11 to 3.6.0 on windows10
Use ssh with private key public key authentication. Connect from wan.
Connect to KUINS-III (Kyoto University VPN) from Linux using IKEv2
[Windows] RDP to Windows via Linux
Connect to BigQuery with Python
Post from Python to Slack
How to install Python [Windows]
Cheating from PHP to Python
Porting from argparse to hydra
Migrating from Chainer v1 to Chainer v2
Connect to Wikipedia with Python
Connect to Postgresql with GO
Anaconda updated from 4.2.0 to 4.3.0 (python3.5 updated to python3.6)
Migrated from Flask-RESTPlus to Flask-RESTX
Update python-social-auth from 0.1.x to 0.2.x
Migrate from requirements.txt to pipenv
Switch from python2.7 to python3.6 (centos7)
React and Flask to GCP
Connect to s3 tokyo region
Deploy Django + React from scratch to GKE (3) Create a GCP project
Everything from building a Python environment to running it on Windows