如何在 Linux/Windows 上安装 Samba

什么是桑巴?

在开始安装之前,让我们先了解一下 Samba 是什么以及如何使用它。 Samba 是一个免费的开源软件,它允许在 Windows 和 Linux 系统之间简单轻松地共享文件。 确切地说,它是 SMB/CIFS 协议的开源实现。

(SMB) 服务器消息块协议是一种客户端-服务器通信协议,用于共享对网络上的文件、打印机、串行端口和其他资源的访问。 (CIFS) 通用 Internet 文件系统协议是 SMB 协议的一种方言。 描述协议的不同变体的消息包集合称为方言。

简而言之,在使用 Samba 时,我们可以在 Linux/Unix 服务器和 Windows 客户端之间共享文件和打印服务、使用身份验证和授权、名称解析和服务公告。

使用 Samba 可以完成五项基本任务。

  • 我们可以与 Windows 机器共享一个 Linux 驱动器。
  • 我们可以使用 Linux 机器访问 SMB 共享。
  • 我们可以与 Windows 机器共享一台 Linux 打印机。
  • 我们可以与 Linux 机器共享一台 Windows 打印机。
  • 我们可以在 Unix/Linux 服务器上设置域控制器,并将 Windows 客户端集成到域控制器。

Samba 还可以充当域控制器或常规域成员。 如果您有一个包含 Windows 和 Linux 计算机的混合网络环境,则此选项使 Samba 几乎成为必备软件。

先决条件

  • 为了安装 Samba,我们需要以用户身份登录到我们的 Linux 服务器 sudo 权限,或作为 root 用户。
  • 为了简化本教程中的步骤,我们将使用 root 用户。 如果您决定继续并以您的用户身份安装软件(使用 sudo 特权),不要忘记输入“sudo” 命令在每个命令的开头。
  • 我们还需要一台 Windows 计算机来连接/连接

在 CentOS 7 上安装 Samba

由于 CentOS 和其他基于 RedHat 的发行版上的安装大多使用 yum 工具进行最佳管理,我们将使用它在 CentOS 7 上安装 Samba 并使我们的系统保持最新。

首先,让我们删除所有可能仍缓存在我们系统中的包。 要一次从任何启用的存储库中清除所有缓存文件,我们将使用以下命令。

[root@host ~]# yum clean all

接下来,让我们确保我们所有的系统包都已更新。 我们使用 -y 标志来自动确认任何提示。

[root@host ~]# yum -y update

现在,我们可以继续安装 Samba。 可以使用以下命令轻松安装 Samba。 请注意我们如何再次使用 -y 标志来自动确认安装提示。

[root@host ~]# yum -y install samba samba-client samba-common

Yum 是一个非常棒的工具,它会自动安装 Samba 所需的任何依赖项。 在安装输出结束时,您应该会看到与此输出类似的内容。

Installed:
  samba.x86_64 0:4.9.1-10.el7_7 samba-client.x86_64 0:4.9.1-10.el7_7 samba-common.noarch 0:4.9.1-10.el7_7

Dependency Installed:
  libsmbclient.x86_64 0:4.9.1-10.el7_7 libwbclient.x86_64 0:4.9.1-10.el7_7
  samba-client-libs.x86_64 0:4.9.1-10.el7_7 samba-common-libs.x86_64 0:4.9.1-10.el7_7
  samba-common-tools.x86_64 0:4.9.1-10.el7_7 samba-libs.x86_64 0:4.9.1-10.el7_7

Complete!

安装完成后,我们可以检查我们的 Samba 版本。

[root@host ~]# smbd --version
Version 4.9.1

配置 Samba

安装 Samba 后,我们需要对其进行配置以符合我们的设置和标准。 在我们开始在我们的 Linux 机器上配置 Samba 之前,我们需要在我们的 Windows 计算机上验证工作组。 为此,您可以右键单击“这台电脑”或“我的电脑”→属性→高级系统设置→计算机名称,这将显示以下窗口,其中包含我们需要的数据

