samba服务器配置

cifs文件系统 — samba服务

一、原理

1、samba简介

SMB是一个协议,而不是一个实现。这个协议已经在许多不同的操作系统中实现。
SMB协议是一个客户端——服务端协议,客户端和服务端进行联系。

2、samba软件功能

  • 共享linux的文件系统
  • 共享安装在samba服务器上的打印机
  • 支持windows客户使用网上邻居共享网络
  • 使用windows系统共享的文件和打印机
  • 支持windows域控制器和windows成员服务器对使用samba资源的用户进行认证
  • 支持wins名称服务器解析及浏览
  • 支持ssl安全套接层协议

二、安装配置

1、简单安装测试

服务端:
[root@localhost ~]# yum install samba-client.x86_64 samba-common.x86_64 samba.x86_64 -y   ##安装提供samba的软件
[root@localhost ~]# systemctl start smb   ##打开服务
[root@localhost ~]# smbclient -L //172.25.254.171   ##列出这个ip里使用smb服务的内容
Enter root's password:        ##不输入密码使用匿名用户登陆,这里的密码不是用户的密码,而是用户登陆smb的密码,需要自己设定
Anonymous login successful
Domain=[MYGROUP] OS=[Unix] Server=[Samba 4.1.1]

    Sharename       Type      Comment
    ---------       ----      -------
    IPC$            IPC       IPC Service (Samba Server Version 4.1.1)
Anonymous login successful
Domain=[MYGROUP] OS=[Unix] Server=[Samba 4.1.1]

    Server               Comment
    ---------            -------

    Workgroup            Master
    ---------            -------
[root@localhost ~]# smbpasswd student   ##设定student用户的samba密码
New SMB password:
Retype new SMB password:

客户端:
[root@localhost Desktop]# yum install samba-client -y
[root@localhost Desktop]# smbclient //172.25.254.171/student -U student   ##使用student用户登陆
Enter student's password:     ##这里输入的是服务端设置的student用户登陆samba的密码
Domain=[MYGROUP] OS=[Unix] Server=[Samba 4.1.1]
smb: \> ls
  .                                   D        0  Thu Jul 10 19:06:52 2014
  ..                                  D        0  Thu Jul 10 18:19:09 2014
  .bash_logout                        H       18  Wed Jan 29 07:45:18 2014
  .bash_profile                       H      193  Wed Jan 29 07:45:18 2014
  .bashrc                             H      231  Wed Jan 29 07:45:18 2014
  .ssh                               DH        0  Thu Jul 10 18:19:10 2014
  .config                            DH        0  Thu Jul 10 19:06:53 2014

        10473900 blocks of size 1024. 7273584 blocks available
[root@localhost Desktop]# mount -o username=student,password=123 //172.25.254.171/student /mnt/   ##将student用户的samba挂载在本机/mnt目录上

2、配置

1)修改默认组名称

Workgroup = xxx ##修改默认名称为xxx

[root@localhost ~]# vim /etc/samba/smb.conf
 89         workgroup = LCL
 [root@localhost ~]# systemctl restart smb.service
[root@localhost ~]# smbclient -L //172.25.254.171
Enter root's password:
Anonymous login successful
Domain=[LCL] OS=[Unix] Server=[Samba 4.1.1]

    Sharename       Type      Comment
    ---------       ----      -------
    IPC$            IPC       IPC Service (Samba Server Version 4.1.1)
Anonymous login successful
Domain=[LCL] OS=[Unix] Server=[Samba 4.1.1]

    Server               Comment
    ---------            -------

    Workgroup            Master
    ---------            -------
2)新建共享目录,并可以用用户登陆

若是一个新建立的共享目录,在selinux开启状态下一定要修改安全上下文

服务器端:
[root@localhost ~]# mkdir /DATA
[root@localhost ~]# touch /DATA/file{1..5}
[root@localhost ~]# vim /etc/samba/smb.conf
321         [DATA]
322         comment = Lcl DATA
323         path = /DATA
[root@localhost ~]# systemctl restart smb.service
[root@localhost ~]# smbclient -L //172.25.254.171 -U student
Enter student's password:
Domain=[LCL] OS=[Unix] Server=[Samba 4.1.1]

    Sharename       Type      Comment
    ---------       ----      -------
    DATA            Disk      Lcl DATA
    IPC$            IPC       IPC Service (Samba Server Version 4.1.1)
    student         Disk      Home Directories
Domain=[LCL] OS=[Unix] Server=[Samba 4.1.1]

    Server               Comment
    ---------            -------

    Workgroup            Master
    ---------            -------
[root@localhost ~]# semanage fcontext -a -t samba_share_t '/DATA(/.*)?'
[root@localhost ~]# restorecon -RvvF /DATA/

客户端:
[root@localhost Desktop]# mount //172.25.254.171/DATA /mnt -o username=student,password=456
[root@localhost Desktop]# cd /mnt/
[root@localhost mnt]# ls
file1  file2  file3  file4  file5
若想要开机自动挂载,可以在/etc/fstab里面添加下面一行
//172.25.254.171/DATA   /mnt    cifs    defaults,username=student,password=456    0 0
3)匿名用户访问

在上面的修改中,用客户端可以测试出匿名用户并不可以挂载访问,在windows里面匿名用户的名称为guest

