一、什么是cifs协议和samba软件?
1、cifs协议
CIFS 是一个新提出的协议,它使程序可以访问远程Internet计算机上的文件并要求此计算机提供服务。CIFS 使用客户/服务器模式。客户程序请求远在服务器上的服务器程序为它提供服务。服务器获得请求并返回响应。CIFS是公共的或开放的SMB协议版本,并由Microsoft使用。SMB协议在局域网上用于服务器文件访问和打印的协议。像SMB协议一样,CIFS在高层运行,而不像TCP/IP协议那样运行在底层。CIFS可以看做是应用程序协议如文件传输协议和超文本传输协议的一个实现。
2、samba软件
samba软件用来提供cifs协议,实现linux和windows系统的文件共享
二、samba服务器的安装及部署
yum install samba samba-client samba-common -y ##下载samba的客户端、常用命令、服务端
systemctl start smb ##启动samba服务
systemctl enable smb ##设置samba开机自启动
systemctl stop firewalld ##关闭防火墙
getsebool -a | grep samba ##查看samba有关的布尔值
setsebool -P samba_enable_home_dirs on ##设定samba用户可以访问自己的家目录
三、samba用户的管理
smbpasswd -a student ##student用户必须为本机用户
pdbedit -L ##查看
pdbedit -x student ##删除用户
测试:
smbclient -L //172.25.254.170 -U student ##用student用户登陆
如若出现报错“protocol negotiation failed: NT_STATUS_IO_TIMEOUT”,尝试清除服务端DNS解析服务器配置文件内容。
四、samba共享目录的建立与设定
1、建立共享目录并设定
1、更改配置文件
mkdir /redhatlinux
vim /etc/samba/smb.conf
[共享名称]
comment = 共享说明
path = 共享目录
2、更改建立目录的安全上下文
semanage fcontext -a -t samba_share_t '/redhatlinux(/.*)?'
restorecon -FvvR /redhatlinux
3、重启服务
systemctl restart smb
4、测试
smbclient //172.25.254.170/共享目录 -U student
smbclient //172.25.254.170/share -U student
2、设定系统目录为共享目录
1、更改配置文件
vim /etc/samba/smb.conf
[share1]
comment = share dir
path = /mnt
2、更改selinux的布尔值
setsebool -P samba_export_all_rw on ##可以读写
setsebool -P samba_export_all_ro on ##只读
3、测试
smbclient //172.25.254.170/share1 -U student
3、共享目录的IP和网段
挂载命令:
mount //172.25.254.170/share /mnt/smb -o username=student,password=redhat
1、基于IP的限制挂载
vim /etc/samba/smb.conf
[share]
comment = share dir
path = /redhatlinux
hosts allow = 172.25.254.170 ##只允许172.25.254.170主机挂载,hosts deny为禁止。
测试:
170挂载
120挂载
2、基于网段的限制挂载
vim /etc/samba/smb.conf
[share]
comment = share dir
path = /redhatlinux
hosts deny = 172.25.254. ##禁止172.25.254.网段挂载,hosts allow为允许
测试:
用刚才可以挂载的172.25.254.170主机挂载
3、设置开机自动挂载
yum install cifs-utils.x86_64 -y
vim /etc/fstab
//172.25.254.170/share /mnt/smb cifs defaults,username=student,password=redhat 0 0
五、samba匿名访问
1、更改配置文件
vim /etc/samba/smb.conf
125 map to guest = bad user
321 [share]
322 comment = share dir
323 path = /redhatlinux
324 guest ok = yes
2、重启服务
systemctlrestart smb
3、测试
mount //172.25.254.170/share /mnt/smb -o username=guest
六、samba共享目录的有关参数
writable = yes ##打开写权限
write list = +student ##对student组可写
write list = student redhat ##对student、redhat用户可写
valid users = student ##student可以挂载目录
valid users = @student ##student组可以挂载目录
browseable = no ##是否隐藏,no隐藏,yes不隐藏
admin users = lee ##共享目录添加管理员
1、设置用户可写
1、更改配置文件
vim /etc/samba/smb.conf
[share]
comment = share dir
path = /redhatlinux
guest ok = yes
writable = yes ##打开写权限,设置后所有用户都可写
systemctl restart smb
2、本地文件系统权限允许
chmod 777 /redhatlinux
3、selinux的布尔值更改
getsebool -a | grep samba
setsebool -P samba_export_all_rw on ##这个布尔值更改后,安全上下文没有更改的目录也可以共享。
4、测试
mount //172.25.254.170/share /mnt/smb/ -o username=student,password=redhat
touch /mnt/smb/filex
ls -l /mnt/smb/
total 0
-rw-r--r--. 1 student student 0 Feb 22 07:03 filex
2、对用户组可写
1、更改配置文件
vim /etc/samba/smb.conf
[share]
comment = share dir
path = /redhatlinux
; writable = yes
write list = +student
2、重启服务
systemctl restart smb
3、测试
首先,以student用户挂载,并建立文件
mount //172.25.254.170/share /mnt/smb/ -o username=student,password=redhat
touch /mnt/smb/filey
成功创建文件!
再以redhat用户挂载,尝试建立文件
mount //172.25.254.170/share /mnt/smb/ -o username=redhat,password=redhat
touch /mnt/smb/filez
touch: cannot touch ‘/mnt/smb/filez’: Permission denied
不能建立文件
把redhat用户加到student用户组,重新挂载目录,尝试建立文件。
usermod -G student redhat
id redhat
uid=1001(redhat) gid=1001(redhat) groups=1001(redhat),1000(student)
mount //172.25.254.170/share /mnt/smb/ -o username=redhat,password=redhat
touch /mnt/smb/file1
加入student用户组后,redhat用户拥有读写权限。
3、对某些用户可写。
1、更改配置文件
vim /etc/samba/smb.conf
[share] ##;表示注释,这一行参数不生效,可以不写。
comment = share dir
path = /redhatlinux
; writable = yes
; write list = +student
write list = student ##可以跟多个用户 用空格隔开
2、重启服务
systemctl restart smb
3、测试
首先用student用户挂载,再尝试创建文件
mount //172.25.254.170/share /mnt/smb/ -o username=student,password=redhat
touch /mnt/smb/file2
再用redhat用户挂载,创建文件
mount //172.25.254.170/share /mnt/smb/ -o username=redhat,password=redhat
touch /mnt/smb/file3
touch: cannot touch ‘/mnt/smb/file3’: Permission denied
不能创建文件。
4、某些用户可以挂载共享目录
1、更改配置文件
vim /etc/samba/smb.conf
[share]
comment = share dir
path = /redhatlinux
; writable = yes
; write list = +student
; write list = student
valid users = student ##可以加多个用户 用空格隔开
2、重启服务
systemctl restart smb
3、测试
先用student用户挂载:
mount //172.25.254.170/share /mnt/smb/ -o username=student,password=redhat
ls /mnt/smb/
file4 file6
再用redhat用户挂载:
mount //172.25.254.170/share /mnt/smb/ -o username=redhat,password=redhat
mount: //172.25.254.170/share is write-protected, mounting read-only
mount: cannot mount //172.25.254.170/share read-only
5、设置用户组可以挂载
1、更改配置文件
vim /etc/samba/smb.conf
[share]
comment = share dir
path = /redhatlinux
guest ok = yes
; writable = yes
; write list = +student
; write list = student
host deny = 172.25.254.
valid users = @student
2、重启服务
systemctl restart smb
3、测试
先用student用户挂载
mount //172.25.254.170/share /mnt/smb/ -o username=student,password=redhat
ls -l /mnt/smb/
再使用redhat用户挂载,再挂载之前先确保redhat用户不属于student用户。
id redhat
mount //172.25.254.170/share /mnt/smb/ -o username=redhat,password=redhat
6、是否隐藏该共享目录
1、更改配置文件
vim /etc/samba/smb.conf
[share]
comment = share dir
path = /redhatlinux
; guest ok = yes
; writable = yes
; write list = +student
; write list = student
; hosts deny = 172.25.254.
; valid users = @student
; valid users = student
browseable = no
2、重启服务
systemctl restart smb
3、测试
smbclient -L //172.25.254.170 -U student
可以看到共享目录share被隐藏了,虽然被隐藏,但是还能挂载。
7、共享目录添加管理员
1、更改配置文件
vim /etc/samba/smb.conf
[share]
comment = share dir
path = /redhatlinux
; guest ok = yes
writable = yes
; write list = +student
; write list = student
; hosts deny = 172.25.254.
; valid users = @student
; valid user = student
; browseable = no
admin users = student
2、重启服务
systemctl restart smb
3、测试
用student用户挂载建立文件,查看属性
[root@samba_client ~]# mount //172.25.254.170/share /mnt/smb/ -o username=student,password=redhat
[root@samba_client ~]# touch /mnt/filem
[root@samba_client ~]# ll /mnt/filem
-rw-r--r-- 1 root root 0 Mar 13 05:54 /mnt/filem
[root@samba_client ~]#
用redhat用户挂载建立文件,查看属性!
[root@samba_client ~]# mount //172.25.254.170/share /mnt/smb/ -o username=redhat,password=redhat
[root@samba_client ~]# touch /mnt/smb/files
[root@samba_client ~]# ll /mnt/smb/files
-rw-r--r-- 1 1001 1001 0 Mar 13 05:56 /mnt/smb/files
[root@samba_client ~]#
挂载共享目录后,建立的文件属于以哪个samba用户的身份挂载的共享目录,并不属于目录的建立者
七、多用户挂载
多用户挂载解决了客户端超级用户挂载之后,该客户端的普通用户同时也可以访问共享目录内容的问题:
这是不合情理的,同时也是不安全的,所以就需要下面的操作
客户端:
管理员操作:
1、下载软件
yum install cifs-utils.x86_64 -y
2、建立共享目录认证文件
vim /root/passfile ##写入samba服务端的用户和密码,文件名称任意
username=student
password=redhat
3、给认证文件设置权限
chmod 600 /root/passfile
4、超级用户挂载共享目录
mount -o credentials=/root/passfile,multiuser,sec=ntlmssp //172.25.254.170/share /mnt
认证文件 说明挂载类型为多用户 默认参数
5、测试
切换到普通用户,尝试查看挂载共享目录的内容
[root@samba_client ~]# su - student
Last login: Wed Mar 13 06:05:40 EDT 2019 on pts/0
[student@samba_client ~]$ cd /mnt
[student@samba_client mnt]$ ls
ls: reading directory .: Permission denied
[student@samba_client mnt]$
此时,默认普通用户是不能查看的!如果普通用户需要访问呢
cifscreds add -u student 172.25.254.170
可以看到,普通用户验证了,客户端管理员samba认证文件的用户及密码后,才可以访问其中的内容。