linux 5.3 管理系统下搭建samba详解

来源:互联网 发布:java web面试题目 编辑:程序博客网 时间:2024/06/08 18:55
samba  
SMB   (server message block )服务消息块协议
windows之间的共享   网上邻居   cifs   (  common interface  file system)
mount -t cifs -o username=administrator //IP/共享名 /mnt
linux之间的共享     nfs     network file system
linux与windows之间    samba

samba
使用netbios (network basic  input and output system)
netbeui      (netbios extened  user interface)

samba  通过两个守护进程来控制
nmbd    用来管理工作组,netbios名的解析等 udp 137,138端口

smbd    用来管理samba主机共享的目录,档案     tcp 139,445

特点:
1,能够支持更详细的访问控制
2,能够跨平台(linux和windows)共享文件
[root@li ~]# yum list |grep samba
This system is not registered with RHN.
RHN support will be disabled.
samba-common.i386               --公共的工具包,包括samba.conf和检测其语法的测试工具testparm  
samba.i386                     --samba服务端         
samba-client.i386               --客户端的工具指令            
samba-swat.i386                 --图形化samba配置工具,其于web接口
[root@~]# yum install samba* -y

  服务器端主配置文件: /etc/samba/smb.conf
  netbios名与ip对应文件 /etc/samba/lmhosts
  密码存放文件: /etc/samba/passdb.tdb   --用sampasswd -a加入用户后就存在了
  日志文件:   /var/log/samba/

man smb.conf --查找帮助
http://write.blog.csdn.net/postedit/6685426
[root@ ~]# cat /etc/samba/smb.conf |grep -v ^# |grep -v ^$ |grep -v ^";"|grep -v "#"
[global] --定义全局的参数
        workgroup = MYGROUP --定义工作组
        server string = Samba Server Version %v  --定义服务器的描述
        security = user   --定义工作模式:share,user,domain,ads
        passdb backend = tdbsam  --使用本地的.tdb文件保存账号密码
        load printers = yes --加载打印机
        cups options = raw --打印机类型
[homes]  --保留的的资源名
        comment = Home Directories
        browseable = no
        writable = yes
[printers] --保留的资源名
        comment = All Printers
        path = /var/spool/samba
        browseable = no
        guest ok = no
        writable = no
        printable = yes

security =
share   不用密码,包括匿名用户
user 使用samba自己的密码资料库
server,domain,ads     使用外部主机的密码
共享目录有关的参数
browseable  是否可见
public    是否让所有可以登入的使用者看到
guest ok   和public一样,使用其中之一就可以
writable   是否可写
read only  是否只读  --这个参数与writable同时存在会有冲突,以后面出现的设定值为主
create  mode   umask类似
valid users   指定能够进入的使用者

  /etc/init.d/smb start
[root@~]# netstat -ntlup | grep bd
tcp        0      0 0.0.0.0:139                 0.0.0.0:*                   LISTEN      3000/smbd           
tcp        0      0 0.0.0.0:445                 0.0.0.0:*                   LISTEN      3000/smbd           
udp        0      0 10.1.1.45:137               0.0.0.0:*                               3003/nmbd           
udp        0      0 10.1.1.46:137               0.0.0.0:*                               3003/nmbd           
udp        0      0 192.168.195.1:137           0.0.0.0:*                               3003/nmbd           
udp        0      0 192.168.205.1:137           0.0.0.0:*                               3003/nmbd           
udp        0      0 0.0.0.0:137                 0.0.0.0:*                               3003/nmbd           
udp        0      0 10.1.1.45:138               0.0.0.0:*                               3003/nmbd           
udp        0      0 10.1.1.46:138               0.0.0.0:*                               3003/nmbd           
udp        0      0 192.168.195.1:138           0.0.0.0:*                               3003/nmbd           
udp        0      0 192.168.205.1:138           0.0.0.0:*                               3003/nmbd           
udp        0      0 0.0.0.0:138                 0.0.0.0:*                               3003/nmbd           

例一.
使用linux的客户端工具做测试:
       smbclient - ftp-like client to access SMB/CIFS resources
       on servers
[root@li ~]# smbclient -L //10.1.1.45
Password:  --直接回车,表示以匿名用户登录,但没有看到共享资源,只有服务器的基本信息
[root@li ~]# smbclient -L //10.1.1.45  -U a --使用用户名登录,登录失败
Password:
session setup failed: NT_STATUS_LOGON_FAILURE

