系统环境

实验环境
VMware? Workstation 12 Pro 12.0.1 build-3160714

系统平台
Red Hat Enterprise Linux 7 64 位

内核
3.10.0-123.el7.x86_64

samba服务器IP
192.168.1.30

samba的版本
4.2.10-6.el7_2

samba-client的版本
4.2.10-6.el7_2

一.Samba的安装

说明
指令

安装
yum -y install samba samba-client

查看
rpm -qa | grep samba

内容
936fea28-d364-422c-950a-d6db1725500a

启动
systemctl start smb && systemctl start nmb

设置开机启动
systemctl enable smb && systemctl enable nmb

配置文件详解

1 # This is the main Samba configuration file. For detailed information about the

2 # options listed here, refer to the smb.conf(5) manual page. Samba has a huge

3 # number of configurable options, most of which are not shown in this example.

4 #

5 # The Official Samba 3.2.x HOWTO and Reference Guide contains step-by-step

6 # guides for installing, configuring, and using Samba:

7 # http://www.samba.org/samba/docs/Samba-HOWTO-Collection.pdf

8 #

9 # The Samba-3 by Example guide has working examples for smb.conf. This guide is

10 # generated daily: http://www.samba.org/samba/docs/Samba-Guide.pdf

11 #

12 # In this file, lines starting with a semicolon (;) or a hash (#) are

13 # comments and are ignored. This file uses hashes to denote commentary and

14 # semicolons for parts of the file you may wish to configure.

15 #

16 # Note: Run the "testparm" command after modifying this file to check for basic

17 # syntax errors.

18 #

19 #---------------

20 # Security-Enhanced Linux (SELinux) Notes: //SELinux安全设置

21 #

22 # Turn the samba_domain_controller Boolean on to allow Samba to use the useradd

23 # and groupadd family of binaries. Run the following command as the root user to

24 # turn this Boolean on:

25 # setsebool -P samba_domain_controller on

26 #

27 # Turn the samba_enable_home_dirs Boolean on if you want to share home

28 # directories via Samba. Run the following command as the root user to turn this

29 # Boolean on:

30 # setsebool -P samba_enable_home_dirs on  //如果不需要在samba使用默认的共享家目录,可以不执行

31 #

32 # If you create a new directory, such as a new top-level directory, label it

33 # with samba_share_t so that SELinux allows Samba to read and write to it. Do

34 # not label system directories, such as /etc/ and /home/, with samba_share_t, as

35 # such directories should already have an SELinux label.

36 #

37 # Run the "ls -ldZ /path/to/directory" command to view the current SELinux

38 # label for a given directory.

39 #

40 # Set SELinux labels only on files and directories you have created. Use the

41 # chcon command to temporarily change a label:

42 # chcon -t samba_share_t /path/to/directory  //samba共享目录中允许创建目录时运行

43 #

44 # Changes made via chcon are lost when the file system is relabeled or commands

45 # such as restorecon are run.

46 #

47 # Use the samba_export_all_ro or samba_export_all_rw Boolean to share system

48 # directories. To share such directories and only allow read-only permissions:

49 # setsebool -P samba_export_all_ro on  //samba共享目录为只读是运行

50 # To share such directories and allow read and write permissions:

51 # setsebool -P samba_export_all_rw on  //samba共享目录为可写是运行

52 #

53 # To run scripts (preexec/root prexec/print command/...), copy them to the

54 # /var/lib/samba/scripts/ directory so that SELinux will allow smbd to run them.

55 # Note that if you move the scripts to /var/lib/samba/scripts/, they retain

56 # their existing SELinux labels, which may be labels that SELinux does not allow

57 # smbd to run. Copying the scripts will result in the correct SELinux labels.

58 # Run the "restorecon -R -v /var/lib/samba/scripts" command as the root user to

59 # apply the correct SELinux labels to these files.

60 #

61 #--------------

62 #

63 #======================= Global Settings =====================================

64

65 [global]   //全局配置

66

67 # ----------------------- Network-Related Options -------------------------//网络相关配置

68 #

69 # workgroup = the Windows NT domain name or workgroup name, for example, MYGROUP.

70 #

71 # server string = the equivalent of the Windows NT Description field.

72 #

73 # netbios name = used to specify a server name that is not tied to the hostname.

74 #

75 # interfaces = used to configure Samba to listen on multiple network interfaces.

76 # If you have multiple interfaces, you can use the "interfaces =" option to

77 # configure which of those interfaces Samba listens on. Never omit the localhost

78 # interface (lo).

79 #

