当前位置:Linux教程 - Samba - samba - 配置samba

samba - 配置samba

配置samba
2004-04-23 15:18 pm
来自:Linux文档
现载:Www.8s8s.coM
地址:无名

  虽然预设情况已经适合大部分用户访问自己在伺服器上的个人目录的要求,但是为了 充分发挥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 = \%LProfiles\%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伺服器。