【文件共享服务之一】 Samba服务

简介:

        ******************理论部分*****************

Samba

   是一款在Linux/Unix系统上实现与windows系统进行文件共享的免费开源软件。广泛应用于Unix-like与Windows之间,提供文件共享和打印机共享服务,使得不同系统之间跨平台共享资源更加便捷.

Samba服务

   由服务端及客户端程序构成,采用的是smb/cifs网络协议。

SMB协议

   英文全称为 Server Message Block(服务协议块),该通信协议是微软公司和英特尔公司在1987年制定的协议,主要是作为Microsoft网络的通讯协议。SMB协议是用在会话层和表示层以及小部分应用层的协议。SMB协议使用NetBIOS的应用程序接口(简称API)。另外,它是一个开放性的协议,允许了协议扩展——使得它变得更大而且复杂;后来微软又把 SMB协议改名为CIFS协议(Common Internet File System),并且加入了许多新的特色。

NETBIOS协议

   全称为Network Basic Input/Output System,由IBM公司开发,Windows基于主机实现通信的机制,主要用于数十台计算机的小型局域网。NetBIOS是跟局域网有关的网络协议,从XP系统开始,这个协议已经集成在TCP/IP协议里面了,一般不需要单独使用这个协议了;

DNS协议与NETBIOS协议的区别

  协议不同:DNS是基于tcp/ip协议的;NETBIOS是基于netbios协议的。
 使用环境不同:DNS用于局域网和广域网,NETBIOS只能用于局域网  

Samba用到的服务端口:

   udp:137, 138 

   tcp:139, 445  

Samba拥有三个服务:
   1、smbd: cifs,使用139、445端口.
   2、nmbd:提供NetBIOS支持,使用137端口.(NetBIOS已逐渐被DNS替代)
   3、winbindd:提供针对windows2003/2008的用户及族信息的解析功能。
      winbindd服务需单独安装:yum -y insall samba-winbind

   注:一般作为文件共享使用Samba,则只需要smbd服务.

samba服务软件:

    客户端: samba-client 

    服务端: samba

服务脚本:

    /etc/rc.d/init.d/nmb
    /etc/rc.d/init.d/smb

主配置文件:
    /etc/samba/smb.conf

samba用户:
    账号:都是系统用户, /etc/passwd. 
    密码:samba服务自由密码文件.

将系统用户添加为samba的命令:smbpasswd 

smbpasswd:
     -a: 添加系统用户为samba用户
     -d: 禁用指定用户
     -e: 启用
     -x: 删除

     不使用任何命令选项时可以用于修改Samba用户的密码.

samba配置文件:
     smb.conf,包括:
          全局设定
          特定共享的设定 
          家目录
          打印机
          自定义共享

自定义共享:
      [shared_name] 
      path = /path/to/share_directory 
      comment = Comment String
      guest ok = {yes|no} 
      public = {yes|no} 
      writable = {yes|no} 
      read only = {yes|no} 
      write list = +GROUP_NAME
                        
测试配置文件是否有语法错误,以及显示最终生效的配置:
     # testparm 
                    
注:共享权限和文件系统权限二者缺一不可.必须都拥有写权限才能写入.

可使用setfacl和getfacl命令对用户授权!



        ******************实操部分*****************

Samba服务安装配置:

准备环境:


系统:  CentOS6.7 x 2台

       windows 7 x 1台. 

IP:   10.68.7.102 -- CentOS 7-102

       10.68.7.103 -- CentOS 7-103

       10.68.7.80 -- Windows

实验要求:

1)Win主机当服务端,Linux主机当客户端;Win主机共享一个目录,使Linux主机能够访问到; 

  a.并且在Windows主机新建两个系统用户user1和user2;

  b.这两用户从Linux主机登录,实现user1有上传下载文件的权限,user2只能读取共享文件。

  c.最后,基于挂载的方式访问共享目录.


