#Samba#
1.samba作用
提供cifs协议实现共享文件
2.安装
[[email protected] ~]# yum install samba samba-common samba-client -y ##samba-服务器应用程序 samba-common-samba的支持文件 samba-client-客户端应用程序
[[email protected] ~]# systemctl start smb nmb ##开启服务
3.添加smb用户
#smb用户必须是本地用户
[[email protected] ~]# pdbedit -L ##查看用户添加情况
[[email protected] ~]# pdbedit -x student ##删除smb用户
[[email protected] ~]# smbpasswd -a student
New SMB password: ##输入smb当前用户密码
Retype new SMB password: ##确认密码
Added user student.
[[email protected] ~]# setsebool -P use_samba_home_dirs on ##在selinux中设定smb用户可以访问自己的家目录
4.共享目录的基本额设定
[[email protected] ~]# mkdir /smbshare
[[email protected] ~]# touch /smbshare/HnSoixxx
[[email protected] ~]# touch /mnt/file{1..6}
[[email protected] ~]# vim /etc/samba/smb.conf
[HnSoi] ##共享名称
comment = local directory ##对共享目录的描述
path = /smbshare ##共享目录的绝对路径
[mnt]
path = /mnt
workgroup = HnSoi ##修改域名
#当共享目录为用户自建立目录时
[[email protected] ~]# semanage fcontext -a -t samba_share_t '/smbshare(/.*)?'
[[email protected] ~]# restorecon -RvvF /smbshare
[[email protected] 桌面]$ smbclient -L//172.25.254.129 -U student
[[email protected] 桌面]$ smbclient //172.25.254.129/HnSoi -U student
#当共享目录为系统建立目录时
[[email protected] ~]# setsebool -P samba_export_all_ro on ##只读共享
[[email protected] ~]# setsebool -P samba_export_all_rw on ##读写共享
[[email protected] 桌面]$ smbclient -L//172.25.254.129 -U student
[[email protected] 桌面]$ smbclient //172.25.254.129/mnt -U student
5.samba的配置参数
#匿名用户访问
[[email protected] ~]# vim /etc/samba/smb.conf
guest ok =yes ##允许匿名访问
map to guest = bad user
[[email protected] 桌面]$ smbclient //172.25.254.129/mnt
#访问控制
[[email protected] ~]# useradd -s /sbin/nologin westos
[[email protected] ~]# smbpasswd -a westos
[[email protected] ~]# vim /etc/samba/smb.conf
hosts allow = 172.25.254.29 ##仅允许172.25.254.29主机访问
hosts deny = 172.25.254.29 ##仅拒绝172.25.254.29主机访问
valid users = westos ##当前共享的有效用户为westos
valid users = @westos ##当前共享的有效用户组为westos
valid users = +westos ##当前共享的有效用户组为westos
[[email protected] 桌面]$ smbclient //172.25.254.129/mnt -u westos
[[email protected] 桌面]$ smbclient //172.25.254.129/mnt -u student
[[email protected] ~]# usermod -G westos student
[[email protected] 桌面]$ smbclient //172.25.254.129/mnt -u student
#读写控制
『所有用户均可写』
[[email protected] ~]# chmod o+w /mnt
[[email protected] ~]# setsebool -P samba_export_all_rw on
[[email protected] ~]# vim /etc/samba/smb.conf
writable = yes
【测试】
[[email protected] ~]# mount -o username=student,password=hansongwei //172.25.254.129/mnt /mnt/
[[email protected] ~]# cd /mnt/
[[email protected] mnt]# touch file29
『设定指定用户可写』
[[email protected] ~]# vim /etc/samba/smb.conf
write list = student ##可写用户
write list = +student ##可写用户组
admin users = westos ##共享的超级用户指定
【可写用户测试】
[[email protected] ~]# mount -o username=westos,password=hansongwei //172.25.254.129/mnt /mnt/
[[email protected] ~]# cd /mnt/
[[email protected] mnt]# touch hahaha
[[email protected] ~]# mount -o username=student,password=hansongwei //172.25.254.129/mnt /mnt/
[[email protected] ~]# cd /mnt/
[[email protected] mnt]# touch studentfile
【可写用户组测试】
[[email protected] ~]# usermod -G student westos
[[email protected] ~]# mount -o username=westos,password=hansongwei //172.25.254.129/mnt /mnt/
[[email protected] ~]# cd /mnt/
[[email protected] mnt]# touch westosfile
【共享的超级用户指定测试】
[[email protected] ~]# chmod o-w /mnt
[[email protected] ~]# mount -o username=westos,password=hansongwei //172.25.254.129/mnt /mnt/
[[email protected] ~]# cd /mnt/
[[email protected] mnt]# touch 123
6.samba多用户挂载
『在client上』
[[email protected] ~]# vim /root/xixi ##建立smb认证文件
username=student
password=hansongwei
[[email protected] ~]# chmod 600 /root/xixi
[[email protected] ~]# yum install cifs-utils -y ##安装cifs-utils软件包,它包含了cifscreds命令
[[email protected] ~]# mount -o credentials=/root/xixi,multiuser,sec=ntlmssp //172.25.254.129/mnt /mnt/
##credentials=/root/xixi 指定挂载时所用到的用户文件
##multiuser 支持多用户认证
##sec=ntlmssp 认证方式为标准smb认证方式
[[email protected] ~]# su - kiosk
[[email protected] ~]$ ls /mnt
ls: cannot access /mnt: Permission denied ##由于没有作smb的认证所以无法访问smb共享
[[email protected] ~]$ cifscreds add -u westos 172.25.254.129 ##挂载smb共享
Password: ##输入smb用户westos的密码
[[email protected] ~]$ ls /mnt/