首页 > 系统相关 >Linux下的samba服务配置详解

Linux下的samba服务配置详解

时间:2023-03-03 15:01:28浏览次数:56  
标签:control samba x86 share 详解 64 Linux root

(Linux下的samba服务配置详解)

一、Samba介绍

1.SMB(Server Messages Block,信息服务块)是一种在局域网上共享文件和打印机的一种通信协议,它为局域网内的不同计算机之间提供文件及打印机等资源的共享服务。 2.Samba是在Linux和UNIX系统上实现SMB协议的一个免费软件,由服务器及客户端程序构成。 3.其他相关概念: CIFS:common internet file system,微软基于SMB发布。 NFS:网络文件系统,英文Network File System(NFS),是由SUN公司研制的UNIX表示层协议(presentation layer protocol),能使使用者访问网络上别处的文件就像在使用自己的计算机一样。

二、Samba工具及特性

1.相关包 ①amba 提供smb服务 ②samba-client 客户端软件 ③samba-common 通用软件 ④cifs-utils smb客户端工具 ⑤samba-winbind 和AD相关

2.相关服务进程 smbd 提供smb(cifs)服务 TCP:139,445 nmbd NetBIOS名称解析 UDP:137,138 3.相关工具及命令 主配置文件:/etc/samba/smb.conf

语法检查: testparm [-v] [/etc/samba/smb.conf]

客户端工具:smbclient,pdnedit -L(列出Samba用户列表,读取passdb.tdb数据库文件)

三、搭建环境介绍

1.使用操作系统为RHEL8.0 2.Samba用户为三个(kitty,buer,alice),都可以浏览共享目录/share/,kitty和buer可以写入文件共享目录。 3.服务端hostname为control,客户端hostname为node1

四、Samba配置步骤

1.服务端操作

①在server端安装软件包

[root@control ~]# yum -y install samba samba-client
Updating Subscription Management repositories.
Unable to read consumer identity
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
Last metadata expiration check: 0:00:36 ago on Sun 02 May 2021 08:10:41 PM CST.
Dependencies resolved.
================================================================================================================================================
 Package                                   Arch                          Version                            Repository                     Size
================================================================================================================================================
Installing:
 samba                                     x86_64                        4.9.1-8.el8                        BaseOS                        708 k
 samba-client                              x86_64                        4.9.1-8.el8                        BaseOS                        636 k
Installing dependencies:
 samba-common-tools                        x86_64                        4.9.1-8.el8                        BaseOS                        461 k
 samba-libs                                x86_64                        4.9.1-8.el8                        BaseOS                        177 k

Transaction Summary
================================================================================================================================================
Install  4 Packages

Total size: 1.9 M
Installed size: 5.6 M
Downloading Packages:
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                                                                                        1/1 
  Installing       : samba-libs-4.9.1-8.el8.x86_64                                                                                          1/4 
  Running scriptlet: samba-libs-4.9.1-8.el8.x86_64                                                                                          1/4 
  Installing       : samba-common-tools-4.9.1-8.el8.x86_64                                                                                  2/4 
  Installing       : samba-4.9.1-8.el8.x86_64                                                                                               3/4 
  Running scriptlet: samba-4.9.1-8.el8.x86_64                                                                                               3/4 
  Installing       : samba-client-4.9.1-8.el8.x86_64                                                                                        4/4 
  Running scriptlet: samba-client-4.9.1-8.el8.x86_64                                                                                        4/4 
  Verifying        : samba-4.9.1-8.el8.x86_64                                                                                               1/4 
  Verifying        : samba-client-4.9.1-8.el8.x86_64                                                                                        2/4 
  Verifying        : samba-common-tools-4.9.1-8.el8.x86_64                                                                                  3/4 
  Verifying        : samba-libs-4.9.1-8.el8.x86_64                                                                                          4/4 
Installed products updated.

Installed:
  samba-4.9.1-8.el8.x86_64     samba-client-4.9.1-8.el8.x86_64     samba-common-tools-4.9.1-8.el8.x86_64     samba-libs-4.9.1-8.el8.x86_64    

Complete!

