Mit Sammlungen den Überblick behalten
Sie können Inhalte basierend auf Ihren Einstellungen speichern und kategorisieren.
Linux
Windows
In diesem Dokument wird beschrieben, wie Sie ein SSH-Schlüsselpaar für Compute Engine-VM-Instanzen (VM) erstellen.
Hinweise
Richten Sie die Authentifizierung ein, falls Sie dies noch nicht getan haben.
Bei der Authentifizierung wird Ihre Identität für den Zugriff auf Google Cloud -Dienste und APIs überprüft.
Zum Ausführen von Code oder Beispielen aus einer lokalen Entwicklungsumgebung können Sie sich so bei der Compute Engine authentifizieren:
Select the tab for how you plan to use the samples on this page:
Console
When you use the Google Cloud console to access Google Cloud services and
APIs, you don't need to set up authentication.
gcloud
Installieren Sie die Google Cloud CLI.
Initialisieren Sie die Google Cloud CLI nach der Installation mit dem folgenden Befehl:
Wenn Sie die REST API-Beispiele auf dieser Seite in einer lokalen Entwicklungsumgebung verwenden möchten, verwenden Sie die Anmeldedaten, die Sie der gcloud CLI bereitstellen.
Installieren Sie die Google Cloud CLI.
Initialisieren Sie die Google Cloud CLI nach der Installation mit dem folgenden Befehl:
Wenn Sie über die Google Cloud Console oder die Google Cloud CLI eine Verbindung zu VMs herstellen, erstellt Compute Engine SSH-Schlüssel in Ihrem Namen. Weitere Informationen dazu, wie Compute Engine Schlüssel konfiguriert und speichert, finden Sie unter Informationen zu SSH-Verbindungen.
Wenn Sie mit Drittanbietertools oder OpenSSH eine Verbindung zu VMs herstellen, müssen Sie der VM einen Schlüssel hinzufügen, bevor Sie eine Verbindung herstellen können. Wenn Sie keinen SSH-Schlüssel haben, müssen Sie einen erstellen. VMs akzeptieren die Schlüsselformate, die in der Datei sshd_config aufgeführt sind.
Linux und macOS
Verwenden Sie auf Linux- und macOS-Workstations das Dienstprogramm ssh-keygen, um ein neues SSH-Schlüsselpaar zu erstellen. Im folgenden Beispiel wird ein RSA-Schlüsselpaar erstellt.
Öffnen Sie ein Terminal und erstellen Sie mit dem Befehl ssh-keygen mit dem Flag -C ein neues SSH-Schlüsselpaar.
Der Dateiname my-ssh-key erzeugt beispielsweise die private Schlüsseldatei my-ssh-key und die öffentliche Schlüsseldatei my-ssh-key.pub.
USERNAME: Ihr Nutzername auf der VM. Beispiel: cloudysanfrancisco oder cloudysanfrancisco_gmail_com.
Bei Linux-VMs kann USERNAME nicht root sein, es sei denn, Sie konfigurieren Ihre VM so, dass eine Root-Anmeldung zugelassen wird. Weitere Informationen finden Sie unter Verbindung als Root-Nutzer herstellen.
Bei Windows-VMs, die Active Directory (AD) verwenden, muss dem Nutzernamen die AD-Domain im Format DOMAIN\ vorangestellt werden. Beispiel: Der Nutzer cloudysanfrancisco innerhalb des AD ad.example.com hat den USERNAMEexample\cloudysanfrancisco.
ssh-keygen speichert Ihre private Schlüsseldatei in ~/.ssh/KEY_FILENAME und Ihre öffentliche Schlüsseldatei in ~/.ssh/KEY_FILENAME.pub.
Ein öffentlicher Schlüssel für den Nutzer cloudysanfrancisco sieht in etwa so aus:
Verwenden Sie auf Workstations mit Windows Version 10 oder höher das Dienstprogramm ssh-keygen, um ein neues SSH-Schlüsselpaar zu erstellen. Im folgenden Beispiel wird ein RSA-Schlüsselpaar erstellt.
Öffnen Sie die Eingabeaufforderung und erstellen Sie mit dem Befehl ssh-keygen mit dem Flag -C ein neues SSH-Schlüsselpaar.
WINDOWS_USER: Ihr Nutzername auf dem Windows-Computer.
KEY_FILENAME: Name der SSH-Schlüsseldatei.
Der Dateiname my-ssh-key erzeugt beispielsweise die private Schlüsseldatei my-ssh-key und die öffentliche Schlüsseldatei my-ssh-key.pub.
USERNAME: Ihr Nutzername auf der VM. Beispiel: cloudysanfrancisco oder cloudysanfrancisco_gmail_com.
Bei Linux-VMs kann USERNAME nicht root sein, es sei denn, Sie konfigurieren Ihre VM so, dass eine Root-Anmeldung zugelassen wird. Weitere Informationen finden Sie unter Verbindung als Root-Nutzer herstellen.
Bei Windows-VMs, die Active Directory (AD) verwenden, muss dem Nutzernamen die AD-Domain im Format DOMAIN\ vorangestellt werden. Beispiel: Der Nutzer cloudysanfrancisco innerhalb des AD ad.example.com hat den USERNAMEexample\cloudysanfrancisco.
ssh-keygen speichert Ihre private Schlüsseldatei in C:\Users\WINDOWS_USER\.ssh\KEY_FILENAME und Ihre öffentliche Schlüsseldatei in C:\Users\WINDOWS_USER\.ssh\KEY_FILENAME.pub.
Ein öffentlicher Schlüssel für den Nutzer cloudysanfrancisco sieht in etwa so aus:
Verwenden Sie auf Workstations mit Windows Version 8 oder früher das PuTTYgen-Tool, um ein neues SSH-Schlüsselpaar zu erstellen. Im folgenden Beispiel wird ein RSA-Schlüsselpaar erstellt.
Type of key to generate: RSA ist der Typ des zu generierenden Schlüssels.
Number of bits in a generated key: 2048 oder mehr ist die Anzahl der Bits in einem generierten Schlüssel.
Klicken Sie auf Generate und folgen Sie der Anleitung auf dem Bildschirm.
Das Tool zeigt den Wert des öffentlichen Schlüssels an.
Ersetzen Sie im Abschnitt Key comment den vorab ausgefüllten Text durch Ihren Nutzernamen. Beispiel: cloudysanfrancisco oder cloudysanfrancisco_gmail_com.
Bei Linux-VMs kann der Key comment nicht root sein, es sei denn, Sie konfigurieren Ihre VM so, dass eine Root-Anmeldung zugelassen wird. Weitere Informationen finden Sie unter Verbindung als Root-Nutzer herstellen.
Bei Windows-VMs, die Active Directory (AD) verwenden, muss dem Key comment die AD-Domain im Format DOMAIN\ vorangestellt werden. Beispiel: Der Nutzer cloudysanfrancisco innerhalb des AD ad.example.com hat den Key commentexample\cloudysanfrancisco.
Optional: Geben Sie eine Key passphrase ein, um Ihren Schlüssel mit einem Passwort zu schützen.
Klicken Sie auf Save private key, um einen Speicherort für den privaten Schlüssel auszuwählen.
PuTTYgen schreibt den privaten Schlüssel in eine Datei mit der Erweiterung .ppk.
Klicken Sie auf Save public key, um einen Speicherort für Ihren öffentlichen Schlüssel auszuwählen. Lassen Sie das PuTTYgen-Fenster geöffnet.
Kopieren Sie den Text aus dem Feld Public key for pasting into OpenSSH authorized_keys file.
Öffnen Sie die Datei mit dem öffentlichen Schlüssel. Der öffentliche Schlüssel hat ein Format wie das folgende:
---- BEGIN SSH2 PUBLIC KEY ----
Comment: "USERNAME"
KEY_VALUE
---- END SSH2 PUBLIC KEY ----
Ersetzen Sie den gesamten Inhalt der öffentlichen Schlüsseldatei durch den Wert, den Sie aus dem Feld Public key for pasting into OpenSSH authorized_keys file kopiert haben, damit die öffentliche Schlüsseldatei das folgende Format hat:
KEY_VALUEUSERNAME
Ein öffentlicher Schlüssel für den Nutzer cloudysanfrancisco sieht in etwa so aus:
[[["Leicht verständlich","easyToUnderstand","thumb-up"],["Mein Problem wurde gelöst","solvedMyProblem","thumb-up"],["Sonstiges","otherUp","thumb-up"]],[["Schwer verständlich","hardToUnderstand","thumb-down"],["Informationen oder Beispielcode falsch","incorrectInformationOrSampleCode","thumb-down"],["Benötigte Informationen/Beispiele nicht gefunden","missingTheInformationSamplesINeed","thumb-down"],["Problem mit der Übersetzung","translationIssue","thumb-down"],["Sonstiges","otherDown","thumb-down"]],["Zuletzt aktualisiert: 2025-08-18 (UTC)."],[[["\u003cp\u003eThis document guides users on creating SSH key pairs for Compute Engine virtual machines (VMs) for connections outside the Google Cloud console or CLI.\u003c/p\u003e\n"],["\u003cp\u003eAuthentication setup is required for accessing Google Cloud services and APIs, which can be done through the Google Cloud CLI or REST API.\u003c/p\u003e\n"],["\u003cp\u003eFor Linux and macOS, and Windows 10 or later, the \u003ccode\u003essh-keygen\u003c/code\u003e utility is used to generate SSH key pairs, specifying the key file name and username for the VM.\u003c/p\u003e\n"],["\u003cp\u003eWindows 8 or earlier users should use PuTTYgen, downloading \u003ccode\u003eputtygen.exe\u003c/code\u003e, selecting RSA key type, and setting key parameters before saving the private and public keys.\u003c/p\u003e\n"],["\u003cp\u003eThe public key file's content must be modified to match the format "KEY_VALUE USERNAME," and users can optionally set a key passphrase for added security.\u003c/p\u003e\n"]]],[],null,["Linux Windows\n\n*** ** * ** ***\n\nThis document describes how to create an SSH key pair for Compute Engine\nvirtual machine (VM) instances.\n\nBefore you begin\n\n- If you haven't already, set up [authentication](/compute/docs/authentication). Authentication verifies your identity for access to Google Cloud services and APIs. To run code or samples from a local development environment, you can authenticate to Compute Engine by selecting one of the following options:\n\n Select the tab for how you plan to use the samples on this page: \n\n Console\n\n\n When you use the Google Cloud console to access Google Cloud services and\n APIs, you don't need to set up authentication.\n\n gcloud\n 1.\n [Install](/sdk/docs/install) the Google Cloud CLI.\n\n After installation,\n [initialize](/sdk/docs/initializing) the Google Cloud CLI by running the following command:\n\n ```bash\n gcloud init\n ```\n\n\n If you're using an external identity provider (IdP), you must first\n [sign in to the gcloud CLI with your federated identity](/iam/docs/workforce-log-in-gcloud).\n | **Note:** If you installed the gcloud CLI previously, make sure you have the latest version by running `gcloud components update`.\n 2. [Set a default region and zone](/compute/docs/gcloud-compute#set_default_zone_and_region_in_your_local_client).\n\n REST\n\n\n To use the REST API samples on this page in a local development environment, you use the\n credentials you provide to the gcloud CLI.\n 1. [Install](/sdk/docs/install) the Google Cloud CLI. After installation, [initialize](/sdk/docs/initializing) the Google Cloud CLI by running the following command: \n\n ```bash\n gcloud init\n ```\n 2. If you're using an external identity provider (IdP), you must first [sign in to the gcloud CLI with your federated identity](/iam/docs/workforce-log-in-gcloud).\n\n\n For more information, see\n [Authenticate for using REST](/docs/authentication/rest)\n in the Google Cloud authentication documentation.\n\nCreate an SSH key pair\n\nIf you connect to VMs using the Google Cloud console or the\nGoogle Cloud CLI, Compute Engine creates SSH keys on your behalf. For\nmore information on how Compute Engine configures and stores keys, see\n[About SSH connections](/compute/docs/instances/ssh).\n\nIf you connect to VMs using third party tools or OpenSSH, you need to\nadd a key to your VM before you can connect. If you don't have an SSH key,\nyou must create one. VMs accept the key formats listed in the `sshd_config`\nfile. \n\nLinux and macOS\n\nOn Linux and macOS workstations, use the\n[`ssh-keygen` utility](https://man.openbsd.org/ssh-keygen)\nto create a new SSH key pair. The following example creates an RSA key pair.\n\nOpen a terminal and use the `ssh-keygen` command with the `-C` flag to\ncreate a new SSH key pair. \n\n```\nssh-keygen -t rsa -f ~/.ssh/KEY_FILENAME -C USERNAME\n```\n\nReplace the following:\n\n- \u003cvar translate=\"no\"\u003eKEY_FILENAME\u003c/var\u003e: the name for your SSH key file.\n\n For example, a filename of `my-ssh-key` generates a private key file named\n `my-ssh-key` and a public key file named `my-ssh-key.pub`.\n- \u003cvar translate=\"no\"\u003eUSERNAME\u003c/var\u003e: your username on the VM. For example,\n `cloudysanfrancisco`, or `cloudysanfrancisco_gmail_com`.\n\n For Linux VMs, the \u003cvar translate=\"no\"\u003eUSERNAME\u003c/var\u003e can't be `root`,\n unless you configure your VM to allow root login. For more information,\n see [Connect to VMs as the root user](/compute/docs/connect/root-ssh).\n\n For Windows VMs that use Active Directory (AD), the username must be\n prepended with the AD domain, in the format of\n \u003cvar translate=\"no\"\u003eDOMAIN\u003c/var\u003e`\\`. For example, the user\n `cloudysanfrancisco` within the `ad.example.com` AD has a\n \u003cvar translate=\"no\"\u003eUSERNAME\u003c/var\u003e of `example\\cloudysanfrancisco`.\n\n`ssh-keygen` saves your private key file to\n`~/.ssh/`\u003cvar translate=\"no\"\u003eKEY_FILENAME\u003c/var\u003e and your public key file to\n`~/.ssh/`\u003cvar translate=\"no\"\u003eKEY_FILENAME\u003c/var\u003e`.pub`.\n\nA public key for the user `cloudysanfrancisco` looks similar to the\nfollowing: \n\n```\nssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDAu5kKQCPF... cloudysanfrancisco\n```\n\nWindows 10 or later\n\nOn workstations with Windows version 10 or later, use the\n[`ssh-keygen` utility](https://man.openbsd.org/ssh-keygen)\nto create a new SSH key pair. The following example creates an RSA key pair.\n\nOpen Command Prompt and use the `ssh-keygen` command with the `-C` flag to\ncreate a new SSH key pair. \n\n```\nssh-keygen -t rsa -f C:\\Users\\WINDOWS_USER\\.ssh\\KEY_FILENAME -C USERNAME\n```\n\nReplace the following:\n\n- \u003cvar translate=\"no\"\u003eWINDOWS_USER\u003c/var\u003e: your username on the Windows\n machine.\n\n- \u003cvar translate=\"no\"\u003eKEY_FILENAME\u003c/var\u003e: the name for your SSH key file.\n\n For example, a filename of `my-ssh-key` generates a private key file named\n `my-ssh-key` and a public key file named `my-ssh-key.pub`.\n- \u003cvar translate=\"no\"\u003eUSERNAME\u003c/var\u003e: your username on the VM. For example,\n `cloudysanfrancisco`, or `cloudysanfrancisco_gmail_com`.\n\n For Linux VMs, the \u003cvar translate=\"no\"\u003eUSERNAME\u003c/var\u003e can't be `root`,\n unless you configure your VM to allow root login. For more information,\n see [Connect to VMs as the root user](/compute/docs/connect/root-ssh).\n\n For Windows VMs that use Active Directory (AD), the username must be\n prepended with the AD domain, in the format of\n \u003cvar translate=\"no\"\u003eDOMAIN\u003c/var\u003e`\\`. For example, the user\n `cloudysanfrancisco` within the `ad.example.com` AD has a\n \u003cvar translate=\"no\"\u003eUSERNAME\u003c/var\u003e of `example\\cloudysanfrancisco`.\n\n`ssh-keygen` saves your private key file to\n`C:\\Users\\`\u003cvar translate=\"no\"\u003eWINDOWS_USER\u003c/var\u003e`\\.ssh\\`\u003cvar translate=\"no\"\u003eKEY_FILENAME\u003c/var\u003e\nand your public key file to\n`C:\\Users\\`\u003cvar translate=\"no\"\u003eWINDOWS_USER\u003c/var\u003e`\\.ssh\\`\u003cvar translate=\"no\"\u003eKEY_FILENAME\u003c/var\u003e`.pub`.\n\nA public key for the user `cloudysanfrancisco` looks similar to the\nfollowing: \n\n```\nssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDAu5kKQCPF... cloudysanfrancisco\n```\n\nWindows 8 or earlier\n\nOn workstations with Windows version 8 or earlier, use the PuTTYgen tool to\ncreate a new SSH key pair. The following example creates an RSA key pair.\n\n1. [Download `puttygen.exe`](http://www.chiark.greenend.org.uk/%7Esgtatham/putty/download.html)\n if you haven't already.\n\n2. Open PuTTYgen.\n\n3. Under **Parameters** specify the following:\n\n - **Type of key to generate** : `RSA`\n - **Number of bits in a generated key** : `2048` or more\n4. Click **Generate** and follow the on-screen instructions.\n\n The tool displays the public key value.\n5. In the **Key comment** section, replace the pre-populated text with your\n username. For example, `cloudysanfrancisco`, or\n `cloudysanfrancisco_gmail_com`.\n\n For Linux VMs, the **Key comment** can't be `root`, unless you configure\n your VM to allow root login. For more information,\n see [Connect to VMs as the root user](/compute/docs/connect/root-ssh).\n\n For Windows VMs that use Active Directory (AD), the **Key comment** must\n be prepended with the AD domain, in the format of\n \u003cvar translate=\"no\"\u003eDOMAIN\u003c/var\u003e`\\`. For example, the user\n `cloudysanfrancisco` within the `ad.example.com` AD has a **Key comment**\n of `example\\cloudysanfrancisco`.\n6. Optional: enter a **Key passphrase** to password-protect your key.\n\n7. Click **Save private key** to choose a location to save the private key\n to.\n\n PuTTYgen writes the private key to a file with a `.ppk` extension.\n8. Click **Save public key** to choose a location to save your public key\n to. Keep the PuTTYgen window open.\n\n9. Copy the text from the **Public key for pasting into OpenSSH\n authorized_keys file** field.\n\n10. Open the public key file. The public key has a format similar to the\n following:\n\n ```\n ---- BEGIN SSH2 PUBLIC KEY ----\n Comment: \"USERNAME\"\n KEY_VALUE\n ---- END SSH2 PUBLIC KEY ----\n ```\n11. Replace the entire contents of the public key file with the value you\n copied from the **Public key for pasting into OpenSSH authorized_keys\n file** field, so that your public key file matches the following format:\n\n ```\n KEY_VALUE USERNAME\n ```\n\nA public key for the user `cloudysanfrancisco` looks similar to the\nfollowing: \n\n```\nssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDAu5kKQCPF... cloudysanfrancisco\n```\n\nWhat's next?\n\n- Learn how to [Add SSH keys to VMs](/compute/docs/connect/add-ssh-keys)\n- Learn [About SSH connections](/compute/docs/instances/ssh) work on Compute Engine"]]