2
1

More than 3 years have passed since last update.

Azure CLI で作成した Windows10 VM にRDPしてみました

Posted at

概要

Azure Portal からではなく、Azure CLI を用いて Windows10 pro を作成し、手元の PC から RDP 接続する手順となります。

ローカル環境

macOS Big Sur 11.3
python 3.8.3

前提条件

  1. Azure環境がすでに用意されていること(テナント/サブスクリプション)。
  2. ローカル環境に「azure cli」がインストールされていること。

事前準備

  • ローカルの環境変数に以下を定義しておきます。
    • ACR_RES_GROUP=rg_ituru_acr

azure cli バージョンの確認

$ az version                    

{
  "azure-cli": "2.26.1",
  "azure-cli-core": "2.26.1",
  "azure-cli-telemetry": "1.0.6",
  "extensions": {
    "azure-iot": "0.10.13"
  }
}

事前の仕込み

使用するテナントのAzure環境へのログイン

$ az login --tenant <tenant_id>

使用サブスクリプションを定義します

$ az account set --subscription '<サブスクリプション名>'

リソースグループの作成

$ az group create --resource-group $AKS_RES_GROUP --location japaneast

仮想ネットワークを作成

$ az network vnet create -g $AKS_RES_GROUP -n vnet_ituru_akscp --address-prefix 10.0.0.0/16 --subnet-name akscpsubnet1 --subnet-prefix 10.0.1.0/24

構築する Windows10 pro 仮想マシン の URN の取得

$ az vm image list --location japaneast --offer Windows-10 --sku 21h1-pro --all --output table

Offer       Publisher                Sku                Urn                                                                         Version
----------  -----------------------  -----------------  --------------------------------------------------------------------------  ---------------------
Windows-10  MicrosoftWindowsDesktop  21h1-pro           MicrosoftWindowsDesktop:Windows-10:21h1-pro:19043.1110.2107101729           19043.1110.2107101729
Windows-10  MicrosoftWindowsDesktop  21h1-pro           MicrosoftWindowsDesktop:Windows-10:21h1-pro:19043.1165.2108031819           19043.1165.2108031819
Windows-10  MicrosoftWindowsDesktop  21h1-pro-g2        MicrosoftWindowsDesktop:Windows-10:21h1-pro-g2:19043.1110.2107101729        19043.1110.2107101729
Windows-10  MicrosoftWindowsDesktop  21h1-pro-g2        MicrosoftWindowsDesktop:Windows-10:21h1-pro-g2:19043.1165.2108031819        19043.1165.2108031819
Windows-10  MicrosoftWindowsDesktop  21h1-pro-zh-cn     MicrosoftWindowsDesktop:Windows-10:21h1-pro-zh-cn:19043.1110.2107101729     19043.1110.2107101729
Windows-10  MicrosoftWindowsDesktop  21h1-pro-zh-cn     MicrosoftWindowsDesktop:Windows-10:21h1-pro-zh-cn:19043.1165.2108031819     19043.1165.2108031819
Windows-10  MicrosoftWindowsDesktop  21h1-pro-zh-cn-g2  MicrosoftWindowsDesktop:Windows-10:21h1-pro-zh-cn-g2:19043.1110.2107101729  19043.1110.2107101729
Windows-10  MicrosoftWindowsDesktop  21h1-pro-zh-cn-g2  MicrosoftWindowsDesktop:Windows-10:21h1-pro-zh-cn-g2:19043.1165.2108031819  19043.1165.2108031819
Windows-10  MicrosoftWindowsDesktop  21h1-pron          MicrosoftWindowsDesktop:Windows-10:21h1-pron:19043.1110.2107101729          19043.1110.2107101729
Windows-10  MicrosoftWindowsDesktop  21h1-pron          MicrosoftWindowsDesktop:Windows-10:21h1-pron:19043.1165.2108031819          19043.1165.2108031819
Windows-10  MicrosoftWindowsDesktop  21h1-pron-g2       MicrosoftWindowsDesktop:Windows-10:21h1-pron-g2:19043.1110.2107101729       19043.1110.2107101729
Windows-10  MicrosoftWindowsDesktop  21h1-pron-g2       MicrosoftWindowsDesktop:Windows-10:21h1-pron-g2:19043.1165.2108031819       19043.1165.2108031819

仮想マシン(VM)

仮想マシンの作成

  • 作成する Windows10 Pro 仮想マシン
    • 名前:win10nmcituru
    • リソースグループ:rg_ituru_aks_cni
    • イメージ URN:MicrosoftWindowsDesktop:Windows-10:21h1-pro:19043.1165.2108031819
    • 仮想ネットワーク:vnet_ituru_akscp
    • サブネットワーク:akscpsubnet1
    • ログインユーザ名:nmcadmin
    • ログインパスワード:Password123!
    • 仮想マシンサイズ:Standard_D2_v2
    • ストレージ:StandardSSD_LRS
$ az vm create -n win10nmcituru -g $AKS_RES_GROUP \
    --image MicrosoftWindowsDesktop:Windows-10:21h1-pro:19043.1165.2108031819 \
    --vnet-name vnet_ituru_akscp --subnet akscpsubnet1 \
    --admin-username nmcadmin --admin-password Password123! \
    --size Standard_D2_v2 --storage-sku StandardSSD_LRS

