为了解决Linux和Windows不兼容的问题,ftp虽然能够做到,但是不支持挂载
SMB:Server Message Block 服务器消息块,IBM发布,最早是DOS网络文件共享协议,是私有协议
CIFS:common internet file system,微软基于SMB发布
SAMBA:1991年Andrew Tridgell,实现 windows和UNIX相通
官方网站:http://www.samba.org/
SAMBA的功能:
Windows计算机网络管理模式:
相关包:
相关服务进程:
主配置文件:/etc/samba/smb.conf 帮助参看:man smb.conf
语法检查: testparm [-v] [/etc/samba/smb.conf]
客户端工具:smbclient,mount.cifs
范例:
dnf install samba
systemctl start smb
systemctl start nmb
UNC路径: Universal Naming Convention,通用命名规范,格式如下
\\sambaserver\sharename
改成$后可以在Windows里隐藏,但是在Linux里不起作用
smbclient -L instructor.example.com
smbclient -L instructor.example.com -U smb用户%password
#可以使用-U选项来指定用户%密码,或通过设置和导出USER和PASSWD环境变量来指定
smbclient //instructor.example.com/shared -U wang
>cd directory
>get file1
>put file2
说明:生产环境中可能用的并不多,生产中一般是Windows当客户端,Linux当服务器端
范例:手动挂载
mount -o user=wang,password=magedu //server//shared /mnt/smb
范例:开机自动挂载(此方法需要安装cifs-utils包)
cat /etc/fstab
#可以用文件代替用户名和密码的输入
//server/homes /mnt cifs credentials或cred=/etc/smb.txt 0 0
cat /etc/smb.txt
username=wang #或 user=wang
password=password #或 pass=password
#给密码文件加上权限,保证安全
chmod 600 /etc/smb.txt
范例: SMB协议版本
[root@centos8 ~]#man mount.cifs
vers=arg
SMB protocol version. Allowed values are:
· 1.0 - The classic CIFS/SMBv1 protocol.
· 2.0 - The SMBv2.002 protocol. This was initially introduced in Windows Vista Service Pack 1, and Windows Server 2008. Note that
the initial release version of Windows Vista spoke a slightly different dialect (2.000) that is not supported.
· 2.1 - The SMBv2.1 protocol that was introduced in Microsoft Windows 7 and Windows Server 2008R2.
· 3.0 - The SMBv3.0 protocol that was introduced in Microsoft Windows 8 and Windows Server 2012.
· 3.02 or 3.0.2 - The SMBv3.0.2 protocol that was introduced in Microsoft Windows 8.1 and Windows Server 2012R2.
· 3.1.1 or 3.11 - The SMBv3.1.1 protocol that was introduced in Microsoft Windows 10 and Windows Server 2016.
· 3 - The SMBv3.0 protocol version and above.
· default - Tries to negotiate the highest SMB2+ version supported by both the client and server.
[root@centos7 ~]#man mount.cifs
vers=
SMB protocol version. Allowed values are:
· 1.0 - The classic CIFS/SMBv1 protocol. This is the default.
· 2.0 - The SMBv2.002 protocol. This was initially introduced in Windows Vista Service Pack 1, and Windows Server 2008. Note that
the initial release version of Windows Vista spoke a slightly different dialect (2.000) that is not supported.
· 2.1 - The SMBv2.1 protocol that was introduced in Microsoft Windows 7 and Windows Server 2008R2.
· 3.0 - The SMBv3.0 protocol that was introduced in Microsoft Windows 8 and Windows Server 2012.
说明:samba用户须是Linux用户,建议使用/sbin/nologin
添加 samba用户(虚拟用户):
#必须加-a选项,否则就是现有的用户改密码
smbpasswd -a <user>
pdbedit -a -u <user>
修改用户密码:
smbpasswd <user>
删除用户和密码:
smbpasswd -x <user>
pdbedit -x -u <user>
查看samba用户列表:
pdbedit -L -v
范例: 创建samba用户
#注意不要用-r 设为系统用户,因为不会生成家目录
[root@centos8 ~]#useradd -s /sbin/nologin smb1
[root@centos8 ~]#useradd -s /sbin/nologin smb2
[root@centos8 ~]#useradd -s /sbin/nologin smb3
[root@centos8 ~]#smbpasswd -a smb1
[root@centos8 ~]#smbpasswd -a smb2
[root@centos8 ~]#smbpasswd -a smb3
[root@centos8 ~]#pdbedit -L
smb1:995:
smb2:994:
smb3:993:
[root@centos8 ~]#pdbedit -L -v
---------------
Unix username: smb1
NT username:
Account Flags: [U ]
User SID: S-1-5-21-564547271-2436613516-3895257592-1000
Primary Group SID: S-1-5-21-564547271-2436613516-3895257592-513
Full Name:
Home Directory: \\centos8\smb1
HomeDir Drive:
Logon Script:
Profile Path: \\centos8\smb1\profile
Domain: CENTOS8
Account desc:
Workstations:
Munged dial:
Logon time: 0
Logoff time: Wed, 06 Feb 2036 23:06:39 CST
Kickoff time: Wed, 06 Feb 2036 23:06:39 CST
Password last set: Sun, 05 Jul 2020 09:44:45 CST
Password can change: Sun, 05 Jul 2020 09:44:45 CST
Password must change: never
Last bad password : 0
Bad password count : 0
Logon hours : FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
---------------
Unix username: smb2
NT username:
Account Flags: [U ]
User SID: S-1-5-21-564547271-2436613516-3895257592-1001
Primary Group SID: S-1-5-21-564547271-2436613516-3895257592-513
Full Name:
Home Directory: \\centos8\smb2
HomeDir Drive:
Logon Script:
Profile Path: \\centos8\smb2\profile
Domain: CENTOS8
Account desc:
Workstations:
Munged dial:
Logon time: 0
Logoff time: Wed, 06 Feb 2036 23:06:39 CST
Kickoff time: Wed, 06 Feb 2036 23:06:39 CST
Password last set: Sun, 05 Jul 2020 09:46:11 CST
Password can change: Sun, 05 Jul 2020 09:46:11 CST
Password must change: never
Last bad password : 0
Bad password count : 0
Logon hours : FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
---------------
Unix username: smb3
NT username:
Account Flags: [U ]
User SID: S-1-5-21-564547271-2436613516-3895257592-1002
Primary Group SID: S-1-5-21-564547271-2436613516-3895257592-513
Full Name:
Home Directory: \\centos8\smb3
HomeDir Drive:
Logon Script:
Profile Path: \\centos8\smb3\profile
Domain: CENTOS8
Account desc:
Workstations:
Munged dial:
Logon time: 0
Logoff time: Wed, 06 Feb 2036 23:06:39 CST
Kickoff time: Wed, 06 Feb 2036 23:06:39 CST
Password last set: Sun, 05 Jul 2020 09:48:11 CST
Password can change: Sun, 05 Jul 2020 09:48:11 CST
Password must change: never
Last bad password : 0
Bad password count : 0
Logon hours : FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
#客户端查看
[root@Centos7 ~]#smbclient -L 10.0.0.8 -U smb2
Enter SAMBA\smb2's password:
Sharename Type Comment
--------- ---- -------
print$ Disk Printer Drivers
IPC$ IPC IPC Service (Samba 4.11.2)
smb2 Disk Home Directories
Reconnecting with SMB1 for workgroup listing.
smbXcli_negprot_smb1_done: No compatible protocol selected by server.
protocol negotiation failed: NT_STATUS_INVALID_NETWORK_RESPONSE
Unable to connect with SMB1 -- no workgroup available
查看samba服务器状态:
smbstatus
范例:
[root@centos8 ~]#smbstatus
Samba version 4.11.2
PID Username Group Machine Protocol Version Encryption Signing
----------------------------------------------------------------------------------------------------------------------------------------
2712 smb1 smb1 10.0.0.1 (ipv4:10.0.0.1:12574) SMB3_11 - partial(AES-128-CMAC)
Service pid Machine Connected at Encryption Signing
---------------------------------------------------------------------------------------------
IPC$ 2712 10.0.0.1 Sun Jul 5 09:52:41 AM 2020 CST - -
No locked files
samba 配置文件 /etc/smb.conf 格式 ,使用.ini文件的格式
帮助:man smb.conf
用 [ ] 分成以下几部分
全局设置:
[global] 服务器通用或全局设置的部分
特定共享设置:
[homes] 用户的家目录共享
[printers] 定义打印机资源和服务
[sharename] 自定义的共享目录配置
其中:#和;开头的语句为注释,大小写不敏感
samba配置中的宏定义:
%m 客户端主机的NetBIOS名
%M 客户端主机的FQDN
%H 当前用户家目录路径
%U 当前用户的用户名
%g 当前用户所属组
%h samba服务器的主机名
%L samba服务器的NetBIOS名
%I 客户端主机的IP,是i的大写字母
%T 当前日期和时间
%S 可登录的用户名
workgroup 指定工作组名称
server string 主机注释信息
netbios name 指定NetBIOS名,注意:netbios name需要启动nmb服务
范例:
[global]
workgroup = workgroup
netbios name = smbserver #此设置需要启动nmb服务才可能生效
interfaces 指定服务侦听接口和IP
hosts allow 可用逗号,空格,或tab分隔,默认允许所有主机访问,也可在每个共享独立配置,如在[global]设置,将应用并覆盖所有共享设置,可以是以下格式:
IPv4 network/prefix: 172.16.0.0/24 IPv4 前缀: 172.16.0.
IPv4 network/netmask: 172.16.0.0/255.255.255.0
主机名: desktop.example.com
以example.com后缀的主机名: .example.com
范例:
hosts allow = 172.16. .example.com
hosts deny 拒绝指定主机访问,格式和hosts allow 相同
config file=/etc/samba/conf.d/%U 用户独立的配置文件
Log file=/var/log/samba/log.%I 不同客户机采用不同日志
log level = 2 日志级别,默认为0,不记录日志
范例:
[global]
Log file=/var/log/samba/log.%I
log level = 2
max log size=50 日志文件达到50K,将轮循rotate,单位KB
Security三种认证方式:
user:samba用户(采有linux用户,samba的独立口令)
share:匿名(CentOS7不再支持),已不建议使用
server:已不建议使用
passdb backend = tdbsam 密码数据库格式
每个共享目录应该有独立的[ ]部分
[共享名称] 远程网络看到的共享名称
comment 注释信息
path 所共享的目录路径
public 能否被guest访问的共享,默认no,和guest=ok 类似
browsable 是否允许所有用户浏览此共享,默认为yes,no为隐藏
writable=yes 可以被所有用户读写,默认为no
read only=no 和writable=yes等价,如与以上设置冲突,放在后面的设置生效,默认只读
write list 用户,@组名,+组名 之间用逗号分隔,如:writable=no,列表中用户或组可读写,不在列表中用户只读
valid users 特定用户才能访问该共享,如为空,将允许所有用户,用户名之间用空格分隔
范例:基于特定用户和组的共享
vim /etc/samba/smb.conf
[share]
path = /data/dir
valid users=smb1,@admins
writeable = no
browseable = no #隐藏共享,不输入文件名就看不到
范例:
vim /etc/samba/smb.conf
[share]
path = /data/dir
valid users=smb1,@admins
writeable = no #写权限如果打开,对应的/data/dir也要给权限
browseable = no
[root@centos8 ~]#groupadd admins
[root@centos8 ~]#groupmems -a smb2 -g admins
[root@centos8 ~]#groupmems -l -g admins
smb2
[root@centos8 ~]#mkdir /data/dir
[root@centos8 ~]#touch /data/dir/dir1.txt
#由于隐藏了共享,看不到文件夹
[root@Centos7 ~]#smbclient -L 10.0.0.8 -U smb1%magedu
Sharename Type Comment
--------- ---- -------
print$ Disk Printer Drivers
IPC$ IPC IPC Service (Samba 4.11.2)
smb1 Disk Home Directories
#输入完整路径就可以看到
[root@Centos7 ~]#smbclient //10.0.0.8/share -U smb1%magedu
Try "help" to get a list of possible commands.
smb: \> ls
. D 0 Sun Jul 5 10:30:29 2020
.. D 0 Sun Jul 5 10:30:15 2020
dir1.txt N 0 Sun Jul 5 10:30:29 2020
52403200 blocks of size 1024. 52004624 blocks available
smb: \> !ls
anaconda-ks.cfg a.txt dir1.txt ifcfg-eth0 init_set.sh reset.sh
smb: \> put anaconda-ks.cfg #由于没有写权限,只能下载不能上传
NT_STATUS_ACCESS_DENIED opening remote file \anaconda-ks.cfg
#授予写权限,同时给文件夹权限
vim /etc/samba/smb.conf
writeable = yes
[root@centos8 ~]#ll -d /data/dir/dir1.txt
-rw-r--r-- 1 root root 0 Jul 5 10:30 /data/dir/dir1.txt
[root@centos8 ~]#ll -d /data/dir/
drwxr-xr-x 2 root root 22 Jul 5 10:30 /data/dir/
[root@centos8 ~]#setfacl -m u:smb1:rwx,g:admins:rwx /data/dir/
[root@centos8 ~]#getfacl /data/dir/
getfacl: Removing leading '/' from absolute path names
# file: data/dir/
# owner: root
# group: root
user::rwx
user:smb1:rwx
group::r-x
group:admins:rwx
mask::rwx
other::r-x
#可以上传文件
[root@Centos7 ~]#smbclient //10.0.0.8/share -U smb1%magedu
Try "help" to get a list of possible commands.
smb: \> put anaconda-ks.cfg
putting file anaconda-ks.cfg as \anaconda-ks.cfg (313.7 kb/s) (average 313.7 kb/s)
#在samba服务器上安装samba包
yum -y install samba
#创建samba用户和组
groupadd -r admins
useradd -s /sbin/nologin -G admins wang
smbpasswd -a wang
useradd -s /sbin/nologin mage
smbpasswd -a mage
#创建samba共享目录,并设置SElinux
mkdir /testdir/smbshare
chgrp admins /testdir/smbshare
chmod 2775 /testdir/smbshare
#samba服务器配置
vim /etc/samba/smb.conf
...省略...
[share]
path = /testdir/smbshare
write list = @admins
systemctl enable --now smb nmb
#samba客户端访问
yum -y install cifs-utils
#用wang用户挂载smb共享并访问
mkdir /mnt/wang
mount -o username=wang //smbserver/share /mnt/wang
echo "Hello wang" >/mnt/wang/wangfile.txt
#用mage用户挂载smb共享并访问
mkdir /mnt/mage
mount -o username=mage //smbserver/share /mnt/mage
touch /mnt/mage/magefile.txt
#创建三个samba用户,并指定密码为magedu
useradd -s /sbin/nologin smb1
useradd -s /sbin/nologin smb2
useradd -s /sbin/nologin smb3
smbpasswd -a smb1
smbpasswd -a smb2
smbpasswd -a smb3
#修改samba配置文件
Vim /etc/samba/smb.conf
#在workgroup下加一行
config file= /etc/samba/conf.d/%U 说明:%U表示用户名
[share]
Path=/data/dir
Read only= NO
Guest ok = yes #任何人都可以访问
write list=@wheel #wheel组的人可写
#针对smb1和smb2用户创建单独的配置文件
Vim /etc/samba/conf.d/smb1
[share]
Path=/data/dir1
Read only= NO 等价于writable = yes
Create mask=0644
#说明:默认为744
Vim /etc/samba/conf.d/smb2
[share]
path=/data/dir2
systemctl restart smb nmb
#用户smb1,smb2,smb3访问share共享目录,看到目录是不同目录
smbclient //sambaserver/share -U smb1%magedu
smbclient //sambaserver/share -U smb2%magedu
smbclient //sambaserver/share -U smb3%magedu