Samba服务的搭建

一、简介

在早期的网络世界中,文件数据在不同主机之间的传输大多是使用ftp,但是使用ftp服务传输档案使得我们无法直接修改主机上面的档案数据。NFS服务可以实现在客户端的机器上直接使用服务器上面的文件,但是NFS主要在unix系列操作系统上使用。windows上面也有类似的文件系统,那就是cifs(Common Internet File system,通用internet文件系统),cifs的最简单的用途就是“网上邻居”,但是cifs只能让windows机器沟通。所以,为了实现windows和unix系列系统之间能够相互沟通,就产生了samba服务。
samba通过两个服务来控制管理工作组和samba主机共享的目录,分别是:
nmbd:这个服务是来管理工作组、NetBIOS Name等的解析。主要利用UDP协议开启port137、port138来负责名称解析的任务。
smbd:这个服务的主要功能是用来管理samba主机共享的目录、文件与打印机等。主要利用可靠的TCP协议来传输数据,开放的端口为139和445(不一定存在)。
服务端使用的软件
samba:该软件主要提供了smb服务器所需的各项服务程序(smbd及nmbd)、相关的文件。
客户端使用的软件
samba-client:这个软件提供了当linux作为samba client时,所需要的工具命令。

实验一、共享用户的家目录

Server:

装包
[root@server ~]# yum install samba\* -y
防火墙和selinux
[root@server ~]# firewall-cmd --add-service=samba --per
[root@server ~]# setenforce 0
[root@server ~]# firewall-cmd --reload
添加测试用户
[root@server ~]# useradd -s /sbin/nologin smb1
[root@server ~]# smbpasswd -a smb1
:这里也可以使用pdbedit -a 用户名来新增samba用户,使用-x选项来删除samba用户
使用pdbedit -L来列出当前已添加的samba用户
添加测试文件
[root@server ~]# echo this is samba test > /home/smb1/test.txt
重启服务
[root@server ~]# systemctl restart smb nmb

Client:

装包
[root@client ~]# yum install samba-client -y
列出共享的目录
[root@client ~]# smbclient -L //192.168.19.101 -U smb1

Enter smb1's password:
Domain=[MYGROUP] OS=[Windows 6.1] Server=[Samba 4.2.3]

        Sharename       Type      Comment
        ---------       ----      -------
        IPC$            IPC       IPC Service (Samba Server Version 4.2.3)
        smb1            Disk      Home Directories
Domain=[MYGROUP] OS=[Windows 6.1] Server=[Samba 4.2.3]

        Server               Comment
        ---------            -------
        SERVER               Samba Server Version 4.2.3

        Workgroup            Master
        ---------            -------
        MYGROUP

注:若提示protocol negotiation failed: NT_STATUS_IO_TIMEOUT
需要服务端在/etc/hosts加上一条127.0.0.1 主机名
比如我这里服务端主机名为server

127.0.0.1 server

客户端挂载
[root@client ~]# mkdir /smb1
[root@client ~]# mount -o username=smb1,password=redhat //192.168.19.101/smb1 /smb1
[root@client ~]# cd /smb1/
[root@client smb1]# ls

test.txt

[root@client smb1]# cat test.txt

this is samba test

Windows端测试:
在这里插入图片描述

实验二、共享自定义目录

Server:

编辑配置文件
[root@server ~]# vim /etc/samba/smb.conf

[public]
		comment = public share	//共享描述,客户端也可通过共享描述挂载
        path = /public			//共享的目录
        browseable = yes		//允许所有人访问,即所有的samba用户登陆上去都能看到此目录,若no则看不到此目录
        writable = yes			//允许所有人写入

添加测试文件
[root@server ~]# mkdir /public
[root@server ~]# chmod o=rwxt /public///除了root之外,所有人对于此目录都是其他人,因此需要o=rwx,samba用户才有写的权限,t是为了不同用户之间不能相互修改对方的文件,例如smb1只能修改自己的文件,不能修改smb2的文件
[root@server ~]# echo this is public test > /public/test.txt
重启服务
[root@server ~]# systemctl restart smb nmb

Client:

列出共享目录
[root@client smb1]# smbclient -L //192.168.19.101 -U smb1

Enter smb1's password:
Domain=[MYGROUP] OS=[Windows 6.1] Server=[Samba 4.2.3]

        Sharename       Type      Comment
        ---------       ----      -------
        public          Disk		  public share
        IPC$            IPC       IPC Service (Samba Server Version 4.2.3)
        smb1            Disk      Home Directories

客户端挂载
[root@client smb1]# mount -o username=smb1,password=redhat //192.168.19.101/public /public
[root@client smb1]# cd /public/
[root@client public]# ls

test.txt

[root@client public]# cat test.txt

this is public test

Windows端测试
在这里插入图片描述

实验三、自定义用户访问

要求:manager用户和jishu组的tech1用户可以向/tech写入,所有人都可以访问

Server:

编辑配置文件
[root@server ~]# vim /etc/samba/smb.conf

[tech]
        comment = tech
        path = /tech
        browseable = yes			//所有人都可访问
        write list = manager,@jishu	//manager和jishu组的人可写

添加测试的用户和组
[root@server ~]# useradd manager -s /sbin/nologin
[root@server ~]# groupadd jishu
[root@server ~]# useradd tech1 -s /sbin/nologin -g jishu
添加samba用户
[root@server ~]# smbpasswd -a manager
[root@server ~]# smbpasswd -a tech1
添加测试文件夹
[root@server ~]# mkdir /tech
[root@server ~]# touch /tech/test
[root@server ~]# chmod o+w /tech/
重启服务
[root@server ~]# systemctl restart smb nmb

Client:

创建挂载点
[root@client /]# mkdir /tech/
使用manager挂载,manager用户可以touch文件
[root@client /]# mount -o username=manager,password=redhat //192.168.19.101/tech /tech
[root@client /]# cd /tech/
[root@client tech]# touch manager
[root@client tech]# ls

manager  test

卸载并使用tech1挂载,tech1也可以touch文件
[root@client /]# umount /tech
[root@client /]# mount -o username=tech1,password=redhat //192.168.19.101/tech /tech
[root@client /]# cd /tech/
[root@client tech]# touch tech1
[root@client tech]# ls

manager  tech1  test

卸载并使用redhat挂载,redhat为服务端新增的一个samba用户,但没有添加到write list当中,因此只有读的权限
[root@client /]# umount /tech/
[root@client /]# mount -o username=redhat,password=redhat //192.168.19.101/tech /tech
[root@client /]# cd /tech/
[root@client tech]# ls
manager tech1 test
[root@client tech]# touch redhat

touch: cannot touch ‘redhat’: Permission denied

实验四、多用户挂载

使用redhat挂载只有读取的权限,临时使用manager获取写权限

Server:

Server端不需要更改任何配置

Client:

装包:
[root@client tech]# yum install cifs-utils -y
创建自动挂载
编辑配置文件,先使用samba用户redhat挂载
[root@client /]# vim /etc/fstab

//192.168.19.101/tech /tech cifs defaults,_netdev,multiuser,username=redhat,password=redhat,sec=ntlmssp 0 0

挂载
[root@client /]# mount -a
切换到本地普通用户redhat
[root@client tech]# su - redhat
进入挂载点
[redhat@client ~]$ cd /tech
[redhat@client tech]$ touch file

touch: cannot touch ‘file’: Permission denied

临时使用samba用户manager获取写权限
[redhat@client tech]$ cifscreds add -u manager 192.168.19.101
[redhat@client tech]$ touch file
[redhat@client tech]$ ls

file  manager  tech1  test

注,多用户挂载权限问题
1.客户端在root的环境下使用manager认证后,进入/tech目录,仍然不能touch文件,由于一开始root用户以redhat用户的角色进行目录的挂载(root的环境变量已改变),再在root用户下使用cifscreds命令把manager用户和密码加入内核密钥环会不生效
2.切换到用户redhat后,由于环境变量的改变,此时redhat连读取的权限都没有,需要使用cifscreds重新认证,此时认证可以使用redhat认证,则只有读权限,使用manager认证,则也具有写权限。
但是
第一次使用cifscreds命令把manager(rw)的用户名和密码加入内核密钥环,成功地获得了manager(rw)所对应的权限
第二次使用cifscreds命令把redhat(ro)的用户名和密码加入内核密钥环,并没有获得redaht(ro)所对应的权限,此时实际权限还是第一次加入密钥环的manager用户所对应的权限(rw)
解决办法:
1.使用root重新挂载后即可重新认证
2.使用cifscreds clearall 命令清除内核密钥环的数据,大概5分钟后才会生效,此后可以成功把其他用户加入到内核密钥环中并生效
服务端配置selinux
[root@server ~]# semanage fcontext -a -t samba_share_t /tech"(/.*)?"
[root@server ~]# restorecon -vvFR /tech

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值