2)Linux主机7-103当服务端,Win主机和Linux主机7-102当客户端

  建立samba共享,共享目录为/data,要求:
  a.共享名为shared, 工作组为WORKSTATION;
  b.添加组develop,添加用户gentoo,centos,ubuntu;其中gentoo和centos

    以develop为附加 组,Ubuntu不属于develop组,密码一致。
  c.添加samba用户gentoo,centos和Ubuntu,密码为各自用户名;
  d.此samba共享文件shared仅允许develop组具有写权限,其他用户只能以只读方式访问;
  e.此samba共享服务仅允许来自于10.68.7.0/24网络主机访问.

  f.需用Linux客户端和windows客户端均验证成功。


1.1 在Linux客户端安装samba client软件:

1
2
3
4
5
6
7
8
9
10
[root@7-102 ~] # yum -y install samba-client
[root@7-102 ~] # rpm -qa |grep samba*   //查看安装的samba服务客户端软件.
samba-winbind-clients-3.6.23-20.el6.x86_64
samba-winbind-3.6.23-20.el6.x86_64
samba-client-3.6.23-20.el6.x86_64
samba-common-3.6.23-20.el6.x86_64
[root@7-102 ~] # rpm -ql samba-client |grep "smbclient"   //smbclient为客户端工具.
/usr/bin/smbclient
/usr/share/man/man1/smbclient .1.gz
[root@7-102 ~] #

1.2 在Windows服务端创建共享目录

  1.创建要共享的目录test:

   wKioL1g4A0KDuP55AAAid10tBqY028.png-wh_50

  2. 创建两个系统用户user1和user2:

   wKiom1g4BR3jZQPnAACo_6WuNXE267.png-wh_50


  3. 设置共享:

   wKioL1g4BAzRsMuEAAC2DiWYNkY930.png-wh_50

  4. 添加用户user1对于该共享目录具有读写的权限,即完全控制权限;user2对于该共享目录只读. 

   wKiom1g4BnfRzptsAABu7RYvyQ0331.png-wh_50

   wKiom1g4BtiD7or6AAB2ecHXwD4875.png-wh_50

  5. 查看windows服务端计算机所属工作组:

   wKiom1g4B5XBOF_XAABkUrNQ_50157.png-wh_50

1.3 进入Linux端samba配置文件:

1
2
3
4
5
[root@7-102 ~] # vim /etc/samba/smb.conf  
  ...
  57 [global]
  74         workgroup = WORKSTATION    // 此处修改为windows服务端计算机所属工作组.
  75         server string = Samba Server Version % v

1.4 然后开始验证实验结果:

 1. 在Linux客户端交互式访问共享的数据目录test_file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[root@7-102 ~] # smbclient -L 10.68.7.80 -U user1
Enter user1's password: 
Domain=[YANGBIN-PC] OS=[Windows 7 Professional 7601 Service Pack 1] Server=[Windows 7 Professional 6.1]
 
     Sharename       Type      Comment
     ---------       ----      -------
     ADMIN$          Disk      远程管理
     C$              Disk      默认共享
     IPC$            IPC       远程 IPC
     test_file       Disk      
     Users           Disk      
session request to 10.68.7.80 failed (Called name not present)
session request to 10 failed (Called name not present)
session request to *SMBSERVER failed (Called name not present)
NetBIOS over TCP disabled -- no workgroup available
[root@7-102 ~] #

 2. 使用用户user1从Linux客户端登录samba共享目录并上传文件/etc/fstab:

1
2
3
4
5
6
7
8
9
10
11
12
13
[root@7-102 ~] # smbclient  //10.68.7.80/test_file -U user1
Enter user1's password: 
Domain=[YANGBIN-PC] OS=[Windows 7 Professional 7601 Service Pack 1] Server=[Windows 7 Professional 6.1]
smb: \> lcd  /etc
smb: \> put fstab 
putting  file  fstab as \fstab (393.0 kb /s ) (average 393.1 kb /s )
smb: \>  ls
   .                                   D        0  Fri Nov 25 05:02:37 2016
   ..                                  D        0  Fri Nov 25 05:02:37 2016
   fstab                               A      805  Fri Nov 25 05:02:37 2016
 
         51148 blocks of size 2097152. 40843 blocks available
