
NSGの追加・変更・削除(Azure CLI)
Azure CLIを使えば、NSGのメンテナンスを効率よく実施できます。よく使うコマンドを記載します。
登録されているNSGをすべて一覧出力する場合
az network nsg list --output table
登録されているNSGをすべて詳細に出力する場合
for nsg in $(az network nsg list --query "[].name" -o tsv); do
echo "NSG: $nsg"
az network nsg rule list \
--resource-group <リソースグループ名> \
--nsg-name $nsg \
--output table
カスタム設定のルールを追加する場合
az network nsg rule create \
--resource-group <リソースグループ名> \
--nsg-name <NSG名> \
--name <ルール名> \
--priority <優先度 (100〜4096)> \
--direction <Inbound|Outbound> \
--access <Allow|Deny> \
--protocol <Tcp|Udp|* (すべて)> \
--source-address-prefixes <送信元IPまたはCIDR> \
--source-port-ranges <送信元ポート> \
--destination-address-prefixes <宛先IPまたはCIDR> \
--destination-port-ranges <宛先ポート> \
--description "<説明>"
サービスタグをルールに追加する場合
例として「MicrosoftDefenderForEndpoint 」を設定
az network nsg rule create \
--resource-group MyResourceGroup \
--nsg-name MyNSG \
--name AllowDefenderForEndpoint \
--priority 200 \
--direction Inbound \
--access Allow \
--protocol Tcp \
--source-address-prefixes MicrosoftDefenderForEndpoint \
--source-port-ranges '*' \
--destination-address-prefixes '*' \
--destination-port-ranges 443 \
--description "Allow inbound traffic from Microsoft Defender for Endpoint on port 443"
ルールの内容を一部更新する場合
az network nsg rule update \
--resource-group <リソースグループ名> \
--nsg-name <NSG名> \
--name <ルール名> \
--priority <新しい優先度> \
--access <Allow|Deny> \
--description "<新しい説明>"
ルールを削除する場合
az network nsg rule delete \
--resource-group <リソースグループ名> \
--nsg-name <NSG名> \
--name <削除するルール名>