SAMBA服务器的配置
前 言
这节课带同学们去跳一种热情有劲的巴西SAMBA舞蹈。其实这是一个网络服务器,我们利用SAMBA可以把LINUX计算机变成一个MICROSOFT网络的一部分。在网络中,SAMBA如果用的得心应手将会超出真正“SAMBA”舞蹈的魅力……下面我们开始。
SAMBA历史
最开始的时候,文件访问用的是FTP服务,但有一个问题是FTP,只能把远程主机的文件下载到本地来修改,而不能在联接过程中直接在远程服务器上修改。如果有需要还要再传回服务器上去。这样时间长了,你也不知道哪个是已经改完的了,哪个是没有改完的。后来就出现了一些别的协议可以让联接的客户主机直接在远程计算机上来做文件的修改。这些协议比如LINUX下的NFS(LINUX下的网上邻居以后的课程会细说)和微软的common internet file system(CIFS公用网际文件系统)微软的网上邻居用的就这种协议。但以上的两种协议都是各为其主。一个为LINUX一个为WINDOWS,那么能不能有一个沟通这两个不同平台相互分享数据的文件系统呢?在91年的时候,一个名叫Andrew Tridgwell的大学生开发出了server message block(SMB服务器信息块协议)也可以把它看作一个文件系统。可以用来沟通微软与UNIX及UNIX-LIKE系统。既然这个大学生写成了软件就要来注册一下商标,开始想用SAMBASERVER做为商标,但商标法规定必须注册的商标是一个有意义的文字,即现实存在的事物。在字典中翻了好久,终于找到一个好听点的名字――SAMBA。因为里面包含SMB三个字母。呵呵
Netbios通信协议
为什么要说这个network basic input/output system协议。因为就像NFS是架构在RPC(远程过程调用协议)上一样,SAMBA这个文件系统是架构在netbios这个通信协议之上的。netbios协议最早是IBM开发,目的是仅让局域网内少数计算机进行网络连接的一个协议,所以考虑的角度不是针对大型网络。因此这个协议是无法跨路由的。这个协议在局域网内的确是很好用,所以微软的网络架构就是用它来进行沟通。比如微软的网上邻居CIFS就是架构在netbios上的。依照微软的意思,CIFS是为客户系统在网络上向服务器请求文件和打印服务的开放跨平台的运行机制。它是建立在广泛应用于个人电脑和工作站等操作系统的标准服务器消息块(SMB)协议。
实际上,SMB是一个通过网络在共享文件,设备,命名管道和邮槽之间操作数据的协议。CIFS是SMB的一个公共版本。而SAMBA的发明人从最开始的目标就是想把UNIX系统加入到WINDOWS的网络,所以自然要以netbios为基础协议来构架。那么有同学就会认为,那netbios是无法跨越路由的。那SAMBA是不是也是无法跨路由的?当前的SAMBA应用多是在LAN内。但如果有特殊应用也能跨越路由器,因为当前一种跨路由的netbios协议即netbios over tcp/ip。
SAMBA使用的DAEMONS
Smbd:这个daemon的主要功能就是用来管理SAMBA主机共享什么目录、文件与打印机等信息。
Nmbd:这个DAEMON则是用来管理组和NETBIOS NAME解析的。
所以一个SAMBA服务器启动时,会打开上述两个守护进程。同时主机会打开137、138、139三个端口,且同时有UDP/TCP的监听服务。
SAMBA的配置文件
一、/etc/samba/smb.conf主配置文件,此文件中有丰富的说明注释
二、/etc/samba/lmhosts此文件的主要用途是对应netbios name与主机名的IP地址。有点像/etc/hosts的功能,但这个文件对应的是NETBIOS名,不是FQDN,不要混了。但现在通常SAMBA的功能很强大,一般开机就能自动检测到netbios name,所以此文件不太重要了。
三、/etc/samba/smbpasswd此文件默认不存在,是SAMBA默认的用户/密码对应表。也就是SAMBA用户和密码存放的文件。此文件拥有人是ROOT,权限一般是600
服务器配置概要
将Linux服务器配置成Samba服务器的步骤:
(1)配置/etc/samba/smb.conf文件(SAMBA的主配置文件)
(2)使用smbpasswd命令使Linux系统用户成为Samba用户,命令格式如下:
smbpasswd –a 用户名
该命令也可用来修改Samba用户的密码。
临时关闭SMB可用参数-d 启用可用-e 彻底删除用-x
(3)要想能用NETBIOS名来让LINUX SAMBA以命令行来访问WINDOWS机器,我们要修改/etc/samba/lmhosts文件。把IP与NETBIOS名对应起来。
主配置文件的大体设置
以井号(#)和分号(;)开始的行和空行被忽略,但分号后面的是有意义的设置只要去除分号即生效,但#号后的是完全没有意义的说明。以外斜线(\)结束的行将在下一行继续。
段名和参数名不分大小写。
[global] 段
在这个段的参数是全局有效的。
workgroup
说明:这个参数用来指定samba所要加入的工作组,另外如果在设置了security=domain,则workgroup可以指定域名。例如:
workgroup = wgp1
指明工作组为wgp1 (一般与WIN所在工作组一致,否则查找速度将减慢)
netbios name
说明:这个参数指定samba的netbios名,可以不设置,samba将会使用机器的DNS名的第一部分,如果你的机器的DNS名是host1.domain,就用host1 例如:
netbios name = public
指定NetBios名为public
server string
说明:这个参数指定在浏览列表里的机器描述,和MS WINDOWS 里配置网络时的描述是一样的。可以是任何字符串。也可以不填,samba会用缺省的 samba %v,即samba 尾随它的版本号,不过如果要模仿NT象一点的话,可以填一下。例如:
server string = Master File Server
hosts allow = 192.168.1. 192.168.2. 127. ;允许访问的计算机的IP地址。具体限制语句格式为,以.开头可加网域,如.redhat.com 以.结尾可加网段IP,如 192.168.1. 或者写成标准的写法也可以如:0.0.0.0/0.0.0.0或0.0.0.0/32。一定要注意当ALLOW与DENY同时出现时允许优先,与WINDOW的DENY优先不同。
安全参数
security
说明:这是个重要的安全配置参数,有4个值,分别是share、user、server和domain,定义了samba的基本安全级,通常是 user
security = user (注意仅对WINDOWS客户生效。本身的LINUX客户无效)
这是samba的默认配置,这种情况下要求用户在访问共享资源之前资源必须先提供用户名和密码进行验证。
securtiy = share
这是几乎没有安全性的级别,任何用户都可以不要用户名和口令访问服务器上的资源。
security = server
和user安全级类似,但用户名和密码是递交到另外一个SMB服务器去验证,比如递交给一台NT服务器。如果递交失败,就退到user安全级,从用户端看来,server和user这两个级别是没什么分别的。
security = domain
这种安全级别要求网络上存在一台NT PDC,samba把用户名和密码递交给NT PDC去验证。
从用户端看来,user级以上的安全级其实是没什么分别的,只是服务器验证的方式不同,但这三种安全级都要求用户在本Linux机器上也要系统帐户。否则是不能访问的。
另外在RED HAT企业 版中还有一个选项是ads,为正在联接一个WINDOWS 2000/2003活动目录中的一个member server。
log file = /var/log/samba/%m.log.会给用户在此目录下做每个SAMBA用户登录的日志。并以每个访问客户主机的NETBIOS主机名作为日志名。
encrypt passwords = yes(加密明文的SAMBA密码进行发送,因为WINDOWS主机都是加密的,明文要改注册表。)
smb passwd file = /etc/samba/smbpasswd验证密码所在的目录
interfaces = 192.168.13.0/24 192.168.1.0/24即在SMB服务器上有多块网卡的时候,设置我们的SMB服务针对哪个网段。默认是所有网段。
wins support = yes把该服务器配置为WINS服务器
wins server = 192.168.1.100也可以指定一个WINS服务器,让自己成为WINS客户
共享资源设置参数
(这部分是对每个共享目录或者设备进行权限方面的设定)
comment
说明:就是对共享的描述,可以是任意的字符串
例如
comment = Share Stuff
path
说明: path是提供共享服务的路径,可以用%u %m这样的宏来代替路径里的unix用户和客户机的Netbios名。
例如:如果我们不打算用home段做为客户的共享,而是在/home/share/ 下为每个Linux用户以他的用户名建个目录,作为他的共享目录,这样path就可以写成
path = /home/share/%u;
用户在连接到这共享时具体的路径会被他的用户名代替,要注意这个用户名路径一定要存在,否则,客户机在访问时会找不到网络路径。
writeable 说明:指定共享路径是否可以写,值是yes或no(与read only参数有些抵触,如果READ ONLY为YES,而WRIEABLE为NO时,谁在后面出现听谁的)
browseable
说明:指定共享是否可以浏览,默认是yes
available
说明:指定共享资源是否可用,默认是yes,设为no则关闭该资源的共享服务,用户无法连接到该资源上。
高级参数的配置
宏
在基本配置里有提到一些参数的可以尾随%u、%m,这是samba里定义的宏,宏用百分号后面跟一字符表示,在具体运作的时候就用实际的参数来代替。常用的宏有
%S = 当前登录的用户名,用于替代共享名
%P = 当前服务的根路径
%u = 当前登录的用户名
%g = 给定%u的所在的主工作组名
%H = 给定的%u的宿主目录
%v = Samba 版本号
%h = 运行Samba的机器的主机名(注意是HOSTNAME即HOSTS中的名字不是NETBIOS名,即LMHOSTS中的名字)
%m = 客户机的NETBIOS名
%M= 客户机的HOSTNAME名
%L = SAMBA服务器的 NetBIOS 名
%T = 当前的日期和时间
%iI=客户当前IP地址。
灵活地应用宏可以很方便地管理比较复杂的网络
共享资源参数
admin users
这个参数用来赋予用户对共享资源的管理员权限,意味着这些用户可以root一样进行所有的文件操作,当然只局限在这个共享资源上。这个选项要慎用,因为任何这样的用户可以对该共享上的文件随意进行操作。缺省是admin users = no。
例如:
admin users = Su_27
那么Su_27这个用户就是这个共享资源的管理员。
valid users
说明:指定共享资源的有效用户,即允许访问该资源的用户。
例如:valid users = user1, user2
那么user1和user2是有效用户(也可以加@组名来设置一个用户组)
invalid users
说明:和valid users 相反,指定那些用户不可访问这共享资源。
public
说明:这个参数指明是否允许匿名帐户访问,值为yes或no,默认为NO。即匿名帐号无法访问。如果为YES则用户访问共享目录时不用输入密码。另一个和public相同意义的参数是guest ok。
max connections (S)
说明:指定一个服务的最大连接数目,用户连接到该服务时如这个服务的连接数目已经达到指定值,这个新的连接请求将被拒绝。
例如: max connections = 100
最大连接数为100
Samba服务器配置一例
# 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]
# workgroup = NT-Domain-Name or Workgroup-Name
workgroup = Workgroup ;计算机所在的工作组名或域名
netbios name = fileserver ;即在,网上邻居中显示的计算机名
# server string is the equivalent of the NT Description field
server string = Samba Server ;计算机描述
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. ;允许访问的计算机的IP地址
; hosts allow = 172.16.0. 172.16.3. 127.
# if you want to automatically load your printer list rather
# than setting them up individually then you'll need this
; printcap name = /etc/printcap加载打印机的配置路径
load printers = no是否将打印机共享
# It should not be necessary to spell out the print system type unless
# yours is non-standard. Currently supported print systems include:
# bsd, sysv, plp, lprng, aix, hpux, qnx
; printing = cups一种打印驱动模式,即LINUX支持的标准打印类型。其LINUX支持的打印类型有bsd, sysv, plp, lprng, aix, hpux, qnx
# Uncomment this if you want a guest account, you must add this to /etc/passwd
# otherwise the user "nobody" is used
; guest account = nobody
guest account = guest ;允许访问(不需要用户名与密码)也可以指定任何系统内置的一个帐号做为来宾帐号。
# this tells Samba to use a separate log file for each machine
# that connects
log file = /var/log/samba/%m.log.会给用户在此目录下做每个SAMBA用户登录的日志。
# Put a capping on the size of the log files (in Kb).
max log size = 50日志 文件限制在50K内
# Security mode. Most people will want user level security. See
# security_level.txt for details.
security = share
; security = user
# Use password server option only with security = server
; password server = 此行的作用为如果上面的选项为SERVER,则SAMBA服务器在做身份识别的时候,把WIN机器发过来的密码同用户名交给此项设定的密码服务器验证。
# Password Level allows matching of _n_ characters of the password for
# all combinations of upper and lower case.
; password level = 8
; username level = 8(对于从前的几款老式的WINDOWS系统对于大小写密码支持的不是很好,所以开启上面的两个参数的意义是在密码为8位的用户名与密码的帐号里自动匹配所有的大小写测试)
# 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(加密明文的SAMBA密码进行发送)
smb passwd file = /etc/samba/smbpasswd验证密码所在的目录
# The following are needed to allow password changing from Windows to
# update the Linux sytsem password also.
# NOTE: Use these with 'encrypt passwords' and 'smb passwd file' above.
# NOTE2: You do NOT need these to allow workstations to change only
# the encrypted SMB passwords. They allow the Unix password
# to be kept in sync with the SMB password.
; unix password sync = Yes
; passwd program = /usr/bin/passwd %u
; passwd chat = *New*UNIX*password* %n\n *ReType*new*UNIX*password* %n\n *pass$
# Unix users can map to different SMB User names
; username map = /etc/smbusers
# 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 = /etc/smb.conf.%m
# Most people will find that this option gives better performance.
# See speed.txt and the manual pages for details
socket ptions = TCP_NODELAY
# 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
# Configure remote browse list synchronisation here
# request announcement to, or browse list sync from:
# a specific host or from / to a whole subnet (see below)
; remote browse sync = 192.168.3.25 192.168.5.255
# Cause this host to announce itself to local subnets here
; remote announce = 192.168.1.255 192.168.2.44
# 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 =
# 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
# All NetBIOS names must be resolved to IP Addresses
# 'Name Resolve Order' allows the named resolution mechanism to be specified
# the default order is "host lmhosts wins bcast". "host" means use the unix
# system gethostbyname() function call that will use either /etc/hosts OR
# DNS or NIS depending on the settings of /etc/host.config, /etc/nsswitch.conf
# and the /etc/resolv.conf file. "host" therefore is system configuration
# dependant. This parameter is most often of use to prevent DNS lookups
# in order to resolve NetBIOS names to IP Addresses. Use with care!
# The example below excludes use of name resolution for machines that are NOT
# on the local network segment
# - OR - are not deliberately to be known via lmhosts or via WINS.
; name resolve rder = wins lmhosts bcast
# Windows Internet Name Serving Support Section:
# WINS Support - Tells the NMBD component of Samba to enable it's WINS Server
; wins support = yes把该服务器配置为WINS服务器
# 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 = 192.168.1.100也可以指定一个WINS服务器,让自己成为WINS客户
# 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(针对老式计算机,不用设置)
# Case Preservation can be handy - system default is _no_
# NOTE: These can be set on a per share basis
; preserve case = no
; short preserve case = no
# Default case is normally upper case for all DOS files
; default case = lower
# Be very careful with case sensitivity - it can break things!
; case sensitive = no
(整个上面这段为讨论与WINDWOS文件系统的大小写问题因为微软的系统是一个大小写不区分 的系统,默认用默认值即可。因为修改可能造成与WINDOWS的不兼容问题)
#============================ Share Definitions ==============================
[homes]作为登录用户,自己访问自己目录的设置。此目录比较特殊没有PATH选项。
; comment = Home Directories目录说明
; browseable = no用户私人目录不需要给别人浏览。
; writable = yes也可以用read nly=no来替代,即用户本身可以写自己的家目录。
# Un-comment the following and create the netlogon directory for Domain Logons
; [netlogon]域用户登录目录设置。
; comment = Network Logon Service
; path = /home/netlogon
; guest k = yes
; writable = no
; share modes = no
# Un-comment the following to provide a specific roving profile share
# the default is to use the user's home directory
;[Profiles]
; path = /home/profiles
; browseable = no
; guest k = yes
# 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 k = no
writable = no
printable = yes用户是否可以打印
# This one is useful for people to share files
;[tmp]
; comment = Temporary file space
; path = /tmp
; read nly = no可以写入
; public = yes即目录是否共享给来宾帐号。
# A publicly accessible directory, but read only, except for people in
# the "staff" group
[public]
comment = Public Stuff
path = /home/soft
public = yes
guest k = yes以上两个public=yes代表来宾帐号能访问此共享目录,但guest ok代表密码不是必须的,可以不加。
Write list = user[@group]即哪些人对此目录有写权限,用户或组超过一个用空格格开。
printable = no
write list = @staff此参数告诉我们不必要让所有人访问共享目录,可以有staff这个组的成员能访问,但在passwd和shadow文件中要存在这个组。
# 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] (为创建一个私有目录,别人都不能访问,只有fred这个用户能访问)
; 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 stuff
; public = no
; writable = yes
; printable = no
; create mask = 0765即客户进入SMB服务器后创建文件时的默认权限
; directory mask = 0770 即客户进入SMB服务器后创建目录时的默认权限。
我们可以自行在此处添加你共享的目录。并按上面的例子进行相应设置可以 了。
待设置都设置好后,可以用testparm命令测试配置文件的正确性
LINUX客户端如何访问WINDOWS资源
(1)用smbtree 可以查看三层网段中的所有在线计算机,类似于WIN的网上邻居。提示输入密码的时候直接按回车即可。Smbtree –b即用广播查询不用WINS浏览器。
(2)用nmblookup netbios名,可以查看具体的WIN主机的IP。可以用nmblookup \*来看当前活动的主机都有谁
(3)使用smbclient –L netbios名或IP地址 –U username 用于显示指定主机的共享目录.其中密码可以和用户名一起写成-U username%password这样就不提示我们输入密码了。
smbclient //netbios名或IP地址/共享名 -U 用户名 以用户身份来访问共享资源
(3)smbmount来挂载远程共享
先创建一个挂载点,mkdir /mnt/winshare
将远程共享挂载到本地挂载点 smbmount //netbios名/共享资源目录 /mnt/winsare -o username=username%password
解除挂载,要用umount
(4)用smbstatus命令可以检测本机共享资源被使用的情况,如谁正在使用共享资源。
(5)配置SAMBA打印共享
一般在图形界面下做,因为配置很简单,在此不再声明。只要将打印机安装成SMB打印机即可。配置完成后检测打印机配置文件的正确性用到如下命令:
Testprns lp0 /etc/printcap即可查看
扩展知识点:
我们都知道,SMB帐号必须由系统帐号升级而来,那么网络内部有些人想攻击我们的SMB服务器就会根据SMB帐号猜测系统帐号。所以不是很安全,那么我们就可以用到用户帐号映射选项来解决这个问题。即
1、 把主配置中的username map = /etc/samba/smbusers前的分号去掉。
2、 然后编辑/etc/samba/smbusers文件。在里面加上映射之后的名字
3、 有些系统里不支持smbuseradd unixname:mapname的命令行,所以还要用smbpasswd命令添加用户名和密码。然后重启系统
4、 告之内网SMB用户映射之后的名字即可。这样就实现了安全性。
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/21173334/viewspace-591807/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/21173334/viewspace-591807/