Samba服务器搭建

Posted 坏坏-5

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Samba服务器搭建相关的知识,希望对你有一定的参考价值。


项目需求

  • 某公司有两个部门,分别是销售部(Sale)和技术部(Technology)。现在想要部署文件共享服务,为每个部门创建账号,实现两个部门都有各自的文件夹,同组内的成员可以对各自文件夹进行读写操作,但是两个部门之间不能互相访问。Public文件夹两个部门都拥有读写权限。老板(Boss)可以访问所有的文件夹。按照需求部署Samba服务。

实验环境说明

主机系统系统版本IP地址
服务端LinuxRHEL 7.0192.168.43.128/24
客户端LinuxRHEL 7.4192.168.43.15/24
客户端Windows 11Windows 11192.168.43.1/24

基础配置

  • 使用仅主机模式(即VMnet 1),配置虚拟网络设置,配置动态分配到的网段为192.168.43.0/24
  • 编辑虚拟网络设置中的仅主机模式,可以修改默认分配到IP网段,如果Linux客户端是通过DHCP自动获取地址,则需要勾选使用本地DHCP服务将IP地址分配给虚拟机,如果是静态IP,则不需要此配置
  • 可以通过更改设置来修改分配给虚拟机的IP地址范围,必须要应用后才能生效

服务端配置静态IP地址,并配置YUM

  • 在服务端配置静态IP地址,也可以使用动态分配地址,前提是保证Windows主机和Linux服务器、客户端是连通的
