linux之samba

samba

1.定义

提供cifs协议实现文件共享通用Internet文件系统(CIFS)也称为服务器信息块(SMB),是适用于Microsoft  Windows服务器和客户端的标准文件和打印机共享系统。Samba服务可用于将Linux文件系统作为CIFS/SMB网络文件共享进行共享,并将Linux打印机作为CIFS/SMB打印机共享进行共享。


2.配置

2.1 安装
[[email protected] ~]# yum install samba samba-common samba-client -y
Samba – 服务器应用程序
Samba-common – Samba的支持文件
Samba-client – 客户端应用程序
 服务名称:smb nmb
[[email protected] ~]# systemctl start smb nmb
[[email protected] ~]# systemctl enable smb nmb
添加用户
[[email protected] ~]# smbpasswd -a student    添加用户
[[email protected] ~]# pdbedit -L               查看用户    
pdbedit -x   smb用户
[[email protected] ~]# getsebool -a | grep samba
[[email protected] ~]# setsebool -P samba_enable_home_dirs on


2.2.共享目录

[[email protected] ~]# vim /etc/samba/smb.conf

添加

         [hahha]               共享文件名称

        comment = sharedir    对共享文件的描述
        path = 共享文件 (/mnt  /sharesmb)

[[email protected] ~]# systemctl restart smb.service


测试:
smbclient  -L  //172.25.254.121/hahaha
Enter student's password:

linux之samba


2.2.2当共享目录为用户自建目录时
        更改文件的安全上下文
        semanage fcontext -a -t  samba_share_t '/smbshare(/.*)?'

        restorecon -RvvF /smbshare

测试:
smbclient //172.25.254.121/student -U student
Enter student's password:

linux之samba


2.2.3当共享目录为系统目录时

linux之samba
    setsebool -P samba_export_all_ro on    #只读共享

    setsebool -P samba_export_all_rw on    #读写共享

测试:
smbclient //172.25.254.121/student -U student
Enter student's password:

linux之samba


2.3 配置参数,控制访问
 2.3.1  匿名用户访问
125             map to guest = bad user
                   guest ok = yes

测试:
smbclient //172.25.254.121/hahaha
Enter student's password:

linux之samba


2.3.2访问控制
     
       hosts allow = 172.25.254.21    #仅允许
       hosts deny = 172.25.254.21    #仅拒绝

测试:
smbclient  //172.25.254.121/hahaha  -U student
Enter student's password:

linux之samba

        valid users = redhat           当前共享的有效用户为redhat
        valid users = +redhat        当前共享的有效用户为redhat组
        valid users = @redhat        当前共享的有效用户为redhat组
        useradd -s /sbin/nologin redhat
        smbpasswd -a redhat
        pdbedit redhat
        usermod -G redhat student

2.3.3读写控制   所有用户的都可以写 

[[email protected] ~]# getsebool -a | grep samba
[[email protected] ~]# setsebool -P samba_export_all_rw on
[[email protected] ~]# chmod o+w /mnt
[[email protected] ~]# vim /etc/samba/smb.conf
    writable = yes

2.3.3读写控制   指定用户可写
#某一个人可写或某一组可写

            writable = no
            write list =  student
            write list =  +student
            write list =  @student

#某一个人以root用户写

         chmod o-w /mnt/
         writable = yes

        admin users = student

linux之samba

linux之samba


2.4  samba多用户认证

##在客户端

[[email protected] /]# vim /root/haha
[[email protected] kiosk]# cat /root/haha
username=student
password=student

[[email protected] /]# chmod 600 /root/haha
[[email protected] /]# yum install cifs-utils -y

[[email protected] /]# umount /mnt/
[[email protected] /]# mount -o credentials=/root/haha,multiuser,sec=ntlmssp  //172.25.254.121/hahha /mnt/
###credentials=/root/haha    指定挂载时所用到的用户文件
###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 redhat 172.25.254.121
Password:                                     ###smb用户redhat的密码
[[email protected] ~]$ ls /mnt/

file  file1  file2

linux之samba