為 Azure 檔案共用啟用 AD DS 驗證

本文說明在儲存體帳戶上啟用 Active Directory Domain Services (AD DS) 驗證的程序,以便使用內部部署 Active Directory (AD) 認證來驗證 Azure 檔案共用。

重要

啟用 AD DS 驗證之前,請確定您已了解概觀文章中支援的案例和需求,並完成必要的必要條件。 如果您的 Active Directory 環境跨越多個樹系,請參閱搭配多個 Active Directory 樹系使用 Azure 檔案儲存體

若要針對 Azure 檔案共用啟用透過 SMB 進行 AD DS 驗證,您需要向內部部署 AD DS 註冊您的 Azure 儲存體帳戶,然後在儲存體帳戶上設定所需的網域屬性。 若要向 AD DS 註冊您的儲存體帳戶,您要在 AD DS 中建立代表它的電腦帳戶 (或服務登入帳戶)。 將此流程視為您在 AD DS 中建立代表內部部署 Windows 檔案伺服器的帳戶。 在儲存體帳戶上啟用此功能時,會套用至帳戶中的所有新的和現有的檔案共用。

適用於

檔案共用類型 SMB NFS
標準檔案共用 (GPv2)、LRS/ZRS Yes No
標準檔案共用 (GPv2)、GRS/GZRS Yes No
進階檔案共用 (FileStorage)、LRS/ZRS Yes No

AzFilesHybrid PowerShell 模組提供用於部署和設定 Azure 檔案儲存體的 Cmdlet。 其中包含將儲存體帳戶加入內部部署 Active Directory 和設定 DNS 伺服器的網域 Cmdlet。 Cmdlet 會進行必要的修改,並為您啟用此功能。 因為 Cmdlet 的某些部分會與您的內部部署 AD DS 互動,所以我們會說明 Cmdlet 的功能,因此您可以判斷這些變更是否符合您的合規性和安全性原則,並確保您有適當的權限可以執行 Cmdlet。 雖然我們建議使用 AzFilesHybrid 模組,但如果您無法這麼做,我們會提供手動步驟

重要

AES-256 Kerberos 加密現在是 AzFilesHybrid 模組支援的唯一加密方法。 如果您想要使用 RC4 加密,請參閱選項二:手動執行啟用動作。 如果您先前使用舊版 AzFilesHybrid 版本 (低於 v0.2.2) 來啟用此功能,該版本使用 RC4 作為預設加密方法,並想要更新以支援 AES-256,請參閱針對 Azure 檔案儲存體 SMB 驗證進行疑難排解

必要條件

下載 AzFilesHybrid 模組

下載並解壓縮 AzFilesHybrid 模組的最新版本

執行 Join-AzStorageAccount

Join-AzStorageAccount Cmdlet 會代表指定的儲存體帳戶執行對等的離線網域聯結。 下方指令碼會使用 Cmdlet 在您的 AD 網域中建立 電腦帳戶。 如果基於任何原因而無法使用電腦帳戶,您可以改為變更指令碼來建立服務登入帳戶。 從 AzFilesHybrid 0.2.5 版開始,支援搭配服務登入帳戶使用 AES-256 加密。

Cmdlet 所建立的 AD DS 帳戶代表儲存體帳戶。 如果 AD DS 帳戶是在強制密碼到期的組織單位 (OU) 下建立,您必須在密碼最長使用期限之前更新密碼。 在該日期之前無法更新帳戶密碼會導致驗證在存取 Azure 檔案共用時失敗。 若要了解如何更新密碼,請參閱更新 AD DS 帳戶密碼

重要

Join-AzStorageAccount Cmdlet 會建立 AD 帳戶,以代表 AD 中的儲存體帳戶 (檔案共用)。 您可以選擇註冊為電腦帳戶或服務登入帳戶,請參閱常見問題以取得詳細資料。 如果 AD 網域或 OU 上已設定預設密碼到期日,則服務登入帳戶密碼可能在 AD 中到期。 由於電腦帳戶密碼變更是由用戶端電腦所驅動,而不是 AD,因此不會在 AD 中過期,即使用戶端電腦預設每隔 30 天變更密碼。 針對這兩種帳戶類型,建議您檢查設定的密碼到期時間,並規劃在密碼最長使用期限之前,更新 AD 帳戶的儲存體帳戶身分識別密碼。 您可以考慮在 AD 中建立新的 AD 組織單位,並據以停用電腦帳戶或服務登入帳戶上的密碼到期原則。

