針對存取 Azure 檔案儲存體的 Linux 用戶端啟用透過 SMB 的 Active Directory 驗證

如需支援選項和考量的詳細資訊,請參閱適用於 SMB 存取的 Azure 檔案儲存體身分識別型驗證選項概觀

Azure 檔案儲存體支援透過下列三種方法,使用 Kerberos 驗證通訊協定透過 Linux 虛擬機器 (VM) 的伺服器訊息區 (SMB) 進行以身分識別為基礎的驗證:

  • 內部部署的 Windows Active Directory Domain Services (AD DS)
  • Microsoft Entra 網域服務

若要使用第一個選項 (AD DS),您必須使用 Microsoft Entra Connect 將 AD DS 同步至 Microsoft Entra ID。

注意

本文會針對範例步驟使用 Ubuntu。 類似的設定適用於 RHEL 和 SLES 電腦,可讓您使用 Active Directory 掛接 Azure 檔案共用。

適用於

檔案共用類型 SMB NFS
標準檔案共用 (GPv2)、LRS/ZRS Yes, this article applies to standard SMB Azure file shares LRS/ZRS. No, this article doesn't apply to NFS Azure file shares.
標準檔案共用 (GPv2)、GRS/GZRS Yes, this article applies to standard SMB Azure file shares GRS/GZRS. No this article doesn't apply to NFS Azure file shares.
進階檔案共用 (FileStorage)、LRS/ZRS Yes, this article applies to premium SMB Azure file shares. No, this article doesn't apply to premium NFS Azure file shares.

Linux SMB 用戶端限制

您無法在開機時使用 fstab 項目,使用身分識別型驗證在 Linux 用戶端上掛接 Azure 檔案共用,因為客戶端無法在開機時提早取得 Kerberos 票證來掛接。 不過,您可以使用 fstab 項目並指定 noauto 選項。 這不會在開機時掛接共用,但它會允許使用者在不使用所有參數的簡單掛接命令登入之後,方便掛接檔案共用。 您也可以使用 autofs 在存取時掛接共用。

必要條件

針對 Azure 檔案共用啟用透過 SMB 的 AD 驗證之前,請確定您已完成下列必要條件。

  • 在 Azure 上執行的 Linux VM (Ubuntu 18.04+ 或對等 RHEL 或 SLES VM)。 VM 在包含 Microsoft Entra Domain Services 的 VNet 上必須至少有一個網路介面,或具有 AD DS 同步至 Microsoft Entra ID 的內部部署 Linux VM。
  • 具有完整 sudo 權限之本機使用者帳戶的根使用者或使用者認證 (適用於本指南,localadmin)。
  • Linux VM 不得已加入任何 AD 網域。 如果它已經是網域的一部分,則必須先離開該網域,才能加入此網域。
  • Microsoft Entra 租用戶已完整設定,且已設定網域使用者。

安裝 Samba 套件並非絕對必要,但它會為您提供一些實用的工具,並自動引進其他套件,例如 samba-commonsmbclient。 執行下列命令以進行安裝。 如果您在安裝期間被要求輸入任何值,請將這些值保留空白。

sudo apt update -y
sudo apt install samba winbind libpam-winbind libnss-winbind krb5-config krb5-user keyutils cifs-utils

wbinfo 工具是 Samba 套件的一部分。 這對於驗證和偵錯用途很有用,例如檢查網域控制站是否可連線、檢查電腦已加入哪些網域,以及尋找使用者的相關資訊。

請確定 Linux 主機會讓時間與網域伺服器保持同步。 請參閱適用於 Linux 發行版本的文件。 對於某些發行版本,您可以使用 systemd-timesyncd 來執行此動作。 使用您慣用的文字編輯器編輯 /etc/systemd/timesyncd.conf,以包含下列內容:

[Time]
NTP=onpremaadint.com
FallbackNTP=ntp.ubuntu.com

然後重新啟動服務:

sudo systemctl restart systemd-timesyncd.service

啟用 AD Kerberos 驗證

請遵循下列步驟來啟用 AD Kerberos 驗證。 此 Samba 文件在參考時可能很有幫助。

確定網域伺服器可連線且可供探索

  1. 請確定提供的 DNS 伺服器包含網域伺服器 IP 位址。
