FreeBSD連載(67):配置samba

2000年1月8日 17:02 王波

配置samba

  雖然預設情況已經適合大部分用戶訪問自己在伺服器上的個人目錄的要求,但是為了充分發揮samba提供的功能 ,與這些複雜多樣的Windows網路相適應,就必須更改smb.conf的設置。以提供更多的共享設置、伺服器設置 ,以及用於支持包括比較簡單的共享方式和複雜的域認証方式在內的認証方式。

  smb.conf中的語法非常簡單,整個配置檔案分為多個不同的部分,每個部分具有一個標題。使用方括號將標 題引起來,標題下包括一些相關設置,每一行都用於一個相關設置。設置檔案中的每個部分或者定義了一個共享資源的名字, 或者設置了一組參數。以下以預設smb.conf為例,介紹smb.conf中的相關設置。

  • [global]設置
# This is the main Samba configuration file. You should read the
# smb.conf(5) manual page in order to understand the options listed
# here. Samba has a huge number of configurable options (perhaps too
# many!) most of which are not shown in this example
#
# Any line which starts with a ; (semi-colon) or a # (hash)
# is a comment and is ignored. In this example we will use a #
# for commentry and a ; for parts of the config file that you
# may wish to enable
#
# NOTE: Whenever you modify this file you should run the command "testparm"
# to check that you have not many any basic syntactic errors.
#
#======================= Global Settings =====================================
[global]

  [global]部分定義了伺服器本身使用的配置參數,以及其他共享資源部分使用的預設配置參數,因此相當重要。

# workgroup = NT-Domain-Name or Workgroup-Name, eg: REDHAT4
   workgroup = MYGROUP

  工作組設置本地網路使用工作組名字(或者是NT域名字),這對於將正確進行瀏覽數據中非常重要。 否則,Windows客戶就不能從其網路鄰居中發現這個Samba伺服器。

# server string is the equivalent of the NT Description field
   server string = Samba Server

  server string是對於本地伺服器的簡單描述,這些信息將作為這台伺服器的屬性,返回給Browser, 顯示在Windows客戶中作為對這個伺服器的描述。

# This option is important for security. It allows you to restrict
# connections to machines which are on your local network. The
# following example restricts access to two C class networks and
# the "loopback" interface. For more examples of the syntax see
# the smb.conf man page
;   hosts allow = 192.168.1. 192.168.2. 127.

  hosts allow參數用於限制可以訪問這台samba伺服器的客戶機的IP地址範圍,通過指定一系列網路地 址,使得只有這些網路中的電腦才能訪問這台伺服器提供的資源,以提供安全限制。預設情況下,這行配 置被注釋了,使得所有的客戶都可以訪問這台電腦,這樣就存在一定的安全問題。

# If you want to automatically load your printer list rather
# than setting them up individually then you'll need this
   load printers = yes
 
# you may wish to override the location of the printcap file
;   printcap name = /etc/printcap
 
# on SystemV system setting printcap name to lpstat should allow
# you to automatically obtain a printer list from the SystemV spool
# system
;   printcap name = lpstat
 
# It should not be necessary to specify the print system type unless
# it is non-standard. Currently supported print systems include:
# bsd, sysv, plp, lprng, aix, hpux, qnx
;   printing = bsd

  這些設置是用於設置samba伺服器的印表機資源,load printer=yes就允許samba伺服器對外共享服 務器的印表機。由於不同的Unix系統中會使用不同的打印系統和不同的打印設置檔案,因此需要設置正確的 printcap和printing類型,對於使用BSD打印系統的FreeBSD來講,打印系統為BSD,使用的設置檔案為 printcap,這些是預設設置,不需要改動。對於使用Unix System V打印系統的Samba,則需要重新進行 設置。

# Uncomment this if you want a guest account, you must add this to /etc/passwd
# otherwise the user "nobody" is used
;  guest account = pcguest

  由於Mircrosoft客戶沒有用戶的概念,因此有時會用沒有用戶和密碼的請求訪問伺服器,就需 要將這個沒有用戶的請求映射為系統中的某個用戶,Samba伺服器才能安全的訪問系統。guest account 就定義這樣的請求在Unix下對應的用戶權限。為了安全的原因,不能讓這個帳戶在系統中有可寫的權限 ,通常可以增加一個專用帳戶,如pcguest。如果這個設置被注釋的情況下,系統預設使用nobody執行 Windows客戶的請求。一些使用者建議不要使用nobody用戶,因為系統中的很多程序預設都使用它,因 此就會有安全問題。

