LDAP系列(三)LDAP + Samba 安裝配置


LDAP + Samba 安裝配置

書接上文:LDAP啟動TLS 完整操作流程

基礎環境:Ubuntu18.04

安裝samba

root@cky:~# apt install samba smbldap-tools -y

查看版本

root@cky:~# dpkg -l samba smbldap-tools
Desired=Unknown/Install/Remove/Purge/Hold
| Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend
|/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)
||/ Name                                            Version                      Architecture                 Description
+++-===============================================-============================-============================-===================================================================================================
ii  samba                                           2:4.7.6+dfsg~ubuntu-0ubuntu2 amd64                        SMB/CIFS file, print, and login server for Unix
ii  smbldap-tools                                   0.9.9-1ubuntu3               all                          Scripts to manage Unix and Samba accounts stored on LDAP

添加 LDAP 的 Samba 配置

為了將OpenLDAP用作Samba的后端,DIT將需要使用可以正確描述Samba數據的屬性。可以通過引入Samba LDAP模式獲得此類屬性。

該模式位於現在安裝的samba軟件包中,並且已經采用ldif格式。

拷貝samba.schema samba.ldif

找不到samba.schema,從源碼搞了一份(git clone https://github.com/samba-team/samba.git)

cp /usr/share/doc/samba/examples/LDAP/samba.ldif.gz /etc/ldap/schema
cp /root/cky/samba/samba-4.7.6+dfsg~ubuntu/examples/LDAP/samba.schema /etc/ldap/schema/

root@cky:/etc/ldap/schema# pwd
/etc/ldap/schema
root@cky:/etc/ldap/schema# gzip -d samba.ldif.gz

導入:

root@cky:/etc/ldap/schema# ldapadd -Q -Y EXTERNAL -H ldapi:/// -f samba.ldif 
adding new entry "cn=samba,cn=schema,cn=config"

要查詢和查看此新架構:

ldapsearch -Q -LLL -Y EXTERNAL -H ldapi:/// -b cn=schema,cn=config 'cn=*samba*'

既然slapd知道了Samba屬性,我們就可以基於它們建立一些索引。索引條目是客戶端對DIT執行篩選搜索時提高性能的一種方法。

創建samba_indices.ldif具有以下內容的文件:

root@cky:~/ldap# pwd
/root/ldap

root@cky:~/ldap# cat samba_indices.ldif 
dn: olcDatabase={1}mdb,cn=config
changetype: modify
replace: olcDbIndex
olcDbIndex: objectClass eq
olcDbIndex: uidNumber,gidNumber eq
olcDbIndex: loginShell eq
olcDbIndex: uid,cn eq,sub
olcDbIndex: memberUid eq,sub
olcDbIndex: member,uniqueMember eq
olcDbIndex: sambaSID eq
olcDbIndex: sambaPrimaryGroupSID eq
olcDbIndex: sambaGroupType eq
olcDbIndex: sambaSIDList eq
olcDbIndex: sambaDomainName eq
olcDbIndex: default sub,eq

使用ldapmodify實用程序加載新索引:

root@cky:~/ldap# ldapmodify -Q -Y EXTERNAL -H ldapi:/// -f samba_indices.ldif
modifying entry "olcDatabase={1}mdb,cn=config"

如果正常到現在,可以使用ldapsearch查看到新索引:

root@cky:~/ldap# ldapsearch -Q -LLL -Y EXTERNAL -H ldapi:/// -b cn=config olcDatabase={1}mdb olcDbIndex
dn: olcDatabase={1}mdb,cn=config
olcDbIndex: objectClass eq
olcDbIndex: uidNumber,gidNumber eq
olcDbIndex: loginShell eq
olcDbIndex: uid,cn eq,sub
olcDbIndex: memberUid eq,sub
olcDbIndex: member,uniqueMember eq
olcDbIndex: sambaSID eq
olcDbIndex: sambaPrimaryGroupSID eq
olcDbIndex: sambaGroupType eq
olcDbIndex: sambaSIDList eq
olcDbIndex: sambaDomainName eq
olcDbIndex: default sub,eq

添加Samba LDAP對象

接下來,配置smbldap-tools軟件包以匹配您的環境。該軟件包帶有一個名為smbldap-config的配置幫助程序腳本。但是,在運行它之前,您應該確定以下兩個重要的配置設置/etc/samba/smb.conf

  • netbios名稱:該服務器的名稱。默認值從服務器的主機名派生,但被截斷為15個字符。
  • workgroup:此服務器的工作組名稱,或者,如果以后決定將其設置為域控制器,則為該域。

配置samba

# file : /etc/samba/smb.conf
[global]
    workgroup = Company 
    server string = Samba Server Version %v
    log file = /var/log/samba/log.%m  
    security = user 

    passdb backend = ldapsam:ldap://company02.com
    ldap suffix = dc=company,dc=com
    ldap user suffix = ou=Dev
    ldap group suffix = ou=Group
    ldap machine suffix = ou=Computers
    ldap idmap suffix = ou=Idmap
    ldap admin dn = cn=admin,dc=company,dc=com
    ldap ssl = start tls
    ldap passwd sync = yes

Note: ubuntu官方文檔的 ldap ssl = start tls

但是設置該項后重啟服務報ERROR:Failed to issue the StartTLS instruction: Protocol error

測試一下samba配置文件

root@cky:~/ldap# testparm -s

通過運行配置腳本來繼續生成smbldap-tools配置

root@cky:~/ldap# smbldap-config

# 只有這三項進行了輸入操作,別的都是直接回車
netbios name [] > Company
ldap master bind password [] > 654321
ldap slave bind password [] > 654321

使用smbldap-populate添加Samba所需的LDAP對象。它將詢問您“域根”用戶的密碼,該用戶也是LDAP中存儲的“根”用戶:

root@cky:~# smbldap-populate -g 10003 -u 10003 -r 10003
Populating LDAP directory for domain Company (S-1-5-21-385293779-2563394074-3374145406)
(using builtin directory structure)

entry dc=company,dc=com already exist. 
entry ou=Dev,dc=company,dc=com already exist. 
entry ou=Groups,dc=company,dc=com already exist. 
adding new entry: ou=Computers,dc=company,dc=com
adding new entry: ou=Idmap,dc=company,dc=com
adding new entry: sambaDomainName=Company,dc=company,dc=com
adding new entry: uid=root,ou=Dev,dc=company,dc=com
adding new entry: uid=nobody,ou=Dev,dc=company,dc=com
adding new entry: cn=Domain Admins,ou=Groups,dc=company,dc=com
adding new entry: cn=Domain Users,ou=Groups,dc=company,dc=com
adding new entry: cn=Domain Guests,ou=Groups,dc=company,dc=com
adding new entry: cn=Domain Computers,ou=Groups,dc=company,dc=com
adding new entry: cn=Administrators,ou=Groups,dc=company,dc=com
adding new entry: cn=Account Operators,ou=Groups,dc=company,dc=com
adding new entry: cn=Print Operators,ou=Groups,dc=company,dc=com
adding new entry: cn=Backup Operators,ou=Groups,dc=company,dc=com
adding new entry: cn=Replicators,ou=Groups,dc=company,dc=com

Please provide a password for the domain root: 
Changing UNIX and samba passwords for root
New password: 654321
Retype new password: 654321
New passwords don't match!

將rootDN用戶的密碼(在slapd軟件包的安裝過程中設置的密碼)告知Samba:

root@cky:~# smbpasswd -W
Setting stored password for "cn=admin,dc=company,dc=com" in secrets.tdb
New SMB password: 654321
Retype new SMB password: 654321

SSSD配置

為了讓LDAP用戶能夠連接到samba並進行身份驗證的最后一步,現在需要這些用戶也以“ unix”用戶身份出現在系統中。

安裝sssd-ldap

root@cky:~# apt -y install sssd-ldap

配置/etc/sssd/sssd.conf

[sssd]
config_file_version = 2
domains = Company

[domain/Company]
id_provider = ldap
auth_provider = ldap
ldap_uri = ldap://192.168.3.188
cache_credentials = True
ldap_search_base = dc=company,dc=com

調整權限並啟動服務

root@cky:~# chmod 0600 /etc/sssd/sssd.conf
root@cky:~# chown root:root /etc/sssd/sssd.conf
root@cky:~# systemctl restart sssd

重新啟動Samba服務:

root@cky:~# systemctl restart smbd.service nmbd.service

要快速測試設置,查看getent是否可以列出Samba組:

root@cky:~# getent group Replicators
root@cky:~# 

有問題,查詢不到用戶組???一個大坑

還是認證有問題,系統中ldap的用戶查不到

google+百度 良久,外帶犧牲了8根秀發,經過N多嘗試之后,我發現以下方法即可解決:

修改配置文件/etc/nsswitch.conf

passwd:         compat systemd sss ldap db
group:          compat systemd sss ldap db
shadow:         compat sss ldap db

主要就是這三項后面加了 ldapdb

安裝nslcd

root@cky:~# apt install nslcd -y

修改配置文件/etc/nslcd.conf

root@cky:~# grep -Ev '^$|^[#;]' /etc/nslcd.conf
uid nslcd
gid nslcd
uri ldapi:///192.168.3.188
base dc=company,dc=com
ldap_version 3
binddn cn=admin,dc=company,dc=com
bindpw 654321
ssl off
tls_cacertfile /etc/ssl/certs/ca-certificates.crt

重啟nslcd服務

root@cky:~# systemctl restart nslcd
root@cky:~# systemctl enable nslcd
nslcd.service is not a native service, redirecting to systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install enable nslcd

重啟slapd服務

root@cky:~# systemctl restart slapd

再次查詢

root@cky:~# getent group Replicators
Replicators:*:552:

所以我也不知道是我看的不夠全,還是官方文檔的缺失,此處再次diss一下Ubuntu官方文檔

使用smbldap-tools管理用戶、組和計算機賬戶

啟用自動主目錄創建:

root@cky:~# pam-auth-update --enable mkhomedir

要添加具有主目錄的新用戶:

root@cky:~# smbldap-useradd -a -P -m ldap_user01
Changing UNIX and samba passwords for ldap_user01
New password: 111111
Retype new password: 111111

查一下ldap中的用戶

root@cky:~# ldapsearch -x -b "uid=ldap_user01,ou=Dev,dc=company,dc=com"

看一下home目錄

root@cky:~# ls /home
cky  ldap_user01

使用getent查詢一下用戶

root@cky:~# getent passwd ldap_user01
ldap_user01:x:10003:513:System User:/home/ldap_user01:/bin/bash


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM