SMB(Samba)服务–文件共享
服务端
安装文件
yum install samba-client.x86_64 samba.x86_64 samba-common-4.4.4-9.el7.noarch -y
开启服务,关闭火墙
[root@nodel ~]# systemctl stop firewalld.service
设置selinux为警告状态
[root@nodel ~]# getenforce
Enforcing
[root@nodel ~]# setenforce 0
[root@nodel ~]# getenforce
Permissive
添加用户
[root@nodel ~]# useradd westos
[root@nodel ~]# smbpasswd -a westos
New SMB password:
Retype new SMB password:
Added user westos.
[root@nodel ~]# smbpasswd -a student
New SMB password:
Retype new SMB password:
Added user student.
[root@nodel ~]# pdbedit -L 查看smb用户
westos:1001:
student:1000:student
删除用户
x为删除
[root@nodel ~]# pdbedit -x student
[root@nodel ~]# pdbedit -L
westos:1001:
[root@nodel ~]#
客户端
yum install samba-client.x86_64 -y
匿名用户浏览
[root@desktop ~]# smbclient -L //172.25.254.134
本地用户浏览
[root@desktop ~]# smbclient -L //172.25.254.134 -U westos
本地smb用户家目录访问
前提:家目录存在
1,直接登陆访问用户家目录
[root@desktop ~]# smbclient //172.25.254.134/westos -U westos
Enter westos's password:
2,以挂载的方式访问本地samba用户家目录
[root@desktop ~]# mount -o username=westos,password=123 //172.25.254.134/westos /mnt/
[root@desktop ~]# df
samba访问控制
服务端
更改域名
[root@nodel westos]# vim /etc/samba/smb.conf
workgroup = WESTOS
服务端
查看
[root@desktop ~]# smbclient -L //172.25.254.133 -U westos
允许selinux访问samba服务
更改selinux为强制状态
[root@nodel ~]# getenforce
Permissive
[root@nodel ~]# setenforce 1
[root@nodel ~]# getenforce
Enforcing
在客户端登陆被拒
[root@desktop ~]# smbclient //172.25.254.133/westos -U westos
Enter westos's password:
Domain=[WESTOS] OS=[Windows 6.1] Server=[Samba 4.4.4]
smb: \> ls
NT_STATUS_ACCESS_DENIED listing \*
smb: \> exit
[root@desktop ~]#
设定布尔值
[root@nodel westos]# getsebool -a |grep samba
##查看samba的布尔值
[root@nodel westos]# setsebool -P samba_enable_home_dirs=1
##设定打开访问家目录的布尔值
[root@nodel westos]# getsebool -a |grep samba
##再次查看
可访问smb用户家目录
[root@nodel ~]# smbclient //172.25.254.133/westos -U westos
用户访问限制
samba用户访问黑名单
[root@nodel westos]# vim /etc/samba/smb.conf
hosts allow = 172.25.254.201
测试
[root@desktop ~]# smbclient //172.25.254.133/westos -U westos
samba用户访问白名单
[root@nodel westos]# vim /etc/samba/smb.conf
hosts deny = 172.25.254.201
测试
ip=172.25.254.133
ip=172.25.254.201
还原环境
[root@nodel westos]# vim /etc/samba/smb.conf
Samba共享文件搭建
[123] 共享目录名称
comment = westos dir share 共享目录说明
path = /westos 共享目录路径
valid users = westos 仅允许westos用户进行访问
writeable = yes 是否可写
write list = 可写列表
browseable = yes 是否隐藏目录(默认为yes,即不隐藏目录)
admin user = student 给student用户root权限
map to guest = bad user 匿名用户使用guest名登陆
guest ok = yes 允许匿名用户登陆
建立自己的共享文件
[root@nodel ~]# mkdir /westos
[root@nodel ~]# cd /westos/
[root@nodel westos]# ls
[root@nodel westos]# touch file{1..3}
[root@nodel westos]# ls
file1 file2 file3
[root@nodel westos]# vim /etc/samba/smb.conf
[123]
comment = westos dir share
path = /westos
查看目录安全上下文
[root@nodel westos]# ls -Zd /westos/
drwxr-xr-x. root root unconfined_u:object_r:default_t:s0 /westos/
[root@nodel westos]# semanage fcontext -a -t samba_share_t '/westos(/.*)?'
##修改安全上下文
[root@nodel westos]# ls -Zd /westos/
drwxr-xr-x. root root unconfined_u:object_r:default_t:s0 /westos/
[root@nodel westos]# restorecon -FvvR /westos/
##刷新
[root@nodel westos]# ls -Zd /westos/
drwxr-xr-x. root root system_u:object_r:samba_share_t:s0 /westos/
##查看
测试
匿名用户登陆
可看到共享目录
[root@desktop ~]# smbclient -L //172.25.254.133/123
smb用户登陆
可查看共享文件中的内容
[root@desktop ~]# smbclient //172.25.254.133/123 -U westos
共享系统目录
编写共享文件
[root@nodel westos]# vim /etc/samba/smb.conf
[rhel]
comment = mnt dir share
path = /mnt
设定布尔值
打开读写权限
[root@nodel westos]# getsebool -a |grep samba
[root@nodel westos]# setsebool -P samba_export_all_ro on
[root@nodel westos]# setsebool -P samba_export_all_rw on
在/mnt目录下加入文件
[root@nodel westos]# touch /mnt/westos
测试
[root@desktop ~]# smbclient -L //172.25.254.133
[root@desktop ~]# smbclient //172.25.254.133/rhel -U westos
samba共享目录权限的管理
添加用户:student
[root@nodel westos]# smbpasswd -a westos
[root@nodel westos]# pdbedit -L
westos:1001:
student:1000:Student User
允许所有用户可写
1.编写文件
[root@nodel westos]# vim /etc/samba/smb.conf
writeable = yes
测试
[root@desktop ~]# smbclient //172.25.254.133/123 -U westos
Enter westos's password:
Domain=[WESTOS] OS=[Windows 6.1] Server=[Samba 4.4.4]
smb: \> ls
. D 0 Wed May 29 18:38:14 2019
.. D 0 Wed May 29 18:34:28 2019
file1 N 0 Wed May 29 18:34:44 2019
file2 N 0 Wed May 29 18:34:44 2019
file3 N 0 Wed May 29 18:34:44 2019
10473900 blocks of size 1024. 7003816 blocks available
smb: \> rm file1
NT_STATUS_ACCESS_DENIED deleting remote file \file1
NT_STATUS_ACCESS_DENIED listing \file1
smb: \> exit
[root@desktop ~]#
无法删除文件,查看文件权限
[root@nodel westos]# ls -ld /westos/
drwxr-xr-x. 2 root root 42 May 29 18:38 /westos/
[root@nodel westos]# ll /westos
2.更改目录权限,打开读写权限
[root@nodel westos]# chmod 777 /westos/
[root@nodel westos]# ls -ld /westos/
drwxrwxrwx. 2 root root 42 May 29 18:38 /westos/
测试
允许指定用户可写
[root@nodel westos]# vim /etc/samba/smb.conf
# writeable = yes
write list = westos
测试
允许指定用户组可写
[root@nodel westos]# vim /etc/samba/smb.conf
write list = +westos
(@也有相同的效果)
[root@nodel westos]# usermod -G westos student
[root@nodel westos]# id student
uid=1000(student) gid=1000(student) groups=1000(student),1001(westos)
测试
允许指定用户能访问
[root@nodel westos]# vim /etc/samba/smb.conf
# write list = +westos
valid users = westos
测试
隐藏目录,但不代表不可用
[root@nodel westos]# vim /etc/samba/smb.conf
browseable = no
##yes指不隐藏目录
测试
指定用户身份为超级用户root
[root@nodel westos]# vim /etc/samba/smb.conf
writeable = yes
#valid users = westos
admin user = student
##指定student用户的权限为root
测试
(上传文件路径应在根目录下)
允许匿名用户访问
映射用户:将匿名用户映射为bad user用户
[root@nodel westos]# vim /etc/samba/smb.conf
map to guest = bad user
guest ok = yes
测试
还原
挂载共享文件
[root@desktop ~]# mount //172.25.254.133/123 /mnt/ -o username=westos,password=123
[root@desktop ~]# df
发现各个用户皆可对其进行读写
root用户
[root@desktop ~]# cd /mnt/
[root@desktop mnt]# ls
file1 file2 file3
[root@desktop mnt]# rm -fr file1
[root@desktop mnt]# ls
file2 file3
student用户
[root@desktop mnt]# su - student
Last login: Tue May 28 14:57:20 EDT 2019 on :0
[student@desktop ~]$ cd /mnt/
[student@desktop mnt]$ ls
file2 file3
[student@desktop mnt]$ rm -fr file2
[student@desktop mnt]$ ls
file3
用户全部可以创建文件,且id默认为登陆smb用户id(westos)
安全性低
[student@desktop mnt]$ touch file1
touch: cannot touch ‘file1’: Permission denied
##有报错但依然创建文件
[student@desktop mnt]$ ll
total 0
-rw-r--r--. 1 1001 1001 0 May 29 20:15 file1
-rw-r--r--. 1 root root 0 May 29 20:11 file3
[student@desktop mnt]$ su
Password:
[root@desktop mnt]# touch file2
[root@desktop mnt]# ll
total 0
-rw-r--r--. 1 1001 1001 0 May 29 20:15 file1
-rw-r--r--. 1 1001 1001 0 May 29 20:16 file2
-rw-r--r--. 1 root root 0 May 29 20:11 file3
查看服务端westos的id
[root@nodel westos]# id westos
uid=1001(westos) gid=1001(westos) groups=1001(westos)
cif协议 实现多用户挂载
用户认证需要匿名用户无法访问
卸载已挂载
[root@desktop /]# cd /
[root@desktop /]# umount /mnt/ ##设备正忙
umount: /mnt: target is busy.
(In some cases useful info about processes that use
the device is found by lsof(8) or fuser(1))
[root@desktop /]# lsof /mnt/ ##查看占用程序
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
bash 6388 root cwd DIR 0,36 0 34126339 /mnt
bash 6772 student cwd DIR 0,36 0 34126339 /mnt
[root@desktop /]# kill -9 6388 ##关闭占用
[root@desktop ~]# umount /mnt/
[root@desktop ~]# df
基于Samba服务的cif文件系统
安装cif
查看版本是否与客户端相匹配
[root@desktop ~]# yum install cifs-utils.x86_64
[root@desktop ~]# rpm -qa |grep samba
##4.4版本
添加samba用户认证(首次用户认证,用户必须是samba的用户)
[root@desktop ~]# vim /root/smbpass
username=westos ##smb用户名
password=123 ##对应的密码
更改认证文件权限
挂载
(非挂载用户无权限)
[root@desktop ~]# chmod 600 /root/smbpass
[root@desktop ~]# ll /root/smbpass
-rw-------. 1 root root 29 May 29 20:38 /root/smbpass
[root@desktop ~]# mount -o credentials=/root/smbpass,sec=ntlmssp,multiuser //172.25.254.133/123 /mnt/
[root@desktop ~]# df
credentials=/root/smbpass ##指定挂载用户信息文件
sec=ntlmssp ##表示使用多用户挂载
multiuser ##指定认证方式
测试
[root@desktop ~]# su - student
Last login: Wed May 29 20:13:29 EDT 2019 on pts/0
[student@desktop ~]$ cd /mnt
[student@desktop mnt]$ ls
ls: reading directory .: Permission denied
非挂载用户认证(通过samba用户认证实现访问挂载目录)
服务端添加samba用户
[root@nodel westos]# useradd user1
[root@nodel westos]# smbpasswd -a user1
New SMB password:
Retype new SMB password:
Added user user1.
[root@nodel westos]# pdbedit -L
westos:1001:
user1:1002:
student:1000:Student User
客户端添加samba用户认证
##本地用户student用户通过任意一samba(user1)用户登陆
[student@desktop mnt]$ cifscreds add -u user1 172.25.254.133
Password:
[student@desktop mnt]$ touch file
建立文件为samba用户id
[student@desktop mnt]$ ll
total 0
-rw-r--r--. 1 1002 1002 0 May 29 20:51 file
-rw-r--r--. 1 1001 1001 0 May 29 20:15 file1
-rw-r--r--. 1 1001 1001 0 May 29 20:16 file2
-rw-r--r--. 1 root root 0 May 29 20:11 file3
使用户必须经过安全认证才能访问共享文件,提高安全性