原因:  
1,默认security = user 模式,这种模式指定访问用户需要密码,匿名用户除外
2, samba账号有两个要求,一,要求是系统用户  二,要把系统用户通过samba的命令把它加入到samba服务里去
  smbpasswd - change a user’s SMB password
[root@ ~]# smbpasswd -a a --把a用户加入到samba用户里
New SMB password:
Retype new SMB password:
Added user a.
[root@li ~]# smbclient -L //10.1.1.45  -U a --再使用a用户登录
Password:
Domain=[LI] OS=[Unix] Server=[Samba 3.0.33-3.14.el5]
        Sharename       Type      Comment
        ---------       ----      -------
        IPC$            IPC       IPC Service (Samba Server Version 3.0.33-3.14.el5)
        a               Disk      Home Directories  --发现多了一个共享资源,共享资源名为用户名一样
[root@ ~]# smbclient //10.1.1.45/a -U a
Password:
Domain=[LI] OS=[Unix] Server=[Samba 3.0.33-3.14.el5]
smb: \> pwd  --查看当前登录的目录
Current directory is \\10.1.1.45\a\
smb: \> ?  --帮助
smb: \> help get --查看get命令的帮助
smb: \> ls  --列表
smb: \> put install.log --上传
putting file install.log as \install.log (966.9 kb/s) (average 966.9 kb/s)
smb: \> get vvv
getting file \vvv of size 0 as vvv (0.0 kb/s) (average 0.0 kb/s)
smb: \> rm vvv

上面的实验知道,就是配置段内的homes这个保留资源
[homes]  --定义共享资源的名字,   自动变成对应的用户名
        comment = Home Directories --资源描述
        browseable = no  --指定在匿名用户使用-L时是否可见
        writable = yes --表示可写,能够上传删除等操作

-------------------------------------------------------

例2.  share模式
# vim /etc/samba/smb.conf
[global]
        workgroup = MYGROUP
        server string = Samba Server Version %v
        security = share --在这里把user改为share
        passdb backend = tdbsam
        load printers = yes
        cups options = raw
[homes]
        comment = Home Directories
        browseable = no
        writable = yes
[printers]
        comment = All Printers
        path = /var/spool/samba
        browseable = no
        guest ok = no
        writable = no
        printable = yes
[share]  --新定义了一个共享的资源,共享名为share
    comment = public for everyone
    path = /share --实际共享的资源目录名
    guest ok = yes --表示可以匿名访问
    browseable = yes

/etc/init.d/smb reload  --重装载smb服务
[root@li ~]# smbclient -L //10.1.1.45 --使用匿名用户查看共享资源列表
Password:
Domain=[MYGROUP] OS=[Unix] Server=[Samba 3.0.33-3.14.el5]
        Sharename       Type      Comment
        ---------       ----      -------
        share           Disk      public for everyone  --可以看到这个新的共享资源

[root@li ~]# smbclient //10.1.1.45/share --使用匿名用户登录共享资源名为share
Password:
Domain=[MYGROUP] OS=[Unix] Server=[Samba 3.0.33-3.14.el5]
Server not using user level security and no password supplied.
smb: \> put install.log  --匿名用户上传失败
NT_STATUS_ACCESS_DENIED opening remote file \install.log
例3 ,share模式下匿名用户可写
修改配置文件:
[share]
    comment = public for everyone
    path = /share
    guest ok = yes
    browseable = yes
    writeable = yes --加上这一句,表示可写

/etc/init.d/smb reload
再次上传,还是发现失败

原因:匿名用户登录的时候,samba是使用nobody的身份去操作的,而共享的/share目录权限属性如下,nobody用户是不能写的
[root@li ~]# ll -d /share/
drwxr-xr-x 5 root root 4096 Aug 17 16:27 /share/
修改/share的目录权限,允许nobody用户可写
chmod 757 /share/
[root@li ~]# smbclient //10.1.1.45/share
Password:
smb: \> put install.log  --再次上传成功
putting file install.log as \install.log (2486.3 kb/s) (average 2486.3 kb/s)
例四:user模式普通用户可写的情况
[share]
    comment = public for users
    path = /share
    browseable = yes

