通过Samba实现Linux与Windows间的文件共享

Samba

Samba,是用来让Linux系列的操作系统与Windows操作系统的SMB/CIFS(Server Message Block/Common Internet File System)网络协议做连结的自由软件,最大的功能就是可以用于Linux与windows系统直接的文件共享和打印共享(Linux与Linux之间的资源共享更多用NFS实现)。
组成Samba运行的有两个服务,一个是SMB,另一个是NetBIOS。SMB是Samba的核心启动服务,主要负责建立Samba服务器与Samba客户机之间的对话,验证用户身份并提供对文件和打印系统的访问,监听139 TCP端口(增强版SMB协议CIFS直接监听445端口,CIFS不需要NetBIOS协议);而NetBIOS服务是负责解析,提供浏览网络上的共享资源列表,监听UDP端口137和138。

实现Samba

  • 安装Samba:
[root@smaba ~]# lsb_release -rRelease: 7.2.1511[root@smaba ~]# yum -y install samba[root@smaba ~]# rpm -qi sambaName : sambaEpoch : 0Version : 4.7.1Release : 6.el7Architecture: x86_64[root@smaba ~]# systemctl stop firewalld.service[root@smaba ~]# setenforce 0
  • Samba相关配置:
  主配置文件:/etc/samba/smb.conf  主程序:nmbd(NetBIOS Name Server Daemon)、smbd(SMB/CIFS Daemon)  Unit文件:smb.service和nmb.service
其中
/etc/samba/smb.conf文件常见参数:  全局配置:[global] workgroup=MYGROUP #工作组名 server string=Samba Server Version %v #服务器信息介绍 netbios name=MYSERVER #用netbios名来指定服务 interfaces=[interface1 interface2...|address1 address2...] #用于让samba服务监听多个网络接口或IP hosts allow=[address1 address2...] #指定允许访问的主机IP log file=/var/log/samba/log.%m #指定日志存放路径,%m为来访的主机名 max log size=50 #定义日志文件最大容量为50K security=user #设置samba服务的安全认证方式为user passdb backend=tdbsam #定义用户后台的类型为tdbsam,其他类型还有smbpasswd、ldapsam load prints=yes #设置是否共享打印机 cups options=raw #打印机选项  其它配置:[homes]:为每个samba用户定义其是否能够通过samba服务访问自己的家目录[printers]:定义打印服务[自定义共享目录]:定义共享的文件系统  常用指令:comment=STRING:注释path=/PATH/TO/FILENAME:当前共享所映射的文件系统路径browseable=YES:是否可浏览,指是否可被所有用户查看pulibc=YES:是否允许匿名访问browseable=No:是否公开目录writable=YES:是否可写read only=no|yes:是否为只读write list=/PATH/TO/user_list|USERNAME:拥有写权限的用户列表
directory mask=MASK:新建目录的权限值
create mask=MASK:新建文件的权限值  更多参数可通过命令 man smb.conf 查看。
  • 自定义共享目录:
[root@samba ~]# vim /etc/samba/smb.conf[shared_dir] comment=shared dir through samba path=/samba_dir writable=yes[root@samba ~]# mkdir /samba_dir #创建共享文件[root@samba ~]# testparm #校验/etc/samba/smb.conf文件的配置是否正确Load smb config files from /etc/samba/smb.confrlimit_max: increasing rlimit_max (1024) to minimum Windows limit (16384)Processing section "[homes]"Processing section "[printers]"Processing section "[print$]"Processing section "[shared_dir]"Loaded services file OK.Server role: ROLE_STANDALONE
  • samba服务默认的验证模式为user,因此需要创建samba用户数据库:
samba帐号必须存在于Linux系统中(/etc/passwd),而其密码却需要单独维护。创建samba用户数据库有专门的命令:pdbedit 和 smbpasswd。  pdbedit [options] account  options:    -a USERNAME:创建samba用户    -x:删除samba用户   -L:列出samba用户列表   -Lv:列出用户详细信息列表  smbpasswd [options] USERNAME  options:    -a:添加账号    -x:删除账号    -d:禁用账号    -e:启用账号[root@samba ~]# useradd samba_user1[root@samba ~]# pdbedit -a samba_user1new password:retype new password:Unix username: samba_user1[root@samba ~]# useradd samba_user2[root@samba ~]# smbpasswd -a samba_user2New SMB password:Retype new SMB password:Added user samba_user2.
  •  windows主机测试:
# 启动服务[root@samba ~]# systemctl start smb.service[root@samba ~]# systemctl start nmb.service
# 客户端测试
  •  可以看到,用户登录后无法在/samba_dir下创建目录,明明已经设置了 writable=yes。这是因/samba_dir目录的属主和属组均为root,其他用户没有写权限。进行如下修改:
[root@samba ~]# groupadd samba[root@samba ~]# usermod -G samba samba_user1[root@samba ~]# usermod -G samba samba_user2[root@samba ~]# id samba_user1uid=1005(samba_user1) gid=1005(samba_user1) groups=1005(samba_user1),1007(samba)[root@samba ~]# id samba_user2uid=1006(samba_user2) gid=1006(samba_user2) groups=1006(samba_user2),1007(samba)[root@samba ~]# ll -d /samba_dirdrwxr-xr-x. 2 root root 6 Jun 29 17:19 /samba_dir[root@samba ~]# chown :samba /samba_dir[root@samba ~]# chmod g+w /samba_dir[root@samba ~]# ll -d /samba_dirdrwxrwxr-x. 2 root samba 6 Jun 29 17:19 /samba_dir
  • windows主机重新测试:

linux主机进行测试:

# 挂载访问。注意,此处访问的是/shared_dir,配置文件/etc/samba/smb.conf中 [] 定义的名称。[root@client ~]# mount -t cifs //192.168.4.119/shared_dir /mnt -o username=samba_user1Password for samba_user1@//192.168.4.119/shared_dir: ******[root@client ~]# ls /mnthello.txt test[root@client ~]# touch /mnt/hello[root@client ~]# ls /mnthello hello.txt test# 使用smbclient客户端访问[root@client ~]# yum install -y samba-client[root@client ~]# smbclient //192.168.4.119/shared_dir -U samba_user2 #交互式访问Enter SAMBA\samba_user2‘s password: Try "help" to get a list of possible commands.smb: \> ls . D 0 Tue Jul 3 17:07:35 2018 .. DR 0 Fri Jun 29 17:19:56 2018 test D 0 Tue Jul 3 15:42:01 2018 hello.txt A 0 Tue Jul 3 17:07:31 2018 hello N 0 Tue Jul 3 17:07:35 2018 121055488 blocks of size 1024. 116173648 blocks availablesmb: \> [root@client ~]# smbclient -L //192.168.4.119/shared_dir -U samba_user1 #查看共享情况Enter SAMBA\samba_user1‘s password: Sharename Type Comment --------- ---- ------- print$ Disk Printer Drivers shared_dir Disk shared dir through samba IPC$ IPC IPC Service (Samba 4.7.1) samba_user1 Disk Home DirectoriesReconnecting with SMB1 for workgroup listing. Server Comment --------- ------- Workgroup Master --------- ------- SAMBA HAPPINESS WORKGROUP DESKTOP-L064DV0

相关文章