systemd-resolve --status
Global 
          DNSSEC NTA: 10.in-addr.arpa
                      16.172.in-addr.arpa
                      168.192.in-addr.arpa
                      17.172.in-addr.arpa
                      18.172.in-addr.arpa
                      19.172.in-addr.arpa
                      20.172.in-addr.arpa
                      21.172.in-addr.arpa
                      22.172.in-addr.arpa
                      23.172.in-addr.arpa
                      24.172.in-addr.arpa
                      25.172.in-addr.arpa
                      26.172.in-addr.arpa
                      27.172.in-addr.arpa
                      28.172.in-addr.arpa
                      29.172.in-addr.arpa
                      30.172.in-addr.arpa
                      31.172.in-addr.arpa
                      corp
                      d.f.ip6.arpa
                      home
                      internal
                      intranet
                      lan
                      local
                      private
                      test 

Link 2 (eth0) 
      Current Scopes: DNS 
       LLMNR setting: yes 
MulticastDNS setting: no 
      DNSSEC setting: no 
    DNSSEC supported: no 
         DNS Servers: 10.0.2.5 
                      10.0.2.4 
                      10.0.0.41
          DNS Domain: domain1.contoso.com 
  1. 如果命令正常運作,請跳過下列步驟,然後繼續進行下一節。

  2. 如果無法運作,請確定網域伺服器 IP 位址正在執行 Ping。

ping 10.0.2.5
PING 10.0.2.5 (10.0.2.5) 56(84) bytes of data.
64 bytes from 10.0.2.5: icmp_seq=1 ttl=128 time=0.898 ms
64 bytes from 10.0.2.5: icmp_seq=2 ttl=128 time=0.946 ms

^C 

--- 10.0.2.5 ping statistics --- 
2 packets transmitted, 2 received, 0% packet loss, time 1002ms
rtt min/avg/max/mdev = 0.898/0.922/0.946/0.024 ms
  1. 如果 Ping 無法運作,請返回必要條件,並確定您的 VM 位於可存取 Microsoft Entra 租用戶的 VNet 上。

  2. 如果 IP 位址正在執行 Ping,但不會自動探索 DNS 伺服器,您可以手動新增 DNS 伺服器。 使用您慣用的文字編輯器編輯 /etc/netplan/50-cloud-init.yaml

# This file is generated from information provided by the datasource.  Changes
# to it will not persist across an instance reboot.  To disable cloud-init's
# network configuration capabilities, write a file
# /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg with the following:
# network: {config: disabled}
network:
    ethernets:
        eth0:
            dhcp4: true
            dhcp4-overrides:
                route-metric: 100
            dhcp6: false
            match:
                macaddress: 00:22:48:03:6b:c5
            set-name: eth0
            nameservers:
                addresses: [10.0.2.5, 10.0.2.4]
    version: 2

然後套用變更:

sudo netplan --debug apply 
  1. Winbind 假設 DHCP 伺服器會將網域 DNS 記錄保持在最新狀態。 不過,對於 Azure DHCP 而言並非如此。 若要設定用戶端進行 DDNS 更新,請使用本指南來建立網路指令碼。 以下是位於 /etc/dhcp/dhclient-exit-hooks.d/ddns-update 的範例指令碼。
#!/bin/sh 

# only execute on the primary nic
if [ "$interface" != "eth0" ]
then
    return
fi 

# When you have a new IP, perform nsupdate
if [ "$reason" = BOUND ] || [ "$reason" = RENEW ] ||
   [ "$reason" = REBIND ] || [ "$reason" = REBOOT ]
then
   host=`hostname -f`
   nsupdatecmds=/var/tmp/nsupdatecmds
     echo "update delete $host a" > $nsupdatecmds
     echo "update add $host 3600 a $new_ip_address" >> $nsupdatecmds
     echo "send" >> $nsupdatecmds

     nsupdate $nsupdatecmds
fi 

連線至 Microsoft Entra Domain Services,並確定服務可供探索

  1. 請確定您可以透過網域名稱 Ping 網域伺服器。