或者,您可以运行 cmd(从 Windows 开始框中)并运行以下命令。

net config workstation

此命令将显示我们需要的有关工作站域的信息。 你会看到这样的东西。

现在我们有了关于我们的 Windows 计算机工作组的信息,我们可以继续在我们的 Linux 系统上进行 Samba 配置。

Samba 配置文件可以在 /etc/samba/smb.conf 中找到。 在我们进行任何更改之前,请确保创建原始配置文件的备份副本。

要创建原始配置文件 smb.conf 的备份,我们将使用以下命令创建一个名为 smb.conf_orig 的备份副本。

[root@host ~]# cp /etc/samba/smb.conf /etc/samba/smb.conf_orig

现在我们已经为编辑做好了充分的准备。 我们将从为匿名文件共享服务配置 Samba 开始。 在此共享中,任何用户都将能够读取或写入。 我们将首先创建一个名为“anonymous_shared_directory”的目录,我们的文件将在其中存储。

[root@host ~]# mkdir -p /samba/anonymous_shared_directory

接下来,我们需要对我们的目录应用适当的权限。

[root@host ~]# chmod -R 0775 /samba/anonymous_shared_directory
[root@host ~]# chown -R nobody:nobody /samba/anonymous_shared_directory

如果您像我们一样使用 SELinux,则需要更改 samba 共享目录的 SELinux 安全上下文。

[root@host ~]# chcon -t samba_share_t /samba/anonymous_shared_directory

接下来,要在我们的配置文件中进行所需的更改,我们需要使用首选的 CLI 文本编辑器(Nano 或 Vim)打开 /etc/samba/smb.conf 文件。

[root@host ~]# vim /etc/samba/smb.conf

现在我们将通过添加和编辑以下指令来配置我们的匿名共享

[global]
        workgroup = WORKGROUP
        security = user
        netbios name = centos7
        printcap name = cups
        idmap config * : backend = tdb
        cups options = raw
        map to guest = bad user

[Anonymous]
        comment = anonymous file share
        path = /samba/anonymous_shared_directory
        browseable = yes
        writable = yes
        guest ok = yes
        guest only = yes
        read only = no
        force user = nobody

我们的 Samba 配置文件现在应该如下所示。

[root@host samba]# cat /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.

[global]
        workgroup = WORKGROUP
        security = user
        netbios name = centos7
        printcap name = cups
        idmap config * : backend = tdb
        cups options = raw
        map to guest = bad user

[Anonymous]
        comment = anonymous file share
        path = /samba/anonymous_shared_directory
        browseable = yes
        writable = yes
        guest ok = yes
        guest only = yes
        read only = no
        force user = nobody

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

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

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

接下来,不要忘记保存所做的更改并 close 文本编辑器。 要验证我们当前的 samba 设置,我们将运行以下命令。

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

现在,按 Enter 键查看服务定义的转储。

在我们继续启动 Samba 服务之前,我们需要配置我们的防火墙以使用它。 Samba 需要打开以下端口才能正常运行。

Port 137 (TCP) - netbios-ns - NETBIOS Name Service
Port 138 (TCP) - netbios-dgm - NETBIOS Datagram Service
Port 139 (TCP) - netbios-ssn - NETBIOS session service
Port 445 (TCP) - microsoft-ds - if you are using Active Directory

其他端口:

Port 389 (TCP) - for LDAP (Active Directory Mode)
Port 445 (TCP) - NetBIOS was moved to 445 after 2000 and beyond, (CIFS)
Port 901 (TCP) - for SWAT service (not related to client communication)

我们如何为 Samba 服务打开这些端口取决于您的 Linux 服务器上的防火墙类型。 由于我们的大多数 Linux 服务器都使用 csf 防火墙,因此我们将从配置 csf.conf 文件开始。 让我们使用您选择的文本编辑器打开我们的 csf 配置文件。