您必須在已加入內部部署 AD DS 網域的裝置上,於 PowerShell 5.1 中執行下列指令碼,並使用具有權限在目標 AD 中建立電腦帳戶或服務登入帳戶的內部部署 AD DS 認證 (例如網域管理員)。 若要遵循最低權限原則,內部部署 AD DS 認證必須具有下列 Azure 角色:

  • 目標儲存體帳戶所在資源群組上的讀取者
  • 要加入 AD DS 之儲存體帳戶上的參與者

如果用來在 AD DS 中加入儲存體帳戶的帳戶是目標資源所在 Azure 訂用帳戶中的擁有者參與者,則該帳戶已啟用以執行聯結,而且不需要進一步指派。

AD DS 認證也必須擁有權限,才能在目標 AD 中建立電腦帳戶或服務登入帳戶。 在執行指令碼之前,請將預留位置值取代為您自己的值。

# Change the execution policy to unblock importing AzFilesHybrid.psm1 module
Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Scope CurrentUser

# Navigate to where AzFilesHybrid is unzipped and stored and run to copy the files into your path
.\CopyToPSPath.ps1 

# Import AzFilesHybrid module
Import-Module -Name AzFilesHybrid

# Login to Azure using a credential that has either storage account owner or contributor Azure role 
# assignment. If you are logging into an Azure environment other than Public (ex. AzureUSGovernment) 
# you will need to specify that.
# See https://learn.microsoft.com/azure/azure-government/documentation-government-get-started-connect-with-ps
# for more information.
Connect-AzAccount

# Define parameters
# $StorageAccountName is the name of an existing storage account that you want to join to AD
# $SamAccountName is the name of the to-be-created AD object, which is used by AD as the logon name 
# for the object. It must be 15 characters or less and has certain character restrictions.
# Make sure that you provide the SamAccountName without the trailing '$' sign.
# See https://learn.microsoft.com/windows/win32/adschema/a-samaccountname for more information.
$SubscriptionId = "<your-subscription-id-here>"
$ResourceGroupName = "<resource-group-name-here>"
$StorageAccountName = "<storage-account-name-here>"
$SamAccountName = "<sam-account-name-here>"
$DomainAccountType = "<ComputerAccount|ServiceLogonAccount>" # Default is set as ComputerAccount
# If you don't provide the OU name as an input parameter, the AD identity that represents the 
# storage account is created under the root directory.
$OuDistinguishedName = "<ou-distinguishedname-here>"
# Encryption method is AES-256 Kerberos.

# Select the target subscription for the current session
Select-AzSubscription -SubscriptionId $SubscriptionId 

# Register the target storage account with your active directory environment under the target OU 
# (for example: specify the OU with Name as "UserAccounts" or DistinguishedName as 
# "OU=UserAccounts,DC=CONTOSO,DC=COM"). You can use this PowerShell cmdlet: Get-ADOrganizationalUnit 
# to find the Name and DistinguishedName of your target OU. If you are using the OU Name, specify it 
# with -OrganizationalUnitName as shown below. If you are using the OU DistinguishedName, you can set it 
# with -OrganizationalUnitDistinguishedName. You can choose to provide one of the two names to specify 
# the target OU. You can choose to create the identity that represents the storage account as either a 
# Service Logon Account or Computer Account (default parameter value), depending on your AD permissions 
# and preference. Run Get-Help Join-AzStorageAccountForAuth for more details on this cmdlet.

