扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
******************理论部分*****************
目前成都创新互联公司已为近千家的企业提供了网站建设、域名、网页空间、网站改版维护、企业网站设计、六合网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。
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软件:
[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:
2. 创建两个系统用户user1和user2:
3. 设置共享:
4. 添加用户user1对于该共享目录具有读写的权限,即完全控制权限;user2对于该共享目录只读.
5. 查看windows服务端计算机所属工作组:
1.3 进入Linux端samba配置文件:
[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:
[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:
[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:
4. 使用用户user2查看并上传文件,上传失败则验证成功:
[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 基于挂载的方式访问共享目录:
[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目录文件,创建工作组,添加用户:
[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软件:
[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,密码为各自用户名:
[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配置文件进行配置:
[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 =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 测试配置文件是否有语法错误,以及显示最终生效的配置:
[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服务:
[root@7-103 ~]# service smb start;service nmb start Starting SMB services: [ OK ] Starting NMB services: [ OK ] [root@7-103 ~]#
2.7 配置共享文件权限:
[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 交互式数据访问:
[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服务,前者可上传文件,后者则只读:
[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: \>
注:
# smbclient //10.68.7.103/shared -U centos //此处填写共享文件,不能包括共享 文件的上级目录.
3.0 在windows客户端验证:
需要出现的结果是,用gentoo用户登录访问共享目录文件,可创建文件:
3.1 用Ubuntu用户登录访问共享目录,创建文件被拒绝,仅能读取:
结果验证成功!
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流