[root@host ~]# vim /etc/csf/csf.conf

现在,确保将端口添加到相应的部分。

# Allow incoming TCP ports
TCP_IN = "20,21,25,53,80,110,139,143,443,445,465,587,993,995,53835"
# Allow outgoing TCP ports
TCP_OUT = "20,21,25,53,80,110,139,143,113,443,445,587,993,995,53835"

最后,保存更改并重新加载防火墙规则。

[root@host ~]# csf -r

如果您正在运行 firewalld 服务,只需使用以下命令添加服务。

[root@host ~]# firewall-cmd --permanent --zone=public --add-service=samba

现在,重新加载 firewalld 服务。

[root@host ~]# firewall-cmd --reload

最后,让我们启动 Samba 服务并确保它在系统启动时自动启动。 Samba 使用两个系统服务来运行:smb.service 和 nmb.service。

smbd 服务实现文件共享和打印服务,侦听端口 139 和 445。 nmbd 服务实现对客户端的 NetBIOS over IP 命名服务并侦听端口 137。

让我们从以下命令开始

[root@host ~]# systemctl start smb.service
[root@host ~]# systemctl start nmb.service

为了确保这些服务在系统启动时自动启动,我们需要启用它们

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

要测试我们新实现的匿名共享,我们将转到我们的 Windows 计算机并单击开始 -> 运行,我们将输入服务器 IP 地址,如下所示。

接下来,按 OK,我们的 Anonymous 目录将显示在我们的文件资源管理器中。

现在,双击访问该目录,然后右键单击创建一个新的文本文档。

选择一个文件名,然后保存。 为此,我们将文件命名为 testfile。

创建文件后,让我们确保它在我们的 Linux 机器上也可用

[root@host ~]# [root@host ~]# ls -l /samba/anonymous_shared_directory
total 0
-rwxr--r--. 1 nobody nobody 0 Mar 30 22:03 testfile.txt

我们已经了解了如何配置 Samba 以使用匿名共享,让我们继续讨论安全共享。

为私有共享配置 Samba

这种类型的共享需要用户名和密码才能访问,因此我们需要创建一个允许访问我们共享的用户/用户组。 一旦我们为 Samba 用户创建了一个组,我们就可以轻松地一次性为所有用户分配任何所需的权限。

要创建一个新组,我们将使用以下命令

[root@host ~]# groupadd sambausergroup

我们现在已经成功地为我们的用户创建了一个名为“sambausergroup”的组。

让我们继续创建我们的用户。

我们将其命名为“newtestuser”,我们将在一个命令中将其分配给我们的组,同时拒绝他的 shell 访问(因为 Samba 共享不需要它或它的密码),这样我们可以确保更多的安全性)。

Samba 用户帐户与系统帐户是分开的,因此我们打算仅授予 Samba 权限的用户不需要 shell 访问/密码。 但是,如果您决定合并,您可以安装“桑巴-winbind” 软件包,用于将系统用户和密码与 samba 用户数据库同步,以及其他功能。

要添加我们的用户,我们将使用以下命令

[root@host ~]# useradd -s /sbin/nologin -g sambausergroup newtestuser

请注意我们如何使用 -s 标志拒绝我们的用户 shell 访问,然后我们使用第二个标志 -g 将其分配给我们的组。

我们的用户还需要一件事来访问我们的共享,那就是密码,所以让我们为他创建一个 Samba 访问密码。

[root@host ~]# smbpasswd -a newtestuser
New SMB password:
Retype new SMB password:
Added user newtestuser.

这个受密码保护的共享的 samba 用户现在需要一个地方来存储他们的文件,所以我们需要为他们创建一个目录。这个目录将被称为“password_protected_directory”

[root@host samba]# mkdir -p /samba/password_protected_directory

来自我们创建的“sambausergroup”组中的用户,将需要读取、写入和执行此目录中文件的权限,因此我们将使用以下命令授予他们这些权限。