# this tells Samba to use a separate log file for each machine
# that connects
   log file = /var/log/log.%m
 
# Put a capping on the size of the log files (in Kb).
   max log size = 50

  這裡定義samba的日誌檔案為/var/log目錄下的以log為前綴的檔案,%m用於代表從訪問的NetBIOS 電腦名,如果使用的用戶級認証,還可以使用%U表示不同的登錄用戶。例如從一台名字為win95c的計算 機訪問samba伺服器的日誌,將記錄在/var/log/log.win95c檔案中。max log size定義每個日誌檔案的存 儲限制。

# Security mode. Most people will want user level security. See
# security_level.txt for details.
   security = user
# Use password server option only with security = server
;   password server = <NT-Server-Name>
 
# 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 = yes

  在SMB協議中,有多個不同的認証方式,包括簡單的共享級認証和用戶級認証。Unix為多用戶操作系統,預設 就使用用戶級認証方式。當使用用戶級認証的時候,Samba伺服器使用Unix操作系統的用戶和密碼(來自/etc/passwd )對用戶進行認証,這是一種獨立的認証方式。而有時候希望所有的伺服器使用同一個認証數據庫進行統一認証 ,因此就導致出現了基於域的統一認証模式。在一個域中,用戶只需要通過域控制器進行認証即可,域中其他SMB伺服器就 將認可域控制器的認証。

  為了使Samba伺服器支持域認証方式,可以有兩種不同的設置方式,一種為真正的域認証,另一種為伺服器認証 方式,將Samba伺服器配置為通過伺服器驗証用戶,這需要指定security=server,以及指定password server的名字為NT的域控制器。認証伺服器的方式不能事先域認証方式提供的一些特征,但它的適用範圍並 不僅限於域,使用工作組的網路也能通過統一的認証伺服器來使用統一認証模式。設置域認証方式在後面進行詳細解釋。

  在Windows NT 4及Windows 98之後,客戶和伺服器之間進行認証時,密碼不再以明文的方式 在網路中傳輸了。為了支持加密密碼傳輸,需要設置encrypt passwords選項。同樣,這也需要其他複雜的 設置。

# Using the following line enables you to customise your configuration
# on a per machine basis. The %m gets replaced with the netbios name
# of the machine that is connecting
;   include = /usr/local/etc/smb.conf.%m

  系統管理員可以為每個特定的電腦定制一個特定的配置檔案,那裡的設置將覆蓋smb.conf中的缺 省設置。這樣就可以針對不同的電腦提供不同的配置。

# Most people will find that this option gives better performance.
# See speed.txt and the manual pages for details
   socket options = TCP_NODELAY

  socket參數用於配置對TCP的處理方式,以適合Microsoft客戶的特征。Microsoft習慣為每個很短 的會話都建立單獨的連接,而不是使用一個連接進行多次傳輸,這樣在傳輸中就需要進行多次連接會話設 定過程,對性能有一定影響。為了改善性能,需要設置TCP_NODELAY或IPTOS_LOWDELAY選項。

# Configure Samba to use multiple interfaces
# If you have multiple network interfaces then you must list them
# here. See the man page for details.
;   interfaces = 192.168.12.2/24 192.168.13.2/24

  如果電腦上有多個網路界面,那麼就需要讓samba向多個界面廣播b-node廣播包,以及為這些 界面提供資源服務,否則samba就會只向某一個界面提供網路服務。當然也可以設置這個參數,使得samba 只服務於某個指定的網路,而不是伺服器連接所有的網路。

# Browser Control Options:
# set local master to no if you don't want Samba to become a master
# browser on your network. Otherwise the normal election rules apply
;   local master = no
 
# OS Level determines the precedence of this server in master browser
# elections. The default value should be reasonable
;   os level = 33
 
