快速入门:在 Azure 门户中创建 Linux 虚拟机Quickstart: Create a Linux virtual machine in the Azure portal

可以通过 Azure 门户创建 Azure 虚拟机 (VM)。Azure virtual machines (VMs) can be created through the Azure portal. Azure 门户是基于浏览器的用户界面,用于创建 Azure 资源。The Azure portal is a browser-based user interface to create Azure resources. 本快速入门介绍如何使用 Azure 门户部署运行 Ubuntu 18.04 LTS 的 Linux 虚拟机 (VM)。This quickstart shows you how to use the Azure portal to deploy a Linux virtual machine (VM) running Ubuntu 18.04 LTS. 若要查看运行中的 VM,也可以通过 SSH 登录到该 VM 并安装 NGINX Web 服务器。To see your VM in action, you also SSH to the VM and install the NGINX web server.

如果没有 Azure 订阅,可在开始前创建一个试用帐户If you don't have an Azure subscription, create a trial account before you begin.

创建 SSH 密钥对Create SSH key pair

需要一个 SSH 密钥对才能完成本快速入门。You need an SSH key pair to complete this quickstart. 如果已有一个 SSH 密钥对,则可以跳过此步骤。If you already have an SSH key pair, you can skip this step.

打开 bash shell,使用 ssh-keygen 创建一个 SSH 密钥对。Open a bash shell and use ssh-keygen to create an SSH key pair.

  1. 在本地计算机上打开 bash shell。Open the bash shell on your local computer.
  2. 键入 ssh-keygen -t rsa -b 2048 以创建 ssh 密钥。Type ssh-keygen -t rsa -b 2048 to create the ssh key.
  3. 系统将提示你输入用于保存密钥对的文件。You will be prompted to enter a file in which to save the key pair. 只需按 Enter 即可保存到括号中列出的默认位置。Just press Enter to save in the default location, listed in brackets.
  4. 系统会要求你输入密码。You will be asked to enter a passphrase. 你可以为 SSH 密钥键入密码,或按 Enter 继续而不键入密码。You can type a passphrase for your SSH key or press Enter to continue without a passphrase.
  5. ssh-keygen 命令会在 ~/.ssh directory 中生成默认名称为 id_rsa 的公钥和私钥。The ssh-keygen command generates public and private keys with the default name of id_rsa in the ~/.ssh directory. 此命令返回公钥的完整路径。The command returns the full path to the public key. 通过键入 cat ~/.ssh/id_rsa.pub 使用公钥的此路径以 cat 显示其内容。Use the path to the public key to display its contents with cat by typing cat ~/.ssh/id_rsa.pub.
  6. 复制此命令的输出,并将其保存到某个位置,以供本文后面使用。Copy the output of this command and save it somewhere to use later in this article. 这是你的公钥,在配置登录 VM 所需的管理员帐户时,需要用到它。This is your public key and you will need it when configuring your administrator account to log in to your VM.

登录 AzureSign in to Azure

如果尚未登录到 Azure 门户,请进行登录。Sign in to the Azure portal if you haven't already.