服务器端:
[root@localhost ~]# vim /etc/samba/smb.conf
        map to guest = bad user   ##设定匿名用户的用户名为guest

        [DATA]
        comment = Lcl DATA
        path = /DATA
        guest ok = yes      ##允许匿名用户访问
[root@localhost ~]# systemctl restart smb.service

客户端:
[root@localhost /]# mount //172.25.254.171/DATA /mnt -o username=guest
[root@localhost /]# cd /mnt/
[root@localhost mnt]# ls
file1  file2  file3  file4  file5
要想开机自动挂载,也可以在/etc/fstab文件里面添加配置
4)用户可写访问控制

查看目前samba用户命令 pdbedit -L

1> 所有存在用户可写,这里以student用户为例:
服务器端:
[root@localhost ~]# vim /etc/samba/smb.conf
        [DATA]
        comment = Lcl DATA
        path = /DATA
        guest ok = yes
        writable = yes    ##可写
[root@localhost ~]# systemctl restart smb.service
[root@localhost ~]# setfacl -m u:student:rwx /DATA/

客户端:
[root@localhost /]# mount //172.25.254.171/DATA /mnt -o username=student,password=456
[root@localhost /]# cd /mnt
[root@localhost mnt]# ls
file1  file2  file3  file4  file5
[root@localhost mnt]# rm -fr file1
[root@localhost mnt]# ls
file2  file3  file4  file5
2> 只让指定用户或指定组可写,其他用户不可以,以student用户为例
服务器端:
[root@localhost ~]# vim /etc/samba/smb.conf
        [DATA]
        comment = Lcl DATA
        path = /DATA
        guest ok = yes
        write list = student    ##在这里修改为+student或者@student,则指定为student组
[root@localhost ~]# systemctl restart smb.service
[root@localhost ~]# setfacl -m u:student:rwx /DATA/
##指定组可写的时候,指定组的权限

客户端:
测试和上一条相同,指定组的时候,可以创建一个在组内的用户,使它可以登陆修改
3> 设置samba的超级用户,用户名为admin

超级用户代表是挂载完成后对文件的管理是超级用户,而不是对samba服务

服务器端:
[root@localhost ~]# useradd admin
[root@localhost ~]# smbpasswd -a admin
New SMB password:
Retype new SMB password:
Added user admin.
[root@localhost ~]# vim /etc/samba/smb.conf
        [DATA]
        comment = Lcl DATA
        path = /DATA
        guest ok = yes
        writable = yes
        admin users = admin   ##让admin用户变为超级用户,后面的名字可以设定为任一存在用户
        ##超级用户是挂载完成后对文件的管理是超级用户
[root@localhost ~]# systemctl restart smb.service

客户端:
[root@localhost /]# mount //172.25.254.171/DATA /mnt -o username=admin,password=456
[root@localhost /]# rm -fr /mnt/file1
[root@localhost /]# ls /mnt
file2  file3  file4  file5
4> 隐藏目标文件
服务器端:
[root@localhost ~]# vim /etc/samba/smb.conf
        browseable = no
        ##在想要隐藏的一系列中添加这一行,这一行默认为yes
[root@localhost ~]# systemctl restart smb.service

测试:
[root@localhost ~]# smbclient -L //172.25.254.171
Enter root's password:
Domain=[LCL] OS=[Unix] Server=[Samba 4.1.1]

    Sharename       Type      Comment
    ---------       ----      -------
    IPC$            IPC       IPC Service (Samba Server Version 4.1.1)
Domain=[LCL] OS=[Unix] Server=[Samba 4.1.1]

    Server               Comment
    ---------            -------

    Workgroup            Master
    ---------            -------
并且客户端可以正常挂载
5> 指定用户或组可以使用samba
服务器端:
[root@localhost ~]# vim /etc/samba/smb.conf
        valid users = student
[root@localhost ~]# systemctl restart smb.service

客户端:
[root@localhost /]# mount //172.25.254.171/DATA /mnt -o username=lcl,password=456
mount: //172.25.254.171/DATA is write-protected, mounting read-only
mount: cannot mount //172.25.254.171/DATA read-only
[root@localhost /]# mount //172.25.254.171/DATA /mnt -o username=student,password=456
[root@localhost /]# ls /mnt
file2  file3  file4  file5
6> samba多用户挂载

如果用root用户登陆的文件,那么在其他用户也可以看到,则不安全,所以需要samba多用户挂载,只有通过认证的用户才可以看到

在客户端里面操作:
[root@localhost /]# yum install cifs-utils.x86_64 -y
[root@localhost /]# vim /root/smbfile ##编写root用户通过这个文件认证
##这个文件只有root用户可以读、执行
username=student
password=456
[root@localhost /]# mount -o credentials=/root/smbfile,multiuser,sec=ntlmssp //172.25.254.171/DATA /mnt
[root@localhost /]# cd /mnt
[root@localhost mnt]# ls
file2  file3  file4  file5
[root@localhost mnt]# touch file1
[root@localhost mnt]# su - kiosk
Last login: Sun Dec  3 08:39:54 EST 2017 on :0
[kiosk@foundation70 ~]$ cd /mnt
[kiosk@foundation70 mnt]$ ls
ls: reading directory .: Permission denied
[kiosk@foundation70 mnt]$ cifscreds add -u westos 172.25.254.171
Password:
[kiosk@foundation70 mnt]$ ls
file1  file2  file3  file4  file5
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值