80 # hosts allow = the hosts allowed to connect. This option can also be used on a

81 # per-share basis.

82 #

83 # hosts deny = the hosts not allowed to connect. This option can also be used on

84 # a per-share basis.

85 #

86 # max protocol = used to define the supported protocol. The default is NT1. You

87 # can set it to SMB2 if you want experimental SMB2 support.

88 #

89         workgroup = MYGROUP    //samba服务器所在的域名或工作组名称

90         server string = Samba Server Version %v   //samba服务器的版本说明

91

92 ;       netbios name = MYSERVER   //设置samba服务器的netbios名称

93

94 ;       interfaces = lo eth0 192.168.12.2/24 192.168.13.2/24  //samba服务器监听的本地网络端口

95 ;       hosts allow = 127. 192.168.12. 192.168.13.  //允许连接到samba服务器的客户端

96

97 ;       max protocol = SMB2    //使用的协议

98

99 # --------------------------- Logging Options -----------------------------//日志记录配置

100 #

101 # log file = specify where log files are written to and how they are split.

102 #

103 # max log size = specify the maximum size log files are allowed to reach. Log

104 # files are rotated when they reach the size specified with "max log size".

105 #

106

107         # log files split per-machine:

108         log file = /var/log/samba/log.%m     //指定samba日志文件

109         # maximum size of 50KB per log file, then rotate:

110         max log size = 50                    //设定日志的大小

111

112 # ----------------------- Standalone Server Options ------------------------//独立服务器选项

113 #

114 # security = the mode Samba runs in. This can be set to user, share

115 # (deprecated), or server (deprecated).

116 #

117 # passdb backend = the backend used to store user information in. New

118 # installations should use either tdbsam or ldapsam. No additional configuration

119 # is required for tdbsam. The "smbpasswd" utility is available for backwards

120 # compatibility.

121 #

122

123         security = user    //登陆设定用户密码

124         passdb backend = tdbsam   //使用默认值,不用修改

125

126

127 # ----------------------- Domain Members Options ------------------------//加入windows域成员的配置

128 #

129 # security = must be set to domain or ads.

130 #

131 # passdb backend = the backend used to store user information in. New

132 # installations should use either tdbsam or ldapsam. No additional configuration

133 # is required for tdbsam. The "smbpasswd" utility is available for backwards

134 # compatibility.

135 #

136 # realm = only use the realm option when the "security = ads" option is set.

137 # The realm option specifies the Active Directory realm the host is a part of.

138 #

139 # password server = only use this option when the "security = server"

140 # option is set, or if you cannot use DNS to locate a Domain Controller. The

141 # argument list can include My_PDC_Name, [My_BDC_Name], and [My_Next_BDC_Name]:

142 #

143 # password server = My_PDC_Name [My_BDC_Name] [My_Next_BDC_Name]

144 #

145 # Use "password server = *" to automatically locate Domain Controllers.

146

147 ;       security = domain

148 ;       passdb backend = tdbsam

149 ;       realm = MY_REALM

150

151 ;       password server = <NT-Server-Name>  //指定身份验证的域控制器ip或主机名称

152

153 # ----------------------- Domain Controller Options ------------------------

154 #samba服务器作为域控制器的配置

155 # security = must be set to user for domain controllers.

156 #

157 # passdb backend = the backend used to store user information in. New

158 # installations should use either tdbsam or ldapsam. No additional configuration

159 # is required for tdbsam. The "smbpasswd" utility is available for backwards

160 # compatibility.

161 #

162 # domain master = specifies Samba to be the Domain Master Browser, allowing

163 # Samba to collate browse lists between subnets. Do not use the "domain master"

164 # option if you already have a Windows NT domain controller performing this task.

165 #

166 # domain logons = allows Samba to provide a network logon service for Windows

167 # workstations.

168 #

169 # logon script = specifies a script to run at login time on the client. These

170 # scripts must be provided in a share named NETLOGON.

171 #

172 # logon path = specifies (with a UNC path) where user profiles are stored.

173 #

174 #

175 ;       security = user          //默认值,不用修改

176 ;       passdb backend = tdbsam    //默认值,不用修改

177

178 ;       domain master = yes         //让samba服务器作为主域控制器

179 ;       domain logons = yes       //允许windows客户端提交验证信息

180

181         # the following login script name is determined by the machine name

182         # (%m):

183 ;       logon script = %m.bat   //用户登录到域执行的启动脚本

184         # the following login script name is determined by the UNIX user used:

185 ;       logon script = %u.bat   //域客户端的开机启动脚本

186 ;       logon path = \\%L\Profiles\%u    //配置文件存放位置