smb: \>

 3. 在windows服务端查看用户user1上传的文件fstab:

wKioL1g4DP-Rz_gkAACLNuctLU0220.png-wh_50

 4. 使用用户user2查看并上传文件,上传失败则验证成功:

1
2
3
4
5
6
7
8
9
10
11
12
13
[root@7-102 ~] # smbclient //10.68.7.80/test_file -U user2
Enter user2's password: 
Domain=[YANGBIN-PC] OS=[Windows 7 Professional 7601 Service Pack 1] Server=[Windows 7 Professional 6.1]
smb: \>  ls
   .                                   D        0  Fri Nov 25 05:02:37 2016
   ..                                  D        0  Fri Nov 25 05:02:37 2016
   fstab                               A      805  Fri Nov 25 05:02:37 2016
 
         51148 blocks of size 2097152. 40843 blocks available
smb: \> lcd  /etc
smb: \> put inittab 
NT_STATUS_ACCESS_DENIED opening remote  file  \inittab    // 提示上传被拒绝.
smb: \>

1.5 基于挂载的方式访问共享目录:

1
2
3
4
5
6
7
8
9
10
11
[root@7-102 ~] # mkdir /mnt/test   
[root@7-102 ~] # mount -t cifs //10.68.7.80/test_file /mnt/test -o username=user1,password=user1
[root@7-102 ~] # df -hT
Filesystem           Type     Size  Used Avail Use% Mounted on
/dev/sda2             ext4      58G  2.9G   52G   6% /
tmpfs                tmpfs    932M     0  932M   0%  /dev/shm
/dev/sda1             ext4     7.5G   53M  7.1G   1%  /boot
/dev/sr0              iso9660  3.7G  3.7G     0 100%  /media
//10 .68.7.80 /test_file
                      cifs     100G   21G   80G  21%  /mnt/test
[root@7-102 ~] #

END

2.1 建立共享samba目录文件,创建工作组,添加用户:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[root@7-103 ~] # mkdir -pv /data/shared
mkdir : created directory ` /data '
mkdir : created directory ` /data/shared '
[root@7-103 ~] # groupadd develop
[root@7-103 ~] # useradd -G develop gentoo   //-G<群组>:指定用户所属的附加群组; 
[root@7-103 ~] # useradd -G develop centos
[root@7-103 ~] # useradd  ubuntu
[root@7-103 ~] # echo gentoo |passwd --stdin gentoo
Changing password  for  user gentoo.
passwd : all authentication tokens updated successfully.
[root@7-103 ~] # echo cnetos |passwd --stdin centos
Changing password  for  user centos.
passwd : all authentication tokens updated successfully.
[root@7-103 ~] # echo ubuntu |passwd --stdin ubuntu
Changing password  for  user ubuntu.
passwd : all authentication tokens updated successfully.

2.2 在Linux服务端安装samba server软件:

1
2
3
4
5
6
7
[root@7-103 ~] # yum -y install samba
[root@7-103 ~] # rpm -qa samba*
samba-winbind-clients-3.6.23-20.el6.x86_64
samba-common-3.6.23-20.el6.x86_64
samba-winbind-3.6.23-20.el6.x86_64
samba-3.6.23-20.el6.x86_64
[root@7-103 ~] #