Join-AzStorageAccount `
        -ResourceGroupName $ResourceGroupName `
        -StorageAccountName $StorageAccountName `
        -SamAccountName $SamAccountName `
        -DomainAccountType $DomainAccountType `
        -OrganizationalUnitDistinguishedName $OuDistinguishedName

# You can run the Debug-AzStorageAccountAuth cmdlet to conduct a set of basic checks on your AD configuration 
# with the logged on AD user. This cmdlet is supported on AzFilesHybrid v0.1.2+ version. For more details on 
# the checks performed in this cmdlet, see Azure Files Windows troubleshooting guide.
Debug-AzStorageAccountAuth -StorageAccountName $StorageAccountName -ResourceGroupName $ResourceGroupName -Verbose

選項二:手動執行啟用動作

大部分的客戶都應該選擇上述選項一,並使用 AzFilesHybrid PowerShell 模組來啟用 Azure 檔案儲存體的 AD DS 驗證。 不過,如果您想要使用 Active Directory PowerShell 手動執行步驟,在此會概述那些步驟。

重要

如果您已成功執行上述 Join-AzStorageAccount 指令碼,請直接移至確認功能已啟用區段。 您不需要執行下列手動步驟。

檢查環境

首先,請檢查環境的狀態。

  • 檢查是否已安裝 Active Directory PowerShell,以及是否以系統管理員權限執行殼層。
  • 請確定已安裝 Az.Storage 模組,如果未安裝,請加以安裝。 您至少需要 2.0 版。
  • 完成這些檢查之後,請檢查您的 AD DS,查看是否有電腦帳戶 (預設) ,或已使用 "cifs/your-storage-account-name-here.file.core.windows.net" 等 SPN/UPN 建立的服務登入帳戶。 如果帳戶不存在,請建立一個帳戶,如下節所述。

重要

本節中的 Windows Server Active Directory PowerShell Cmdlet 必須在 Windows PowerShell 5.1 中執行。 PowerShell 7.x 和 Azure Cloud Shell 在此案例中無法運作。

以手動方式建立代表 AD 儲存體帳戶的身分識別

若要手動建立此帳戶,請先為您的儲存體帳戶建立新的 Kerberos 金鑰,然後使用下列 PowerShell Cmdlet 取得存取金鑰。 此金鑰只會在安裝期間使用。 其不能用於針對儲存體帳戶進行的任何控制或資料平面作業。

# Create the Kerberos key on the storage account and get the Kerb1 key as the password for the AD identity 
# to represent the storage account
$ResourceGroupName = "<resource-group-name-here>"
$StorageAccountName = "<storage-account-name-here>"

New-AzStorageAccountKey -ResourceGroupName $ResourceGroupName -Name $StorageAccountName -KeyName kerb1
Get-AzStorageAccountKey -ResourceGroupName $ResourceGroupName -Name $StorageAccountName -ListKerbKey | where-object{$_.Keyname -contains "kerb1"}

Cmdlet 應該會傳回金鑰值。 擁有 kerb1 金鑰後,請在您 OU 下的 AD 中建立電腦帳戶服務帳戶,並使用金鑰作為 AD 身分識別的密碼。

  1. 在 AD GUI 中,或以管理員身分從 Windows 命令列執行 Setspn 命令,將 SPN 設定為 cifs/your-storage-account-name-here.file.core.windows.net (請記得將範例文字取代為您的儲存體帳戶名稱,並將 <ADAccountName> 取代為您的 AD 帳戶名稱)。

    Setspn -S cifs/your-storage-account-name-here.file.core.windows.net <ADAccountName>
    
  2. 如果您有使用者帳戶,請修改 UPN 以符合 AD 物件的 SPN (您必須已安裝 AD PowerShell Cmdlet,並執行 PowerShell 5.1 中具有更高權限的 Cmdlet)。

    Set-ADUser -Identity $UserSamAccountName -UserPrincipalName cifs/<StorageAccountName>.file.core.windows.net@<DNSRoot>
    
  3. 將 AD 帳戶密碼設定為 kerb1 金鑰的值。

    Set-ADAccountPassword -Identity servername$ -Reset -NewPassword (ConvertTo-SecureString -AsPlainText "kerb1_key_value_here" -Force)
    

如果您的 OU 強制密碼到期,必須在密碼最長使用期限之前更新密碼,以避免在存取 Azure 檔案共用時發生驗證失敗。 請參閱在 AD 中更新儲存體帳戶身分識別的密碼以取得詳細資料。

保留新建立之身分識別的 SID,您將在下一個步驟中需要用到。 您所建立代表儲存體帳戶的身分識別不需要同步至 Microsoft Entra ID。

在您的儲存體帳戶上啟用此功能

修改下列命令,以在下列命令中包含網域屬性的設定詳細資料,然後執行以啟用此功能。 下列命令所需的儲存體帳戶 SID,是您在上一節的 AD DS 中建立之身分識別的 SID。 請確定您提供 ActiveDirectorySamAccountName 屬性,但不包含委後置的 '$' 符號。

# Set the feature flag on the target storage account and provide the required AD domain information
Set-AzStorageAccount `
        -ResourceGroupName "<your-resource-group-name>" `
        -Name "<your-storage-account-name>" `
        -EnableActiveDirectoryDomainServicesForFile $true `
        -ActiveDirectoryDomainName "<your-domain-dns-root>" `
        -ActiveDirectoryNetBiosDomainName "<your-domain-dns-root>" `
        -ActiveDirectoryForestName "<your-forest-name>" `
        -ActiveDirectoryDomainGuid "<your-guid>" `
        -ActiveDirectoryDomainsid "<your-domain-sid>" `
        -ActiveDirectoryAzureStorageSid "<your-storage-account-sid>" `
        -ActiveDirectorySamAccountName "<your-domain-object-sam-account-name>" `
        -ActiveDirectoryAccountType "<your-domain-object-account-type, the value could be 'Computer' or 'User'>"