# Domain Master specifies Samba to be the Domain Master Browser. This
# allows Samba to collate browse lists between subnets. Don't use this
# if you already have a Windows NT domain controller doing this job
;   domain master = yes
 
# Preferred Master causes Samba to force a local browser election on startup
# and gives it a slightly higher chance of winning the election
;   preferred master = yes

# Use only if you have an NT server on your network that has been
# configured at install time to be a primary domain controller.
;   domain controller = <NT-Domain-Controller-SMBName>

  這些設置選項主要用於SMB網路中進行瀏覽時,設置samba伺服器的行為。預設情況不讓samba伺服器參 加broswser的推舉過程,為了使得samba伺服器能成為browser,就需要設定local master =yes。然後samba服務就可以根據os level設置的權重進行推舉,預設的os level為0,這個權重 不會贏得推舉。但可以取消注釋,將os level設置為33,這將在與所有Windows電腦(包括Windows NT) 的推舉競賽中獲得勝利,因為NT server的權重為32。設置比33更高的權重,只是在不同的samba 伺服器之間進行選擇時才有意義。

  由於Unix及Samba伺服器在同樣硬體配置下具有更高的網路性能,因此一般情況下,使用Samba伺服器 作Browser更佳。當然可能在Samba伺服器啟動之前,網路中都已經存在了Browser了,一般情況下不必重 新進行推舉過程,讓網路中的現有Browser繼續發揮作用。如果希望Samba伺服器總是成為Browser,可以 設置當Samba伺服器啟動時,迫使網路中重新進行推舉過程,這需要設定perfered master參數。

  通常Samba能夠勝任master Browser的角色,然而在多個子網的情況時,網路上就會存在Local master Browser和Domain master Browser。以上的設置只能使Samba成為 Local master Browser,預設Samba並不參加Domain master Brower的推舉 。設置Samba伺服器成為Domain master Browser就必須使用domain master參數進 行設置,這對於使用工作組方式進行跨子網瀏覽意義重大。

  但是如果網路使用的是NT的域方式,就要使用NT的域控制器作為Domain master Browser ,不要設置與NT域控制器同一個子網中的Samba伺服器的domain brower與browser功能,以免它 和域控制器競爭。不讓Samba伺服器和NT域控制器競爭的原因是由於Windows NT中域控制器還涉及域認証( 不是認証域用戶,而是認証域上的合法成員電腦),SMB協議中各種複雜功能交錯混合,瀏覽和認証兩種不同的功能竟也 混合在一起。由於域控制器完成的域認証任務,Samba伺服器不能夠完成,因此為了避免與NT域控制器相互衝突,最好 使用域控制器作Domain master Browser。

  讓Samba伺服器了解目前域中存在的域控制器的方法是指定在設置檔案中指定域基本控制器PDC(primary Domain Controller)的NetBIOS名字,這需要使用domain controller參 數進行設置。

# Enable this if you want Samba to be a domain logon server for
# Windows95 workstations.
;   domain logons = yes
 
# if you enable domain logons then you may want a per-machine or
# per user logon script
# run a specific logon batch file per workstation (machine)
;   logon script = %m.bat
# run a specific logon batch file per username
;   logon script = %U.bat
 
# Where to store roving profiles (only for Win95 and WinNT)
#        %L substitutes for this servers netbios name, %U is username
#        You must uncomment the [Profiles] share below
;   logon path = \\%L\Profiles\%U

  當用戶通過一台Windows 9x客戶機使用用戶級認証訪問網路時,他可以選擇登錄進網路中的域。這意味著 他不但要在SMB伺服器中進行驗証,而且會執行伺服器中相應目錄中的登錄腳本,以自動執行一些相關設置。通常這種登錄 功能是由Windows NT伺服器來執行的,然而Samba伺服器也能完成這個任務,但必須設定domain logons 參數。此後就可以對不同的客戶電腦或不同用戶指定相應的登錄腳本。這個功能還必須要求在本配置檔案後面設置 netlogon共享選項,以便客戶機能訪問相應的登錄腳本。

  除了登錄腳本之外,Windows NT中為了支持移動用戶在不同地點進行漫游,也提供了針對每個用戶的描述 其使用環境的Profile檔案,Samba伺服器也可以通過logon path來支持對windows客戶機這種 漫游能力的支持。這樣當Windows客戶機使用漫游功能的時候,客戶機會自動將用戶的配置檔案保存到伺服器上,此後 每次登錄進域的時候都重新下載這個配置檔案,設置Windows桌面環境。

