Mantieni tutto organizzato con le raccolte
Salva e classifica i contenuti in base alle tue preferenze.
Questa pagina descrive come utilizzare la CLI gcloud per connetterti alla tua
workstation da una macchina locale utilizzando SSH (o qualsiasi altro protocollo TCP).
Cloud Workstations utilizza un tunnel per inoltrare il traffico TCP tra una porta sulla tua macchina locale e una porta sulla tua workstation senza esporre apertamente la tua workstation a internet. Le connessioni vengono autenticate utilizzando
le credenziali dell'interfaccia a riga di comando gcloud e autorizzate in base alle
norme IAM della workstation di destinazione.
Una volta stabilito il tunnel TCP tra la porta locale e la workstation, puoi utilizzarlo per inoltrare il traffico da un client SSH, curl o qualsiasi altra applicazione che utilizza TCP.
Per comodità, Cloud Workstations fornisce il comando
gcloud workstations ssh, che stabilisce il tunnel TCP ed esegue un client SSH con un singolo comando CLI gcloud.
Per tutti gli altri casi d'uso, utilizza il comando
gcloud workstations start-tcp-tunnel
per stabilire il tunnel TCP ed eseguire l'applicazione che utilizzerà il
tunnel (ad esempio, curl) in un terminale separato.
Per stabilire una connessione SSH sicura alla tua workstation, utilizza il comando
gcloud workstations ssh, che avvia un tunnel TCP ed esegue un client SSH.
Per copiare il comando nel buffer di copia e incolla, fai clic su
content_copyCopia codice campione e poi incolla il comando in una finestra del terminale locale:
PROJECT_ID: l'ID progetto Google Cloud del progetto contenente la workstation. Se omesso, viene utilizzato il progetto corrente.
REGION: la regione in cui si trova il cluster della workstation, ad esempio us-central1.
CLUSTER_NAME: il nome del cluster di workstation
che contiene la workstation.
CONFIG_NAME: il nome della configurazione della workstation
contenente questa workstation.
WORKSTATION_PORT (facoltativo): la porta sulla workstation
a cui deve essere inviato il traffico. Se omesso, il traffico verrà inviato alla porta
22. Tutte le immagini Cloud Workstations preconfigurate includono un server SSH in esecuzione
sulla porta 22 della workstation.
LOCAL_PORT (facoltativo): la porta localhost da cui
verrà inviato il traffico. I numeri di porta validi sono compresi tra 1024 e 65535. Se ometti il flag --local-host-port o specifichi una porta 0, viene selezionata automaticamente una porta non utilizzata.
WORKSTATION_NAME: il nome della workstation.
(Facoltativo) Per passare flag e argomenti posizionali all'implementazione ssh
sottostante, aggiungili al comando dopo un doppio trattino (--).
Utilizza un tunnel TCP per inoltrare traffico TCP arbitrario alla workstation
Esegui il seguente comando CLI gcloud per creare un tunnel TCP autenticato.
Per copiare il comando nel buffer di copia e incolla, fai clic su
content_copyCopia codice campione e poi incolla il comando in una finestra del terminale locale:
PROJECT_ID: l'ID progetto Google Cloud del progetto contenente la workstation. Se omesso, viene utilizzato il progetto corrente.
REGION: la regione in cui si trova il cluster della workstation, ad esempio us-central1.
CLUSTER_NAME: il nome del cluster di workstation
che contiene la workstation.
CONFIG_NAME: il nome della configurazione della workstation che contiene queste workstation.
LOCAL_PORT (facoltativo): la porta localhost da cui
verrà inviato il traffico. I numeri di porta validi sono compresi tra 1024 e 65535. Se ometti il flag --local-host-port o specifichi una porta 0, viene selezionata automaticamente una porta non utilizzata.
WORKSTATION_NAME: il nome della workstation.
WORKSTATION_PORT: la porta della workstation a cui
deve essere inviato il traffico.
Le immagini Cloud Workstations preconfigurate includono un server SSH in esecuzione sulla porta della workstation 22.
Il comando CLI gcloud esegue un test di connettività con la
workstation, apre un tunnel e poi visualizza un numero di porta:
Listening on port [LOCAL_PORT].
Tutto il traffico inviato a localhost:LOCAL_PORT viene inoltrato alla workstation. La porta è accessibile solo dalle applicazioni in esecuzione sul tuo
computer locale.
Lascia in esecuzione la CLI gcloud e apri un altro terminale per eseguire l'applicazione che si connette alla tua workstation.
Ad esempio, se esegui un server sulla tua workstation che gestisce
la porta WORKSTATION_PORT e nel passaggio precedente hai
creato un tunnel TCP che inoltra il traffico tra la porta locale
LOCAL_PORT e la porta della workstation
WORKSTATION_PORT, puoi eseguire curl sulla tua macchina
locale per connetterti al server sulla tua workstation:
curl localhost:LOCAL_PORT
Hello, world!
Al termine, torna al terminale in cui hai avviato il tunnel TCP e interrompi la CLI gcloud premendo Ctrl+C.
Utilizzare server SSH su porte diverse
Le immagini container personalizzate
possono utilizzare anche server SSH su qualsiasi porta. Per supportare le connessioni dal tunnel della CLI gcloud, devi configurare server SSH personalizzati per consentire l'autenticazione tramite password e impostare l'utente di destinazione con una password vuota.
Cloud Workstations utilizza
Cloud IAM
per garantire che solo il traffico autorizzato venga inviato al server SSH.
[[["Facile da capire","easyToUnderstand","thumb-up"],["Il problema è stato risolto","solvedMyProblem","thumb-up"],["Altra","otherUp","thumb-up"]],[["Difficile da capire","hardToUnderstand","thumb-down"],["Informazioni o codice di esempio errati","incorrectInformationOrSampleCode","thumb-down"],["Mancano le informazioni o gli esempi di cui ho bisogno","missingTheInformationSamplesINeed","thumb-down"],["Problema di traduzione","translationIssue","thumb-down"],["Altra","otherDown","thumb-down"]],["Ultimo aggiornamento 2025-09-04 UTC."],[],[],null,["# SSH support\n\nThis page describes how to use the `gcloud` CLI to connect to your\nworkstation from a local machine using SSH (or any other TCP protocol).\n\nCloud Workstations uses a tunnel to forward TCP traffic between a port on your\nlocal machine and a port on your workstation without openly exposing\nyour workstation to the internet. Connections are authenticated using\ncredentials from the `gcloud` CLI and authorized according to the\ntarget workstation's IAM policies.\n\nOnce the TCP tunnel is established between your local port and the workstation,\nyou can use it to forward traffic from an SSH client, curl, or any other\napplication that uses TCP.\n\nFor convenience, Cloud Workstations provides the\n[`gcloud workstations ssh`](/sdk/gcloud/reference/workstations/ssh)\ncommand, which establishes the TCP tunnel and runs an SSH client with a\nsingle `gcloud` CLI command.\n\nFor all other use cases, use the\n[`gcloud workstations start-tcp-tunnel`](/sdk/gcloud/reference/workstations/start-tcp-tunnel)\ncommand to establish the TCP tunnel and run the application that will use the\ntunnel (for example, `curl`) in a separate terminal.\n| **Note:** All preconfigured Cloud Workstations images include an SSH server that runs on workstation port `22`.\n\nBefore you begin\n----------------\n\n1. If you don't already have a workstation to connect to,\n [set up a workstation](/workstations/docs/quickstart-set-up-workstations-console).\n\n2.\n\n\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\n \u003cbr /\u003e\n\n3. Make sure that you have the\n [Cloud Workstations User](/workstations/docs/access-control#workstations-user)\n IAM role on the workstation you will connect to.\n\n [Go to IAM](https://console.cloud.google.com/iam-admin/iam)\n\nConnect to your workstation using SSH\n-------------------------------------\n\nTo establish a secure SSH connection to your workstation, use the\n[`gcloud workstations ssh`](/sdk/gcloud/reference/workstations/ssh)\ncommand, which starts a TCP tunnel and runs an SSH client.\n\nTo copy the command to the copy-paste buffer, click\ncontent_copy\n**Copy code sample** and then paste the command into a local terminal window: \n\n gcloud workstations ssh \\\n --project=\u003cvar translate=\"no\"\u003ePROJECT_ID\u003c/var\u003e \\\n --region=\u003cvar translate=\"no\"\u003eREGION\u003c/var\u003e \\\n --cluster=\u003cvar translate=\"no\"\u003eCLUSTER_NAME\u003c/var\u003e \\\n --config=\u003cvar translate=\"no\"\u003eCONFIG_NAME\u003c/var\u003e \\\n --port=\u003cvar translate=\"no\"\u003eWORKSTATION_PORT\u003c/var\u003e \\\n --local-host-port=localhost:\u003cvar translate=\"no\"\u003eLOCAL_PORT\u003c/var\u003e \\\n \u003cvar translate=\"no\"\u003eWORKSTATION_NAME\u003c/var\u003e\n\nReplace the following values:\n\n- \u003cvar translate=\"no\"\u003ePROJECT_ID\u003c/var\u003e: the Google Cloud project ID for the\n project containing the workstation. If omitted, the current project is\n used.\n\n- \u003cvar translate=\"no\"\u003eREGION\u003c/var\u003e: the region where the workstation's cluster is\n located---for example, `us-central1`.\n\n- \u003cvar translate=\"no\"\u003eCLUSTER_NAME\u003c/var\u003e: the name of the workstation cluster\n containing the workstation.\n\n- \u003cvar translate=\"no\"\u003eCONFIG_NAME\u003c/var\u003e: the name of the workstation configuration\n containing this workstations.\n\n- \u003cvar translate=\"no\"\u003eWORKSTATION_PORT\u003c/var\u003e (Optional): the port on the workstation\n to which traffic should be sent. If omitted, traffic will be sent to port\n `22`. All preconfigured Cloud Workstations images include an SSH server that runs\n on workstation port `22`.\n\n- \u003cvar translate=\"no\"\u003eLOCAL_PORT\u003c/var\u003e (Optional): the localhost port from which\n traffic will be sent. Valid port numbers are `1024` to `65535`. If you omit\n the `--local-host-port` flag or specify a port of `0` an unused port is\n selected automatically.\n\n- \u003cvar translate=\"no\"\u003eWORKSTATION_NAME\u003c/var\u003e: the name of the workstation.\n\nOptional: To pass flags and positionals to the underlying `ssh`\nimplementation, append them to the command after a double-dash (`--`).\n\nUse a TCP tunnel to forward arbitrary TCP traffic to your workstation\n---------------------------------------------------------------------\n\nTo connect to a workstation using a TCP application other than `ssh`, use the\n[`gcloud workstations start-tcp-tunnel`](https://cloud.google.com/sdk/gcloud/reference/workstations/start-tcp-tunnel)\ncommand:\n\n1. Run the following `gcloud` CLI command to create an authenticated\n TCP tunnel.\n\n To copy the command to the copy-paste buffer, click\n content_copy\n **Copy code sample** and then paste the command into a local terminal window: \n\n gcloud workstations start-tcp-tunnel \\\n --project=\u003cvar translate=\"no\"\u003ePROJECT_ID\u003c/var\u003e \\\n --region=\u003cvar translate=\"no\"\u003eREGION\u003c/var\u003e \\\n --cluster=\u003cvar translate=\"no\"\u003eCLUSTER_NAME\u003c/var\u003e \\\n --config=\u003cvar translate=\"no\"\u003eCONFIG_NAME\u003c/var\u003e \\\n --local-host-port=localhost:\u003cvar translate=\"no\"\u003eLOCAL_PORT\u003c/var\u003e \\\n \u003cvar translate=\"no\"\u003eWORKSTATION_NAME\u003c/var\u003e \\\n \u003cvar translate=\"no\"\u003eWORKSTATION_PORT\u003c/var\u003e\n\n Replace the following values:\n - \u003cvar translate=\"no\"\u003ePROJECT_ID\u003c/var\u003e: the Google Cloud project ID for the\n project containing the workstation. If omitted, the current project is\n used.\n\n - \u003cvar translate=\"no\"\u003eREGION\u003c/var\u003e: the region where the workstation's cluster is\n located---for example, `us-central1`.\n\n - \u003cvar translate=\"no\"\u003eCLUSTER_NAME\u003c/var\u003e: the name of the workstation cluster\n containing the workstation.\n\n - \u003cvar translate=\"no\"\u003eCONFIG_NAME\u003c/var\u003e: the name of the workstation\n configuration containing this workstations.\n\n - \u003cvar translate=\"no\"\u003eLOCAL_PORT\u003c/var\u003e (Optional): the localhost port from which\n traffic will be sent. Valid port numbers are `1024` to `65535`. If you\n omit the `--local-host-port` flag or specify a port of `0` an unused\n port is selected automatically.\n\n - \u003cvar translate=\"no\"\u003eWORKSTATION_NAME\u003c/var\u003e: the name of the workstation.\n\n - \u003cvar translate=\"no\"\u003eWORKSTATION_PORT\u003c/var\u003e: the workstation port to which\n traffic should be sent.\n [Preconfigured Cloud Workstations images](/workstations/docs/preconfigured-base-images)\n include an SSH server that runs on workstation port `22`.\n\n2. The `gcloud` CLI command performs a connectivity test with the\n workstation, opens a tunnel, and then displays a port number:\n\n Listening on port [\u003cvar translate=\"no\"\u003eLOCAL_PORT\u003c/var\u003e].\n\n All traffic sent to `localhost:`\u003cvar translate=\"no\"\u003eLOCAL_PORT\u003c/var\u003e is forwarded to\n the workstation. The port is only accessible by applications running on your\n local computer.\n3. Leave the `gcloud` CLI running and open another terminal to run the\n application that connects to your workstation.\n\n For example, if you are running a server on your workstation that serves\n port \u003cvar translate=\"no\"\u003eWORKSTATION_PORT\u003c/var\u003e, and in the previous step you\n created a TCP tunnel that forwards traffic between your local port\n \u003cvar translate=\"no\"\u003eLOCAL_PORT\u003c/var\u003e and the workstation port\n \u003cvar translate=\"no\"\u003eWORKSTATION_PORT\u003c/var\u003e, you could run `curl` on your local\n machine to connect to the server on your workstation: \n\n curl localhost:\u003cvar translate=\"no\"\u003eLOCAL_PORT\u003c/var\u003e\n Hello, world!\n\n4. When you are finished, return to the terminal where you started the TCP\n tunnel and interrupt the `gcloud` CLI by pressing\n \u003ckbd\u003eControl+C\u003c/kbd\u003e.\n\nUse SSH servers on different ports\n----------------------------------\n\n[Custom container images](/workstations/docs/custom-container-images)\ncan also use SSH servers on any port. To support connections from the\n`gcloud` CLI tunnel, you must configure custom SSH servers to allow\npassword authentication and set the target user with an empty password.\nCloud Workstations uses\n[Cloud IAM](/iam/docs/overview)\nto help ensure that only authorized traffic is sent to the SSH server.\n\nWhat's next\n-----------\n\n- [Set container environment variables in SSH sessions](/workstations/docs/customize-container-images#container_image_that_sets_container_environment_variables_in_ssh_sessions) for custom container images\n- [Enable X11 forwarding](/workstations/docs/customize-container-images#container_image_that_enables_x11_forwarding_for_ssh_sessions) for custom container images"]]