如何生成密钥连接Linux实例

Table of Contents

SRC:https://cloud.google.com/compute/docs/instances/adding-removing-ssh-keys

1 Creating a new SSH key

If you do not have an existing private SSH key file and a matching public SSH key file that you can use, generate a new SSH key. If you want to use an existing SSH key, locate the public SSH key file.

  1. Open a terminal on your workstation and use the ssh-keygen command to generate a new key. Specify the -C flag to add a comment with your username.
ssh-keygen -t rsa -f ~/.ssh/[KEY_FILENAME] -C [USERNAME]
  1. Restrict access to your private key so that only you can read it and nobody can write to it.
chmod 400 ~/.ssh/[KEY_FILENAME]

2 Locating an SSH key

If you created a key on a Linux or macOS workstation by using the ssh-keygen tool, your key was saved to the following locations:

+ Public key file: ~/.ssh/[KEY_FILENAME].pub
+ Private key file: ~/.ssh/[KEY_FILENAME]

3 Connect to instance

ssh -i ~/.ssh/[KEY_FILENAME] [HOSTNAME/HOSTIP]