# Windows Internet Name Serving Support Section:
# WINS Support - Tells the NMBD component of Samba to enable it's WINS Server
;   wins support = yes

# WINS Server - Tells the NMBD components of Samba to be a WINS Client
#       Note: Samba can be either a WINS Server, or a WINS Client, but NOT both
;   wins server = w.x.y.z
 
# WINS Proxy - Tells Samba to answer name resolution queries on
# behalf of a non WINS capable client, for this to work there must be
# at least one  WINS Server on the network. The default is NO.
;   wins proxy = yes
 
# DNS Proxy - tells Samba whether or not to try to resolve NetBIOS names
# via DNS nslookups. The built-in default for versions 1.9.17 is yes,
# this has been changed in version 1.9.18 to no.
   dns proxy = no

  上面的這些選項是用於設置NetBIOS名字解析方式,wins support選項使得nmdb能對外表現 為一個NBNS伺服器。wins server用於指定一個外部名字伺服器的地址(可以為NT上的wins伺服器或另 一個Samba伺服器),使得nmbd能用做NBNS客戶端,通過訪問該wins名字伺服器解析NetBIOS名字。

  此外,還有一些不使用名字伺服器進行解析的NetBIOS客戶,如果名字伺服器位於另一個子網上,那麼它們就 無法正確解析名字,而Samba可以使用wins proxy幫助它們使用名字伺服器解析地址。例如一個WINS服務 器在另一個子網上,本地子網內的Samba伺服器配置了wins server的地址,其他Windows客戶沒有設 置wins伺服器地址,它們使用b-node廣播方式查詢名字,在這台Samba伺服器設置了wins proxy能 力之後,它就能代替wins伺服器回應客戶請求。因此要用作wins代理,就必須本身能使用名字伺服器進行解析,就要 求先設置wins server參數。

  此外,NetBIOS名字解析也可以擴展到通過DNS查詢進行幫助,使用dns proxy設置就能讓Samba 伺服器通過dns進行查詢,回應NetBIOS名字查詢請求。

  • [homes]個人目錄共享
#============================ Share Definitions ==============================
[homes]
   comment = Home Directories
   browseable = no
   writable = yes

  [homes]部分使得每個Unix用戶通過SMB客戶登錄上來的時候,可以共享使用他自己的個人目錄。這個 共享資源具備特別屬性,當用戶登錄上之後,共享名就不是homes,而是被伺服器映射為用戶自己的標識符。

  設置這個特別的共享選項,就允許每個用戶訪問自己的個人目錄,而不必為每個用戶都配置一個共享資源部分。正由 於系統會自動映射,因此就不需要定義要共享的檔案目錄路徑,而在其它檔案資源共享選項中,都需要使用path參數進行 定義。

  由於Samba系統會自動將homes共享名改變為用戶的個人標識符,因此要設定browseable=no ,設置homes本身這個名字不出現在資源列表中。writable為用戶寫權限設置,由於是在用戶的個人目錄下,用 戶可以具備寫權限。

  • [netlogon]與[Profiles]
# Un-comment the following and create the netlogon directory for Domain Logons
; [netlogon]
;   comment = Network Logon Service
;   path = /usr/local/samba/lib/netlogon
;   guest ok = yes
;   writable = no
;   share modes = no

  當允許Samba伺服器支持客戶的網路登錄功能之後(設置domain logon),就需要設置[netlogon] 部分(刪除注釋符號),為guest用戶打開登錄路徑的訪問權限,以保証每個用戶都能訪問其自己的登錄腳本 。由於需要使用guest對應的Unix帳戶訪問登錄腳本,這個登錄路徑及其下面的腳本檔案都要允許該Unix帳號可 以讀取。

  建立這個目錄之後,要設定正確的權限,並且創建這個目錄下對應各個電腦或用戶的登錄腳本,以提供給客戶正確 的登錄腳本。