2.3 添加samba用户gentoo,centos和Ubuntu,密码为各自用户名:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[root@7-103 ~] # smbpasswd -a gentoo
New SMB password:
Interrupted by signal.
[root@7-103 ~] # smbpasswd -a gentoo
New SMB password:
Retype new SMB password:
Added user gentoo.
[root@7-103 ~] # smbpasswd -a centos
New SMB password:
Retype new SMB password:
Added user centos.
[root@7-103 ~] # smbpasswd -a ubuntu
New SMB password:
Retype new SMB password:
Added user ubuntu.
[root@7-103 ~] #

2.4 在samba配置文件进行配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
     [root@7-103 ~] # vim /etc/samba/smb.conf
      1        
      2    [global]
      3        
      4        workgroup = WORKSTATION    // 设置工作组为WORKSTATION.
      5        server string = Samba Server Version % v
      6        
      7    ;    netbios name = MYSERVER
      8        
      9    ;    interfaces = lo eth0 192.168.12.2 /24  192.168.13.2 /24 
     10    ;    hosts allow = 10.68.7.    // 此处仅允许10.68.7.0 /24 网段主机访问.
     11        
     12        
     13         # logs split per machine
     14        log  file  /var/log/samba/log .%m
     15         # max 50KB per log file, then rotate
     16        max log size = 50
     17        
     18    
     19        security = user   // 安全级别,user表示需由本服务器验证用户名及密码.
     20        passdb backend = tdbsam
                  // 该项表示samba用户密码以tdbsam格式保存,安全性很高.
     21    
     22    
     23        
     24        
     25    ;    security = domain
     26    ;    passdb backend = tdbsam
     27    ;    realm = MY_REALM
     28    
     29    ;    password server = <NT-Server-Name>
     30    
     31    ;    security = user
     32    ;    passdb backend = tdbsam
     33        
     34    ;    domain master =  yes 
     35    ;    domain logons =  yes
     36        
     37         # the login script name depends on the machine name
     38    ;    logon script = %m.bat
     39         # the login script name depends on the unix user used
     40    ;    logon script = %u.bat
     41    ;    logon path = \\%L\Profiles\%u
     42         # disables profiles support by specifing an empty path
     43    ;    logon path =          
     44        
     45    ;    add user script =  /usr/sbin/useradd  "%u"  -n -g  users
     46    ;    add group script =  /usr/sbin/groupadd  "%g"
     47    ;    add machine script =  /usr/sbin/useradd  -n -c  "Workstation (%u)"  -M -d  /nohome  -s  /bin/false  "%u"
     48    ;    delete user script =  /usr/sbin/userdel  "%u"
     49    ;    delete user from group script =  /usr/sbin/userdel  "%u"  "%g"
     50    ;    delete group script =  /usr/sbin/groupdel  "%g"
     51        
     52        
     53    ;     local  master = no
     54    ;    os level = 33
     55    ;    preferred master =  yes
     56        
     57        
     58    ;    wins support =  yes
     59    ;    wins server = w.x.y.z
     60    ;    wins proxy =  yes
     61        
     62    ;    dns proxy =  yes
     63        
     64        
     65        load printers =  yes
     66        cups options = raw
     67    
     68    ;     printcap  name =  /etc/printcap
     69         #obtain list of printers automatically on SystemV
     70    ;     printcap  name = lpstat
     71    ;    printing = cups
     72    
     73    
     74    ;    map archive = no
     75    ;    map hidden = no
     76    ;    map  read  only = no
     77    ;    map system = no
     78    ;    store dos attributes =  yes
     79    
     80    
     81        
     82    [homes]
     83        comment = Home Directories
     84        browseable = no
     85        writable =  yes
     86    ;    valid  users  = %S
     87    ;    valid  users  = MYDOMAIN\%S
     88        
     89    [printers]
     90        comment = All Printers
     91        path =  /var/spool/samba
     92        browseable = no
     93        guest ok = no
     94        writable = no
     95        printable =  yes
     96        
     97    ;    [netlogon]
     98    ;    comment = Network Logon Service
     99    ;    path =  /var/lib/samba/netlogon
    100    ;    guest ok =  yes
    101    ;    writable = no
    102    ;    share modes = no
    103        
    104        
    105    ;    [Profiles]
    106    ;    path =  /var/lib/samba/profiles
    107    ;    browseable = no
    108    ;    guest ok =  yes
    109        
    110        
    111    ;    [public]
    112    ;    comment = Public Stuff
    113    ;    path =  /home/samba
    114    ;    public =  yes
    115    ;    writable =  yes
    116    ;    printable = no
    117    ;    write list = +staff
    118
    119    // 自定义共享:
    120        [shared]
    121        comment = shared  test  file
    122        path =  /data/shared
    123        writable =  yes
    124        guest ok =  yes
     
  注:关于samba配置文件的详细解释,可参考如下这篇博客:
       
    
 
    http: //10166561 .blog.51cto.com /10156561/1683136