[root@li ~]# smbclient //10.1.1.45/share  -U a
Password:
Domain=[LI] OS=[Unix] Server=[Samba 3.0.33-3.14.el5]
smb: \> put 123  --不能上传
NT_STATUS_ACCESS_DENIED opening remote file \123

[share]
    comment = public for everyone
    path = /share
    browseable = yes
    read only = no  --和writable = yes一样,表示可写
/etc/init.d/smb  reload
[root@li ~]# smbclient //10.1.1.45/share  -U a
Password:
Domain=[LI] OS=[Unix] Server=[Samba 3.0.33-3.14.el5]
smb: \> put 123  --再次put上传文件成功
putting file 123 as \123 (0.0 kb/s) (average 0.0 kb/s)

例五:验证writable 参数和 read only参数同时存在的情况
[share]
    comment = public for everyone
    path = /share
    browseable = yes
    read only = yes --这里表示只读,也就是不可写
    writeable = yes --这里表示可写,与上面的参数冲突,后面的生效,所以应该是可写的
/etc/init.d/smb reload   
[root@li ~]# smbclient //10.1.1.45/share  -U a
Password:
Domain=[LI] OS=[Unix] Server=[Samba 3.0.33-3.14.el5]
smb: \> put 456  --上传成功,表示可写,验证成功
putting file 456 as \456 (0.0 kb/s) (average nan kb/s)

例六.关于用户互相能删除文件的解决
使用b用户登录/share共享资源,也上传两个文件   678 和 789
查看一下用户上传的文件属性
[root@li ~]# ls /share/ -l
total 60
-rwxr--r--  1 a      a          0 Sep  2 11:39 123
-rwxr--r--  1 a      a          0 Sep  2 11:39 456
-rwxr--r--  1 b      b          0 Sep  2 11:43 678
-rwxr--r--  1 b      b          0 Sep  2 11:43 789

[root@li ~]# smbclient //10.1.1.45/share  -U a
smb: \> rm 678
[root@li ~]# smbclient //10.1.1.45/share  -U b
smb: \> rm 123
把a用户上传的456改成577,也就是自己对自己的文件没有写权限
[root@li ~]# chmod 577 /share/456
[root@li ~]# ls /share/ -l
total 60
-r-xrwxrwx  1 a      a          0 Sep  2 11:39 456
[root@li ~]# smbclient //10.1.1.45/share  -U b --用b用户去删除,删除失败
smb: \> rm 456
NT_STATUS_CANNOT_DELETE deleting remote file \456

再次把a用户上传的456改成200,也就是自己对自己的文件只有写权限
[root@li ~]# chmod 200 /share/456
[root@li ~]# ls /share/ -l
total 60
--w-------  1 a      a          0 Sep  2 11:39 456
[root@li ~]# smbclient //10.1.1.45/share  -U b  --用b用户去删除,删除成功
smb: \> rm 456

   分析总结:  只要文件的拥有者对这个文件有可写的权限,那么别人就可以删除它 (200权限例子)
      文件的拥有者对这个文件没有可写的权限,别人就不能删除它  (577权限的例子)

解决方法:
  chmod  o+t /share

-------------------------------------------------------------

例七: create mask 和 directory mask 直接定义上传后的文件或者目录的权限
[share]
    comment = public for everyone
    path = /share
    browseable = yes
    read only = yes
    writeable = yes
    create mask = 0555  --表示创建的或者上传的文件权限为555
    directory mask = 0666 --表示创建的或者上传的目录权限为666
/etc/init.d/smb  restart     

例八:user模式下,实现a用户可以上传下载,b用户只能下载,拒绝匿名用户访问
   valid users  = a,b,@group   --只允许a,b用户和group组访问
   write list  = a,@group         --允许写的用户列表
[share]
    comment = public for everyone
    path = /share
    guest ok = no --拒绝匿名用户,或者使用public = no
    write list = a --写列表只允许a用户写,表示只允许a用户上传
/etc/init.d/smb  reload

测试

例九:user模式下,实现a用户可以上传下载,b用户只能下载,属于smb组的用户可以登录和上传下载,别的普通用户和匿名用户都拒绝
[share]
    comment = public for a,b,@smb
    path = /share
    public = no
    valid users = a,b,@smb --注意users不要写成user
    write list = a,@smb

