samba CIFS文件的存储

来源:互联网 发布:虚拟机ubuntu使用教程 编辑:程序博客网 时间:2024/05/16 10:41

##########################
########samba#############
##########################

1.samba作用

提供cifs协议实现共享文件

 

2.安装

[root@localhost ~]# systemctl stop firewalld

yum install samba samba-common samba-client -y

systemctl start smb nmb

systemctl enable smb.service nmb.service

 

3.添加smb用户

smb用户必须是本地用户

[root@localhost ~]# smbpasswd -a student

New SMB password:

Retype new SMB password:

Added user student.

[root@localhost ~]# setsebool -P samba_enable_home_dirs on

pdbedit -L          ##查看用户信息

pdbedit -x smb用户   ##删除smb用户

 

4.共享目录的基本设定

vim /etc/samba/smb.conf

[haha]   共享名称

comment = local directory 共享目录的描述

path =  /smbshare         共享目录的绝对路径

workgroup = WESTOS

systemctl restart smb

 

当共享目录为用户自建目录时如:/smbshare

mkdir /smbshare

touch /smbshare/westosxxx

vim /etc/samba/smb.conf

[haha]   

comment = local directory

path =  /smbshare       

semanage fcontext -a -t samba_share_t '/smbshare(/.*)?'

restorecon -RvvF /smbshare

测试:

[kiosk@foundation2 Desktop]$ smbclient //172.25.254.102/westos -U student

Enter student's password:

Domain=[MYGROUP] OS=[Unix] Server=[Samba 4.1.1]

smb: \> ls

  .                                   D        0  Sat Jun  3 14:27:19 2017

  ..                                  D        0  Sat Jun  3 14:27:02 2017

  westosxxx                           N        0  Sat Jun  3 14:27:19 2017

 

10473900 blocks of size 1024. 7314476 blocks available

smb: \> quit

 

当共享目录为系统建立目录 /mnt

setsebool -P samba_enable_home_dirs 0

cd /mnt

touch file{1..10}

ls

vim /etc/samba/smb.conf

[haha]   

comment = local directory

path =  /mnt

systemctl restart smb

setsebool -P samba_export_all_ro on

测试;

[kiosk@foundation2 Desktop]$ smbclient //172.25.254.102/westos -U student

Enter student's password:

Domain=[MYGROUP] OS=[Unix] Server=[Samba 4.1.1]

smb: \> ls

  .                                   D        0  Sat Jun  3 14:37:50 2017

  ..                                  D        0  Sat Jun  3 14:27:02 2017

  file1                               N        0  Sat Jun  3 14:37:50 2017

  file2                               N        0  Sat Jun  3 14:37:50 2017

  file3                               N        0  Sat Jun  3 14:37:50 2017

  file4                               N        0  Sat Jun  3 14:37:50 2017

  file5                               N        0  Sat Jun  3 14:37:50 2017

  file6                               N        0  Sat Jun  3 14:37:50 2017

  file7                               N        0  Sat Jun  3 14:37:50 2017

  file8                               N        0  Sat Jun  3 14:37:50 2017

  file9                               N        0  Sat Jun  3 14:37:50 2017

  file10                              N        0  Sat Jun  3 14:37:50 2017

 

10473900 blocks of size 1024. 7314256 blocks available

smb: \>

 

5.samba的配置参数

##匿名用户访问

[root@localhost ~]# vim /etc/samba/smb.conf

325 guest ok = yes

125 map to guest = bad user

systemctl restart smb

[kiosk@foundation2 Desktop]$ smbclient //172.25.254.102/westos

 

##访问控制

hosts allow = ip #仅允许

hosts deny = ip  #仅拒绝

 

valid users =    #当前共享的有效用户

[root@localhost ~]# useradd westos -s /sbin/nologin

[root@localhost ~]# smbpasswd -a westos

New SMB password:

Retype new SMB password:

Added user westos.