2.5 测试配置文件是否有语法错误,以及显示最终生效的配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
[root@7-103 ~] # testparm
Load smb config files from  /etc/samba/smb .conf
rlimit_max: increasing rlimit_max (1024) to minimum Windows limit (16384)
Processing section  "[homes]"
Processing section  "[printers]"
Processing section  "[shared]"
Loaded services  file  OK.
Server role: ROLE_STANDALONE
Press enter to see a dump of your service definitions
 
[global]
     workgroup = WORKSTATION
     server string = Samba Server Version % v
     log  file  /var/log/samba/log .%m
     max log size = 50
     idmap config * : backend = tdb
     cups options = raw
 
[homes]
     comment = Home Directories
     read  only = No
     browseable = No
 
[printers]
     comment = All Printers
     path =  /var/spool/samba
     printable = Yes
     print ok = Yes
     browseable = No
 
[shared]
     comment = shared  test  file
     path =  /data/shared
     read  only = No
     guest ok = Yes
[root@7-103 ~] #

2.6 启动samba服务:

1
2
3
4
[root@7-103 ~] # service smb start;service nmb start
Starting SMB services:                                     [  OK  ]
Starting NMB services:                                     [  OK  ]
[root@7-103 ~] #

2.7 配置共享文件权限:

1
2
3
4
5
[root@7-103 ~] # setfacl -m group:develop:rwx /data/shared
[root@7-103 ~] # set -m user:ubuntu:rx /data/shared
[root@7-103 ~] # ls -ld /data/shared
drwxrwxr-x+ 3 root root 4096 Sep 21 04:55  /data/shared
[root@7-103 ~] #

 注:共享权限和文件系统权限二者缺一不可.必须都拥有写权限才能写入.

2.8 交互式数据访问:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[root@7-102 ~] # smbclient -L 10.68.7.103 -U gentoo
Enter gentoo's password: 
Domain=[WORKSTATION] OS=[Unix] Server=[Samba 3.6.23-20.el6]
 
     Sharename       Type      Comment
     ---------       ----      -------
     shared          Disk      shared  test  file
     IPC$            IPC       IPC Service (Samba Server Version 3.6.23-20.el6)
     gentoo          Disk      Home Directories
Domain=[WORKSTATION] OS=[Unix] Server=[Samba 3.6.23-20.el6]
 
     Server               Comment
     ---------            -------
     7-103                Samba Server Version 3.6.23-20.el6
 
     Workgroup            Master
     ---------            -------
     WORKSTATION          7-103
[root@7-102 ~] #

2.9 测试结果

  首先以一台Linux 7-102客户端分别用centos和Ubuntu用户登录samba服务,前者可上传文件,后者则只读:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
[root@7-102 ~] # smbclient //10.68.7.103/shared -U centos
Enter centos's password: 
Domain=[WORKSTATION] OS=[Unix] Server=[Samba 3.6.23-20.el6]
smb: \>  ls
   .                                   D        0  Wed Sep 21 01:54:46 2016
   ..                                  D        0  Wed Sep 21 01:54:46 2016
 
         58930 blocks of size 1048576. 53326 blocks available
