您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn.

Bash in Azure Cloud Shell 快速入门Quickstart for Bash in Azure Cloud Shell

本文档详细介绍如何在 Azure 门户中使用 Bash in Azure Cloud Shell。This document details how to use Bash in Azure Cloud Shell in the Azure portal.

备注

此外,还提供了 PowerShell in Azure Cloud Shell 快速入门。A PowerShell in Azure Cloud Shell Quickstart is also available.

启动 Cloud ShellStart Cloud Shell

  1. 从 Azure 门户的顶部导航栏启动 Cloud ShellLaunch Cloud Shell from the top navigation of the Azure portal
  2. 选择用于创建存储帐户和 Microsoft Azure 文件共享的订阅Select a subscription to create a storage account and Microsoft Azure Files share
  3. 选择“创建存储”Select "Create storage"

提示

在每个会话中会自动针对 Azure CLI 2.0 对你进行身份验证。You are automatically authenticated for Azure CLI 2.0 in every sesssion.

选择 Bash 环境Select the Bash environment

  1. 在 shell 窗口的左侧选择环境下拉列表Select the environment drop down from the the left hand side of shell window
  2. 选择 BashSelect Bash

设置订阅Set your subscription

  1. 列出可以访问的订阅:List subscriptions you have access to:
    az account list
  2. 设置首选订阅:Set your preferred subscription:
    az account set --subscription my-subscription-name

提示

将使用 /home/<user>/.azure/azureProfile.json 为将来的会话记住订阅。Your subscription will be remembered for future sessions using /home/<user>/.azure/azureProfile.json.

创建资源组Create a resource group

在 WestUS 中创建一个名为“MyRG”的新资源组:Create a new resource group in WestUS named "MyRG":
az group create -l westus -n MyRG

创建 Linux VMCreate a Linux VM

在新资源组中创建 Ubuntu VM。Create an Ubuntu VM in your new resource group. Azure CLI 2.0 将创建 SSH 密钥并使用它们设置虚拟机。The Azure CLI 2.0 will create SSH keys and setup the VM with them.
az vm create -n my_vm_name -g MyRG --image UbuntuLTS --generate-ssh-keys

备注

默认情况下,用于对 VM 进行身份验证的公钥和私钥由 Azure CLI 2.0 置于 /User/.ssh/id_rsa/User/.ssh/id_rsa.pub 中。The public and private keys used to authenticate your VM are placed in /User/.ssh/id_rsa and /User/.ssh/id_rsa.pub by Azure CLI 2.0 by default. .ssh 文件夹保存在附加的 Azure 文件共享的 5 GB 映像中。Your .ssh folder is persisted in your attached Azure Files share's 5-GB image.

此 VM 中的用户名将是你在 Cloud Shell 中使用的用户名 ($User@Azure:)。Your username on this VM will be your username used in Cloud Shell ($User@Azure:).

通过 SSH 连接到 Linux VMSSH into your Linux VM

  1. 在 Azure 门户搜索栏中搜索 VM 名称Search for your VM name in the Azure portal search bar
  2. 单击“连接”并运行:ssh username@ipaddressClick "Connect" and run: ssh username@ipaddress

建立 SSH 连接后,应看到 Ubuntu 欢迎提示。Upon establishing the SSH connection, you should see the Ubuntu welcome prompt.

清理Cleaning up

删除资源组及其中的任何资源:Delete your resource group and any resources within it:
运行 az group delete -n MyRGRun az group delete -n MyRG

后续步骤Next steps

了解如何为 Bash in Cloud Shell 持久保存文件Learn about persisting files for Bash in Cloud Shell
了解 Azure CLI 2.0 Learn about Azure CLI 2.0
了解 Azure 文件存储 Learn about Azure Files storage