[root@Server ~]# vim /etc/sysconfig/network-scripts/ifcfg-eno16777736 
[root@Server ~]# cat !$     //!$表示上一条命令的最后一个参数
cat /etc/sysconfig/network-scripts/ifcfg-eno16777736
TYPE=Ethernet
BOOTPROTO=static            //配置为静态IP,也可以写none,动态获取配置为dhcp
NAME=eno16777736
DEVICE=eno16777736
ONBOOT=yes                  //开机自启动
IPADDR=192.168.43.128       //配置静态IP地址
NETMASK=255.255.255.0       //配置子网掩码
GATEWAY=192.168.43.254      //配置默认网关
DNS1=8.8.8.8                //配置DNS
[root@Server ~]# systemctl restart network    //重新启动网卡,使配置生效
[root@Server ~]# ifconfig 
eno16777736: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.43.128  netmask 255.255.255.0  broadcast 192.168.43.255
        inet6 fe80::20c:29ff:fe49:e6d9  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:49:e6:d9  txqueuelen 1000  (Ethernet)
        RX packets 2694  bytes 242306 (236.6 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 1632  bytes 184737 (180.4 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 0  (Local Loopback)
        RX packets 24  bytes 2546 (2.4 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 24  bytes 2546 (2.4 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@Server ~]#
  • 服务端配置YUM
[root@Server ~]# mount /dev/cdrom /mnt/
mount: /dev/sr0 写保护,将以只读方式挂载
[root@Server ~]# cd /etc/yum.repos.d/
[root@Server yum.repos.d]# rm -rf *
[root@Server yum.repos.d]# ls
rhel.repo
[root@Server yum.repos.d]# vim rhel.repo
[root@Server yum.repos.d]# cat rhel.repo 
[Base]
name=RHEL
baseurl=file:///mnt
gpgcheck=0
enabled=1
[root@Server yum.repos.d]#  yum clean all 
已加载插件:langpacks, product-id, subscription-manager
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
正在清理软件源: Base
Cleaning up everything
[root@Server yum.repos.d]# yum repolist all 
已加载插件:langpacks, product-id, subscription-manager
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
Base                                                     | 4.1 kB     00:00     
(1/2): Base/group_gz                                       | 137 kB   00:00     
(2/2): Base/primary_db                                     | 4.0 MB   00:00     
源标识                             源名称                            状态
Base                               RHEL                              启用: 4,986
repolist: 4,986
[root@Server yum.repos.d]# 

Linux客户端配置静态IP,并配置YUM

  • 如服务端相同的配置,配置IP与YUM仓库
[root@Client ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33 
[root@Client ~]# cat !$
cat /etc/sysconfig/network-scripts/ifcfg-ens33
TYPE=Ethernet
BOOTPROTO=static
NAME=ens33
DEVICE=ens33
ONBOOT=yes   
IPADDR=192.168.43.15
NETMASK=255.255.255.0
GATEWAY=192.168.43.254
DNS1=8.8.8.8
[root@Client ~]# systemctl restart network
[root@Client ~]# ifconfig 
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.43.15  netmask 255.255.255.0  broadcast 192.168.43.255
        inet6 fe80::20c:29ff:fe9e:9b29  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:9e:9b:29  txqueuelen 1000  (Ethernet)
        RX packets 1950  bytes 167786 (163.8 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 997  bytes 118512 (115.7 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1  (Local Loopback)
        RX packets 192  bytes 15168 (14.8 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 192  bytes 15168 (14.8 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@Client ~]# mount /dev/cdrom /mnt/
mount: /dev/sr0 写保护,将以只读方式挂载
[root@Client ~]# cd /etc/yum.repos.d/
[root@Client yum.repos.d]# rm -rf *
[root@Client yum.repos.d]# vim rhel.repo
[root@Client yum.repos.d]# cat rhel.repo 
[Base]
name=RHEL
baseurl=file:///mnt
gpgcheck=0
enabled=1
[root@Client yum.repos.d]# yum clean all 
已加载插件:langpacks, product-id, search-disabled-repos, subscription-manager
This system is not registered with an entitlement server. You can use subscription-manager to register.
Repodata is over 2 weeks old. Install yum-cron? Or run: yum makecache fast
正在清理软件源: Base
Cleaning up everything
Maybe you want: rm -rf /var/cache/yum, to also free up space taken by orphaned data from disabled or removed repos
[root@Client yum.repos.d]# yum repolist all 
已加载插件:langpacks, product-id, search-disabled-repos, subscription-manager
This system is not registered with an entitlement server. You can use subscription-manager to register.
Base                                                     | 4.1 kB     00:00     
(1/2): Base/group_gz                                       | 137 kB   00:00     
(2/2): Base/primary_db                                     | 4.0 MB   00:00     
源标识                             源名称                            状态
Base                               RHEL                              启用: 4,986
repolist: 4,986
[root@Client yum.repos.d]#

Windows客户端配置IP地址

  • 修改VMnet 1的虚拟网卡IP获取方式为静态IP

  • 双击VMnet 1网卡,查看静态IP地址

测试服务端与客户端的连通性

  • 在服务端使用ping命令测试服务端与客户端的连通性
[root@Server ~]# ping -c 4 192.168.43.15
PING 192.168.43.15 (192.168.43.15) 56(84) bytes of data.
64 bytes from 192.168.43.15: icmp_seq=1 ttl=64 time=0.356 ms
64 bytes from 192.168.43.15: icmp_seq=2 ttl=64 time=0.486 ms
64 bytes from 192.168.43.15: icmp_seq=3 ttl=64 time=0.405 ms
64 bytes from 192.168.43.15: icmp_seq=4 ttl=64 time=1.17 ms

--- 192.168.43.15 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3008ms
rtt min/avg/max/mdev = 0.356/0.605/1.174/0.332 ms
[root@Server ~]# ping -c 4 192.168.43.1
PING 192.168.43.1 (192.168.43.1) 56(84) bytes of data.
64 bytes from 192.168.43.1: icmp_seq=1 ttl=64 time=0.298 ms
64 bytes from 192.168.43.1: icmp_seq=2 ttl=64 time=0.573 ms
64 bytes from 192.168.43.1: icmp_seq=3 ttl=64 time=1.30 ms
64 bytes from 192.168.43.1: icmp_seq=4 ttl=64 time=1.14 ms

--- 192.168.43.1 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3003ms
rtt min/avg/max/mdev = 0.298/0.831/1.308/0.412 ms
[root@Server ~]# 
  • Linux中的ping命令默认会一直ping,-c 4参数表示只ping4个数据包,如果直接使用ping,需要用Ctrl + C来终止ping
  • 至此,基础环境配置完成

Samba安装与基本配置

服务端配置

  • 在服务端安装samba软件包
[root@Server ~]# yum -y install samba
已加载插件:langpacks, product-id, subscription-manager
This system is not registered to Red Hat Subscription Management. You bscription-manager to register.
正在解决依赖关系
--> 正在检查事务
---> 软件包 samba.x86_64.0.4.6.2-8.el7 将被 安装
...

已安装:
  samba.x86_64 0:4.6.2-8.el7                                          

作为依赖被安装:
  samba-client-libs.x86_64 0:4.6.2-8.el7                              
  samba-common-libs.x86_64 0:4.6.2-8.el7                              
  samba-common-tools.x86_64 0:4.6.2-8.el7                             

作为依赖被升级:
  krb5-libs.x86_64 0:1.15.1-8.el7         libldb.x86_64 0:1.1.29-1.el7
  libsmbclient.x86_64 0:4.6.2-8.el7       libtalloc.x86_64 0:2.1.9-1.e
  libtdb.x86_64 0:1.3.12-2.el7            libtevent.x86_64 0:0.9.31-1.
  libwbclient.x86_64 0:4.6.2-8.el7        pytalloc.x86_64 0:2.1.9-1.el
  samba-common.noarch 0:4.6.2-8.el7       samba-libs.x86_64 0:4.6.2-8.

完毕!
[root@Server ~]# 
  • 关闭SELinux
[root@Server ~]# vim /etc/selinux/config 
[root@Server ~]# cat /etc/selinux/config | grep -v "#" | grep -v "^$"
SELINUX=disabled 
SELINUXTYPE=targeted 
[root@Server ~]# getenforce 
Disabled
[root@Server ~]# 
  • 修改配置文件关闭SELinux是永久关闭,也可以使用以下命令进行关闭
    • setenforce 0:临时生效,下次重启后,会重新打开SELinux
    • getenforce:查看SELinux的状态
  • 防火墙放行Samba服务
[root@Server ~]# firewall-cmd --permanent --add-service="samba"
success
[root@Server ~]# firewall-cmd --reload 
success
[root@Server ~]# firewall-c
firewall-cmd     firewall-config  
[root@Server ~]# firewall-cmd --list-all
public (default, active)
  interfaces: eno16777736
  sources: 
  services: dhcp dhcpv6-client samba ssh
  ports: 
  masquerade: no
  forward-ports: 
  icmp-blocks: 
  rich rules: 
	
[root@Server ~]#
  • 配置开机自启动Samba服务
[root@Server ~]# systemctl enable smb nmb
ln -s '/usr/lib/systemd/system/smb.service' '/etc/systemd/system/multi-user.target.wants/smb.service'
ln -s '/usr/lib/systemd/system/nmb.service' '/etc/systemd/system/multi-user.target.wants/nmb.service'
[root@Server ~]# systemctl restart smb nmb
  • Samba的服务名是依赖于smbnmb的,所以在启动时,需要启动这两项服务

修改Samba配置文件

  • 创建相关的文件夹,并创建用户和组
[root@Server ~]# mkdir /Public
[root@Server ~]# mkdir /Sale
[root@Server ~]# mkdir /Technology
[root@Server ~]# groupadd Sale
[root@Server ~]# groupadd Technology
[root@Server ~]# useradd -g Sale Sale1
[root@Server ~]# useradd -g Technology Technology1
[root@Server ~]# useradd Boss
  • 修改Samba的配置文件
[root@Server ~]# vim /etc/samba/smb.conf
[root@Server ~]# cat !$ | grep -v "#" | grep -v "^$"
cat /etc/samba/smb.conf | grep -v "#" | grep -v "^$"
[global]
	workgroup = SAMBA
	security = user            //登录的安全模式,user表示需要输入用户名和密码
	passdb backend = tdbsam
	printing = cups
	printcap name = cups
	load printers = yes
	cups options = raw
[Public]
	path = /Public                         //共享文件夹的目录
	writable = yes                         //是否可写
	write list = Bass,Sale1,Technology1      //可写用户列表,如果是用户组,前面需要有"+"
	create mask = 0664                     //创建文件的默认权限
	directory mask = 0775
[Sale]
	path = /Sale
	writable = yes
	write list = Sale1,Boss
	create mask = 0664
	directory mask = 0775
[Technology]
	path = /Technology 
	write list = Boss,Technology1
	create mask = 0664
	directory mask = 0775
[root@Server ~]# 
  • 向Samba服务中添加用户,并重新启动Samba服务
[root@Server ~]# pdbedit -au Boss   //在Samba服务中添加用户Boss
new password:123                    //为用户设置密码,不显示
retype new password:123                //再次输入密码,不显示
Unix username:        Boss
NT username:          
Account Flags:        [U          ]
User SID:             S-1-5-21-252106335-936056488-221418339-1000
Primary Group SID:    S-1-5-21-252106335-936056488-221418339-513
Full Name:            
Home Directory:       \\\\server\\boss
HomeDir Drive:        
Logon Script:         
Profile Path:         \\\\server\\boss\\profile
Domain:               SERVER
Account desc:         
Workstations:         
Munged dial:          
Logon time:           0
Logoff time:          三, 06 22036 23:06:39 CST
Kickoff time:         三, 06 22036 23:06:39 CST
Password last set:    五, 10 122021 17:02:47 CST
Password can change:  五, 10 122021 17:02:47 CST
Password must change: never
Last bad password   : 0
Bad password count  : 0
Logon hours         : FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
[root@Server ~]# pdbedit -au Sale1        //同样的将另外两个用户加入到Samba用户列表中
[root@Server ~]# pdbedit -au Technology1
[root@Server ~]# pdbedit -L   //列出Samba服务的用户列表
Boss:1003: 
Technology1:1002:
Sale1:1001:
[root@Server ~]# systemctl restart smb nmb   //重新启动Samba服务
[root@Server ~]#
  • 为不同的文件夹设置特殊权限
[root@Server ~]# chmod o+x /Public/         //修改共享文件夹的权限
[root@Server ~]# chmod o+x /Sale/
[root@Server ~]# chmod o+x /Technology/
[root@Server ~]# setfacl -m u:Sale1:rwx /Public/             //设置特殊权限,使Sale1可以对共享文件夹进行读写操作
[root@Server ~]# setfacl -m u:Technology1:rwx /Public/
[root@Server ~]# setfacl -m u:Boss:rwx /Public/
[root@Server ~]# setfacl -m u:Boss:rwx /Sale/
[root@Server ~]# setfacl -m u:Boss:rwx /Technology/
[root@Server ~]# setfacl -m g:Sale:rwx /Sale/         //使Sale用户组的用户都可以对Sale文件夹进行读写操作
[root@Server ~]# setfacl -m g:Technology:rwx /Technology/

客户端配置

  • Linux客户端安装Samba客户端
[root@Client ~]# yum -y install samba-client
已加载插件:langpacks, product-id, search-disabled-repos, subscription-manager
This system is not registered with an entitlement server. You can use subscription-manager to register.
正在解决依赖关系
--> 正在检查事务
---> 软件包 samba-client.x86_64.0.4.6.2-8.el7 将被 安装

...

已安装:
  samba-client.x86_64 0:4.6.2-8.el7                                             

作为依赖被安装:
  samba-common-libs.x86_64 0:4.6.2-8.el7                                        

完毕!
[root@Client ~]# 

Linux客户端进行测试

  • Linux客户端进行访问测试
[root@Client ~]# smbclient -L //192.168.43.128 -U Boss
Enter SAMBA\\Boss's password: 
Domain=[SERVER] OS=[Windows 6.1] Server=[Samba 4.6.2]

	Sharename       Type      Comment
	---------       ----      -------
	Public          Disk      
	Sale            Disk      
	Technology      Disk      
	IPC$            IPC       IPC Service (Samba 4.6.2)
Domain=[SERVER] OS=[Windows 6.1] Server=[Samba 4.6.2]

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

	Workgroup            Master
	---------            -------
	SAMBA                SERVER
	WORKGROUP            HASEE-BAD
[root@Client ~]# 
  • 以上可以看到可以正常访问Samba服务器

Linux客户端将Samba服务器的共享目录进行本地挂载

  • 将Samba服务器的共享目录进行本地多用户挂载
    • Public目录以Boss身份登入,挂载到/Samba/Public目录下
    • Sale目录以Sale1身份登入,挂载到/Samba/Sale_Sale1目录下
    • Sale目录以Technology1身份登入,挂载到/Samba/Sale_Technology1目录下
    • Technology目录以Technology1身份登入,挂载到/Samba/Technology目录下
[root@Client ~]# vim /etc/fstab 
[root@Client ~]# cat /etc/fstab | grep -v "#" | grep -v "^$"
/dev/mapper/rhel-root   /                       xfs     defaults        0 0
UUID=303f3e9b-272c-45d4-b73d-54daff254def /boot                   xfs     defaults        0 0
/dev/mapper/rhel-swap   swap                    swap    defaults        0 0
/dev/cdrom /mnt iso9660 defaults 0 0

# 共享目录              挂载点                     username表示以哪个用户进行登录挂载
//192.168.43.128/Public /Samba/Public cifs multiuser,username=Boss,password=123,sec=ntlmssp 0 0
//192.168.43.128/Sale /Samba/Sale_Sale1 cifs multiuser,username=Sale1,password=123,sec=ntlmssp 0 0
//192.168.43.128/Sale /Samba/Sale_Technology1 cifs multiuser,username=Technology1,password=123,sec=ntlmssp 0 0
//192.168.43.128/Technology /Samba/Technology cifs multiuser,username=Technology1,password=123,sec=ntlmssp 0 0

[root@Client ~]# mkdir -p /Samba/Public
[root@Client ~]# mkdir /Samba/Sale_Sale1
[root@Client ~]# mkdir /Samba/Sale_Technology1
[root@Client ~]# mkdir /Samba/Technology
[root@Client ~]# mount -a
[root@Client ~]#
  • 进行权限测试
[root@Client ~]# cd /Samba/
[root@Client Samba]# ls
Public  Sale_Sale1  Sale_Technology1  Technology
[root@Client Samba]# cd Public/
[root@Client Public]# touch file1
[root@Client Public]# ll
总用量 0
-rw-r--r-- 1 1003 1003 0 1211 04:55 file1
[root@Client Public]# cd ..
[root@Client Samba]# cd Sale_Sale1/
[root@Client Sale_Sale1]# touch file1
[root@Client Sale_Sale1]# ll
总用量 0
-rw-r--r-- 1 1001 1001 0 1211 04:56 file1
[root@Client Sale_Sale1]# cd ..
[root@Client Samba]# cd Sale_Technology1/
[root@Client Sale_Technology1]# touch file1
touch: 无法创建"file1": 权限不够
[root@Client Sale_Technology1]# cd ..
[root@Client Samba]# cd Technology/
[root@Client Technology]# touch file1
[root@Client Technology]# ll
总用量 0
-rw-r--r-- 1 1002 1002 0 1211 04:58 file1
[root@Client Technology]# 
  • 如上,在不同的目录中进行了文件权限测试
    • Public目录中,是以Boss身份登入挂载的,所以可以进行读写操作,创建文件的用户UID为1003,可以在服务端看到即为Boss账户的UID
    • Sale_Sale1目录中,是将Sale共享目录以Sale1身份登入挂载的,所以可以进行读写操作,文件的所有者UID为1001,即为Sale1账户的UID
    • Sale_Technology1目录中,是将Sale共享目录以Technology1身份登入挂载的,所以没有权限进行读写操作
    • Technology目录中,是将Technology共享目录以Technology1身份登入挂载的,所以可以进行读写操作,文件的所有者UID为1002,即为Technology1账户的UID
  • 服务器端检查是否创建相应文件以及文件所有者信息
root@Server ~]# cd /Sale/
[root@Server Sale]# ll
总用量 0
-rw-r--r-- 1 Sale1 Sale 0 1211 2021 file1
[root@Server Sale]# cd /Technology/
[root@Server Technology]# ll
总用量 0
-rw-r--r-- 1 Technology1 Technology 0 1211 2021 file1
[root@Server Technology]# cd /Public/
[root@Server Public]# ll
总用量 0
-rw-r--r-- 1 Boss Boss 0 1211 2021 file1
[root@Server Public]# 

Windows客户端进行测试

  • 在Windows客户端进行访问测试
    • 在此电脑中输入\\\\192.168.43.128,进行访问测试
    • Win + R输入\\\\192.168.43.128,进行访问测试
  • 以不同的身份登入到Samba服务器,分别在不同的文件夹中创建文件,查看是否具有权限
  • 创建完成后,可以在服务端,查看相应的目录下是否存在该文件,以及该文件的所有者信息
  • 在Windows客户端访问后,需要切换账户登录,则需要退出登录
    • 在CMD中执行net use * /del /y命令,以清除所有远程


  • 如上则表示可以正常访问Samba服务器,可以使用不同的用户以及设置的密码进行登录测试
  • 如果出现无法访问Samba,则在服务端进行如下配置
    • 将配置的DNS域名注释,并添加search localdomain
[root@Server ~]# cat /etc/resolv.conf
# Generated by NetworkManager
nameserver 8.8.8.8
[root@Server ~]# vim /etc/resolv.conf
[root@Server ~]# cat !$
cat /etc/resolv.conf
# Generated by NetworkManager
#nameserver 8.8.8.8
search localdomain
[root@Server ~]# 
  • 避免下次服务端重启后,又存在无法访问Samba服务的问题,可以将网卡配置文件中的DNS注释掉

以上内容均属原创,如有不详或错误,敬请指出。

以上是关于Samba服务器搭建的主要内容,如果未能解决你的问题,请参考以下文章

Samba远程代码执行漏洞(CVE-2017-7494)复现

linux搭建samba服务器

SAMBA 服务的搭建

SAMBA 服务的搭建

SAMBA服务器搭建

linux下实现文件共享:搭建samba服务器