资源描述
实训指导书三: Samba服务器配置及访问Samba共享一. 实训目的:1掌握rpm命令安装软件包的方法2了解samba服务器的配置文件的编辑和使用方法;3 掌握Samba服务器的安装和配置方法;4 使用samba共享用户认证和文件系统;5掌握Linux与Windows的资源共享和互访方法。二. 实训内容:1 利用rpm命令安装软件包的方法;2 安装、配置Samba服务器并启动;3 从Linux访问Windows资源;4 从Windows访问Linux的资源;5 Samba项目练习。三.实训练习:1. SMB协议和Samba简介1.1 SMB协议为了使Windows 主机间的资源能够共享,微软于1980年开发了SMB(Server Message Block服务信息块)通信协议,并通过SMB通信协议,使网络上各台主机之间能够共享文件、打印机等资源;目前类似这种资源共享的通信协议还有NFS、Appletalk、Netware等。1.2 Samba简介在局域网中,Windows 主机彼此间可利用“网上邻居”来访问共享的资源,NFS也能使Linux 主机之间实现资源共享。但如何能使Windows 主机和Linux主机之间实现资源共享呢?微软的“网上邻居”就是Windows 上利用SMB通信协议实现资源共享的程序。通过使用“网上邻居”,用户在网上共享资源的操作简单方便。有鉴于此, Linux 爱好者开始在Linux 主机上实现SMB通信协议。Samba是用来实现SMB的一种软件,由澳大利亚的Andew Tridgell开发,是一种在Linux 环境里运行的自由软件。Samba的工作原理是让NetBIOS和SMB运行在TCP/IP之上,且使用NetBIOS的nameserver让Linux机器可以在Windows 网络邻居里被浏览。它使得Windows 主机和Linux主机之间实现了资源共享。1.3 Samba软件的功能SMB通信协议采用的是Client/Server架构,因此Samba 软件可以分为客户端和服务器端两部分。通过执行Samba 客户端程序,Linux 主机便可以使用网络上Windows 主机所共享的资源;而在Linux 主机上安装Samba 服务器,则可以使Windows 主机访问Linux Samba 服务器共享的资源Samba 提供了以下功能:(1)共享Linux 的文件系统(2)共享安装在Samba 服务器上的打印机(3)使用Windows 系统共享的文件和打印机(4)支持 Windows 域控制器和Windows 成员服务器对使用Samba 资源的用户进行认证(5)支持WINS名字服务器解析及浏览(6)支持SSL安全套接层协议2. Samba实训在本次试验开始之前,请确保把Linux的防火墙规划清掉,让相应的端口通过。任务一 Samba安装和启动 检测系统是否安装了samba服务,如果没有安装挂载Linux第一张安装光盘,利用rpm ivh命令安装samba,samba-common和samba-client RPM 软件包并且启动smb服务。任务一步骤:1)如果用户在安装Linux 时已经安装了Samba ,就无需再安装了,可以通过下列命令确定是否已经安装了Samba 。查询系统是否安装Samba:# rpm qa | grep samba samba-commom -2.2.7a-7.9.0 samba-2.2.7a-7.9.0 samba-client-2.2.7a-7.9.0 redhat-config-samba-1.0.4-1 samba-swat-2.2.7a-7.9.0 若出现这5个软件包,则表示已经安装了Samba ,否则必须安装Samba 。2)单击菜单栏“虚拟机”,选择“可移动设备CD-ROM” ,载入第一张ISO映像文件,例如: D:RedHatLinux_ISOFilesRedHatLinux9_i386_disc1.iso,将光盘挂载到 /mnt/cdrom目录;# mount /dev/cdrom /mnt/cdrom 3)切换到放置rpm文件的目录;# cd /mnt/cdrom/RedHat/RPMS 4)Samba 必备的程序,必须安装;# rpm -ivh samba-commom -2.2.7a-7.9.0i386.rpm 5)安装Samba 服务器程序,可以使Windows 主机访问Samba 服务器共享的资源;# rpm -ivh samba-2.2.7a-7.9.0.i386.rpm 6)安装Samba 客户端程序,可以使Linux 主机访问Windows 主机上的共享资源;# rpm -ivh samba-client-2.2.7a-7.9.0.i386.rpm 或者4),5)和6)合为如下操作:# rpm ivh /mnt/cdrom/RedHat/RPMS /samba-c*# rpm ivh /mnt/cdrom/RedHat/RPMS /samba-2*7)启动smb服务;#service smb start当能看到下面的显示,就表示启动成功。Starting SMB service 确定Starting NMB service 确定启动smb服务也可以使用命令:# /etc/init.d/smb start8) 停止smb服务 ;#service smb stop或者:# /etc/init.d/smb stop8) 重启smb服务# /etc/init.d/smb restart或者:#service smb restart任务二 Windows资源共享与使用 1Windows资源共享: 1)使用TCP/IP协议作为Windows系统网络默认通讯协议;2) 在主机Windows操作系统中的DOS命令模式下运行ipconfig /all指令得到本地连接的IP地址和子网掩码,例如:192.168.1.6,255.255.255.0;3)在虚拟机中以root用户登录,确保把Linux的防火墙规划清掉。启动终端,使用ifconfig指令对虚拟机的网络地址进行配置,将虚拟机和主机配置在一个网段:(具体地址视实际情况而定);#man ifconfig (查看ifconfig命令的用法)#ifconfig (查看当前虚拟网卡配置)#ifconfig eth0 192.168.1.16 netmask 255.255.255.0 (配置当前虚拟网卡)#ifconfig eth0 up(启动网络连接)如果能ping通,表明连接正常。如果不能ping通,检查Windows主机防火墙配置(是否防止Ping入);4) 设置好Windows系统计算机名(例如:WINSERVER)和所属工作组(例如:WORKGROUP); 6)在Windows系统建立文件夹smb_win_test,使用写字板建立文件windows_test.txt,内容为:“This is a file for the Linux smb test!”,共享文件夹smb_win_test ;7)在Windows系统建立用户test,密码为:“123456”2Windows共享资源的使用:1)确认Linux虚拟机和Windows主机配置在一个网段上,网络连接正常;2)在Linux虚拟机上启动smb服务;# service smb start当能看到下面的显示,就表示启动成功;Starting SMB service 确定Starting NMB service 确定3) 在Linux计算机上,执行以下命令;# smbclient -L 192.168.1.6将在Linux计算机上列表显示出Windows系统计算机WINSERVER所提供的所有共享信息;4) 在Linux计算机上,执行以下命令;# smbclient /192.168.1.6/smb_win_test -U test 其中test是Windows 计算机上的用户,系统提示输入test的密码,输入“123456”, 密码验证正确后,系统提示:smb: 此时表明已正确登录到Samba服务器上,可以用smbclient的指令,象用FTP指令一样上传和下载文件。smbclient 命令说明命令 说明?或help command提供关于帮助或某个命令的帮助;!shell command执行所用的SHELL命令,或让用户进入 SHELL提示符;cd 目录切换到服务器端的指定目录,如未指定,则返回当前本地目录;lcd 目录切换到客户端指定的目录;dir 或ls 列出服务器端当前目录下的文件;exit 或quit 退出smbclient;get file1 file2 从服务器上下载file1,并以文件名file2存在本地机上;如果不想改名,可以把file2省略;mget file1 file2 file3 filen 从服务器上下载多个文件;md或mkdir 目录在服务器上创建目录;rd或rmdir 目录删除服务器上的目录put file1 file2向服务器上传一个文件file1,传到服务器上改名为file2;mput file1 file2 filen 向服务器上传多个文件5)使用md命令在Windows服务器上建立“passwd_bak”目录,将Linux系统中/etc/passwd,/etc/group和/etc/shadow等3个文件上传到Windows服务器的“passwd_bak”目录中,在Windows系统中使用写字板查看这3个文件。写出操作命令和过程,并加以验证。6)在Linux系统中/root目录下建立目录 /smb_linux_test,将Widows系统中在 “smb_win_test”目录下的文件 windows_test.txt下载到本机/root /smb_linux_test目录下,更命为windows_test_to_linux.txt,在Linux系统中使用gedit查看这个文件。写出操作命令和过程,并加以验证。7)使用smbmount命令知道某台主机所共享的资源后,执行smbmonut命令将远程共享挂载到本地。先创建挂载点目录:# mkdir p /mnt/smb/win_share_dir将远程共享/192.168.1.6/smb_win_test挂载到本地目录/mnt/smb/win_share_dir# smbmount /192.168.1.6/smb_win_test /mnt/smb/win_share_dir# cd /mnt/smb/win_share_dir这样就可以象访问本机目录一样操作此挂载的目录了。若要卸载已挂载的目录,则执行umount命令即可。# umount /mnt/smb/win_share_dir任务三 samba服务器的配置文件的编辑和使用方法Samba最主要的配置文件是 /etc/samba/smb.conf,其中有很多选项可以设置,但作为初学者,只需要掌握其中一小部分选项的设置就可以配置所需要的Samba服务器了。在了解基本配置的基础上,就能很快架设一个基本的Samba服务器。在基本配置文件中,也只用到其中一小部分命令选项。 全局参数的配置在global字段中,没有出现(或被注释)的参数,Samba 采用的是默认配置。 1)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 #= Global Settings = global #这是配置文件中关于全局参数的设置部分。 workgroup = SMBGROUP #这是设置服务器所要加入的工作组的名称,会在Windows 的“网上邻居”中能看到MYGROUP工作组,可以在此设置所需要的工作组的名称。netbios name=smb-server #设置出现在“网上邻居”中的主机名。默认情况下,则使用真正的主机名。 server string = Samba Server #这是设置服务器主机的说明信息,当在Windows 的“网上邻居”中打开Samba 上设置的工作组时,在Windows 的资源管理器窗口,会列出“名称”和“备注”栏,其中“名称”栏会显示出Samba服务器的NetBios名称,而“备注”栏则显示出此处设置的“Samba Server”。当然,可以修改默认的“Sambe Server”,使用自己的描述信息。hosts allow = 192.168.1. 127 #这里是设置允许什么样的IP地址的主机访问Samba服务器。默认的情况下,hosts allow选项被注释,表示允许所有IP地址的主机访问。 guest account = pcguest #设置当访问那些被设置了“guest ok=yes”参数的资源时所要使用的帐号名。默认的帐号为”nobody”,如果不想用默认的值,则应该去掉注释用的分号,用你想要的帐号(如pcguest、 zhangsan等),然后,你必需将这一新帐号加入到/etc/passwd文件中去。client code page=950 设置客户端访问Samba 服务器时所使用的字符编码表(code page),默认值为850,如果Samba 服务器要使用年中文名称来来命名共享的资源,要将此值改为950log file = /var/log/samba/%m.log #这一选项要求Samba服务器为每一个连接的机器使用一个单独的日志文件,指定文件的位置,名称。Samba会自动将%m转换成连接主机的NetBios名。 max log size = 0 指定日志文件的最大容量(以KB为单位),设置为0,表示没有限制。默认值为5000。max disk size =1000#设置能够共享的最大磁盘空间,单位为MB,默认值为0,表示不作任何限制。max open file =100 #设置同一客户端最多能打开文件的数目,默认值为10000个。 security = user #设置Samba服务器的安全等级。默认情况下,使用user等级。#Samba服务器一共有四种安全等级。share: 使用此等级,用户不需要帐号及密码可以登陆Samba服务器。user: 使用此等级,由提供服务的Samba服务器检查用户帐号及密码。server: 使用此等级,检查帐号及密码的工作可指定另一台Samba服务器负责。domain: 使用此等级,需要指定一台Windows NT/2000/XP服务器(通常为域控制器),以验证用户输入的帐号及密码。; password server = #如果安全等级为“server”或“domain”,则使用此选项指定要验证密码的主机名。 password level = 8 username level = 8 #设置当验证用户口令和帐号时最多允许几个大小写字不同。默认值为0。 encrypt passwords = yes #设置当Samba客户端将帐号及密码传送到服务器端时,是否采取密码加密的方式。客户的操作系统如果是Windows 95 OSR2 及NT SP3以后的版本,应该将此选项的值设为yes,默认值为no。 smb passwd file = /etc/samba/smbpasswd #设置在Samba服务器上存放加密的密码文件的位置(注意:Samba服务器与Linux采用不同的密码文件)。2)设置共享资源参数comment #针对共享资源所作的说明、注释部分 browseable #设置用户是否可以看到此共享资源。默认值为yes,若将此参数设置为no,用户虽然看不到此资源,但是拥有权限的用户仍可直接输入该资源的网址来访问该资源。 writable #设置共享的资源是否可以写入。若共享资源是打印机,则不需设置此参数 #设置可访问的用户。系统会自动将%S转换成登录帐号。 create mode # 设置文件的访问权限,默认值为0744。 directory mode 设置目录的访问权限,默认值为0755。 path #若共享资源是目录,则指定目录的位置;若为打印机,则指定打印机队列的位置。 read only #设置共享资源是否只读或可以写入,默认值为yes。若共享资源为打印机时,此参数无任何意义。这一项与writable相反。 Public #等同于guest ok选项,表示是否允许用户不使用帐号和密码便能访问此资源。如果起用此功能,当用户没有帐号和密码时,则会利用“guest account=”所设置的帐号名登录。该选项默认值为no,即不允许没有帐号即密码的用户使用此资源。 avalible #设置是否启用此共享资源。默认值为yes。若将此参数设置为no,则不管其他参数设置为什么,所有人均不得使用此资源。 valid users 3)配置Samba文件共享举例以下通过配置3个不同的共享目录为例,可以知道配置Samba文件共享的一般方法。global #采用前面的global配置 homes #用来配置用户访问自己的目录 comment = Home Directories #注释 browseable = no #用户私人目录,不给别人浏览(并不是不允许别人访问) writable = yes #允许用户写入自己的目录 valid users = %S #可访问的用户局限于用户自己。%s会被自动转换为登录帐号。 create mode = 0664 #文件的访问权限 directory mode = 0775 #目录的访问权限 # This one is useful for people to share files tmp #这个部分为所有用户提供临时共享的方式 comment = Temporary file space #注释文字 path = /tmp #指定位置 read only = no #可以读写 public = yes #允许用户不用帐号和密码访问 public # 这个部分为所有用户提供可以共同访问的目录。允许staff组用户写入,但其他用户只可访问,不能写入。 comment = Public Stuff path = /home/samba public = yes writable = yes printable = no write list = staff # write list参数是用来设置具有写权限的用户列表。这里只允许staff组的成员有写的权限 fredsdir #这个部分用来设置某一用户fred的访问权限。 comment = Freds Service path = /usr/fred/private valid users = fred #只有fred可以访问(注意:即使security=share,也不代表用户登录Linux 主机后可以访问任意资源。)该共享目录 public = no writable = yes printable = no 任务四 Linux资源共享与使用项目1:四个用户joe、karl、mary和smith除了有他们自己的在服务器上的共享,这四位用户同时在同一个部门工作并且需要一个地方来存储部门的文件。我们将需要一个Linux用户组,建立二个目录给这些用户来存储它们的内容,/smb_test/public允许组中的用户都可以写,而/smb_test/legal只允许joe写,且配置samba服务器来共享目录。 增加用户组:# groupadd -g 6000 legal 增加用户:# useradd -m -u 5001 -g 6000 joe# useradd -m -u 5002 -g 6000 karl# useradd -m -u 5003 -g 6000 mary# useradd -m -u 5004 -g 6000 smith 修改用户密码:# passwd joe# passwd karl# passwd mary# passwd smith 建立目录:# mkdir -p /smb_test/legal# mkdir -p /smb_test/public 修改目录的组拥有者:# chgrp legal /smb_test/legal# chgrp legal /smb_test/public 修改目录的拥有者:# chown joe /smb_test/legal# chown karl /smb_test/public 修改目录的FAP:# chmod 770 /smb_test/legal# chmod 770 /smb_test/public 建立samba用户密码文件:# cat /etc/passwd | mksmbpasswd.sh/etc/samba/smbpasswd 修改用户samba密码:# smbpasswd joe# smbpasswd karl# smbpasswd mary# smbpasswd smith 修改samba配置文件:#gedit /etc/samba/smb.conf# 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 made any basic syntactic errors. #= Global Settings =global# workgroup = NT-Domain-Name or Workgroup-Name workgroup = MSHOME# 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.# if you want to automatically load your printer list rather# than setting them up individually then youll need this printcap name = /etc/printcap load printers = yes# 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, cups printing = cups# Uncomment this if you want a guest account, you must add this to /etc/passwd# otherwise the user nobody is used; guest account = pcguest# this tells Samba to use a separate log file for each machine# that connects log file = /var/log/samba/%m.log# Put a capping on the size of the log files (in Kb). max log size = 80# 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# The argument list may include:# password server = My_PDC_Name My_BDC_Name My_Next_BDC_Name# or to auto-locate the domain controller/s# password server = *; password server = # Password Level allows matching of _n_ characters of the password for# all combinations of upper and lower case.; password level = 8; username level = 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 smb passwd file = /etc/samba/smbpasswd# The following is needed to keep smbclient from spouting spurious errors# when Samba is built with support for SSL.; ssl CA certFile = /usr/share/ssl/certs/ca-bundle.crt# The following are needed to allow password changing from Windows to# update the Linux system 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*password* %nn *Retype*new*password* %nn *passwd:*all*authentication*tokens*updated*successfully*# You can use PAMs password change control flag for Samba. If# enabled, then PAM will be used for password changes when requested# by an SMB client instead of the program listed in passwd program.# It should be possible to enable this without changing your passwd# chat parameter for most setups. pam password change = yes# Unix users can map to different SMB User names; username map = /etc/samba/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/samba/smb.conf.%m# This parameter will control whether or not Samba should obey PAMs# account and session management directives. The default behavior is# to use PAM for clear text authentication only and to ignore any# account or session management. Note that Samba always ignores PAM# for authentication in the case of encrypt passwords = yes obey pam restrictions = yes# Most people will find that this option gives better performance.# See speed.txt and the manual pages for details socket options = TCP_NODELAY SO_RCVBUF=8192 SO_SNDBUF=8192# Configure Samba to use multiple interfaces# If you have multiple network interfaces then you must list them# here. See the man page for details.# 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)# Cause this host to announce itself to local subnets here# Browser Control Options:# set local master to no if you dont 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. Dont 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# 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 Internet Name Serving Support Section:# WINS Support - Tells the NMBD component of Samba to enable its 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 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 oneWINS 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#= Share Definitions =homes comment = Home Directories browseable = no writable = yes valid users = %S create mode = 0664 directory mode = 0775# If you want users samba doesnt recognize to be mapped to a guest user; map to guest = bad user# 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# Un-comment the following to provide a specific roving profile share# the default is to use the users home directory;Profiles; path = /usr/local/samba/profiles; browseable = no; guest ok = yes# NOTE: If you have a BSD-style print system there is no need to # specifically define each individual printerprinters 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# 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 grouppublic comment = Public Stuff path = /smb_test/public public = nocreate mask =0660 printable = no write list = legal# Other examples. # A private printer, usable only by fred. Spool data will be placed in freds# home directory. Note that fred must have write access to the spool directory,# wherever it is.;fredsprn; comment = Freds Printer; valid users = fred; path = /home/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 = Freds 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/local/pc
展开阅读全文