Copyright © 2005 本文遵從GNU 的自由文檔許可證(Free Document License)的條款,歡迎轉載、修改、散佈。
發佈時間:2005年07月08日
最新更新:2005年07月11日
Abstract
Samba是Linux與Windows信息溝通的橋樑。它可使Linux系統變成Windows文件和打印服務器,供Linux用戶和Windows用戶共同使用。在現在Windows和Linux多操作平臺共存的環境下,這種技術就更顯得重要,它能幫我們實現信息的跨平臺交流和平臺的平穩遷移。Samba成功突破了Windows的技術封鎖,是自由軟件的一面旗枳和典範。
Table of Contents
Chapter 1. 安裝
Chapter 2. 配置
Table of Contents
Samba的配置文件是smb.conf,如果是debian系統,則位於/etc/samba目錄下,如果從源碼編譯安裝,則位於安裝目錄的lib目錄下。
2.1. 服務器角色
Samba 3服務器可擔任主域控制器、獨立域服務器和域成員服務器等角色。設置方法如下:
主域控制器:
[global] ... ...... domain master = yes preferred master = yes domain logons = yes security = user ...... ...
使用testparm命令測試一下:
debian:~# testparm Load smb config files from /etc/samba/smb.conf Processing section \"[homes]\" Processing section \"[printers]\" Loaded services file OK. Server role: ROLE_DOMAIN_PDC #服務器角色爲主域控制器 Press enter to see a dump of your service definitions ......
備份域控制器:
[global] ... ...... domain master = no preferred master = yes domain logons = yes security = user ...... ...
運行testparm測試一下:
testparm Load smb config files from /etc/samba/smb.conf Processing section \"[homes]\" Processing section \"[printers]\" Loaded services file OK. Server role: ROLE_DOMAIN_BDC #服務器角色爲備份域控制器 Press enter to see a dump of your service definitions
獨立服務器:
[global] ... ...... domain master = yes preferred master = yes domain logon = no security = user ...... ...
再使用testparm命令測試一下。
debian:~# testparm Load smb config files from /etc/samba/smb.conf Processing section \"[homes]\" Processing section \"[printers]\" Loaded services file OK. Server role: ROLE_STANDALONE #服務器的角色爲獨立服務器 Press enter to see a dump of your service definitions ......
域的成員服務器:
[global] ... ...... domain master = yes preferred master = yes domain logon = no security = domain ...... ...
運行testparm測試一下:
Load smb config files from /etc/samba/smb.conf Processing section \"[homes]\" Processing section \"[printers]\" Loaded services file OK. Server role: ROLE_DOMAIN_MEMBER #服務器的角色爲域成員服務器 Press enter to see a dump of your service definitions ......
2.2. 使用openldap目錄服務進行samba用戶驗證
samba可把用戶信息存放到ldap目錄服務器上,使用目錄服務器上的用戶數據進行用戶驗證。這樣做的好處是:
集中存放用戶信息,多臺Samba服務器可共用一套用戶數據。
當用戶數量多時,採用ldap目錄服務器可加快用戶驗證的速度。因爲ldap目錄服務是專門爲讀操作而優化的,搜索數據的速度是非常快的。
維護方便,可利用ldap客戶端進行可視化維護,如phpldapadmin。
用戶可自由更改自已的密碼,而不用經過系統管理員,可大大減輕系統管理員的工作量。
要使samba能夠使用ldap目錄服務器作爲用戶驗證的後臺數據庫,需在編譯samba時選擇了“--with-ldapsam”選項。在確保samba服務器和openldap服務器正常運行的前提下,進行以下配置。
要openldap能認識samba的用戶信息,我們需在openldap服務器中導入samba.schema。該文件是不包含在openldap源碼中的,我們可在samba源碼目錄中的/examples/LDAP路徑中找到該文件。把它拷貝到openldap的schema目錄。接着編輯slapd.conf文件,用include指令把samba.schema導入openldap。如:
# Allow LDAPv2 binds allow bind_v2 # This is the main slapd configuration file. See slapd.conf(5) for more # info on the configuration options. ####################################################################### # Global Directives: # Features to permit #allow bind_v2 # Schema and objectClass definitions include /etc/ldap/schema/core.schema include /etc/ldap/schema/cosine.schema include /etc/ldap/schema/nis.schema include /etc/ldap/schema/inetorgperson.schema include /etc/ldap/schema/samba.schema #導入samba.schema文件
重啓服務器,使配置生效。
下來我們要配置samba服務器了。打開smb.conf文件,添加以下內容:
# You may wish to use password encryption. Please read ENCRYPTION.txt, # Win95.txt and WinNT.txt in the Samba documentation. Do not enable this # option unless you have read those documents encrypt passwords = true ; passdb backend = smbpasswd guest #取消smbpasswd驗證 ldap admin dn = "cn=admin,dc=com" #指定管理員 passdb backend = ldapsam:ldap://ldapserver #指定用戶驗證的ldap目錄服務器 ldap user suffix = dc=samba,dc=it #用戶信息存放的目錄路徑 ldap delete dn = no #不允許刪除dn ldap suffix = dc=com #指定搜索後綴
存盤退出,重啓samba服務器。運行以下命令,把cn=admin,dc=com管理員的密碼“secret”保存到secrets.tdb文件中,以便samba服務器與openldap服務器進行通信。
debian:~# smbpasswd -w secret Setting stored password for "cn=admin,dc=com" in secrets.tdb #成功保存的提示信息
把Samba用戶的驗證信息存放到ldap目錄服務器的方式有兩種。一種是利用ldap裏已有的目錄項,不用另外增加一個目錄項來專門存放samba用戶驗證信息,而是把samba用戶的驗證信息存放到目錄服務器中同名的目錄項。採用這種方式,sambaSamAccount對象類型需手工維護,當用戶數量大的時候就很難保證sambaSID號碼的唯一。另外一種是直接運行smbpasswd -a test命令,重新創建一個uid=test,dc=samba,dc=it,dc=com的目錄項。sambaSamAccount對象類型會自動創建,sambaSID號會由程序自動維護。但如果要以uid=test,dc=samba,dc=it,dc=com登錄目錄服務器維護samba用戶信息,則要手工添加一個simpleSecurityObject對象類型,增加userPassword屬性才行。這兩種方式各有優缺點,可滿足不同場合的需求。第二種方式相對簡單,這裏就不具體介紹。下面詳細介紹一下第一種方式的配置步驟。
首先,我們在ldap目錄服務器上要有一個叫“cn=test,dc=samba,dc=it,dc=com”的目錄項,且使用該dn能正常登錄ldap目錄服務器。在cn=test,dc=samba,dc=it,dc=com目錄項中還要添加一個sambaSamAccount對象類型,以便smbpasswd程序寫入samba帳號信息。添加該對象類型時需要填兩個屬性,一個是sambaSID,一個是uid,sambaSID類似Windows NT中的SID,它是以S開頭一串數字,由Samba服務器的SID + UID組成。當你的samba服務器成功連接到ldap目錄服務器時,它已在ldap目錄服務器中自動創建一個sambaDomainName=hostname的目錄項。該目錄項的sambaSID就是Samba服務器的SID了,類似於S-1-5-21-3129426798-1813742091-3551573396。UID手動填一個即可,如3001。每個用戶的UID都是不同的。最後的sambaSID是S-1-5-21-3129426798-1813742091-3551573396-3001。
配置完成,使用smbpasswd命令就可把用戶信息保存到ldap目錄服務器上了。
debian:~# smbpasswd -a test New SMB password: Retype new SMB password: Added user test. #成功添加用戶
用phpldapadmin以cn=test,dc=samba,dc=it,dc=com登錄進ldapserver服務器,你會發現該用戶添加了samba用戶的驗證信息。其中,uid屬性記錄的是samba的用戶名,sambaLMPassword和sambaNTPassword兩個屬性分別記錄了用戶的加密密碼資料,分別對應windows98和NT兩種加密方式。
到此爲止,使用ldap目錄服務器進行Samba用戶驗證已配置完成。但現在還是要管理員在samba服務器上使用smbpasswd命令爲用戶維護密碼。從維護工作量和保密的角度出發,由用戶自已維護自已的密碼是一種最好的方法。我們藉助phpldapadmin這個工具可幫我們實現該功能。具體設置方法是這樣的:
其實,不使用phpldapadmin來維護samba用戶密碼也可以,我們完全可以用python-ldap配合mkntpwd程序自已DIY一個用戶密碼管理程序。
首先,在設置前,需先把phpldapadmin安裝好,具體的安裝步驟請參考我網站上的openldap學習筆記。安裝好phpldapadmin後,我們已可正常使用該工具維護目錄項了。同時,通過目錄服務器的權限管理,我們也可把test用戶密碼屬性設置成只有dn是cn=test,dc=samba,dc=it,dc=com的用戶和目錄服務器管理員才能修改。要達到該項果,請把以下ACL加入到slapd.conf中。
access to attrs=sambaLMPassword by dn="cn=admin,dc=com" write by anonymous auth by self write by * none access to attrs=sambaNTPassword by dn="cn=admin,dc=com" write by anonymous auth by self write by * none
重啓openldap服務器使配置生效。
要修改samba用戶的密碼,我們只要修改smbaLMPassword和sambaNTPassword兩個屬性值即可,但會出錯,提示修改失敗,要我們去修改phpldapadmin的template_config.php文件。這是因爲phpldapadmin使用了一個叫mkntpwd的程序來生成samba的加密密碼。在template_config.php文件中設置了mkntpwd程序的執行路徑。mkntpwd程序默認是沒有安裝在系統中的,我們需安裝它,並把它放到在template_config.php文件裏指定的執行位置(/usr/bin/)。mkntpwd的源碼包含在samba的源碼中,我們可進入samba的源碼目下的examples/LDAP/smbldap-tools/mkntpwd目錄,mkntpwd的源碼全部在這裏了,運行make命令,就可生成mkntpwd程序。最後把mkntpwd拷貝到/usr/bin目錄下即可。
如果是debian系統,template_config.php文件就位於/etc/phpldapadmin/templates目錄下。節選有關定義mkntpwd程序路徑的內容如下:
... ## SAMBA TEMPLATE CONFIGURATION ## ## ---------------------------- ## ## ## ## In order to use the samba templates, you might edit the following properties: ## ## 1 - $mkntpwdCommand : the path to the mkntpwd utility provided with/by Samba. ## ## 2 - $samba3_domains : the domain name and the domain sid. ## ## ## ######################################################################################*/ // path 2 the mkntpwd utility (Customize) $mkntpwdCommand = "/usr/bin/mkntpwd"; ...
以cn=test,dc=samba,dc=it,dc=com登錄進入ldap服務器,我們就可修改smbaLMPassword和sambaNTP\assword兩個屬性值了。其它用戶的這兩個屬性test用戶是看不到的。
Chapter 3. 小技巧
使用tdbdump程序可以查看samba的tbd格式文件。