ping contosodomain.contoso.com
PING contosodomain.contoso.com (10.0.2.4) 56(84) bytes of data.
64 bytes from pwe-oqarc11l568.internal.cloudapp.net (10.0.2.4): icmp_seq=1 ttl=128 time=1.41 ms
64 bytes from pwe-oqarc11l568.internal.cloudapp.net (10.0.2.4): icmp_seq=2 ttl=128 time=1.02 ms
64 bytes from pwe-oqarc11l568.internal.cloudapp.net (10.0.2.4): icmp_seq=3 ttl=128 time=0.740 ms
64 bytes from pwe-oqarc11l568.internal.cloudapp.net (10.0.2.4): icmp_seq=4 ttl=128 time=0.925 ms 

^C 

--- contosodomain.contoso.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3016ms
rtt min/avg/max/mdev = 0.740/1.026/1.419/0.248 ms 
  1. 請確定您可以在網路上探索 Microsoft Entra 服務。
nslookup
> set type=SRV
> _ldap._tcp.contosodomain.contoso.com.
Server:         127.0.0.53
Address:        127.0.0.53#53

Non-authoritative answer: 

_ldap._tcp.contosodomain.contoso.com service = 0 100 389 pwe-oqarc11l568.contosodomain.contoso.com.
_ldap._tcp.contosodomain.contoso.com service = 0 100 389 hxt4yo--jb9q529.contosodomain.contoso.com. 

設定主機名稱和完整網域名稱 (FQDN)

  1. 使用文字編輯器,以最終 FQDN (加入網域之後) 和主機的別名更新 /etc/hosts 檔案。 IP 位址目前並不重要,因為此行主要用於將簡短主機名轉譯為 FQDN。 如需詳細資料,請參閱將 Samba 設定為網域成員
127.0.0.1       contosovm.contosodomain.contoso.com contosovm
#cmd=sudo vim /etc/hosts   
#then enter this value instead of localhost "ubuntvm.contosodomain.contoso.com UbuntuVM" 
  1. 現在,您的主機名應該會解析。 您可以忽略它目前解析為的 IP 位址。 簡短主機名應該解析為 FQDN。
getent hosts contosovm
127.0.0.1       contosovm.contosodomain.contoso.com contosovm
dnsdomainname
contosodomain.contoso.com
hostname -f
contosovm.contosodomain.contoso.com 

注意

某些發行版本會要求您執行 hostnamectl 命令,才能更新主機名 -f:

hostnamectl set-hostname contosovm.contosodomain.contoso.com

設定 krb5.conf

  1. 設定 /etc/krb5.conf,以便連絡 Kerberos 金鑰發佈中心 (KDC) 與網域伺服器進行驗證。 如需詳細資訊,請參閱 MIT Kerberos 文件。 範例 /etc/krb5.conf 檔案如下。
[libdefaults]
        default_realm = CONTOSODOMAIN.CONTOSO.COM
        dns_lookup_realm = false
        dns_lookup_kdc = true

設定 smb.conf

  1. 識別 smb.conf 的路徑。
sudo smbd -b | grep "CONFIGFILE"
   CONFIGFILE: /etc/samba/smb.conf
  1. 將 SMB 組態變更為作為網域成員。 如需詳細資訊,請參閱將 Samba 設定為網域成員。 範例 smb.conf 檔案如下。

注意

此範例適用於 Microsoft Entra Domain Services,建議您在設定 idmap 時設定 backend = rid。 內部部署 AD DS 使用者可能偏好選擇不同的 idmap 後端

[global]
   workgroup = CONTOSODOMAIN
   security = ADS
   realm = CONTOSODOMAIN.CONTOSO.COM

   winbind refresh tickets = Yes
   vfs objects = acl_xattr
   map acl inherit = Yes
   store dos attributes = Yes

   dedicated keytab file = /etc/krb5.keytab
   kerberos method = secrets and keytab

   winbind use default domain = Yes 

   load printers = No
   printing = bsd
   printcap name = /dev/null
   disable spoolss = Yes

   log file = /var/log/samba/log.%m
   log level = 1

   idmap config * : backend = tdb
   idmap config * : range = 3000-7999

   idmap config CONTOSODOMAIN : backend = rid
   idmap config CONTOSODOMAIN : range = 10000-999999

   template shell = /bin/bash
   template homedir = /home/%U 
  1. 強制 winbind 重新載入已變更的組態檔。
sudo smbcontrol all reload-config

加入網域

  1. 使用 net ads join 命令將主機加入 Microsoft Entra Domain Services 網域。 如果命令擲回錯誤,請參閱疑難排解 Samba 網域成員以解決此問題。
sudo net ads join -U contososmbadmin    # user  - garead

Enter contososmbadmin's password:
Using short domain name -- CONTOSODOMAIN
Joined 'CONTOSOVM' to dns domain 'contosodomain.contoso.com' 
  1. 請確定網域伺服器上存在此主機的 DNS 記錄。
nslookup contosovm.contosodomain.contoso.com 10.0.2.5
Server:         10.0.2.5
Address:        10.0.2.5#53

Name:   contosovm.contosodomain.contoso.com
Address: 10.0.0.8

如果使用者將主動登入用戶端電腦或 VM 並存取 Azure 檔案共用,您必須 設定 nsswitch.conf,然後為 winbind 設定 PAM。 如果存取權僅限於需要 Kerberos 驗證才能存取檔案共用的使用者帳戶或電腦帳戶所代表的應用程式,您可以跳過這些步驟。

設定 nsswitch.conf

  1. 既然主機已加入網域,您必須在尋找使用者和群組時,將 winbind 程式庫放在要尋找的地方。 藉由更新 nsswitch.conf 中的 passwd 和群組項目來這樣做。 使用文字編輯器來編輯 /etc/nsswitch.conf 並新增下列項目:
passwd:         compat systemd winbind
group:          compat systemd winbind
  1. 啟用 winbind 服務以在重新啟動時自動啟動。
sudo systemctl enable winbind
Synchronizing state of winbind.service with SysV service script with /lib/systemd/systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install enable winbind
  1. 然後重新啟動服務。
sudo systemctl restart winbind
sudo systemctl status winbind
winbind.service - Samba Winbind Daemon
   Loaded: loaded (/lib/systemd/system/winbind.service; enabled; vendor preset: enabled)
   Active: active (running) since Fri 2020-04-24 09:34:31 UTC; 10s ago
     Docs: man:winbindd(8)
           man:samba(7)
           man:smb.conf(5)
 Main PID: 27349 (winbindd)
   Status: "winbindd: ready to serve connections..."
    Tasks: 2 (limit: 4915)
   CGroup: /system.slice/winbind.service
           ├─27349 /usr/sbin/winbindd --foreground --no-process-group
           └─27351 /usr/sbin/winbindd --foreground --no-process-group

Apr 24 09:34:31 contosovm systemd[1]: Starting Samba Winbind Daemon...
Apr 24 09:34:31 contosovm winbindd[27349]: [2020/04/24 09:34:31.724211,  0] ../source3/winbindd/winbindd_cache.c:3170(initialize_winbindd_cache)
Apr 24 09:34:31 contosovm winbindd[27349]:   initialize_winbindd_cache: clearing cache and re-creating with version number 2
Apr 24 09:34:31 contosovm winbindd[27349]: [2020/04/24 09:34:31.725486,  0] ../lib/util/become_daemon.c:124(daemon_ready)
Apr 24 09:34:31 contosovm systemd[1]: Started Samba Winbind Daemon.
Apr 24 09:34:31 contosovm winbindd[27349]:   STATUS=daemon 'winbindd' finished starting up and ready to serve connections 
  1. 請確定已探索網域使用者和群組。
getent passwd contososmbadmin
contososmbadmin:*:12604:10513::/home/contososmbadmin:/bin/bash
getent group 'domain users'
domain users:x:10513: 

如果上述項目無法運作,請使用 wbinfo 工具檢查網域控制站是否可連線:

wbinfo --ping-dc

設定 winbind 的 PAM

  1. 您必須將 winbind 放在驗證堆疊中,讓網域使用者透過 winbind 進行驗證,方法是設定 winbind 的 PAM (插入式驗證模組)。 第二個命令可確保 homedir 會在第一次登入此系統時為網域使用者建立。
sudo pam-auth-update --enable winbind
sudo pam-auth-update --enable mkhomedir 
  1. 確定 PAM 驗證組態在 /etc/pam.d/common-auth 中具有下列引數:
grep pam_winbind.so /etc/pam.d/common-auth
auth    [success=1 default=ignore]      pam_winbind.so krb5_auth krb5_ccache_type=FILE cached_login try_first_pass 
  1. 您現在應該能夠透過 ssh、su 或任何其他驗證方式,以網域使用者身分登入此系統。