[root@localhost ~]#vim /etc/samba/smb.conf

valid users = westos

[root@localhost ~]# systemctl restart smb

[kiosk@foundation2 Desktop]$ smbclient //172.25.254.102/westos -U westos  ##student用户不可访问

 

[root@localhost ~]# id student

uid=1000(student) gid=1000(student) groups=1000(student),10(wheel)

[root@localhost ~]# usermod -G westos student

[root@localhost ~]#vim /etc/samba/smb.conf

valid users = @westos/+westos

[root@localhost ~]# systemctl restart smb

[kiosk@foundation2 Desktop]$ smbclient //172.25.254.102/westos -U student  ##student属于westos组可访问共享

 

##读写控制

所有用户均可写

[root@localhost ~]# chmod o+w /mnt

[root@localhost ~]# setsebool -P samba_export_all_rw on

[root@localhost ~]# vim /etc/samba/smb.conf

writable = yes

[root@localhost ~]# systemctl restart smb

 

[root@foundation2 Desktop]# mount -o username=westos,password=redhat //172.25.254.102/westos /mnt/

[root@foundation2 Desktop]# cd /mnt

[root@foundation2 mnt]# ls

file1  file10  file2  file3  file4  file5  file6  file7  file8  file9

[root@foundation2 mnt]# touch file0

[root@foundation2 ~]# umount /mnt

 

设定指定用户可写

1>[root@localhost ~]# vim /etc/samba/smb.conf

write list = student            ##可写用户

[root@localhost ~]# systemctl restart smb

[root@foundation2 ~]# mount -o username=student,password=student //172.25.254.102/westos /mnt/

[root@foundation2 ~]# cd /mnt

[root@foundation2 mnt]# ls

file0  file1  file10  file2  file3  file4  file5  file6  file7  file8  file9

[root@foundation2 mnt]# rm -fr *

[root@foundation2 mnt]# ls

 

2>[root@localhost ~]# id westos

uid=1001(westos) gid=1001(westos) groups=1001(westos)

[root@localhost ~]# usermod -G student westos

write list = +student/@student  ##可写用户组

 

3>[root@localhost ~]# vim /etc/samba/smb.conf

admin users = westos            ##共享的超级用户指定

[root@localhost ~]# systemctl restart smb

[root@localhost ~]# chmod o-w /mnt/

[root@localhost ~]# ll -d /mnt/

drwxr-xr-x. 2 root root 6 Jun  3 03:57 /mnt/

[root@foundation2 ~]# mount -o username=westos,password=redhat //172.25.254.102/westos /mnt/

[root@foundation2 ~]# cd /mnt

[root@foundation2 mnt]# ls

[root@foundation2 mnt]# touch file

 

[root@localhost ~]# ll /mnt/

total 0

-rw-r--r--. 1 root westos 0 Jun  3 04:04 file

 

6.smb多用户挂载

client上

[root@foundation2 ~]# vim /root/haha

username=student

password=student

[root@foundation2 ~]# chmod 60 /root/haha

[root@foundation2 ~]# yum install cifs-utils - y

[root@foundation2 ~]# mount -o credentials=/root/haha,multiuser,sec=ntlmssp //172.25.254.102/westos /mnt  

#credentials=/root/haha 指定挂载时所用到的用户文件

#multiuser                   支持多用户认证

#sec=ntlmssp             认证方式为标准smb认证方式

 

[root@foundation2 mnt]# su - kiosk

[kiosk@foundation2 ~]$ cd /mnt

[kiosk@foundation2 mnt]$ ls

ls: reading directory .: Permission denied  ##因为没有做smb的认证无法访问共享

 

[kiosk@foundation2 mnt]$ cifscreds add -u westos 172.25.254.102

Password:      ##smb用户为westos的密码

[kiosk@foundation2 mnt]$ ls

file

[kiosk@foundation2 mnt]$ touch file1

[kiosk@foundation2 mnt]$ ls

file  file1