187         # use an empty path to disable profile support:

188 ;       logon path =

189

190         # various scripts can be used on a domain controller or a stand-alone

191         # machine to add or delete corresponding UNIX accounts:

192

193 ;       add user script = /usr/sbin/useradd "%u" -n -g users  //添加用户信息同步脚本

194 ;       add group script = /usr/sbin/groupadd "%g"  //添加组信息同步脚本

195 ;       add machine script = /usr/sbin/useradd -n -c "Workstation (%u)" -M -d /nohome -s /b    in/false "%u"  //添加计算机信息同步脚本

196 ;       delete user script = /usr/sbin/userdel "%u"  //删除用户信息同步脚本

197 ;       delete user from group script = /usr/sbin/userdel "%u" "%g" //删除用户组信息同步脚本

198 ;       delete group script = /usr/sbin/groupdel "%g" //删除组信息同步脚本

199

200

201 # ----------------------- Browser Control Options ----------------------------

202 #配置浏览器

203 # local master = when set to no, Samba does not become the master browser on

204 # your network. When set to yes, normal election rules apply.

205 #

206 # os level = determines the precedence the server has in master browser

207 # elections. The default value should be reasonable.

208 #

209 # preferred master = when set to yes, Samba forces a local browser election at

210 # start up (and gives itself a slightly higher chance of winning the election).

211 #

212 ;       local master = no   //是否允许samba服务器作为主浏览器

213 ;       os level = 33  //数字越大代表被选举成为主浏览器的可能更大

214 ;       preferred master = yes  //yes时选为主浏览器的可能更高

215

216 #----------------------------- Name Resolution -------------------------------

217 #samba服务器的名称解析

218 # This section details the support for the Windows Internet Name Service (WINS).

219 #

220 # Note: Samba can be either a WINS server or a WINS client, but not both.

221 #

222 # wins support = when set to yes, the NMBD component of Samba enables its WINS

223 # server.

224 #

225 # wins server = tells the NMBD component of Samba to be a WINS client.

226 #

227 # wins proxy = when set to yes, Samba answers name resolution queries on behalf

228 # of a non WINS capable client. For this to work, there must be at least one

229 # WINS server on the network. The default is no.

230 #

231 # dns proxy = when set to yes, Samba attempts to resolve NetBIOS names via DNS

232 # nslookups.

233

234 ;       wins support = yes   //设置nmbd进程支持wins服务器

235 ;       wins server = w.x.y.z   //wins的ip地址

236 ;       wins proxy = yes   //samba服务器是否作为wins的代理

237

238 ;       dns proxy = yes  //是否通过dns去解析主机的netbios名

239

240 # --------------------------- Printing Options -----------------------------

241 #samba服务器的打印配置

242 # The options in this section allow you to configure a non-default printing

243 # system.

244 #

245 # load printers = when set you yes, the list of printers is automatically

246 # loaded, rather than setting them up individually.

247 #

248 # cups options = allows you to pass options to the CUPS library. Setting this

249 # option to raw, for example, allows you to use drivers on your Windows clients.

250 #

251 # printcap name = used to specify an alternative printcap file.

252 #

253

254         load printers = yes    //是否自动共享打印机

255         cups options = raw

256

257 ;       printcap name = /etc/printcap   //获取打印机描述信息的位置

258         # obtain a list of printers automatically on UNIX System V systems:

259 ;       printcap name = lpstat

260 ;       printing = cups

261

262 # --------------------------- File System Options ---------------------------

263 #文件系统的配置

264 # The options in this section can be un-commented if the file system supports

265 # extended attributes, and those attributes are enabled (usually via the

266 # "user_xattr" mount option). These options allow the administrator to specify

267 # that DOS attributes are stored in extended attributes and also make sure that

268 # Samba does not change the permission bits.

269 #

270 # Note: These options can be used on a per-share basis. Setting them globally

271 # (in the [global] section) makes them the default for all shares.

272

273 ;       map archive = no  //Windows客户端复制或者移动文件到samba服务器共享目录时,是否保留文件在windows中的存档属性

274 ;       map hidden = no  //Windows客户端复制或者移动文件到samba服务器共享目录时,是否保留文件在windows中的隐藏文件属性

275 ;       map read only = no  //Windows客户端复制或者移动文件到samba服务器共享目录时,是否保留文件在windows中的只读属性

276 ;       map system = no  //Windows客户端复制或者移动文件到samba服务器共享目录时,是否保留文件在windows中的系统属性