su - contososmbadmin
Password:
Creating directory '/home/contososmbadmin'.
contososmbadmin@contosovm:~$ pwd
/home/contososmbadmin
contososmbadmin@contosovm:~$ id
uid=12604(contososmbadmin) gid=10513(domain users) groups=10513(domain users),10520(group policy creator owners),10572(denied rodc password replication group),11102(dnsadmins),11104(aad dc administrators),11164(group-readwrite),11165(fileshareallaccess),12604(contososmbadmin) 

確認設定

若要確認用戶端電腦已加入網域,請在網域控制站上查閱用戶端的 FQDN,並尋找為此特定用戶端列出的 DNS 項目。 在許多情況下,<dnsserver> 與用戶端加入的網域名稱相同。

nslookup <clientname> <dnsserver>

接下來,使用 klist 命令來檢視 Kerberos 快取中的票證。 開頭應該有類似下列 krbtgt 的項目:

krbtgt/CONTOSODOMAIN.CONTOSO.COM@CONTOSODOMAIN.CONTOSO.COM

如果您未設定 winbind 的 PAM,則 klist 可能不會顯示票證項目。 在此情況下,您可以手動驗證使用者以取得票證:

wbinfo -K contososmbadmin

您也可以在指令碼中執行命令:

wbinfo -K 'contososmbadmin%SUPERSECRETPASSWORD'

掛接檔案共用

啟用 AD (或 Microsoft Entra ID) Kerberos 驗證並將您的 Linux VM 加入網域之後,您可以掛接檔案共用。

如需詳細的掛接指示,請參閱使用掛接隨選掛接 Azure 檔案共用

使用下列其他掛接選項搭配所有存取控制模型來啟用 Kerberos 安全性:sec=krb5

注意

此功能僅支援使用無模式位元的 NT ACL 來強制執行伺服器存取控制模型。 更新 NT ACL 的 Linux 工具最少,因此透過 Windows 更新 ACL。 目前不支援客戶端強制執行的存取控制 (modefromsid,idsfromsid) 和客戶端轉譯的存取控制 (cifsacl) 模型。

其他掛接選項

單一使用者與多使用者掛接

在單一使用者掛接使用案例中,掛接點是由 AD 網域的單一使用者存取,且不會與網域的其他使用者共用。 每個檔案存取的發生都與使用者相關,其 krb5 認證是用來掛接檔案共用。 本機系統上存取掛接點的任何使用者都會模擬該使用者。

在多使用者掛接使用案例中,仍有單一掛接點,但多個 AD 使用者可以存取相同的掛接點。 在相同用戶端上多個使用者存取相同共用的情況下,系統會針對 Kerberos 進行設定並使用 sec=krb5 掛接,同時考慮使用 multiuser 掛接選項。

檔案權限

檔案權限很重要,尤其是 Linux 和 Windows 用戶端都會存取檔案共用時。 若要將檔案權限轉換為檔案上的 DACL,請使用預設掛接選項,例如 file_mode=<>,dir_mode=<>。 指定為 file_modedir_mode 的 檔案權限只會在用戶端內強制執行。 伺服器會根據檔案或目錄的安全性描述元強制執行存取控制。

檔案所有權

檔案擁有權很重要,特別是當 Linux 和 Windows 用戶端都存取檔案共用時。 選擇下列其中一個掛接選項,將檔案擁有權 UID/GID 轉換為檔案 DACL 上的擁有者/群組 SID:

  • 使用預設值,例如 uid=<>,gid=<>
  • 透過 RFC2307 和 Active Directory 設定 UID/GID 對應 (nss_winbindnss_sssd)

檔案屬性快取一致性

效能很重要,即使檔案屬性不一定正確也是如此。 actimeo 的預設值為 1 (秒),這表示如果快取的屬性早於 1 秒,則會從伺服器再次擷取檔案屬性。 將值增加至 60 表示至少快取 1 分鐘的屬性。 針對大部分的使用案例,我們建議針對此選項使用 30 的值 (actimeo=30)。

對於較新的核心,請考慮更細微地設定 actimeo 功能。 您可以使用 acdirmax 進行目錄項目重新驗證快取,並使用 acregmax 來快取檔案中繼資料,例如 acdirmax=60,acregmax=5

下一步

如需如何在 Linux 上掛接 SMB 檔案共用的詳細資訊,請參閱: