你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

为 Azure 文件共享存储启用 AD DS 身份验证

本文介绍在存储帐户上启用 Active Directory 域服务 (AD DS) 身份验证以使用本地 Active Directory (AD) 凭据对 Azure 文件共享进行身份验证的过程。

重要

在启用 AD DS 身份验证之前,请确保了解概述文章中支持的方案和要求并完成必要的先决条件。 如果你的 Active Directory 环境跨多个林,请参阅将 Azure 文件存储用于多个 Active Directory 林

若要通过 SMB 为 Azure 文件共享启用 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

选项 2:手动执行启用操作

大多数客户应选择上方的选项一并使用 AzFilesHybrid PowerShell 模块在 Azure 文件存储中启用 AD DS 身份验证。 但如果需要,可以使用 Active Directory PowerShell 手动执行如下步骤。

重要

如果你在前面已成功执行 Join-AzStorageAccount 脚本,请直接转到确认已启用此功能部分。 不需要执行以下手动步骤。

检查环境

首先,检查你的环境状态。

  • 检查是否安装了 Active Directory PowerShell,以及是否正在以管理员权限执行 shell。
  • 确保 Az.Storage 模块 已安装,如果未安装,请安装。 至少需要 2.0 版。
  • 完成这些检查后,请检查 AD DS,查看是否有已使用 SPN/UPN(如“cifs/your-storage-account-name-here.file.core.windows.net”)创建的计算机帐户(默认)或服务登录帐户。 如果帐户不存在,请按照下一节的说明创建一个帐户。

重要

本部分中的 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。 请确保提供的 Active Directory SamAccountName 属性不带尾随的“$”符号。

# 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 域服务或 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。 若要使用此功能,必须分配共享级权限