{
  "fqdns": "",
  "id": "/subscriptions/eeeeeeee-1717-dada-977a-6654eee51113/resourceGroups/rg_ituru_aks_cni/providers/Microsoft.Compute/virtualMachines/win10nmcituru",
  "location": "japaneast",
  "macAddress": "00-22-48-67-01-11",
  "powerState": "VM running",
  "privateIpAddress": "10.0.1.99",
  "publicIpAddress": "20.89.96.83",
  "resourceGroup": "rg_ituru_aks_cni",
  "zones": ""
}

ネットワーク・セキュリティ・グループ (NSG)

仮想マシン作成後 RDP 用の NSG ルール を追加します

ローカル端末の外部アクセスIPアドレスの取得

$ MY_IP=$(curl inet-ip.info)

$ echo $MY_IP
155.123.22.111

NSG の名前取得

$ NSG_NAME=$(az network nsg list -g $AKS_RES_GROUP --query '[].name' -o tsv)

$ echo $NSG_NAME
win10nmcituruNSG

仮想マシン作成時に自動生成される NSG ルールの取得と削除

## ルールの取得
$ az network nsg rule list -g $AKS_RES_GROUP --nsg-name $NSG_NAME -o table

Name    ResourceGroup     Priority    SourcePortRanges    SourceAddressPrefixes    SourceASG    Access    Protocol    Direction    DestinationPortRanges    DestinationAddressPrefixes    DestinationASG
------  ----------------  ----------  ------------------  -----------------------  -----------  --------  ----------  -----------  -----------------------  ----------------------------  ----------------
rdp     rg_ituru_aks_cni  1000        *                   *                        None         Allow     Tcp         Inbound      3389                     *                             None

## ルールの削除
$ az network nsg rule delete -g $AKS_RES_GROUP --nsg-name $NSG_NAME -n rdp

仮想マシンへの RDP 接続ルールの追加

$ az network nsg rule create \
    --resource-group $AKS_RES_GROUP \
    --name RDP_01 \
    --nsg-name $NSG_NAME \
    --priority 1001 \
    --source-address-prefixes $MY_IP/32 \
    --destination-port-ranges 3389 \
    --access Allow \
    --protocol Tcp

作成ルールの確認 (NSGルールの取得)

$ az network nsg rule list -g $AKS_RES_GROUP --nsg-name $NSG_NAME -o table

Name    ResourceGroup     Priority    SourcePortRanges    SourceAddressPrefixes    SourceASG    Access    Protocol    Direction    DestinationPortRanges    DestinationAddressPrefixes    DestinationASG
------  ----------------  ----------  ------------------  -----------------------  -----------  --------  ----------  -----------  -----------------------  ----------------------------  ----------------
RDP_01  rg_ituru_aks_cni  1001        *                   155.123.22.111/32        None         Allow     Tcp         Inbound      3389                     *                             None

仮想マシンへの接続

RDP接続

仮想マシンへの RDP接続については、この記事 を参考にして実施ください。

接続(ログイン)後の確認

コンソールを立ち上げ、OS名やバージョン、ネットワーク情報等を確認します

C:\Users\nmcadmin> systeminfo | findstr OS
OS Name:                   Microsoft Windows 10 Pro
OS Version:                10.0.19043 N/A Build 19043
OS Manufacturer:           Microsoft Corporation
OS Configuration:          Standalone Workstation
OS Build Type:             Multiprocessor Free
BIOS Version:              American Megatrends Inc. 090008 , 12/7/2018

C:\Users\nmcadmin> ipconfig
Windows IP Configuration
Ethernet adapter Ethernet 2:
   Connection-specific DNS Suffix  . : hogehogehogehogehoge.lx.internal.cloudapp.net
   Link-local IPv6 Address . . . . . : ff88::a55a:e4e4:9363:4a4a%7
   IPv4 Address. . . . . . . . . . . : 10.0.1.99
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   Default Gateway . . . . . . . . . : 10.0.1.1

まとめ

Azure Portal からやるのもよいですが、Azure CLI もいいですね。


付録

仮想マシンの停止・開始・削除

$ az vm stop --name <仮想マシン名> -g <リソースグループ名>
$ az vm start --name <仮想マシン名> -g <リソースグループ名>
$ az vm delete --name <仮想マシン名> -g <リソースグループ名>

仮想マシンのステータス確認

$ az vm list -g <リソースグループ名> -o table -d

## VM停止時
Name           ResourceGroup     PowerState      PublicIps    Fqdns    Location    Zones
-------------  ----------------  --------------  -----------  -------  ----------  -------
win10nmcituru  rg_ituru_aks_cni  VM deallocated                        japaneast

## VM稼働時
Name           ResourceGroup     PowerState    PublicIps      Fqdns    Location    Zones
-------------  ----------------  ------------  -------------  -------  ----------  -------
win10nmcituru  rg_ituru_aks_cni  VM running    20.194.239.27           japaneast

参考記事

以下の記事を参考にさせていただきました。感謝申し上げます。
- Windows Server 2022 Preview を Azure CLI で作成する
- サブスクリプション内の NSG ルール一覧を取得する Azure CLI

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1