0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

PowerShellでMicrosoft Azure上に仮想マシンを作成した際の作業メモ

Last updated at Posted at 2024-05-29

作成するものの情報

Azure

  • リージョン:東日本
  • リソースグループ名:DEV-RG
  • ネットワークセキュリティグループ名:DEV-NSG
  • ネットワークサブネット名:DEV-Subnet
  • 仮想ネットワーク名:DEV-VNet
  • 仮想マシン
    • 仮想マシン名:CentOS-VM
    • ネットワークインターフェイス名:CentOS-VM-NIC
    • パブリックIPアドレス名:CentOS-VM-PIP
      • 静的IPv4
    • ディスク名:CentOS-VM-OSDisk
    • サイズ:Standard_B2s

環境など

  • 仮想マシン
    • OS:CentOS 7.9
    • ホスト名:CentOS
    • ユーザー一覧
      • azureVMRoot / **********
      • root / **********

PowerShellでの作業

各種情報の事前定義

$prefix        = "DEV";
$rgName         = $prefix + "-RG";
$nsgName        = $prefix + "-NSG";
$subnetName     = $prefix + "-Subnet";
$vnetName       = $prefix + "-VNet";

$machineName    = "CentOS";
$vmName         = $machineName + "-VM";
$nicName        = $machineName + "-VM-NIC";
$pipName        = $machineName + "-VM-PIP";
$osDiskName     = $machineName + "-VM-OSDisk";

$location = "japaneast";

$pipAllocationMethod     = "Static";
$pipVersion              = "IPv4";
$vmSize                  = "Standard_B2s";
$imagePublisher          = "OpenLogic";
$imageOffer              = "CentOS";
$imageSKU                = "7_9-gen2";
$imageVersion            = "latest";
$osDiskSizeInGB          = 64;
$osDiskStrageAccountType = "StandardSSD_LRS";
$username                = "azureVMRoot";
$password                = "**********";

Azureへログイン

Connect-AzAccount;

リソースグループの作成

$rg = New-AzResourceGroup `
  -Location $location `
  -Name     $rgName;

ネットワークサブネットの作成

$subnetConfig = New-AzVirtualNetworkSubnetConfig `
  -Name          $subnetName `
  -AddressPrefix "10.0.2.0/24";

仮想ネットワークの作成

$vnet = New-AzVirtualNetwork `
  -Location          $location `
  -ResourceGroupName $rgName `
  -Name              $vnetName `
  -AddressPrefix     "10.0.0.0/16" `
  -Subnet            $subnetConfig;

パブリックIPアドレスの作成

$pip = New-AzPublicIpAddress `
  -Location          $location `
  -ResourceGroupName $rgName `
  -Name              $pipName `
  -AllocationMethod  $pipAllocationMethod `
  -IpAddressVersion  $pipVersion;

ネットワークセキュリティグループの作成

$nsg = New-AzNetworkSecurityGroup `
  -Location          $location `
  -ResourceGroupName $rgName `
  -Name              $nsgName;

ネットワークインターフェイスの作成

$nic = New-AzNetworkInterface `
  -Location             $location `
  -ResourceGroupName    $rgName `
  -Name                 $nicName `
  -Subnet               $vnet.Subnets[0] `
  -PublicIpAddress      $pip `
  -NetworkSecurityGroup $nsg;

仮想マシンの作成

$image = Get-AzVMImage `
  -Location      $location `
  -PublisherName $imagePublisher `
  -Offer         $imageOffer `
  -Skus          $imageSku `
  -Version       $imageVersion;


$vmConfig = New-AzVMConfig `
  -VMName       $vmName `
  -VMSize       $vmSize `
  -SecurityType "Standard";


$vmConfig = Add-AzVMNetworkInterface `
  -VM $vmConfig `
  -Id $nic.Id;


$vmConfig = Set-AzVMSourceImage `
  -VM            $vmConfig `
  -PublisherName $image[0].PublisherName `
  -Offer         $image[0].Offer `
  -Skus          $image[0].Skus `
  -Version       $image[0].Version;


$password = ConvertTo-SecureString $password -AsPlainText -Force;


$cred = New-Object PSCredential ($username, $password);


$vmConfig = Set-AzVMOperatingSystem `
  -VM           $vmConfig `
  -ComputerName $machineName `
  -Credential   $cred `
  -Linux;


$vmConfig = Set-AzVMOSDisk `
  -VM                 $vmConfig `
  -Name               $osDiskName `
  -CreateOption       fromImage `
  -DiskSizeInGB       $osDiskSizeInGB `
  -StorageAccountType $osDiskStrageAccountType `
  -Linux;


$vm = New-AzVM `
  -ResourceGroupName  $rgName `
  -Location           $location `
  -VM                 $vmConfig `
  -DiagnosticsEnabled $false;

ネットワークセキュリティグループの設定

受信セキュリティ規則を追加

$nsg = Get-AzNetworkSecurityGroup `
  -ResourceGroupName $rgName `
  -Name $nsgName;


$nsg | Add-AzNetworkSecurityRuleConfig `
  -Name                     "Allow_SSH_All" `
  -Description              "Allow_SSH_All" `
  -Protocol                 "Tcp" `
  -Direction                "Inbound" `
  -Priority                 100 `
  -SourceAddressPrefix      "*" `
  -SourcePortRange          "*" `
  -DestinationAddressPrefix "*" `
  -DestinationPortRange     "22" `
  -Access                   "Allow";


$nsg | Set-AzNetworkSecurityGroup;

仮想マシン上での作業

rootユーザーへスイッチ

[azureVMRoot@CentOS ~]$ sudo su -
[root@CentOS ~]# 

rootユーザーのパスワード設定

[root@CentOS ~]# passwd
Changing password for user root.
New password: **********
Retype new password: **********
passwd: all authentication tokens updated successfully.
[root@CentOS ~]# 

初期設定などなど

パッケージのアップデート

[root@CentOS ~]# sudo yum update
Loaded plugins: langpacks
~略~
Is this ok [y/d/N]: y
~略~
Complete!
[root@CentOS ~]# 

SELinuxの無効化

[root@CentOS ~]# getenforce
Enforcing
[root@CentOS ~]# sudo setenforce 0
[root@CentOS ~]# getenforce
Permissive
[root@CentOS ~]# vi /etc/selinux/config


#SELINUX=enforcing
SELINUX=disabled


[root@CentOS ~]# reboot

再起動後

[root@CentOS ~]# getenforce
Disabled
[root@CentOS ~]# 

時刻同期設定

[root@CentOS ~]# date
Wed May 29 03:30:38 UTC 2024
[root@CentOS ~]# sudo yum install -y ntp
Loaded plugins: langpacks
~略~
Complete!
[root@CentOS ~]# sudo systemctl start ntpd
[root@CentOS ~]# sudo systemctl enable ntpd
[root@CentOS ~]# systemctl status ntpd
[root@CentOS ~]# sudo timedatectl set-timezone "Asia/Tokyo"
[root@CentOS ~]# date
Wed May 29 12:31:44 JST 2024
[root@CentOS ~]# 
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?