创建虚拟机Create virtual machine

  1. 在 Azure 门户的左上角,选择“创建资源” 。Choose Create a resource in the upper left corner of the Azure portal.

  2. 在“新建” 页的搜索框中,搜索并选择 Canonical 的“Ubuntu Server 18.04 LTS” ,然后选择“创建” 。In the search box of New page, search for and select Ubuntu Server 18.04 LTS by Canonical, then select Create.

  3. 在“基本信息” 选项卡中的“项目详细信息” 下,确保选择了正确的订阅,然后在“资源组”下选择“新建” 。In the Basics tab, under Project details, make sure the correct subscription is selected and then choose to Create new under Resource group. 键入 myResourceGroup 作为资源组的名称,然后选择“确定” 。Type myResourceGroup for the name of the resource group and then choose OK.

    为 VM 创建新的资源组

  4. 在“实例详细信息” 下,对于“虚拟机名称” 键入“myVM” ,对于“区域” 选择“中国东部” 。Under Instance details, type myVM for the Virtual machine name and choose China East for your Region. 保留其他默认值。Leave the other defaults.

    “实例详细信息”部分

  5. 在“管理员帐户” 下,选择“SSH 公钥” ,键入用户名,然后粘贴公钥。Under Administrator account, select SSH public key, type your user name, then paste in your public key. 删除公钥中的任何前导或尾随空格。Remove any leading or trailing white space in your public key.

    管理员帐户

  6. 在“入站端口规则” > “公共入站端口”下,选择“允许所选端口”,然后从下拉列表中选择“SSH (22)”和“HTTP (80)”。 Under Inbound port rules > Public inbound ports, choose Allow selected ports and then select SSH (22) and HTTP (80) from the drop-down.

    为 RDP 和 HTTP 打开端口

  7. 保留剩余的默认值,然后选择页面底部的“查看 + 创建” 按钮。Leave the remaining defaults and then select the Review + create button at the bottom of the page.

  8. 在“创建虚拟机”页上, 可以查看要创建的 VM 的详细信息。On the Create a virtual machine page, you can see the details about the VM you are about to create. 准备好以后,选择“创建” 。When you are ready, select Create.

部署 VM 需要数分钟。It will take a few minutes for your VM to be deployed. 部署完成后,请转到下一部分。When the deployment is finished, move on to the next section.

连接到虚拟机Connect to virtual machine

创建与 VM 的 SSH 连接。Create an SSH connection with the VM.

  1. 选择 VM 的概述页面上的“连接”按钮。 Select the Connect button on the overview page for your VM.

    门户 9

  2. 在“连接到虚拟机”页面中,请保留默认选项,以使用 IP 地址通过端口 22 进行连接。 In the Connect to virtual machine page, keep the default options to connect by IP address over port 22. 在“使用 VM 本地帐户登录” 中,将显示一个连接命令。In Login using VM local account a connection command is shown. 选择相应的按钮来复制该命令。Select the button to copy the command. 下面的示例展示了 SSH 连接命令的样式:The following example shows what the SSH connection command looks like:

    ssh azureuser@10.111.12.123
    
  3. 使用创建 SSH 密钥对时使用过的 bash shell(例如本地 bash shell)将 SSH 连接命令粘贴到 shell 中,以便创建一个 SSH 会话。Using the same bash shell you used to create your SSH key pair (like your local bash shell), paste the SSH connection command into the shell to create an SSH session.

安装 Web 服务器Install web server

若要查看运行中的 VM,请安装 NGINX Web 服务器。To see your VM in action, install the NGINX web server. 在 SSH 会话中更新包源,然后安装最新的 NGINX 包。From your SSH session, update your package sources and then install the latest NGINX package.

sudo apt-get -y update
sudo apt-get -y install nginx

完成后,键入 exit 以离开 SSH 会话。When done, type exit to leave the SSH session.

查看运行中的 Web 服务器View the web server in action

使用所选的 Web 浏览器查看默认的 NGINX 欢迎页。Use a web browser of your choice to view the default NGINX welcome page. 键入 VM 的公共 IP 地址作为 Web 地址。Type the public IP address of the VM as the web address. 可以在 VM 概览页上或此前使用过的 SSH 连接字符串中找到公共 IP 地址。The public IP address can be found on the VM overview page or as part of the SSH connection string you used earlier.

NGINX 默认站点

清理资源Clean up resources

当不再需要时,可以删除资源组、虚拟机和所有相关资源。When no longer needed, you can delete the resource group, virtual machine, and all related resources. 为此,请选择虚拟机的资源组,选择“删除” ,然后确认要删除的资源组的名称。To do so, select the resource group for the virtual machine, select Delete, then confirm the name of the resource group to delete.

后续步骤Next steps

在本快速入门中,你部署了一台简单的虚拟机、一条网络安全组规则组和规则,并安装了一台基本 Web 服务器。In this quickstart, you deployed a simple virtual machine, created a Network Security Group and rule, and installed a basic web server. 若要详细了解 Azure 虚拟机,请继续学习 Linux VM 的教程。To learn more about Azure virtual machines, continue to the tutorial for Linux VMs.