smb: \> 
smb: \> lcd  /etc
smb: \> put fstab 
putting  file  fstab as \fstab (262.0 kb /s ) (average 262.0 kb /s
smb: \>  ls
   .                                   D        0  Wed Sep 21 04:36:45 2016
   ..                                  D        0  Wed Sep 21 01:54:46 2016
   fstab                               A      805  Wed Sep 21 04:36:45 2016
 
         58930 blocks of size 1048576. 53326 blocks available
smb: \> 
[root@7-102 ~] # smbclient //10.68.7.103/shared -U ubuntu
Enter ubuntu's password: 
Domain=[WORKSTATION] OS=[Unix] Server=[Samba 3.6.23-20.el6]
smb: \> 
smb: \>  ls
   .                                   D        0  Wed Sep 21 04:36:45 2016
   ..                                  D        0  Wed Sep 21 01:54:46 2016
   fstab                               A      805  Wed Sep 21 04:36:45 2016
 
         58930 blocks of size 1048576. 53326 blocks available 
smb: \> lcd  /etc
smb: \> put inittab 
NT_STATUS_ACCESS_DENIED opening remote  file  \inittab
smb: \>

注:

1
2
# smbclient //10.68.7.103/shared -U centos  //此处填写共享文件,不能包括共享
文件的上级目录.

3.0 在windows客户端验证:

  需要出现的结果是,用gentoo用户登录访问共享目录文件,可创建文件:

  wKioL1g4TiWgGzOxAABrATS-lw0764.png-wh_50

  wKioL1g4TjfRjYm5AAButt9_Njk595.png-wh_50

  wKiom1g4Tk6zHdBmAADbZHPsgC0360.png-wh_50

 

3.1 用Ubuntu用户登录访问共享目录,创建文件被拒绝,仅能读取:

  wKioL1g4TqLxfgxDAAFb_7svgMI726.png-wh_50

  wKiom1g4TrbijQdhAAGAXJ8J7IQ692.png-wh_50

  结果验证成功!



本文转自 羽丰1995 51CTO博客,原文链接:http://blog.51cto.com/13683137989/1876840

相关文章
|
6天前
|
安全 Linux 网络安全
Samba局域网共享文件
Samba局域网共享文件
|
8天前
|
网络协议 Linux
基于 NFS 的文件共享实现
基于 NFS 的文件共享实现
|
存储 网络协议 Linux
使用Samba实现文件共享
SMB服务消息块协议,指在解决局域网内的文件或打印机等资源的共享问题,这也使得在多个主机之间共享文件变得越来越简单.到了1991年,当时还在读大学的Tridgwell为了解决Linux系统与Windows系统的文件共享问题,基于SMB协议,开发出了SMBServer服务程序,后来由于名称冲突,改名为Samba,而如今Samba服务已经成为了,Linux系统与Windows系统之间数据传输的最佳选择.
259 0
|
存储 网络协议 Linux
Linux网络服务之NFS(文件共享服务)
1 NFS简介 NFS(Network File System 网络文件服务),是一种基于 TCP/IP 传输的网络文件系统协议,最初由 Sun 公司开发。
668 1
|
存储 网络安全 Windows
SMB文件共享及用户权限使用配置
SMB文件共享及用户权限使用配置
652 0
SMB文件共享及用户权限使用配置
|
Linux 开发工具 数据库
Linux使用Samba实现文件共享
Linux使用Samba实现文件共享Samba服务是现在Linux系统与Windows系统之间共享文件的最佳选择。 [root@study ~]# yum install samba -y  #安装samba服务 [root@study ~]# cat -n /etc/samba/smb.
1319 0
|
Linux 开发工具 数据安全/隐私保护
|
安全 网络协议 数据安全/隐私保护
|
安全 数据安全/隐私保护 Windows

热门文章

最新文章