②创建samba共享目录/share,设置权限,并拷贝文件到共享目录

[root@control ~]# mkdir /share
[root@control ~]# ll /share
total 0
[root@control ~]# ll -ld /share
drwxr-xr-x. 2 root root 6 May  2 20:24 /share
[root@control ~]# chmod 777 /share
[root@control ~]# ll -ld /share
drwxrwxrwx. 2 root root 6 May  2 20:24 /share

[root@control ~]# cp /etc/passwd /share
[root@control ~]# cp /etc/group /share
[root@control ~]# ls /share
group  passwd


③创建samba用户

[root@control ~]# groupadd it
[root@control ~]# useradd -g it kitty
[root@control ~]# useradd alice
[root@control ~]# useradd buer
[root@control ~]# smbpasswd -a kitty
New SMB password:
Retype new SMB password:
Added user kitty.
[root@control ~]# smbpasswd -a alice
New SMB password:
Retype new SMB password:
Added user alice.
[root@control ~]# smbpasswd -a buer
New SMB password:
Retype new SMB password:
Added user buer.

#查看samba用户列表
[root@control ~]# pdbedit -L
kitty:2034:
buer:2036:
alice:2035:

④编辑samba主配置文件 进入配置目录/etc/samba/smb.conf

[share]
      path = /share
      browseable = yes
      write list = @it,buer
      hosts allow = 192.168.200.0/24

测试配置文件语法

[root@control ~]# testparm /etc/samba/smb.conf
Load smb config files from /etc/samba/smb.conf
rlimit_max: increasing rlimit_max (1024) to minimum Windows limit (16384)
Processing section "[homes]"
Processing section "[printers]"
Processing section "[print$]"
Processing section "[share]"
Loaded services file OK.
Server role: ROLE_STANDALONE

Press enter to see a dump of your service definitions

# Global parameters
[global]
	printcap name = cups
	security = USER
	workgroup = SAMBA
	idmap config * : backend = tdb
	cups options = raw


[homes]
	browseable = No
	comment = Home Directories
	inherit acls = Yes
	read only = No
	valid users = %S %D%w%S


[printers]
	browseable = No
	comment = All Printers
	create mask = 0600
	path = /var/tmp
	printable = Yes


[print$]
	comment = Printer Drivers
	create mask = 0664
	directory mask = 0775
	force group = @printadmin
	path = /var/lib/samba/drivers
	write list = @printadmin root


[share]
	hosts allow = 192.168.200.0/24
	path = /share
	write list = @it buer

⑤启动samba服务

[root@control ~]# systemctl start smb
[root@control ~]# systemctl start nmb
[root@control ~]# systemctl enable nmb
Created symlink /etc/systemd/system/multi-user.target.wants/nmb.service → /usr/lib/systemd/system/nmb.service.
[root@control ~]# systemctl enable smb
Created symlink /etc/systemd/system/multi-user.target.wants/smb.service → /usr/lib/systemd/system/smb.service.

⑥防火墙放行

[root@control ~]# firewall-cmd --permanent --add-service=samba
success
[root@control ~]# firewall-cmd --reload 
success

⑦配置selinux安全标签

[root@control ~]# semanage fcontext -a -t samba_share_t '/share(/.*)?'
[root@control ~]# restorecon
restorecon        restorecon_xattr  
[root@control ~]# restorecon -vvFR /share
Relabeled /share from unconfined_u:object_r:default_t:s0 to system_u:object_r:samba_share_t:s0
Relabeled /share/passwd from unconfined_u:object_r:default_t:s0 to system_u:object_r:samba_share_t:s0
Relabeled /share/group from unconfined_u:object_r:default_t:s0 to system_u:object_r:samba_share_t:s0

2.在客户端操作

①客户端安装相关软件包

[root@node1 yum.repos.d]# yum -y install samba-client cifs-utils
Updating Subscription Management repositories.
Unable to read consumer identity
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
Repository AppStream is listed more than once in the configuration
Repository BaseOS is listed more than once in the configuration
AppStream                                                                                                       3.1 MB/s | 3.2 kB     00:00    
BaseOS                                                                                                          2.7 MB/s | 2.7 kB     00:00    
ansiable                                                                                                        0.0  B/s |   0  B     00:00    
Failed to synchronize cache for repo 'ansiable', ignoring this repo.
Dependencies resolved.
================================================================================================================================================
 Package                              Arch                           Version                               Repository                      Size