root@host samba]# setfacl -R -m "g:sambausergroup:rwx" /samba/password_protected_directory

我们还需要在这个上应用 SELinux 安全上下文

[root@host samba]# chcon -t samba_share_t /samba/password_protected_directory/

我们现在可以继续配置此共享。 打开 samba 配置文件并为我们的私有共享添加设置

[root@host ~]# vim /etc/samba/smb.conf
[Password Protected]
		comment = needs username and password to access
		path = /samba/password_protected_directory
		valid users = @sambausergroup
		guest ok = no
		writable = yes
		browsable = yes

完整的配置文件现在将如下所示

# 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.

[global]
        workgroup = WORKGROUP
        security = user
        netbios name = centos7
        printcap name = cups
        idmap config * : backend = tdb
        cups options = raw
        map to guest = bad user

[Password Protected]
        comment = needs username and password to access
        path = /samba/password_protected_directory
        valid users = @sambausergroup
        guest ok = no
        writable = yes
        browsable = yes

[Anonymous]
        comment = anonymous file share
        path = /samba/anonymous_shared_directory
        browseable = yes
        writable = yes
        guest ok = yes
        guest only = yes
        read only = no
        force user = nobody

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

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

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

保存更改后,让我们使用“测试参数” 命令。

[root@host samba]# testparm
rlimit_max: increasing rlimit_max (1024) to minimum Windows limit (16384)
Registered MSG_REQ_POOL_USAGE
Registered MSG_REQ_DMALLOC_MARK and LOG_CHANGED
Load smb config files from /etc/samba/smb.conf
rlimit_max: increasing rlimit_max (1024) to minimum Windows limit (16384)
Processing section "[Password Protected]"
Processing section "[Anonymous]"
Processing section "[homes]"
Processing section "[printers]"
Processing section "[print$]"
Loaded services file OK.
Server role: ROLE_STANDALONE

Press enter to see a dump of your service definitions

就在我们测试我们的新共享之前,让我们重新启动 Samba 服务以确保我们所做的更改生效。

[root@host samba]# systemctl restart smb nmb

为了测试,让我们再次从我们的 Windows 计算机连接并单击开始 -> 运行并输入我们的服务器 IP 服务器IP ->好的

输入.server.ip

现在,我们将拥有两个可用的目录,我们的匿名目录和受密码保护的目录

可用的目录

双击我们的密码保护目录将打开以下提示,我们需要在其中输入我们的“newtestuser”凭据。

输入.network.credentials

为了确保一切正常,让我们在受密码保护的目录中创建一个新目录。

创建.new.directory

为此,该目录将被命名为“test_directory”。

测试目录

最后让我们从我们的 Linux 机器上确认我们的目录是否存在。

[root@host /]# ls -l /samba/password_protected_directory/
total 0
drwxr-xr-x. 2 newtestuser sambausergroup 6 Apr  3 21:12 test_directory

结论

我们已经了解了 Samba 是什么、如何安装它以及如何配置和使用它进行文件共享,但 Samba 作为一个免费和开源软件带来了更多。 您可以配置和使用它以一种或另一种方式从 Linux 访问连接到 Windows 系统的打印机,也可以将其设置为可以集成 Linux 服务器的 Active Directory 域控制器。

在连接方面,Samba 非常灵活,您可以从 Linux 计算机、Windows 计算机以及 macOS 连接到 Samba。

由于设置 Samba 既快速又简单,如果您想在 Windows 和 Linux 机器的混合网络环境中轻松共享文件或打印机,这绝对是值得考虑的事情。

如果这篇文章看起来冗长而复杂,或者如果您对所采取的某些步骤感到不舒服,我们会在这里为您提供帮助。 立即致电 800.580.4985 联系我们,或与我们进行聊天或咨询,与我们一位知识渊博、经验丰富的托管顾问交谈!