CIFS:
CIFS(Common Internet File System)是一种通用网络文件系统,主要用于网络设备之间的文件共享。CIFS可以在linux系统和windows系统之间共享文件,因此这种文件系统主要用于客户端是windows系统。
samba:
提供cifs协议实现共享文件,主要用于windows与linux的网络文件系统
配置步骤:
【服务端】: 172.25.254.173
yum install samba-client samba-common samba.x86_64 -y
systemctl start smb
Samba-common ##Samba 的支持文件
Samba-client ##客户端应用程序
Samba.x86_64 ##服务器应用程序
systemctl status firewalld
(查看火墙状态,确认关闭)
setsebool -P samba_enable_home_dirs on
(selinux为强制状态时,不能进入家目录,需要打开)
smbpasswd -a student
##把studnet用户添加进smba(此处的用户必须是本机存在的)
pdbedit -L
##查看添加进samba列表的用户
pdbedit -x student
##删除用户
测试:
【客户端】: 172.25.254.93
yum install samba-client -y
smbclient -L //172.25.254.173
smbclient -L //172.25.254.173 -U westos
##可以看到共享的目录
smbclient //172.25.254.173/westos -U westos
##进入student家目录
【服务端】 172.25.254.173
vim /etc/samba/smb.conf(samba共享目录设定) 38 [GongXiang] ##访问后看到的目录名字即共享名称 39 comment = GongXiang data ##共享说明 40 path = /westoslinux ##共享目录路径
systemctl restart smb
共享目录自建立目录时需要修改它的selinux属性,此方式修改只针对修改目录
semanage fcontext -a -t samba_share_t '/westoslinux(/.*)?'
(永久修改)
chcon -t samba_share_t /westoslinux/
(临时修改)
restorecon -FvvR /westoslinux/
测试:
共享系统目录时修改的Selinux属性,此方式修改针对所有目录,一旦开放,selinux不保护目录访问
vim /etc/samba/smb.conf 38 [GongXiang] 39 comment = GongXiang data 40 path = /westoslinux 41 42 [opt] 43 comment = opt dir 44 path = /opt
setsebool -P samba_export_all_rw on
##可以读写
setsebool -P samba_export_all_ro on
##只读
测试:
【客户端】 172.25.254.93
smbclient //172.25.254.173/共享名称 -U westos
借助用户身份访问
vim /etc/samba/smb.conf 7 workgroup = WESTOS ##此处写的是共享名称,域名更改 38 [GongXiang] 39 comment = gongxiang data 40 path = /westoslinux
systemctl restart smb
mount //172.25.254.173/haha /mnt -o username=westos,password=redhat
##挂载
df
查看
vim /etc/fstab ##设定永久挂载 11 //172.25.254.173/GongXiang /mnt cifs defaults,username=student,password=redhat 0 0
测试:(默认的是全局的)
【服务端】 172.25.254.173 可以挂载
umount /mnt mount -a df
【客户端】 172.25.254.93 可以挂载
mount //172.25.254.111/haha /mnt -o username=student,password=redhat
##挂载
如何禁止某网段用户挂载
vim /etc/samba/smb.conf 41 hosts deny = 172.25.254. ##禁止可以访问的ip进行挂载,多个ip用空格隔开
测试:
匿名访问:
vim /etc/samba/smb.conf 123 security = user 124 passdb backend = tdbsam 125 map to guest = bad user 326 [mnt] 327 comment = mntdate 328 path = /mnt 329 guest ok = yes ##只允许mnt进行挂载
测试:
【服务端】 172.25.254.173
匿名用户访问:
smbclient //172.25.254.111/mnt
【客户端】 172.25.254.93
匿名用户挂载:
mount //172.25.254.173/mnt /mnt/ -o username=guest
(输入密码时回车)
或
mount //172.25.254.173/mnt /mnt/ -o username=guest,password=""
vim /etc/samba/smb.conf 322 [haha] 323 comment = data westos 324 path = /westos 325 writeable = yes 可写的权限打开 326 write list = @westos 指定组可写,无符号表示单个用户 327 valid users = +westos 指定组可登陆 328 browseable = yes 隐藏目录 329 admin users = westos 指定管理员
注意:@与+ 都表示在westos组下的用户,影响此实验的原因selinux,系统服务,服务本身
确保系统服务允许,selinux允许
chmod 777 /westos
setsebool -P samba_export_all_rw on
1.设定目录可写
vim /etc/samba/smb.conf 42 writeable = yes
测试:
2.指定组可写
vim /etc/samba/smb.conf 43 write list = @westos
测试:
3.设定组可以登录
vim /etc/samba/smb.conf 44 valid users = +westos
测试:
4.隐藏目录
vim /etc/samba/smb.conf 45 browseable = no
测试:
5.指定管理员
vim /etc/samba/smb.conf 42 writeable = yes 46 admin users = westos
测试:
客户端:在root身份下将共享目录挂载到/mnt,可以看到里面的文件
但是切换到student用户身份后,仍然可以看到/mnt的内容,这样是不合理的
【服务端】: 172.25.254.111
yum install cifs-utils -y
【客户端】: 172.25.254.11
yum install cifs-utils -y
vim /root/smbpass 1 username=smb用户 2 password=smb用户密码
mount -o credentials=/root/sampass,multiuser,sec=ntlmssp //172.25.254.111/haha /mnt
chmod 600 /root/sampass
##让其他用户无法查看
credentials=/root/passwd ##为指定挂载时用到的用户文件,即用户名和密码
sec=ntlmssp ##表示认证方式为标准smb认证方式(加密方式为ntlmssp)
multiuser ##采用多用户挂载
测试:
useradd test su - test ls /mnt ##查看不成功
cifscreds add -u westos 172.25.254.111
##给该用户添加一个smb用户
ls /mnt
##查看成功
或者:
su - lee ##与smb用户同名 ls /mnt cifscreds add 172.25.254.111 ls /mnt 查看成功 su -test ##与smb用户不同名 ls /mnt cifscreds add 172.25.254.111 ls /mnt 依然不成功