若要啟用 AES-256 加密,請遵循本節中的步驟進行。 如果您打算使用 RC4 加密,請跳過本節。

重要

若要啟用 AES-256 加密,代表您儲存體帳戶的網域對象必須是內部部署 AD 網域中的電腦帳戶 (預設) 或服務登入帳戶。 如果您的網域物件不符合這項需求,請刪除,然後建立符合需求的新網域物件。 此外,您必須具有物件 msDS-SupportedEncryptionTypes 屬性的寫入權限。

您執行以設定 AES-256 支援的 Cmdlet 取決於代表儲存體帳戶的網域物件是否為電腦帳戶或服務登入帳戶 (使用者帳戶)。 無論如何,您必須安裝 AD PowerShell Cmdlet,並在 PowerShell 5.1 中以較高的權限執行 Cmdlet。

若要在電腦帳戶上啟用 AES-256 加密,請執行下列命令。 以您自己的值取代 <domain-object-identity><domain-name>

Set-ADComputer -Identity <domain-object-identity> -Server <domain-name> -KerberosEncryptionType "AES256"

若要在服務登入帳戶上啟用 AES-256 加密,請執行下列命令。 以您自己的值取代 <domain-object-identity><domain-name>

Set-ADUser -Identity <domain-object-identity> -Server <domain-name> -KerberosEncryptionType "AES256"

執行上方 Cmdlet 之後,請將下列指令碼中的 <domain-object-identity> 取代為您的值,然後執行指令碼以重新整理您的網域物件密碼:

$KeyName = "kerb1" # Could be either the first or second kerberos key, this script assumes we're refreshing the first
$KerbKeys = New-AzStorageAccountKey -ResourceGroupName $ResourceGroupName -Name $StorageAccountName -KeyName $KeyName
$KerbKey = $KerbKeys.keys | Where-Object {$_.KeyName -eq $KeyName} | Select-Object -ExpandProperty Value
$NewPassword = ConvertTo-SecureString -String $KerbKey -AsPlainText -Force

Set-ADAccountPassword -Identity <domain-object-identity> -Reset -NewPassword $NewPassword

重要

如果您先前使用 RC4 加密並將儲存體帳戶更新為使用 AES-256,您應該在用戶端上執行 klist purge,然後重新掛接檔案共用,以使用 AES-256 取得新的 Kerberos 票證。

偵錯

您可以視需要執行 Debug-AzStorageAccountAuth Cmdlet,利用已登入的 AD 使用者對 AD 設定執行一套基本檢查。 AzFilesHybrid v0.1.2+ 版本和更高版本均支援此 Cmdlet。 此 Cmdlet 僅適用於 AD DS 驗證。 它不適用於已啟用 Microsoft Entra Domain Services 或 Microsoft Entra Kerberos 的儲存體帳戶。 如需此 Cmdlet 中所執行檢查的詳細資訊,請參閱無法使用 AD 認證掛接 Azure 檔案共用

Debug-AzStorageAccountAuth -StorageAccountName $StorageAccountName -ResourceGroupName $ResourceGroupName -Verbose

確認已啟用功能

您可以使用下列指令碼,檢查以確認您的儲存體帳戶是否已啟用 Active Directory:

# Get the target storage account
$storageaccount = Get-AzStorageAccount `
        -ResourceGroupName "<your-resource-group-name-here>" `
        -Name "<your-storage-account-name-here>"

# List the directory service of the selected service account
$storageAccount.AzureFilesIdentityBasedAuth.DirectoryServiceOptions

# List the directory domain information if the storage account has enabled AD DS authentication for file shares
$storageAccount.AzureFilesIdentityBasedAuth.ActiveDirectoryProperties

如果成功,輸出看起來應該像這樣:

DomainName:<yourDomainHere>
NetBiosDomainName:<yourNetBiosDomainNameHere>
ForestName:<yourForestNameHere>
DomainGuid:<yourGUIDHere>
DomainSid:<yourSIDHere>
AzureStorageID:<yourStorageSIDHere>

下一步

您現在已成功在您的儲存體帳戶上啟用 AD DS。 若要使用此功能,您必須指派共用層級權限