# Un-comment the following to provide a specific roving profile share
# the default is to use the user's home directory
;[Profiles]
;    path = /usr/local/samba/profiles
;    browseable = no
;    guest ok = yes

  當支持Windows電腦漫游能力時,可以設定[Profiles]部分,並為相應的路徑建立目錄,及分配權限。 來為Window電腦用戶設置桌面環境。

  • [Printers]印表機設置
# NOTE: If you have a BSD-style print system there is no need to
# specifically define each individual printer
[printers]
   comment = All Printers
   path = /var/spool/samba
   browseable = no
# Set public = yes to allow user 'guest account' to print
   guest ok = no
   writable = no
   printable = yes

  這個部分就用於設置將printcap中定義的所有印表機使用的相關設置,預設情況下允許所有的合法客戶使用 所有的印表機。BSD風格的打印系統,不需要任何設置就能正確共享所有的印表機,當然也可以使用printer參數指 定具體的印表機以提供共享。而path參數定義的是印表機緩衝區的位置。guest ok、writeable用於設 置正確的權限,而printable用於設置打印屬性。

  由於Windows的打印驅動已經將要打印的檔案轉化為印表機支持的那種描述語言,因此這些打印檔案不再需要 任何過濾器進行轉換。因此需要在printcap中定義一個不使用任何過濾器、直接將打印文檔輸出到印表機端口的打印 機選項。

  • 其他專有共享目錄
# This one is useful for people to share files
;[tmp]
;   comment = Temporary file space
;   path = /tmp
;   read only = no
;   public = yes
 
# A publicly accessible directory, but read only, except for people in
# the "staff" group
;[public]
;   comment = Public Stuff
;   path = /home/samba
;   public = yes
;   writable = yes
;   printable = no
;   write list = @staff
 
# Other examples.
#
# A private printer, usable only by fred. Spool data will be placed in fred's
# home directory. Note that fred must have write access to the spool directory,
# wherever it is.
;[fredsprn]
;   comment = Fred's Printer
;   valid users = fred
;   path = /homes/fred
;   printer = freds_printer
;   public = no
;   writable = no
;   printable = yes
 
# A private directory, usable only by fred. Note that fred requires write
# access to the directory.
;[fredsdir]
;   comment = Fred's Service
;   path = /usr/somewhere/private
;   valid users = fred
;   public = no
;   writable = yes
;   printable = no
 
# a service which has a different directory for each machine that connects
# this allows you to tailor configurations to incoming machines. You could
# also use the %U option to tailor it by user name.
# The %m gets replaced with the machine name that is connecting.
;[pchome]
;  comment = PC Directories
;  path = /usr/pc/%m
;  public = no
;  writable = yes
 
# A publicly accessible directory, read/write to all users. Note that all files
# created in the directory by users will be owned by the default user, so
# any user with access can delete any other user's files. Obviously this
# directory must be writable by the default user. Another user could of course
# be specified, in which case all files would be owned by that user instead.
;[public]
;   path = /usr/somewhere/else/public
;   public = yes
;   only guest = yes
;   writable = yes
;   printable = no
 