[root@li ~]# useradd smb
[root@li ~]# useradd smb2 -g smb
[root@li ~]# smbpasswd -a smb
[root@li ~]# smbpasswd -a smb2
[root@li ~]# id smb
uid=521(smb) gid=521(smb) groups=521(smb)
[root@li ~]# id smb2
uid=522(smb2) gid=521(smb) groups=521(smb)
验证OK

例十: user模式下,实现a上传下载(可以下载c上传的),b只能下载,c只能上传不能下载(但可以下载自己上传的),互相不能删除它人文件,其他用户和匿名用户登录拒绝
此例的关键难点是在c用户只能上传,首先思考什么情况才可以下载
-rw-r-----  1 root root     0 Sep  2 14:04 c1
-rw-r--r--  1 root root     0 Sep  2 14:28 c2
能否下载的关键就是登录的用户对下载的文件是否有读权限,
所以上面两个文件a用户只能下载c2,不能下载c1

chmod o+t /share

vim /etc/samba/smb.conf
config file = /etc/samba/smb.conf.%U --手动加上这一句,表示针对每个用户都可以单独写一个配置文件,加上上面的全局[global]

[share]
    comment = public for a,b,@smb
    path = /share
    public = no
    valid users = a,b,c   
    write list = a --这里先不写c,在下面c用户自己的配置文件里写
    create mask = 0740 --上传文件的权限为740,表示c用户没有r权限,所以c用户下载不了

vim /etc/samba/smb.conf.c  --建立一个c用户的单独的配置文件
[share]
   comment = share for c
   path = /share
   writeable = yes --上面的写列表里没有c,所以这里要为yes,表示能上传
   create mask = 0404   --第一个4表示c用户上传的权限自己能读,实现的是自己能下载自己上传的需求;第二个4表示a,b用户能读,实现的是a,b用户能够下载c上传的文件

例十一:访问控制
注意写到全局参数[global]下
1,拒绝10.1.1网段
hosts deny = 10.1.1.
1,拒绝10.1.1网段,但是允许10.1.1.20
hosts deny = 10.1.1.
hosts allow = 10.1.1.20
2,拒绝所有,但允许10.1.1网段,但又不允许10.1.1.45
hosts deny = all
hosts allow = 10.1.1.  EXCEPT 10.1.1.45

---------------------------------
samba补充知识点:
1,
smbpasswd
-a    添加smb用户
-d    禁用smb用户
-x    删除smb用户
-e    启用被禁用的smb用户

2,
linux客户端的访问除了
smbclient //10.1.1.45/share  -U a
还可以挂载
mount -t smbfs -o username=a //10.1.1.45/share /mnt --samba-2.x版本
mount -t cifs -o username=a //10.1.1.45/share /mnt     --samba-3.x版本

3,
testparm  /etc/samba/smb.conf    --测试配置文件

4,网页samba控制工具
  yum install samba-swat  -y
vim /etc/xinetd.d/swat
  
service swat
{
        port            = 901 --端口为901
        socket_type     = stream
        wait            = no
        only_from       = 127.0.0.1 --只允许本地回环访问
        user            = root
        server          = /usr/sbin/swat
        log_on_failure  += USERID
        disable         = no --把yes改为no代表启动
}

/etc/init.d/xinetd restart

[root@li test]# netstat -ntlup |grep 901
tcp        0      0 0.0.0.0:901                 0.0.0.0:*                   LISTEN      8201/xinetd   
访问方法:
http://127.0.0.1:901/ --只能用回环地址访问,因为/etc/xinet.d/swat的定义,
输入smb用户和密码就可以进入

----------------------------------------------------------
DHCP
dynamic  host configuration  protocol
  动态主机配置协议

软件包: dhcp  dhcp-devel

yum install dhcp*  --安装软件包
vim /etc/dhcpd.conf  --主配置文件,默认为空,有下面的信息
# DHCP Server Configuration file.
#   see /usr/share/doc/dhcp*/dhcpd.conf.sample  
拷贝模块配置文件
    cp /usr/share/doc/dhcp-3.0.5/dhcpd.conf.sample /etc/dhcpd.conf
