SMB 服务器
SMB 服务介绍
Samba 是在Linux和UNIX系统上实现SMB协议(Server Messages Block,信息服务块),是一种在局域网上共享文件和打印机的一种通信协议。曾用名CIFS(通用互联网文件系统 Common Internet File System), 公元1983年诞生于IBM。SMB协议是C/S架构,通过该协议客户机(包括Windows、linux、Unix系统)可以访问服务器(包括Windows、linux、Unix系统)上的共享文件系统、打印机及其他资源。
Linux既可以充当服务器提供SMB文件共享,也可以充当客户端访问SMB共享。
-
客户端挂载SMB文件共享,需要安装cifs-utils软件包。
-
服务器端提供SMB共享,需要安装samba程序包。
本课程介绍如何将Samba配置为独立服务器。 在该配置中,服务器使用Samba数据库管理Samba用户帐户,并向本地工作组的成员提供文件共享。
提示:Windows server 2016 以及之后的版本都可以配置samba服务器。
安装Samba并准备共享目录
bash
[root@server ~]# yum install -y samba samba-client
[root@server ~]# mkdir -p /shares/samba
在此基本配置中,您可以使用Linux文件权限来控制对目录的访问。 例如,要向developers组的成员授予写访问权,并向其他所有人授予读访问权,请使用以下命令:
bash
[root@server ~]# groupadd -g 2000 developers
[root@server ~]# chgrp developers /shares/samba
[root@server ~]# chmod 2775 /shares/samba
[root@server ~]# ls -ld /shares/samba
drwxrwsr-x. 2 root developers 6 May 26 08:56 /shares/samba
SGID位可确保新内容自动属于开发人员组。
准备 Samba 用户
每个Samba帐户必须具有一个具有相同用户名的关联Linux帐户。 要创建仅Samba的用户帐户,请锁定其Linux密码,并将其登录Shell设置为/sbin/nologin。 此配置可防止用户使用SSH或从控制台登录Linux系统。
例如,要为operator1用户创建一个锁定的Linux帐户,请运行以下命令。
bash
[root@server ~]# useradd -s /sbin/nologin -u 1001 operator1
创建Linux帐户后,使用samba-common-tools 软件包中的smbpasswd命令将它们添加到Samba数据库中。
要将用户添加到Samba数据库,请使用带有-a选项的smbpasswd命令,并将用户名作为参数。 该命令提示输入密码。
bash
[root@server ~]# smbpasswd -a operator1
New SMB password: `123`
Retype new SMB password: `123`
Added user operator1.
# 查看samba数据库中用户清单
[root@server ~]# pdbedit -L
operator1:1001:
要从Samba数据库中删除用户,请使用-x选项。
bash
[root@server ~]# smbpasswd -a operator2
[root@server ~]# pdbedit -L
[root@server ~]# smbpasswd -x operator2
Deleted user operator1.
Samba在/var/lib/samba/private/目录下维护其数据库。 切勿直接修改该目录中的文件。
bash
# 增加开发人员用户
[root@server ~]# useradd -s /sbin/nologin -G developers -u 1011 developer1
[root@server ~]# useradd -s /sbin/nologin -G developers -u 1012 developer2
[root@server ~]# smbpasswd -a developer1
New SMB password: `123`
Retype new SMB password: `123`
Added user developer1.
[root@server ~]# smbpasswd -a developer2
New SMB password: `123`
Retype new SMB password: `123`
Added user developer2.
# 也可以使用以下命令设置密码
[root@server ~]# echo -e "123\n123" | smbpasswd -a -s developer2
Added user developer2.
[root@server ~]# pdbedit -L
developer1:1011:
operator1:1001:
developer2:1012:
设置 SELinux 上下文类型
bash
[root@server ~]# semanage fcontext -a -t samba_share_t '/shares/samba(/.*)?'
[root@server ~]# restorecon -Rv /shares/samba
Samba还可以提供带有public_content_t (只读)和public_content_rw_t(读/写)类型的文件。 使用public_content_rw_t类型,启用SELinux smbd_anon_write布尔值以允许读/写访问。 当您希望Apache HTTP Server或NGINX服务能够写入共享目录内容时,这两种SELinux类型非常有用。
提示:不要使用Samba共享NFS文件系统或FTP共享。 这样做可能导致文件损坏或其他文件访问问题。
配置 Samba
Samba的配置文件是**/etc/samba/smb.conf**。
/etc/samba/smb.conf配置文件以[global]节开头。 该部分提供了常规服务器配置和默认值,您可以在随后的部分中将其覆盖。接下来的部分定义文件或打印机共享。
要注释掉一行,请使用分号(;)或井号(#)字符。
配置 global
global 部分定义Samba服务器的基本配置。 在该部分中,常用的参数如下:
-
workgroup 参数,指定服务器的Windows工作组。 当客户端系统查询服务器时,该名称将显示在客户端系统上。 默认值为 SAMBA。
-
security 参数,控制Samba如何验证客户端。默认security = user,客户端使用用户名和密码登录,本地Samba服务器在其数据库中管理该用户名和密码。
-
**server min protocol **参数,指定服务器支持的最低SMB版本。 默认情况下,服务器支持协议的所有版本,并与客户端协商该版本。 由于第一个版本SMB1(或CIFS)存在安全问题,因此Red Hat建议将该参数设置为SMB2来排除该版本。 但是,使用该配置,Microsoft Windows XP或更早版本的客户端将无法使用您的服务器,因为它们仅支持SMB1。 SMB协议的当前版本为版本3。
-
smb crypty 参数,激活流量加密。 默认情况下,服务器和客户端协商加密。 要强制加密,请将smb crypty参数设置为required,并将服务器min协议设置为SMB3。 仅SMB3提供对加密的支持。 Microsoft Windows 8,Microsoft Windows Server 2012和更高版本的操作系统支持具有加密功能的SMB3。
-
hosts allow 参数,设置允许访问Samba服务器的客户端列表(以逗号、空格或制表符分隔)。如果未指定,则所有主机均可访问Samba。如果global块中未指定此设置,则可以单独在每个共享中设置。如果在global块中指定此设置,则适用于所有共享。
配置共享块
在[global]部分之后,定义共享部分。 括号中的名称定义了共享的名称,客户端可以看到。 该部分中常用的指令如下:
-
**path **指令,提供要在您的服务器上共享的目录的全名。
-
writeable 指令,指示经过身份验证的用户是否对共享具有读/写访问权限(设置为yes或no)。 默认设置为no。
writeable ,writable 和write ok 指令含义相同。read only = no 指令与writeable = yes指令含义相同。
-
write list ,当writeable指令的值为no(默认值)时,可以使用write list指令提供以逗号分隔的用户列表,列表中的用户对共享具有读/写访问权限,不在列表中的用户仅具有读取访问权限。在列表中,您可以通过在组名前面加上@字符来指定本地Linux组。
以下示例将对operator1用户和developers组成员的读写权限授予。
write list = operator1, @developers -
valid users,默认情况下,所有经过身份验证的用户都可以访问共享。 如果要限制该访问,请使用该指令。 该指令采用逗号分隔的应具有访问权限的用户列表。
以下示例声明webapp共享,并授予对/shares/samba目录的访问权限。 所有经过身份验证的用户都具有对该共享的读取访问权限,但是只有开发人员组的成员具有读/写访问权限
ini[webapp] comment = webapp valid users = operator1,@developers path = /shares/samba write list = @developers
本次实验实例
声明webapp共享,并授予了对/shares/wordpress目录的访问权限:
- 只有developers组中成员和operator1用户可以访问。
- operator1用户可以读访问。
- developers组中成员具有读取/写入访问权限。
bash
[root@server ~]# vim /etc/samba/smb.conf
......
# 文件最后添加如下内容
[webapp]
comment = webapp
path = /shares/samba/
browseable = Yes
write list = @developers
valid users = operator1,@developers
create mask = 0664
directory mask = 0775
验证配置文件
要验证/etc/samba/smb.conf文件中是否没有错误,请运行不带参数的testparm命令。
bash
[root@server ~]# testparm
Load smb config files from /etc/samba/smb.conf
Loaded services file OK.
Server role: ROLE_STANDALONE
Press enter to see a dump of your service definitions
Enter
# 按回车继续
...output omitted...
[root@server ~]# echo $?
0
启动 Samba
bash
[root@server ~]# systemctl enable smb --now
#139/TCP和445/TCP
[root@server ~]# firewall-cmd --permanent --add-service=samba
[root@server ~]# firewall-cmd --reload
Samba会定期检查/etc/samba/smb.conf是已更改。 如果配置文件已更改,则Samba会自动重新加载它。已经与Samba服务建立的任何连接不受影响。 运行systemctl reload smb命令立即重新加载配置文件,或者运行systemctl restart smb命令完全重新启动Samba。
Linux 客户端挂载
Microsoft Windows和Linux系统都可以从Samba服务器访问SMB共享。
在Linux系统上,安装 cifs-utils 软件包,以便可以在本地系统上挂载SMB共享。
- 在Linux上,您可以使用**//servername/sharename**访问共享。
- 标准的 Microsoft Windows 使用统一命名约定(UNC-Uniform Naming Convention),格式
\\servername\sharename,代表网络资源。 但由于\字符是shell中的转义字符,因此Linux实用程序通常改用/字符。
要挂载SMB共享,必须提供用户凭据以通过Samba服务器进行身份验证。 这些凭据确定对共享上文件的访问权限。 对于手动挂载,您可以使用username挂载选项。 该命令提示用户输入密码。
bash
# 安装 cifs-utisl
[root@client ~]# yum install -y cifs-utils samba-client nginx
# 查看服务端提供的共享
[root@client ~]# smbclient -L //10.1.8.10 -U operator1
Enter SAMBA\operator1's password: `123`
Sharename Type Comment
--------- ---- -------
print$ Disk Printer Drivers
webapp Disk
IPC$ IPC IPC Service (Samba 4.10.16)
operator1 Disk Home Directories
Reconnecting with SMB1 for workgroup listing.
Server Comment
--------- -------
Workgroup Master
--------- -------
[root@client ~]# mount -o username=operator1,password=123 //10.1.8.10/webapp /usr/share/nginx/html
Password for operator1@//server/webapp: `123`
[root@client ~]# df /usr/share/nginx/html
Filesystem 1K-blocks Used Available Use% Mounted on
//server/webapp 52403200 1613496 50789704 4% /usr/share/nginx/html
# 准备测试文件
[root@server ~]# cp /etc/host* /shares/samba/
[root@client ~]# ls /usr/share/nginx/html
host.conf hostname hosts
组权限验证
developer1和developer2同属于developers组,developer1创建的文件,developer2可以修改
bash
# 确认配置了create mask 和 directory mask
[root@server ~]# vim /etc/samba/smb.conf
......
[webapp]
comment = webapp
path = /shares/samba/
browseable = Yes
write list = @developers
valid users = operator1,@developers
create mask = 0664
directory mask = 0775
# 重启服务
[root@server ~]# systemctl restart smb.service
客户端验证:
developer1挂载/usr/share/nginx/html/,写入index.html,
卸载/usr/share/nginx/html/,developer2再挂载,修改index.html

要自动挂载共享 ,使用samba.secrets 选项。
bash
[root@client ~]# vim /etc/fstab
# 最后添加记录
//server/webapp /usr/share/nginx/html cifs samba.secrets=/etc/samba/samba.secrets 0 0
[root@client ~]# vim /etc/samba/samba.secrets
username=operator1
password=123
[root@client ~]# chmod 600 /etc/samba/samba.secrets
# 验证开机自动挂载
[root@client ~]# umount /usr/share/nginx/html
[root@client ~]# mount /usr/share/nginx/html
[root@client ~]# df /usr/share/nginx/html
Filesystem 1K-blocks Used Available Use% Mounted on
//server/webapp 52403200 1613496 50789704 4% /usr/share/nginx/html
# 重启验证
[root@client ~]# reboot
[root@client ~]# df /usr/share/nginx/html
bash
[root@client ~ 10:32:38]# mount /usr/share/nginx/html/
Password for root@//10.1.8.10/webapp: ***
mount error(13): Permission denied
Refer to the mount.cifs(8) manual page (e.g. man mount.cifs)
挂载时有以上报错,则根据提示使用man mount.cifs,查询mount.cifs的具体用法:

Linux 多用户挂载
客户端准备用户
bash
# 添加用户operator1
[root@client ~]# useradd -u 1001 operator1
# 添加用户组 developers 和用户developer1、developer2
[root@client ~]# groupadd -g 2000 developers
[root@client ~]# useradd -G developers -u 1011 developer1
[root@client ~]# useradd -G developers -u 1012 developer2
# 设置用户密码
[root@client ~]# echo 123 | passwd --stdin operator1
[root@client ~]# echo 123 | passwd --stdin developer1
[root@client ~]# echo 123 | passwd --stdin developer2
使用samba.secrets 或username 选项挂载SMB共享时,本地系统上的所有用户都将使用这些凭据。 理想情况下,您希望每个用户都使用自己的SMB凭据来确定对已装载共享上文件的访问。 您可以使用multiuser挂载选项进行设置。
使用multiuser 挂载选项时,可以使用对共享具有最小访问权限的凭据来根安装SMB共享。 用户登录时,他们使用cifscreds命令将其SMB密码临时添加到安全内核密钥环中。 然后,客户端的Linux内核将使用其SMB凭据来确定对共享的访问,而不是用于安装共享的根目录。
以下步骤描述了针对多用户选项的系统配置:
-
创建用于装载SMB共享的凭据文件。 您在凭据文件中使用的用户必须对SMB共享具有最小的访问权限,并且对共享目录只具有读取访问权限。
-
在挂载命令中添加多用户选项。 以下/etc/fstab条目使用凭据和多用户选项。 该条目在一行中,没有换行符。
bash[root@client ~]# vim /etc/fstab # 修改挂载记录,添加multiuser //server/webapp /usr/share/nginx/html cifs samba.secrets=/etc/samba/samba.secrets,multiuser 0 0 # 卸载后,再次挂载 [root@client ~]# umount /usr/share/nginx/html;mount /usr/share/nginx/html使用mount命令挂载SMB共享。
bash[operator1@client ~]$ ls /usr/share/nginx/html ls: cannot access /usr/share/nginx/html: Permission denied # 运行cifscreds命令配置凭据。 [operator1@client ~]$ cifscreds add server Password: [operator1@client ~]$ ls /usr/share/nginx/html/ hosts hosts.allow hosts.deny [operator1@client ~]$ touch /usr/share/nginx/html/operator-f1 touch: cannot touch '/usr/share/nginx/html/operator-f1': Permission deniedcifscreds 命令将子命令作为其第一个参数,并将Samba服务器名称作为第二个参数。
- add 子命令,将SMB凭据加载到内核密钥环。
- clear 子命令,从用户的特定主机的内核密钥环中删除凭证。
- update 子命令,将您在内核密钥环中的凭据替换为新用户和密码的凭据。
默认情况下,cifscreds假定要与SMB凭据一起使用的用户名与当前Linux用户名匹配。 您可以在add或clear子命令后使用**-u username**选项为SMB凭据指定其他用户名。
-
使用 developer1 账户测试
bash[developer1@client ~]$ cifscreds add server Password: [developer1@client ~]$ touch /usr/share/nginx/html/developer1-f1 [developer1@client ~]$ ls /usr/share/nginx/html developer1-f1 hosts hosts.allow hosts.deny
Windows 客户端挂载
使用 win+R 快捷键弹出运行对话框,输入\\server



已经使用operator1登录,再使用developer1登录,如果有以下提示:

需要先清除会话,清理方式使用命令:net use * /del /y

清除后,可以正常使用developer1登录:

继续清理,再使用developer2用户登录:


重要的挂载选项
bash
# 重要说明
# FILE AND DIRECTORY OWNERSHIP AND PERMISSIONS
The core CIFS protocol does not provide unix ownership information or mode for files and directories. Because of this, files and directories will generally appear to be owned by whatever values the uid= or gid= options are set, and will have permissions set to the default file_mode and dir_mode for the mount.
Attempting to change these values via chmod/chown will return success but have no effect.
When the client and server negotiate unix extensions, files and directories will be assigned the uid, gid, and mode provided by the server. Because CIFS mounts are generally single-user, and the same samba.secrets are used no matter what user accesses the mount, newly created files and directories will generally be given ownership corresponding to whatever samba.secrets were used to mount the share.
# uid=arg,sets the uid that will own all files or directories on the mounted
filesystem when the server does not provide ownership information.
# gid=arg,sets the gid that will own all files or directories on the mounted
filesystem when the server does not provide ownership information.
# file_mode=arg,If the server does not support the CIFS Unix extensions this overrides the default file mode.
# dir_mode=arg,If the server does not support the CIFS Unix extensions this overrides the default mode for directories.
# forceuid,instructs the client to ignore any uid provided by the server for files and directories and to always assign the owner to be the value of the uid= option.
# forcegid,instructs the client to ignore any gid provided by the server for files and directories and to always assign the owner to be the value of the gid= option.
总结:
如果要切换用户登录,需要先断开旧的会话。因为Windows 会缓存之前访问 Samba 服务器的用户凭据,且默认不允许同一客户端以多个用户身份同时连接同一台 Samba 服务器。只需在 Windows 端清除旧连接和凭据缓存,再用新用户重新连接即可。
以下是具体操作步骤:
-
命令行强制断开所有旧连接,这是最直接有效的方法。按下
Win+R输入cmd打开命令提示符,执行命令强制删除所有网络共享连接,命令如下:bashnet use * /del /y执行后会断开当前所有的网络驱动器和 Samba 共享连接,清除旧用户的连接会话。
-
清除凭据管理器中的缓存凭据 :若执行上一步后仍无法切换,可能是凭据被单独缓存了。按
Win+R输入control keymgr.dll打开凭据管理器,找到对应 Samba 服务器(以服务器 IP 或主机名命名)的凭据条目,选中后点击删除;也可直接进入控制面板的 "用户帐户和家庭安全→凭据管理器",删除相关 Windows 凭据。 -
重启 Workstations 服务确保生效 :部分情况下旧连接进程可能残留,按
Win+R输入services.msc打开服务面板,找到 "Workstations" 服务,右键选择重启,彻底释放旧连接占用的资源。 -
用权限更大的用户重新连接:两种方式可选择。
一是直接在文件资源管理器地址栏输入
\\Samba服务器IP,此时系统会弹出登录框,输入权限更大的用户名和密码即可;二是通过命令行指定用户连接,命令如下(将 Z: 改为自定义盘符,替换服务器 IP、共享名和新用户信息):
bashnet use Z: \\服务器IP\共享名 /user:权限更大的用户名 *输入后按提示输入对应密码,就能以新用户身份建立连接。
centos服务器总结
DHCP 提供网络配置服务,额外提供tftp,告知客户端通过哪个tftp服务实现引导。
DNS 提供域名解析的。
tftp 小型文件传输服务器,专门用来实现网络引导。
vsftpd,sftpd 实现专用的文件传输。
NFS 网络文件共享,不支持跨平台,为Linux、unix之间共享。
Samba 网络文件共享,支持跨平台,Linux、unix、windows之间共享。
iSCSI 网络块存储共享,多路径。
mariadb 数据库服务
nginx web服务
rsync 同步,配合sersync(实时监控变化)实现实时同步。