一、Samba简介
- Samba文件共享服务是一种开源的软件,用于在Linux和Windows操作系统之间共享文件和打印机。它允许Windows客户端通过SMB/CIFS协议访问和共享Linux主机上的文件和打印机。
- Samba可以作为文件服务器运行,让Windows用户能够像访问本地文件夹一样访问Linux主机上的文件夹。此外,Samba还允许Linux主机共享打印机,使Windows用户能够从他们的计算机中打印文件。
- Samba还支持透明地传输文件和打印作业,以及文件和目录的访问控制。它可以与Active Directory集成,方便地管理和控制用户访问权限。
- Samba服务程序现在已经成为在Linux系统与Windows系统之间共享文件的最佳选择。
二、实验
2.1 实验拓扑
如下图,Win1-3主机与Linux主机接入同一网络,Net1网络已开启NAT服务,4台主机互通且可以访问Internet
2.2 实验需求
小王、小张和小李三人是同事,小李是项目主管,分别使用Win1、Win2和Win3主机办公,要求在Net1网络中配置Samba文件共享服务,为三人各自创建一个共享目录,对于小王和小张,要求对自己的共享目录拥有完整权限,而对其他人的共享目录拥有只读权限;对于小李,要求对三人的共享目录均有完整权限
2.3 实验规划
主机 |
主机名 |
IP地址 |
操作系统 |
Linux |
localhost |
172.16.0.100/24 |
Rocky Linux 8.9 (Green Obsidian) |
Win1 |
N/A |
172.16.0.10/24 |
Windows 10 Pro 21H1 |
Win2 |
N/A |
172.16.0.20/24 |
Windows 10 Pro 21H1 |
Win3 |
N/A |
172.16.0.30/24 |
Windows 10 Pro 21H1 |
2.4 配置共享资源
在Linux主机上配置Samba服务,通过软件仓库安装samba服务程序:
[root@localhost ~]# yum -y install samba
查看系统中samba的相关包:
[root@localhost ~]# rpm -qa | grep samba
samba-common-libs-4.18.6-2.el8_9.x86_64
samba-dcerpc-4.18.6-2.el8_9.x86_64
samba-common-tools-4.18.6-2.el8_9.x86_64
samba-common-4.18.6-2.el8_9.noarch
samba-client-libs-4.18.6-2.el8_9.x86_64
samba-libs-4.18.6-2.el8_9.x86_64
samba-ldb-ldap-modules-4.18.6-2.el8_9.x86_64
samba-4.18.6-2.el8_9.x86_64
查看samba主程序的配置文件:
[root@localhost ~]# rpm -qc samba-common
/etc/logrotate.d/samba
/etc/samba/lmhosts
/etc/samba/smb.conf # 我们将要编辑这个配置文件
/etc/sysconfig/samba
Samba服务程序的主配置文件与Apache服务很相似,包括全局配置参数和区域配置参数。全局配置参数用于设置整体的资源共享环境,对里面的每一个独立的共享资源都有效。区域配置参数则用于设置单独的共享资源,且仅对该资源有效。创建共享资源的方法很简单,只要将下表中的参数写入到Samba服务程序的主配置文件中,然后重启该服务即可
smb.conf配置文件的常用参数及作用示例:
参数 |
作用 |
[database] |
共享名称为database |
comment =Do not arbitrarily modify the database file |
警告用户不要随意修改数据库 |
path = /home/database |
共享目录为/home/database |
public = no |
关闭“所有人可见” |
writable = yes |
允许写入操作 |
force create mode = 0755 |
指定创建文件默认具有rwxr-xr-x权限 |
force directory mode = 0755 |
指定创建目录默认具有rwxr-xr-x权限 |
有关“/etc服务器托管网/samba/smb.conf”文件的更多配置参数详解参阅官方文档:
smb.conf — The configuration file for the Samba suite
下面我们按照需求进行配置,在Linux主机中分别创建共享目录“/share/xiaowang”、“/share/xiaozhang”和“/share/xiaoli”
[root@localhost ~]# mkdir -p /share/xiaowang
[root@localhost ~]# mkdir -p /share/xiaozhang
[root@localhost ~]# mkdir -p /share/xiaoli
分别创建用户xiaozhang、xiaowang和xiaoli,配置密码与用户名相同:
[root@localhost ~]# useradd xiaowang
[root@localhost ~]# echo 'xiaowang' | passwd --stdin xiaowang
Changing password for user xiaowang.
passwd: all authentication tokens updated successfully.
[root@localhost ~]# useradd xiaozhang
[root@localhost ~]# echo 'xiaozhang' | passwd --stdin xiaozhang
Changing password for user xiaozhang.
passwd: all authentication tokens updated successfully.
[root@localhost ~]# useradd xiaoli
[root@localhost ~]# echo 'xiaoli' | passwd --stdin xiaoli
Changing password for user xiaoli.
passwd: all authentication tokens updated successfully.
创建组“office”,作为三位用户的附加组:
[root@localhost ~]# groupadd office
[root@localhost ~]# usermod -G office xiaowang
[root@localhost ~]# usermod -G office xiaozhang
[root@localhost ~]# usermod -G office xiaoli
作为系统管理员,我希望三位用户能够看到和进入“/share”目录,但是不能够在“/share”目录下新建和删除文件,防止出现文件管理混乱和共享文件夹被删除的情况发生,所以可以将“/share”目录的所属组更改为“office”,并配置”/share”目录的权限为“0750”
[root@localhost ~]# chgrp -R office /share
[root@localhost ~]# chmod 0750 /share
[root@localhost ~]# ls -l /share
total 0
drwxr-xr-x. 2 root office 6 Dec 24 12:54 xiaoli
drwxr-xr-x. 2 root office 6 Dec 24 12:54 xiaowang
drwxr-xr-x. 2 root office 6 Dec 24 12:54 xiaozhang
[root@localhost ~]# ls -ld /share
drwxr-x---. 5 root office 53 Dec 24 12:54 /share
配置共享目录的SELinux安全上下文,如果SELinux已关闭,这一步可忽略:
[root@localhost ~]# chcon -R -t samba_share_t /share
[root@localhost ~]# ls -lZ /share
total 0
drwxr-xr-x. 2 root office unconfined_u:object_r:samba_share_t:s0 6 Dec 24 12:54 xiaoli
drwxr-xr-x. 2 root office unconfined_u:object_r:samba_share_t:s0 6 Dec 24 12:54 xiaowang
drwxr-xr-x. 2 root office unconfined_u:object_r:samba_share_t:s0 6 Dec 24 12:54 xiaozhang
[root@localhost ~]# ls -ldZ /share
drwxr-x---. 5 root office unconfined_u:object_r:samba_share_t:s0 53 Dec 24 12:54 /share
设置SELinux服务与策略,使其允许通过Samba服务程序访问普通用户家目录。执行getsebool命令,筛选出所有与Samba服务程序相关的SELinux域策略,根据策略的名称(和经验)选择出正确的策略条目进行开启即可:
[root@localhost ~]# getsebool -a | grep samba
samba_create_home_dirs --> off
samba_domain_controller --> off
samba_enable_home_dirs --> off
samba_export_all_ro --> off
samba_export_all_rw --> off
samba_load_libgfapi --> off
samba_portmapper --> off
samba_run_unconfined --> off
samba_share_fusefs --> off
samba_share_nfs --> off
sanlock_use_samba --> off
tmpreaper_use_samba --> off
use_samba_home_dirs --> off
virt_use_samba --> off
[root@localhost ~]# setsebool -P samba_enable_home_dirs on
创建用于访问共享资源的账户信息。在RHEL 8系统中,Samba服务程序默认使用的是用户密码认证模式(user)。这种认证模式可以确保仅让有密码且受信任的用户访问共享资源,而且认证过程也十分简单。不过,只有建立账户信息数据库之后,才能使用用户密码认证模式。另外,Samba服务程序的数据库要求账户必须在当前系统中已经存在,否则日后创建文件时将导致文件的权限属性混乱不堪,由此引发错误
pdbedit命令用于管理Samba服务程序的账户信息数据库,格式为“pdbedit [选项]账户”。在第一次把账户信息写入到数据库时需要使用-a参数,以后在执行修改密码、删除账户等操作时就不再需要该参数了。pdbedit命令中使用的参数以及作用如下表所示:
参数 |
作用 |
-a 用户名 |
建立Samba用户 |
-x 用户名 |
删除Samba用户 |
-L |
列出用户列表 |
-Lv |
列出用户详细信息的列表 |
[root@localhost ~]# pdbedit -a -u xiaowang
new password:此处输入该账户在Samba服务数据库中的密码
retype new password:再次输入密码进行确认
Unix username: xiaowang
NT username:
Account Flags: [U ]
User SID: S-1-5-21-2640330995-3986422831-213597558-1000
Primary Group SID: S-1-5-21-2640330995-3986422831-213597558-513
Full Name:
Home Directory: LOCALHOSTxiaowang
HomeDir Drive:
Logon Script:
Profile Path: LOCALHOSTxiaowangprofile
Domain: LOCALHOST
Account desc:
Workstations:
Munged dial:
Logon time: 0
Logoff time: Wed, 06 Feb 2036 23:06:39 CST
Kickoff time: Wed, 06 Feb 2036 23:06:39 CST
Password last set: Sun, 24 Dec 2023 13:48:50 CST
Password can change: Sun, 24 Dec 2023 13:48:50 CST
Password must change: never
Last bad password : 0
Bad password count : 0
Logon hours : FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
[root@localhost ~]#
[root@loc服务器托管网alhost ~]# pdbedit -a -u xiaozhang
new password:
retype new password:
Unix username: xiaozhang
NT username:
Account Flags: [U ]
User SID: S-1-5-21-2640330995-3986422831-213597558-1001
Primary Group SID: S-1-5-21-2640330995-3986422831-213597558-513
Full Name:
Home Directory: LOCALHOSTxiaozhang
HomeDir Drive:
Logon Script:
Profile Path: LOCALHOSTxiaozhangprofile
Domain: LOCALHOST
Account desc:
Workstations:
Munged dial:
Logon time: 0
Logoff time: Wed, 06 Feb 2036 23:06:39 CST
Kickoff time: Wed, 06 Feb 2036 23:06:39 CST
Password last set: Sun, 24 Dec 2023 13:49:46 CST
Password can change: Sun, 24 Dec 2023 13:49:46 CST
Password must change: never
Last bad password : 0
Bad password count : 0
Logon hours : FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
[root@localhost ~]#
[root@localhost ~]# pdbedit -a -u xiaoli
new password:
retype new password:
Unix username: xiaoli
NT username:
Account Flags: [U ]
User SID: S-1-5-21-2640330995-3986422831-213597558-1002
Primary Group SID: S-1-5-21-2640330995-3986422831-213597558-513
Full Name:
Home Directory: LOCALHOSTxiaoli
HomeDir Drive:
Logon Script:
Profile Path: LOCALHOSTxiaoliprofile
Domain: LOCALHOST
Account desc:
Workstations:
Munged dial:
Logon time: 0
Logoff time: Wed, 06 Feb 2036 23:06:39 CST
Kickoff time: Wed, 06 Feb 2036 23:06:39 CST
Password last set: Sun, 24 Dec 2023 13:50:15 CST
Password can change: Sun, 24 Dec 2023 13:50:15 CST
Password must change: never
Last bad password : 0
Bad password count : 0
Logon hours : FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
下面修改“/etc/samba/smb.conf”配置文件:
[root@localhost ~]# vim /etc/samba/smb.conf
# See smb.conf.example for a more detailed config file or
# read the smb.conf manpage.
# Run 'testparm' to verify the config is correct after
# you modified it.
#
# Note:
# SMB1 is disabled by default. This means clients without support for SMB2 or
# SMB3 are no longer able to connect to smbd (by default).
[global]
workgroup = SAMBA
security = user
passdb backend = tdbsam
[share_xiaowang]
comment = share for xiaowang
path = /share/xiaowang
public = yes
writable = yes
[share_xiaozhang]
comment = share for xiaozhang
path = /share/xiaozhang
public = yes
writable = yes
[share_xiaoli]
comment = share for xiaoli
path = /share/xiaoli
public = yes
writable = yes
启动samba服务,并设置开机自启:
[root@localhost ~]# systemctl enable --now smb.service
Created symlink /etc/systemd/system/multi-user.target.wants/smb.service → /usr/lib/systemd/system/smb.service.
防火墙放行samba服务:
[root@localhost ~]# firewall-cmd --add-service=samba --permanent
success
[root@localhost ~]# firewall-cmd --reload
success
2.5 客户机登录验证
登录Win1主机,打开文件资源管理器,访问“172.16.0.100,输入小王的用户名和密码”
登录成功,查看到三个共享目录
此处鼠标右键查看,发现没有新建文件的选项,也不能对共享目录进行删除
进入“share_xiaowang”目录,新建一个文件,没有权限,访问被拒绝
此时我们需要在Linux主机上更改一下共享目录的拥有者:
[root@localhost ~]# ls -l /share/
total 0
drwxr-xr-x. 2 root office 6 Dec 24 12:54 xiaoli
drwxr-xr-x. 2 root office 6 Dec 24 12:54 xiaowang
drwxr-xr-x. 2 root office 6 Dec 24 12:54 xiaozhang
[root@localhost ~]# chown xiaowang /share/xiaowang
[root@localhost ~]# chown xiaozhang /share/xiaozhang
[root@localhost ~]# chown xiaoli /share/xiaoli
[root@localhost ~]# ls -l /share/
total 0
drwxr-xr-x. 2 xiaoli office 6 Dec 24 12:54 xiaoli
drwxr-xr-x. 2 xiaowang office 6 Dec 24 12:54 xiaowang
drwxr-xr-x. 2 xiaozhang office 6 Dec 24 12:54 xiaozhang
再次尝试新建一个文件,点击“Try again”
返回Linux主机上查看文件属性,发现新建的文件并没有继承上级目录”/share“的所属组:
[root@localhost ~]# ls -l /share
total 0
drwxr-xr-x. 2 xiaoli office 6 Dec 24 12:54 xiaoli
drwxr-xr-x. 3 xiaowang office 53 Dec 24 14:13 xiaowang
drwxr-xr-x. 2 xiaozhang office 6 Dec 24 12:54 xiaozhang
[root@localhost ~]#
[root@localhost ~]# cd /share/xiaowang
[root@localhost xiaowang]# ls -l
total 0
drwxr-xr-x. 2 xiaowang xiaowang 6 Dec 24 14:13 'New folder'
-rwxr--r--. 1 xiaowang xiaowang 0 Dec 24 14:12 'New Text Document.txt'
对共享目录添加SGID权限,实现新建文件继承拥有组:
[root@localhost share]# ls -l
total 0
drwxr-xr-x. 2 xiaoli office 6 Dec 24 12:54 xiaoli
drwxr-xr-x. 3 xiaowang office 53 Dec 24 14:27 xiaowang
drwxr-xr-x. 2 xiaozhang office 6 Dec 24 12:54 xiaozhang
[root@localhost share]#
[root@localhost share]# chmod g+s xiaowang
[root@localhost share]# chmod g+s xiaozhang
[root@localhost share]# chmod g+s xiaoli
[root@localhost share]# ls -l
total 0
drwxr-sr-x. 2 xiaoli office 6 Dec 24 12:54 xiaoli
drwxr-sr-x. 3 xiaowang office 53 Dec 24 14:27 xiaowang
drwxr-sr-x. 2 xiaozhang office 6 Dec 24 12:54 xiaozhang
再次新建文件和目录查看属性,自动继承拥有组,且目录继承了SGID权限:
[root@localhost share]# ls -l xiaowang/
total 0
drwxr-xr-x. 2 xiaowang xiaowang 6 Dec 24 14:13 'New folder'
drwxr-sr-x. 2 xiaowang office 6 Dec 24 14:34 'New folder (2)'
-rwxr--r--. 1 xiaowang office 0 Dec 24 14:34 'New Text Document (2).txt'
-rwxr--r--. 1 xiaowang xiaowang 0 Dec 24 14:12 'New Text Document.txt'
如果小王进入小张的共享目录,误操作点击了新建文件,提示权限拒绝:
小张登录Win2主机,访问共享目录,尝试在自己的目录下新建一个文本文件,并在文件内编辑一些内容:
当小王访问小张的共享目录时,可以查看到小张的文件内容,而没有权限删除:
还有最后一个需求没有满足,需要让小李对小王和小张的共享目录有完整的权限,也就是可以对他们的文件进行新增、编辑和删除,可以用ACL权限来实现:
[root@localhost share]# setfacl -m u:xiaoli:rwx xiaowang/
[root@localhost share]# setfacl -m u:xiaoli:rwx xiaozhang/
[root@localhost share]#
[root@localhost share]# getfacl xiaowang/
# file: xiaowang/
# owner: xiaowang
# group: office
# flags: -s-
user::rwx
user:xiaoli:rwx
group::r-x
mask::rwx
other::r-x
[root@localhost share]# getfacl xiaozhang/
# file: xiaozhang/
# owner: xiaozhang
# group: office
# flags: -s-
user::rwx
user:xiaoli:rwx
group::r-x
mask::rwx
other::r-x
小李登录win3主机,访问共享目录,清空小王目录下的文件,然后分别在各自的目录下新建一个文本文件,并编辑一些内容:
到此,已满足实验需求。另外,对于一台Win主机访问同一个网络地址,Windows系统只会在第一次登录时询问用户名和密码,如果实验资源紧缺,希望在一台Win主机上验证多个用户的权限,可以在“cmd”命令行中输入“net use * /delete”删除访问记录:
参阅
samba.org/samba/docs/man/
第12章 使用Samba或NFS实现文件共享 | 《Linux就该这么学》
服务器托管,北京服务器托管,服务器租用 http://www.fwqtg.net
机房租用,北京机房租用,IDC机房托管, http://www.fwqtg.net
特效 源码 index.html Input Text Animation First Name Last Name 服务器托管网 css @import服务器托管网 url(‘https://fonts.googleapis.com/cs服务器托管,北京服务…