================================================================================================================================================
Installing:
 cifs-utils                           x86_64                         6.8-2.el8                             BaseOS                          93 k
 samba-client                         x86_64                         4.9.1-8.el8                           BaseOS                         636 k

Transaction Summary
================================================================================================================================================
Install  2 Packages

Total size: 729 k
Installed size: 2.3 M
Downloading Packages:
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                                                                                        1/1 
  Installing       : samba-client-4.9.1-8.el8.x86_64                                                                                        1/2 
  Running scriptlet: samba-client-4.9.1-8.el8.x86_64                                                                                        1/2 
  Installing       : cifs-utils-6.8-2.el8.x86_64                                                                                            2/2 
  Running scriptlet: cifs-utils-6.8-2.el8.x86_64                                                                                            2/2 
  Verifying        : cifs-utils-6.8-2.el8.x86_64                                                                                            1/2 
  Verifying        : samba-client-4.9.1-8.el8.x86_64                                                                                        2/2 
Installed products updated.

Installed:
  cifs-utils-6.8-2.el8.x86_64                                          samba-client-4.9.1-8.el8.x86_64                                         

Complete!

②使用smbclient工具测试kitty和alice用户是否可以查看共享文件夹

[root@node1 yum.repos.d]# smbclient -L //192.168.200.150 -U kitty%redhat

	Sharename       Type      Comment
	---------       ----      -------
	print$          Disk      Printer Drivers
	share           Disk      
	IPC$            IPC       IPC Service (Samba 4.9.1)
	kitty           Disk      Home Directories
Reconnecting with SMB1 for workgroup listing.

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

	Workgroup            Master
	---------            -------
	SAMBA                CONTROL
[root@node1 yum.repos.d]# smbclient -L //192.168.200.150 -U alice%redhat

	Sharename       Type      Comment
	---------       ----      -------
	print$          Disk      Printer Drivers
	share           Disk      
	IPC$            IPC       IPC Service (Samba 4.9.1)
	alice           Disk      Home Directories
Reconnecting with SMB1 for workgroup listing.

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

	Workgroup            Master
	---------            -------
	SAMBA                CONTROL

③创建samba认证用户文件smbur,使用alice作为认证用户

[root@node1 yum.repos.d]# echo "username=alice" >> /etc/samba/smbur.txt
[root@node1 yum.repos.d]# echo "password=redhat" >> /etc/samba/smbur.txt

④编辑/etc/fstab文件,使挂载共享目录永久生效

[root@node1 yum.repos.d]# cat /etc/fstab 

#
# /etc/fstab
# Created by anaconda on Fri Mar 19 22:21:55 2021
#
# Accessible filesystems, by reference, are maintained under '/dev/disk/'.
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info.
#
# After editing this file, run 'systemctl daemon-reload' to update systemd
# units generated from this file.
#
UUID=b7190d80-906f-4b9d-9ab4-5a503ecaea2c /                       xfs     defaults        0 0
UUID=525a30a7-d484-4ed5-9f38-f827f54e29ff /boot                   xfs     defaults        0 0
UUID=e6cf8733-5eec-4942-9429-c3e9087b6ff0 swap                    swap    defaults        0 0


//192.168.200.150/share /media cifs defaults,credentials=/etc/samba/smbur.txt,multiuser,sec=ntlmssp 0 0
[root@node1 yum.repos.d]# df -h
Filesystem               Size  Used Avail Use% Mounted on
devtmpfs                 970M     0  970M   0% /dev
tmpfs                    984M     0  984M   0% /dev/shm
tmpfs                    984M  9.1M  974M   1% /run
tmpfs                    984M     0  984M   0% /sys/fs/cgroup
/dev/nvme0n1p3            18G  4.0G   14G  23% /
/dev/nvme0n1p1           495M  140M  356M  29% /boot
tmpfs                    197M  4.0K  197M   1% /run/user/0
/dev/sr0                 6.7G  6.7G     0 100% /mnt/cdrom
//192.168.200.150/share   13G  8.5G  4.1G  68% /media

