一.SMB文件的共享
用Internet文件系统(CIFS)也称为服务器,适用于Microsoftwindows服务器和客户端的标准文件和打印机共享系统模块(SMB),属于一种协议;CIFS是公共的或开放的SMB协议版本
Samba服务可适用于将Linux文件系统作为CIFS/SMB网络文件共享进行共享,并将linux打印机作为CIFS/SMB打印机共享进行共享。
二.samba的共享
1.服务端的部署
[root@localhost ~]# yum install samba samba-client.x86_64 samba-common -y ##安装客户端 服务端 及常用命令
Loaded plugins: langpacks, product-id, search-disabled-repos, subscription-
: manager
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
Resolving Dependencies
--> Running transaction check
---> Package samba.x86_64 0:4.4.4-9.el7 will be installed
---> Package samba-client.x86_64 0:4.4.4-9.el7 will be installed
---> Package samba-common.noarch 0:4.4.4-9.el7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
[root@localhost ~]# systemctl start smb ##开启smb服务
[root@localhost ~]# systemctl enable smb ##smb服务开机自启
Created symlink from /etc/systemd/system/multi-user.target.wants/smb.service to /usr/lib/systemd/system/smb.service.
[root@localhost ~]# systemctl stop firewalld.service ##关闭火墙
[root@localhost ~]# getsebool -a | grep samba ##查看selinux 对smb的限制
samba_create_home_dirs --> off
samba_domain_controller --> off
samba_enable_home_dirs --> off
samba_export_all_ro --> off
samba_export_all_rw --> off
samba_load_libgfapi --> off
samba_portmapper --> off
samba_run_unconfined --> off
samba_share_fusefs --> off
samba_share_nfs --> off
sanlock_use_samba --> off
tmpreaper_use_samba --> off
use_samba_home_dirs --> off
virt_use_samba --> off
[root@localhost ~]# setsebool -P samba_enable_home_dirs on ##开启允许用户访问家目录的开关
[root@localhost ~]# getsebool -a | grep samba
samba_create_home_dirs --> off
samba_domain_controller --> off
samba_enable_home_dirs --> on ##开启成功
samba_export_all_ro --> off
2.建立samba用户并允许其访问smb服务
[root@localhost ~]# smbpasswd -a student ##建立用户student并指定密码,用户必须为系统中存在的用户
New SMB password:
Retype new SMB password:
Added user student.
[root@localhost ~]# pdbedit -L ##显示已经存在的用户
student:1000:student
[root@localhost ~]# pdbedit -x student ##删除用户student
[root@localhost ~]# pdbedit -L
[root@localhost ~]# smbpasswd -a student
New SMB password:
Retype new SMB password:
Added user student.
3.客户端的部署
[root@localhost ~]# yum install samba-client.x86_64 ##下载客户端
Loaded plugins: langpacks
westos | 4.1 kB 00:00
Resolving Dependencies
--> Running transaction check
---> Package samba-client.x86_64 0:4.1.1-31.el7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
================================================================================
Package Arch Version Repository Size
================================================================================
Installing:
samba-client x86_64 4.1.1-31.el7 westos 513 k
Transaction Summary
================================================================================
Install 1 Package
Total download size: 513 k
4.客户端测试
(1)以student用户身份登陆smb,访问student 家目录
[root@localhost ~]# smbclient -L //172.25.254.133/student -U student
Enter student's password:
Domain=[MYGROUP] OS=[Unix] Server=[Samba 4.1.1]
Sharename Type Comment
--------- ---- -------
IPC$ IPC IPC Service (Samba Server Version 4.1.1)
student Disk Home Directories
Domain=[MYGROUP] OS=[Unix] Server=[Samba 4.1.1]
Server Comment
--------- -------
Workgroup Master
--------- -------
(2)以student用户身份进入其家目录
[root@client ~]# smbclient //172.25.254.133/student -U student
Enter student's password:
Domain=[MYGROUP] OS=[Unix] Server=[Samba 4.1.1]
smb: \> ls
. D 0 Wed May 8 21:56:11 2019
.. D 0 Wed May 8 21:56:11 2019
40913 blocks of size 262144. 28159 blocks available
smb: \>
(3)客户端通过smb服务在家目录上传文件
[root@client ~]# touch file ##任意建立一个文件
[root@client ~]# smbclient //172.25.254.133/student -U student
Enter student's password:
Domain=[MYGROUP] OS=[Unix] Server=[Samba 4.1.1]
smb: \> ls
. D 0 Thu May 11 20:23:54 2017
.. D 0 Thu Dec 7 22:26:40 2017
.bash_profile H 193 Wed Jan 29 07:45:18 2014
.config DH 0 Thu May 11 20:23:54 2017
.bashrc H 231 Wed Jan 29 07:45:18 2014
.bash_logout H 18 Wed Jan 29 07:45:18 2014
.cache DH 0 Thu May 11 20:23:54 2017
.ssh DH 0 Thu Jul 10 18:19:10 2014
60458 blocks of size 8192. 56479 blocks available
smb: \> put file
putting file file as \file (0.0 kb/s) (average 0.0 kb/s)
smb: \> ls
. D 0 Wed May 8 22:45:44 2019
.. D 0 Thu Dec 7 22:26:40 2017
.bash_profile H 193 Wed Jan 29 07:45:18 2014
file A 0 Wed May 8 22:45:44 2019 ##file上传成功
.config DH 0 Thu May 11 20:23:54 2017
.bashrc H 231 Wed Jan 29 07:45:18 2014
.bash_logout H 18 Wed Jan 29 07:45:18 2014
.cache DH 0 Thu May 11 20:23:54 2017
.ssh DH 0 Thu Jul 10 18:19:10 2014
60458 blocks of size 8192. 56479 blocks availabl
(4)利用挂载方式对服务端家目录进行更改
[root@client ~]# mount -o username=student,password=student //172.25.254.133/student /mnt ##将服务端student的家目录挂载在客户端的/mnt
[root@client ~]# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/vda1 10473900 3178584 7295316 31% /
devtmpfs 469332 0 469332 0% /dev
tmpfs 484920 140 484780 1% /dev/shm
tmpfs 484920 12768 472152 3% /run
tmpfs 484920 0 484920 0% /sys/fs/cgroup
//172.25.254.133/student 483670 2342 451837 1% /mnt ##挂载成功
[root@client ~]# cd /mnt
[root@client mnt]# ls
file
[root@client mnt]# touch file{1..3} ##在/mnt下建立文件相当于在家目录创建文件
[root@client mnt]# ls
file file1 file2 file3
[root@client mnt]# cd
[root@client ~]# umount /mnt ##卸载
[root@client ~]# smbclient //172.25.254.133/student -U studentEnter student's password:
Domain=[MYGROUP] OS=[Unix] Server=[Samba 4.1.1]
smb: \> ls ##file1 file2 file3 创建成功
. D 0 Wed May 8 22:53:39 2019
.. D 0 Thu Dec 7 22:26:40 2017
file3 N 0 Wed May 8 22:53:39 2019
.bash_profile H 193 Wed Jan 29 07:45:18 2014
file A 0 Wed May 8 22:45:44 2019
file1 N 0 Wed May 8 22:53:39 2019
file2 N 0 Wed May 8 22:53:39 2019
.config DH 0 Thu May 11 20:23:54 2017
.bashrc H 231 Wed Jan 29 07:45:18 2014
.bash_logout H 18 Wed Jan 29 07:45:18 2014
.cache DH 0 Thu May 11 20:23:54 2017
.ssh DH 0 Thu Jul 10 18:19:10 2014
60458 blocks of size 8192. 56479 blocks available
5.samba共享域的修改
修改前
[root@client ~]# smbclient -L //172.25.254.133/student -U student
Enter student's password:
Domain=[MYGROUP] OS=[Unix] Server=[Samba 4.1.1] ##共享域为MYGROUP
Sharename Type Comment
--------- ---- -------
IPC$ IPC IPC Service (Samba Server Version 4.1.1)
student Disk Home Directories
Domain=[MYGROUP] OS=[Unix] Server=[Samba 4.1.1]
Server Comment
--------- -------
Workgroup Master
--------- -------
共享域修改
[root@server ~]# vim /etc/samba/smb.conf
[root@server ~]# systemctl restart smb
修改后
[root@client ~]# smbclient -L //172.25.254.133/student -U student
Enter student's password:
Domain=[WESTOS] OS=[Unix] Server=[Samba 4.1.1]
Sharename Type Comment
--------- ---- -------
IPC$ IPC IPC Service (Samba Server Version 4.1.1)
student Disk Home Directories
Domain=[WESTOS] OS=[Unix] Server=[Samba 4.1.1] ##共享域为WESTOS
Server Comment
--------- -------
Workgroup Master
--------- -------
6.samba共享目录的建立与设定
服务端设定
[root@server ~]# mkdir /smbshare ##建立新目录smbshare
[root@server ~]# touch /smbshare/test{1..3} ##目录中建立文件
[root@server ~]# cd /smbshare/
[root@server smbshare]# ls
test1 test2 test3
[root@server smbshare]# ll /smbshare/
total 0
-rw-r--r--. 1 root root 0 May 8 23:13 test1
-rw-r--r--. 1 root root 0 May 8 23:13 test2
-rw-r--r--. 1 root root 0 May 8 23:13 test3
[root@server smbshare]# vim /etc/samba/smb.conf
[root@server smbshare]# systemctl restart smb
[root@server smbshare]# ls -Zd /smbshare/ ##查看smbshare的安全上下文
drwxr-xr-x. root root unconfined_u:object_r:default_t:s0 /smbshare/
[root@server smbshare]# semanage fcontext -a -t samba_share_t '/smbshare(/.*)?' ##更改安全上下文与samba一致
[root@server smbshare]# restorecon -RvvF /smbshare/ ##刷新安全上下文 更改成功
restorecon reset /smbshare context unconfined_u:object_r:default_t:s0->system_u:object_r:samba_share_t:s0
restorecon reset /smbshare/test1 context unconfined_u:object_r:default_t:s0->system_u:object_r:samba_share_t:s0
restorecon reset /smbshare/test2 context unconfined_u:object_r:default_t:s0->system_u:object_r:samba_share_t:s0
restorecon reset /smbshare/test3 context unconfined_u:object_r:default_t:s0->system_u:object_r:samba_share_t:s0
客户端检测
[root@client ~]# smbclient //172.25.254.133/luck -U student
Enter student's password:
Domain=[WESTOS] OS=[Unix] Server=[Samba 4.1.1] ##目录创建成功
smb: \> ls
. D 0 Wed May 8 23:13:46 2019
.. D 0 Wed May 8 23:13:43 2019
test1 N 0 Wed May 8 23:13:46 2019
test2 N 0 Wed May 8 23:13:46 2019
test3 N 0 Wed May 8 23:13:46 2019
40913 blocks of size 262144. 28306 blocks available
smb: \> pwd
Current directory is \\172.25.254.133\luck\
smb: \> quit
[root@client ~]# mount -o username=student,password=student //172.25.254.133/luck /mnt ##挂载方式查看
[root@client ~]# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/vda1 10473900 3178856 7295044 31% /
devtmpfs 469332 0 469332 0% /dev
tmpfs 484920 140 484780 1% /dev/shm
tmpfs 484920 12796 472124 3% /run
tmpfs 484920 0 484920 0% /sys/fs/cgroup
//172.25.254.133/luck 10473900 3227528 7246372 31% /mnt
[root@client ~]# cd /mnt
[root@client mnt]# ls ##目录建立成功
test1 test2 test3
7.samba的黑白名单设定
(1)黑名单设定
[root@server smbshare]# vim /etc/samba/smb.conf
[root@server smbshare]# systemctl restart smb
客户端测试
[root@client mnt]# smbclient //172.25.254.133/luck -U studentEnter student's password:
protocol negotiation failed: NT_STATUS_INVALID_NETWORK_RESPONSE ##连接失败
(2)白名单测试
[root@server smbshare]# vim /etc/samba/smb.conf
[root@server smbshare]# systemctl restart smb
客户端测试
[root@client mnt]# smbclient //172.25.254.133/luck -U student
Enter student's password:
Domain=[WESTOS] OS=[Unix] Server=[Samba 4.1.1]
smb: \> ls
. D 0 Wed May 8 23:13:46 2019
.. D 0 Wed May 8 23:13:43 2019
test1 N 0 Wed May 8 23:13:46 2019
test2 N 0 Wed May 8 23:13:46 2019
test3 N 0 Wed May 8 23:13:46 2019
40913 blocks of size 262144. 28290 blocks available
8.samba匿名用户的访问及访问限制
[root@server smbshare]# vim /etc/samba/smb.conf
[root@server smbshare]# systemctl restart smb
客户端测试
[root@client ~]# smbclient //172.25.254.133/luck -U guest ##以匿名用户登陆smb服务
Enter guest's password:
Domain=[WESTOS] OS=[Unix] Server=[Samba 4.1.1]
smb: \> ls
. D 0 Wed May 8 23:13:46 2019
.. D 0 Wed May 8 23:13:43 2019
test1 N 0 Wed May 8 23:13:46 2019
test2 N 0 Wed May 8 23:13:46 2019
test3 N 0 Wed May 8 23:13:46 2019
40913 blocks of size 262144. 28291 blocks available
9.samba共享目录权限的更改
(1)设置所有用户均可写
[root@server smbshare]# vim /etc/samba/smb.conf
[root@server smbshare]# chmod 777 /smbshare/
[root@server smbshare]# ls -ld /smbshare/
drwxrwxrwx. 2 root root 42 May 8 23:13 /smbshare/
[root@server smbshare]# systemctl restart smb
客户端的测试
以student用户挂载
[root@client ~]# mount -o username=student,password=student //172.25.254.133/luck /mnt ##student用户挂载
[root@client ~]# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/vda1 10473900 3182592 7291308 31% /
devtmpfs 469332 0 469332 0% /dev
tmpfs 484920 140 484780 1% /dev/shm
tmpfs 484920 12796 472124 3% /run
tmpfs 484920 0 484920 0% /sys/fs/cgroup
//172.25.254.133/luck 10473900 3231232 7242668 31% /mnt ##挂载成功
[root@client ~]# cd /mnt
[root@client mnt]# ls
test1 test2 test3
[root@client mnt]# rm -fr test3
[root@client mnt]# ls ##可以删除文件,权限给与成功
test1 test2
以其他用户挂载
[root@client ~]# mount -o username=luck,password=luck //172.25.254.133/luck /mnt
[root@client ~]# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/vda1 10473900 3182800 7291100 31% /
devtmpfs 469332 0 469332 0% /dev
tmpfs 484920 140 484780 1% /dev/shm
tmpfs 484920 12796 472124 3% /run
tmpfs 484920 0 484920 0% /sys/fs/cgroup
//172.25.254.133/luck 10473900 3231644 7242256 31% /mnt
[root@client ~]# cd /mnt
[root@client mnt]# ls
test1 test2
[root@client mnt]# touch file1
[root@client mnt]# ls
file1 test1 test2
(2)设置指定用户对目录可写
[root@server smbshare]# vim /etc/samba/smb.conf
[root@server smbshare]# systemctl restart smb
客户端测试
student用户挂载
[root@client ~]# mount -o username=student,password=student //172.25.254.133/luck /mnt
[root@client ~]# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/vda1 10473900 3182896 7291004 31% /
devtmpfs 469332 0 469332 0% /dev
tmpfs 484920 140 484780 1% /dev/shm
tmpfs 484920 12768 472152 3% /run
tmpfs 484920 0 484920 0% /sys/fs/cgroup
//172.25.254.133/luck 10473900 3231480 7242420 31% /mnt
[root@client ~]# cd /mnt
[root@client mnt]# rm -fr test1
[root@client mnt]# ls
file1 test2
[root@client mnt]# cd
[root@client ~]# umount /mnt
其他用户挂载
[root@client ~]# mount -o username=luck,password=luck //172.25.254.133/luck /mnt
[root@client ~]# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/vda1 10473900 3182896 7291004 31% /
devtmpfs 469332 0 469332 0% /dev
tmpfs 484920 140 484780 1% /dev/shm
tmpfs 484920 12768 472152 3% /run
tmpfs 484920 0 484920 0% /sys/fs/cgroup
//172.25.254.133/luck 10473900 3231384 7242516 31% /mnt
[root@client ~]# cd /mnt
[root@client mnt]# ls
file1 test2
[root@client mnt]# touch file2
touch: cannot touch ‘file2’: Permission denied ##权限被拒绝
(3)设置属于某组的用户对目录可写
[root@server smbshare]# vim /etc/samba/smb.conf
[root@server smbshare]# systemctl restart smb
[root@server smbshare]# usermod -G student luck ##更改luck的组为student
[root@server smbshare]# id luck
uid=1001(luck) gid=1001(luck) groups=1001(luck),1000(student)
客户端测试
[root@client ~]# mount -o username=luck,password=luck //172.25.254.133/luck /mnt
[root@client ~]# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/vda1 10473900 3182900 7291000 31% /
devtmpfs 469332 0 469332 0% /dev
tmpfs 484920 140 484780 1% /dev/shm
tmpfs 484920 12768 472152 3% /run
tmpfs 484920 0 484920 0% /sys/fs/cgroup
//172.25.254.133/luck 10473900 3231332 7242568 31% /mnt
[root@client ~]# cd /mnt
[root@client mnt]# ls
file1 test2
[root@client mnt]# touch test1
[root@client mnt]# ls ##可写权力更改成功
file1 test1 test2
(4)指定共享目录的有效用户
[root@server smbshare]# vim /etc/samba/smb.conf
[root@server smbshare]# systemctl restart smb
客户端测试
[root@client ~]# mount -o username=student,password=student //172.25.254.133/luck /mnt ##student用户可以实现挂载
[root@client ~]# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/vda1 10473900 3182904 7290996 31% /
devtmpfs 469332 0 469332 0% /dev
tmpfs 484920 140 484780 1% /dev/shm
tmpfs 484920 12768 472152 3% /run
tmpfs 484920 0 484920 0% /sys/fs/cgroup
//172.25.254.133/luck 10473900 3231456 7242444 31% /mnt
[root@client ~]# umount /mnt
[root@client ~]# mount -o username=luck,password=luck //172.25.254.133/luck /mnt ##luck用户挂载被拒绝
mount: //172.25.254.133/luck is write-protected, mounting read-only
mount: cannot mount //172.25.254.133/luck read-only
(5)指定共享目录的有效用户组
[root@server smbshare]# vim /etc/samba/smb.conf
[root@server smbshare]# systemctl restart smb
客户端测试
[root@client ~]# mount -o username=luck,password=luck //172.25.254.133/luck /mnt
[root@client ~]# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/vda1 10473900 3182904 7290996 31% /
devtmpfs 469332 0 469332 0% /dev
tmpfs 484920 140 484780 1% /dev/shm
tmpfs 484920 12768 472152 3% /run
tmpfs 484920 0 484920 0% /sys/fs/cgroup
//172.25.254.133/luck 10473900 3231564 7242336 31% /mnt
(6)设置隐藏共享目录
[root@server smbshare]# vim /etc/samba/smb.conf
[root@server smbshare]# systemctl restart smb
客户端测试
[root@client ~]# smbclient -L //172.25.254.133
Enter root's password:
Domain=[WESTOS] OS=[Unix] Server=[Samba 4.1.1] ##sharename为空白被隐藏
Sharename Type Comment
--------- ---- -------
IPC$ IPC IPC Service (Samba Server Version 4.1.1)
Domain=[WESTOS] OS=[Unix] Server=[Samba 4.1.1]
Server Comment
--------- -------
Workgroup Master
--------- -------
(7)共享目录管理员的添加
[root@server smbshare]# vim /etc/samba/smb.conf
[root@server smbshare]# systemctl restart smb
客户端测试
[root@client ~]# mount -o username=luck,password=luck //172.25.254.133/luck /mnt ##以luck身份进行挂载
[root@client ~]# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/vda1 10473900 3182908 7290992 31% /
devtmpfs 469332 0 469332 0% /dev
tmpfs 484920 140 484780 1% /dev/shm
tmpfs 484920 12768 472152 3% /run
tmpfs 484920 0 484920 0% /sys/fs/cgroup
//172.25.254.133/luck 10473900 3231364 7242536 31% /mnt
[root@client ~]# cd /mnt
[root@client mnt]# ls
file1 test1 test2
[root@client mnt]# touch file2 ##建立文件file2
[root@client mnt]# ls
file1 file2 test1 test2
[root@client mnt]# ll
total 0
-rw-r--r--. 1 1001 1001 0 May 9 01:09 file1
-rw-r--r--. 1 1001 1001 0 May 9 01:48 file2
-rw-r--r--. 1 1001 1001 0 May 9 01:28 test1
-rw-r--r--. 1 root root 0 May 8 23:13 test2
[root@client mnt]# ll file2 ##文件file2的用户及组id为1001,与student的id相同
-rw-r--r--. 1 1001 1001 0 May 9 01:48 file2
10.samba多用户的挂载
多用户挂载主要是用来解决:当客户端使用超级用户对共享目录进行挂载之后,其他用户同样可以直接使用该挂载目录,存在安全隐患。使用多用户挂载之后,其他用户想访问挂载之后的共享目录时候,必须经过验证
客户端的设置
[root@client ~]# yum install cifs-utils -y ##安装此插件
Loaded plugins: langpacks
Resolving Dependencies
--> Running transaction check
---> Package cifs-utils.x86_64 0:6.2-6.el7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
================================================================================
Package Arch Version Repository Size
================================================================================
Installing:
cifs-utils x86_64 6.2-6.el7 westos 83 k
Transaction Summary
================================================================================
Install 1 Package
Total download size: 83 k
Installed size: 174 k
Downloading packages:
cifs-utils-6.2-6.el7.x86_64.rpm | 83 kB 00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : cifs-utils-6.2-6.el7.x86_64 1/1
Verifying : cifs-utils-6.2-6.el7.x86_64 1/1
Installed:
cifs-utils.x86_64 0:6.2-6.el7
Complete!
[root@client ~]# vim /root/smbpass ##编辑共享目录认证文件
[root@client ~]# cat /root/smbpass
username=student ##编辑文件内容为smb的用户及密码
password=student
[root@client ~]# chmod 600 /root/smbpass ##给与认证更文件600权限
[root@client ~]# ls -ld /root/smbpass
-rw-------. 1 root root 34 May 9 01:53 /root/smbpass
[root@client ~]# mount //172.25.254.133/luck /mnt -o credentials=/root/smbpass,sec=ntlmssp,multiuser ##root用户以认证文件的方式对目录进行挂载
[root@client ~]# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/vda1 10473900 3183208 7290692 31% /
devtmpfs 469332 0 469332 0% /dev
tmpfs 484920 140 484780 1% /dev/shm
tmpfs 484920 12768 472152 3% /run
tmpfs 484920 0 484920 0% /sys/fs/cgroup
//172.25.254.133/luck 10473900 3231376 7242524 31% /mnt
[root@client ~]# cd /mnt
[root@client mnt]# ls
file1 file2 test1 test2
服务端的更改
[root@server smbshare]# vim /etc/samba/smb.conf
[root@server smbshare]# systemctl restart smb
客户端普通用户的验证
[root@client mnt]# su - student
[student@client ~]$ cd /mnt 普通用户查看共享目录
[student@client mnt]$ ls
ls: reading directory .: Permission denied ##权限被拒绝
[student@client mnt]$ cifscreds add -u student 172.25.254.133 ##普通用户认证输入密码
Password:
[student@client mnt]$ ls ##查看共享目录成功
file1 file2 test1 test2
[student@client mnt]$ touch test3 ##更改共享目录
[student@client mnt]$ ls
file1 file2 test1 test2 test3 ##更改成功