# The following two entries demonstrate how to share a directory so that two
# users can place files there that will be owned by the specific users. In this
# setup, the directory should be writable by both users and should have the
# sticky bit set on it to prevent abuse. Obviously this could be extended to
# as many users as required.
;[myshare]
;   comment = Mary's and Fred's stuff
;   path = /usr/somewhere/shared
;   valid users = mary fred
;   public = no
;   writable = yes
;   printable = no
;   create mask = 0765

  此後,預設smb.conf中給出了一些設定各種共享檔案資源、印表機資源的例子,例如設置特定印表機的 [fredprn],設置私人檔案共享資源的[freddir]和[myshare],使用宏為多個用戶設置共享的[pchome], 提供公共訪問的[public]和[tmp],FreeBSD的管理員可以根據具體情況,根據這些設置例 子設置相應的共享資源,並設置相應的訪問權限,以保護檔案系統不被非法訪問。

  需要注意的是,當使用valid user用來設置合法訪問用戶時,或者使用wirte list定義具備寫 權限的用戶時,都可以使用@staff的形式使用Unix的組名,這樣凡是屬於該staff組成員的用戶,都會具備相 應權限。

  從這些例子中可以看到[global]用於設定全局參數,不會出現在資源列表中,[homes]用於設定個人 目錄共享,其共享的名字映射為個人標識符而非homes,[printers]設定印表機共享,將共享所有的印表機名 字,除了這些標題的共享名字與標題不同之外,其他每個標題都將代表一個共享資源的名字。

  當更改設置之後,可以重新啟動Samba伺服器,提供新的共享服務了。但在重新啟動伺服器之前,最好使用 Samba軟體包中提供的測試軟體,檢查一下設置是否正確。這些小工具程序包括檢查smb.conf設置的testparm ,檢查印表機名字的正確性的程序testprn,用於進行NetBIOS名字解析的nmblookup等。

  • 使用swat配置samba

  samba 2.0提供了一個能夠通過瀏覽器來配置samba的工具──swat。它是一個專用www伺服器,使用inetd 來啟動,然後經過認証,可以允許用戶通過瀏覽器來配置smb.conf。為了達到這個目的,首先要為swat分配一個 固定的端口,這個工作可以通過在/etc/services中增加一行來完成:

swat    901/tcp

  出於安全的考慮,應該使用1024以下的端口,這是因為非root用戶也能占用1024以上的端口,如果使用 1024之上的端口,可能在某些情況下,普通用戶就可能通過欺騙的方法獲得管理員的密碼。

  為了啟動swat,需要在inetd.conf中增加一個入口,並重起inetd:

swat  stream    tcp  nowait  	root /usr/local/sbin/swat     swat

  然後就能使用Netscape等瀏覽器來配置samba了,為了避免認証密碼通過網路傳輸,應該在本地啟動瀏 覽器(並進一步可以使用tcp_wrapper提供保護)。輸入URL為:http://localhost:901 /,瀏覽器將提示用戶名和密碼對用戶進行認証,可以輸入任意一個Unix用戶進入swat頁面,但普通用戶只能讀取 samba設置,而不能更改,為了設置samba,必須使用root用戶進行認証。一些功能簡單的瀏覽器不支持瀏覽器方 式的用戶認証,就無法使用swat設置Samba。

  由於改變smb.conf對於系統安全有嚴重的影響,因此使用swat一定要小心。為了防止進行swat認証 時root密碼被竊聽,不應該在遠程啟動瀏覽器進行設置,應該在控制台或本地安全網路內進行設置。而且一旦設置完畢, 應該立即退出瀏覽器,因為瀏覽器在內存中保存用戶的認証信息,直到瀏覽器退出。

  注意,swat將刪除原有smb.conf中的全部注釋,並重新安排各個設置項的位置,因此應該在使用swat 設置之前備份原有的smb.conf。而且swat只是提供了一個容易使用的設置界面,並不能保証設置一定就是正確 的,因此還是要在理解smb.conf的基礎上才能正確設置。

  swat提供7個不同功能的頁面,提供管理員訪問。主頁面HOME,提供了對Samba文檔的連接,用於管理 員實時查看相關手冊﹔GLOBALS頁面提供對smb.conf中[globals]部分中的各個參數進行設置﹔SHARES 用於選擇、增加、刪除各個共享資源,查看並更改其設置選項﹔PRINTERS用於設置印表機選項﹔STATUS 用於檢查目前Samba伺服器的狀態,包括客戶電腦的連接狀態和用戶的連接信息,管理員可以使用它來重新啟動 Samba,這樣就能在更改設置之後使新設置生效﹔VIEW用於查看目前設置的smb.conf的內容﹔PASSWORD 用於管理加密密碼,提供加密認証情況下增加和管理用戶,並可以保持Unix和Windows NT的密碼一致,這個功 能只在使用了加密密碼功能之後才有用,更改的密碼將直接保存在/usr/local/private目錄下的smbpasswd 檔案中。PASSWORD功能將不但更改smb.conf,還將更改Samba使用的密碼選項,因此對於使 用加密密碼認証方式的系統,最為有用。

  使用SWAT,設置Samba就非常容易,不再需要手工編輯設置檔案,而可以直接使用瀏覽器更改設置,並重新 啟動Samba伺服器。