五、测试用户的权限情况

1.检查在/media目录下以alice用户挂载权限

[root@node1 yum.repos.d]# cd /media/
[root@node1 media]# touch file1
touch: cannot touch 'file1': Permission denied
[root@node1 media]# ls
group  passwd

2.切换本地admin用户,以kitty和buer这两个samba用户访问测试权限

[root@node1 yum.repos.d]# cd /media/
[root@node1 media]# touch file1
touch: cannot touch 'file1': Permission denied
[root@node1 media]# ls
group  passwd
[root@node1 media]# useradd admin
[root@node1 media]# su - admin
[admin@node1 ~]$ cd /media
[admin@node1 media]$ cifscreds add -u kitty control
Password: 
[admin@node1 media]$ ls
group  passwd
[admin@node1 media]$ touch file1
[admin@node1 media]$ ls
file1  group  passwd
[admin@node1 media]$ cifscreds update  -u buer control
Password: 
[admin@node1 media]$ ls
file1  group  passwd
[admin@node1 media]$ touch file2
[admin@node1 media]$ ls
file1  file2  group  passwd

标签:control,samba,x86,share,详解,64,Linux,root
From: https://blog.51cto.com/u_14664141/6098354

相关文章

  • Linux开发C++
    首先在windows上安装linux系统。VM简介VMwareWorkstation中文版是一个“虚拟PC”软件。它使你可以在一台机器上同时运行二个或更多Windows、DOS、LINUX系统。与“多......
  • 干货!超实用的 Linux 初始化脚本
    咸鱼今天给大家分享一个无论是学习还是工作中都很实用的Linux系统初始化脚本,其实就是各种命令的集合 完整代码在文章最后哦 定义相关变量   配置yum镜像......
  • linux中条件变量和信号量的区别!
    在Linux系统中,条件变量和信号量是非常专业的词汇,也是我们每个人都会接触到的知识,那么Linux中条件变量和信号量有什么区别?以下是具体内容介绍。条件变量条件变......
  • Jmeter 的常见参数化方式详解
    在接口测试和性能测试中,参数化都是一个必须掌握的知识点。参数化的使用场景,例如:多个请求都是同一个ip地址,若服务器地址更换了,则脚本需要更改每个请求的ip注册账号,不允许......
  • IDEA 服务器热部署详解(On Update action/On frame deactivation)
    配置属性:OnUpdateaction:当代码改变的时候,Idea执行什么操作;-Updateresource:如果有更新,并且更新资源为(*.jsp,*.xml,不包括java文件),就会立即生效-Updateclassesandr......
  • 《程序员的自我修养》学习笔记——静态链接详解【第三弹】
    静态链接详解现在对ELF可重定位文件的整体轮廓和某些局部细节都有了一定了解。那么对于两个可重定位文件,如何将其链接起来形成可执行文件呢!以如下两个文件为例:/*main.......
  • Linux 磁盘扩容
    原文链接:https://blog.csdn.net/zzq100zzq/article/details/125178843一、查看系统磁盘1、使用df-hl,查看系统的磁盘使用情况二、linux磁盘扩容当LVM分区空间不足的时......
  • 串口通信原理详解232、422、485,入门必看!
    串口通信原理详解232、422、485,入门必看!混说Linux ​关注他 50人赞同了该文章本文介绍了串口通讯的基本概念、数据格式、通讯方式、典型的串口通讯......
  • Linux运用一些常用命令,优秀的PHPer都需掌握
    作为一名优秀的phper,Linux是必备的一项技能,工作3-5年的基本能明白我讲的道理!今天搜集整理了一些Linux服务器运维常用命令,希望对大家有帮助:1.删除0字节文件 find -type......
  • 教你在Red Hat Linux 9下架设FTP服务器
    现如今越来越多的人选择了开源的Linux操作系统,其不逊于Windows的性能和平易近人的价格是人们选择它的最好理由,而RedHatLinux9是Linux操作系统中使用最广的一种。RedHat......