[root@li test]# cat /etc/dhcpd.conf
ddns-update-style interim; --动态更新DNS记录的方式
ignore client-updates; --不启用动态DNS记录的功能
subnet 192.168.195.0 netmask 255.255.255.0 {   --DNS所在网段,服务器本身IP一定是固定,而且必须要在同一个网段内
# --- default gateway
        option routers                  192.168.195.1;--指定客户端获取的默认路由
        option subnet-mask              255.255.255.0;--默认路由的掩码
        option nis-domain               "cluster.org";--客户端获取的NIS域名
        option domain-name              "cluster.org";--客户端获取的域名
        option domain-name-servers      192.168.195.1;--给客户端分配DNS的指向,也就是自动加到客户端的/etc/reslov.conf
        option time-offset              -18000; # Eastern Standard Time
#       option ntp-servers              192.168.1.1;--指定客户端获取的ntp服务器
#       option netbios-name-servers     192.168.1.1;
# --- Selects point-to-point node (default is hybrid). Don't change this unless
# -- you understand Netbios very well
#       option netbios-node-type 2;
        range dynamic-bootp 192.168.195.50 192.168.195.52;  --分配给客户端的IP地址范围
        default-lease-time 21600; --默认租约时间
        max-lease-time 43200; --最大租约时间
        # we want the nameserver to appear at a fixed address
        host ns {  --以MAC地址为基准,静态IP地址与MAC绑定
                next-server marvin.redhat.com;
                hardware ethernet 12:34:56:78:AB:CD;
                fixed-address 207.175.42.254;
        }
}

实验,给虚拟机DHCP分配IP,配置文件如上,对应的192.168.195网段是我本机的vmnet1网段
,指定只给虚拟机分配192.168.195.50-192.168.195.52
第一步:
/etc/init.d/dhcpd restart

第二步:
虚拟机把网卡改成连接对应的vmnet1

第三步: 虚拟机上使用dhcp自动获取

第四步:验证

客户端的验证
ifconfig
vim /etc/reslov.conf
route -n


[root@slave ~]# dhclient eth1
Internet Systems Consortium DHCP Client V3.0.5-RedHat
Copyright 2004-2006 Internet Systems Consortium.
All rights reserved.
For info, please visit http://www.isc.org/sw/dhcp/
Listening on LPF/eth1/00:0c:29:fa:8f:4b
Sending on   LPF/eth1/00:0c:29:fa:8f:4b
Sending on   Socket/fallback
DHCPDISCOVER on eth1 to 255.255.255.255 port 67 interval 6
DHCPOFFER from 192.168.195.1
DHCPREQUEST on eth1 to 255.255.255.255 port 67
DHCPACK from 192.168.195.1
bound to 192.168.195.52 -- renewal in 10119 seconds.


cat /var/log/messages --messages里有DHCP获取IP过程的信息
Sep  2 16:35:31 slave dhclient: DHCPDISCOVER on eth1 to 255.255.255.255 port 67 interval 3
Sep  2 16:35:31 slave dhclient: DHCPOFFER from 192.168.195.1
Sep  2 16:35:31 slave dhclient: DHCPREQUEST on eth1 to 255.255.255.255 port 67
Sep  2 16:35:31 slave dhclient: DHCPACK from 192.168.195.1
Sep  2 16:35:32 slave avahi-daemon[2439]: Joining mDNS multicast group on interface eth1.IPv4 with address 192.168.195.52.


cat /var/lib/dhclient/dhclient-eth1.leases
lease {
  interface "eth1";
  fixed-address 192.168.195.52;
  option subnet-mask 255.255.255.0;
  option time-offset -18000;
  option routers 192.168.195.1;
  option dhcp-lease-time 21600;
  option dhcp-message-type 5;
  option domain-name-servers 192.168.195.1;
  option dhcp-server-identifier 192.168.195.1;
  option nis-domain "cluster.com";
  option domain-name "cluster.com";
  renew 4 2010/9/2 11:14:45;
  rebind 4 2010/9/2 13:50:31;
  expire 4 2010/9/2 14:35:31;
}


服务端
cat /var/lib/dhcpd/dhcpd.leases
lease 192.168.195.52 {
  starts 4 2010/09/02 08:35:32;
  ends 4 2010/09/02 14:35:32;
  binding state active;
  next binding state free;
  hardware ethernet 00:0c:29:fa:8f:4b;
}

注意的是:同一个网段不要有多个DHCP服务器,会造成客户端获取的混乱