277 ;       store dos attributes = yes  //Windows客户端复制或者移动文件到samba服务器共享目录时,是否保留文件在windows中的相关属性

278

279

280 #============================ Share Definitions 共享的资源=============================

281

282 [homes]

283         comment = Home Directories  //说明:comment是对该共享的描述,可以是任意字符串。

284         browseable = no   //说明:browseable用来指定该共享是否可以浏览。

285         writable = yes  //说明:writable用来指定该共享路径是否可写。

286 ;       valid users = %S  //说明:valid users用来指定允许访问该共享资源的用户。

287 ;       valid users = MYDOMAIN\%S

288

289 [printers]

290         comment = All Printers

291         path = /var/spool/samba

292         browseable = no

293         guest ok = no  //用来指定该共享是否允许guest账户访问。

294         writable = no

295         printable = yes

296

297 # Un-comment the following and create the netlogon directory for Domain Logons:

298 ;       [netlogon]

299 ;       comment = Network Logon Service

300 ;       path = /var/lib/samba/netlogon

301 ;       guest ok = yes

302 ;       writable = no

303 ;       share modes = no

304

305 # Un-comment the following to provide a specific roving profile share.

306 # The default is to use the user's home directory:

307 ;       [Profiles]

308 ;       path = /var/lib/samba/profiles

309 ;       browseable = no

310 ;       guest ok = yes

311

312 # A publicly accessible directory that is read only, except for users in the

313 # "staff" group (which have write permissions):

314 ;       [public]

315 ;       comment = Public Stuff

316 ;       path = /home/samba

317 ;       public = yes

318 ;       writable = yes

319 ;       printable = no

320 ;       write list = +staff

创建共享目录        
mkdir /share

配置samba
vim /etc/samba/smb.conf

文件最后添加内容

[public]               //设置针对的是共享目录个别的设置,只对当前的共享资源起作用

        comment = Public Stuff     //对共享目录的说明文件,自己可以定义说明信息

        path = /share             //用来指定共享的目录,必选项.使用绝对路径

修改selinux
chcon -R -t samba_share_t /share

测试成功
testparm 
a8b9c25f-3ef2-4f47-9452-838b3a1a4c55

创建系统用户
useradd wjb10000

系统用户密码
passwd wjb10000

设置smb密码
smbpasswd -a wjb10000
6072907b-3833-40a1-aa11-f0c619058b5a

关闭防火墙
systemctl stop firewalld && systemctl disable firewalld

访问结果
9407132f-8b35-4a33-b5ca-0698859ad9dc

不显示家目录
注释掉04300cd5-9b59-4b43-80fc-03eee0c1e0e136e002cf-53bd-4433-9f49-2c666012e083

使用别名用户           
vim /etc/samba/smbusers

添加内容
wjb10000 = wjb   注:本机系统用户 = 别名

编译配置文件
vim /etc/samba/smb.conf

添加内容
[global] 全局变量下添加新内容 username map = /etc/samba/smbusers
abcf680a-56f1-47f6-ae2b-767516f237fd

客户端断开samba用户链接  
win客户端机器(开始->运行->输入cmd)在dos下输入命令net use * /delete /y,就可以断开所有网络用户的链接了。如果登录的时候勾选了保存用户,就(开始->运行->输入control keymgr.dll,在弹出的对话框中选择你samba服务器的主机名删除即可。

samba服务器全部搭建完成

samba服务器扩展知识

用户浏览权限控制
browseable=yes时,将显示共享资源。browseable=no时,将隐藏共享资源。

用户访问权限控制
public=yes时,允许匿名用户访问共享资源。public=no时,不允许匿名用户访问共享资源。
guest ok和public的功能相同。同时设置发生冲突时,最后的参数优先。

用户和组的权限
valid users = wjb10000 //允许wjb10000用户访问共享目录。
valid users = @wjb     //允许wjb组的所有成员访问共享目录
invalid users = wjb10000 //不允许wjb10000用户访问共享目录。
invalid users = @wjb     //不允许wjb组的所有成员访问共享目录

用户读写权限控制
readonly = yes时,共享目录为只读。readonly = no时,不使用只读方式。
read list = @组名,设置只读的用户组
writable = yes时,共享目录为可写。writable = no时,共享目录为不可写。
write list = @组名,设置只写的用户组
force user = wjb //访问共享资源时,建立文件和目录的拥有组都是wjb
group = wjb      //功能同force user一样。
veto files = /*root*/   不能上传含有root关键字的文件或者目录

文件和目录权限
create mask = 444  

文件权限
chmod 1777 /share  //每个人都能读写。但不能删除别人的文件