# 32.11.用于 Microsoft® Windows® 客户端的文件和打印服务（Samba）

Samba 是一个流行的开源软件包，提供使用 SMB/CIFS 协议的文件和打印服务。该协议内置于 Microsoft® Windows® 系统中。通过安装 Samba 客户端库，非 Microsoft® Windows® 系统也可以使用该协议。该协议允许客户端访问共享的数据和打印机。这些共享可以映射为本地磁盘驱动器，共享的打印机也可以像本地打印机一样使用。

在 FreeBSD 上，可以使用 [net/samba416](https://cgit.freebsd.org/ports/tree/net/samba416/) Port 或包来安装 Samba 客户端库。客户端允许 FreeBSD 系统访问 Microsoft® Windows® 网络中的 SMB/CIFS 共享。

FreeBSD 系统也可以通过安装相同的 [net/samba416](https://cgit.freebsd.org/ports/tree/net/samba416/) Port 或包配置为 Samba 服务器。这样管理员就可以在 FreeBSD 系统上创建 SMB/CIFS 共享，供运行 Microsoft® Windows® 或 Samba 客户端库的客户端访问。

## 32.11.1. 服务器配置

Samba 在 **/usr/local/etc/smb4.conf** 中进行配置。必须先创建此文件才能使用 Samba。

以下是一个简单的 **smb4.conf** 文件，用于在工作组中与 Windows® 客户共享目录和打印机。对于涉及 LDAP 或 Active Directory 的更复杂的设置，可以使用 [samba-tool(8)](https://man.freebsd.org/cgi/man.cgi?query=samba-tool\&sektion=8\&format=html) 来创建初始的 **smb4.conf** 文件。

```ini
[global]
workgroup = WORKGROUP
server string = Samba Server Version %v
netbios name = ExampleMachine
wins support = Yes
security = user
passdb backend = tdbsam

# 示例：仅允许 'developer' 用户访问 /usr/src
[src]
path = /usr/src
valid users = developer
writable  = yes
browsable = yes
read only = no
guest ok = no
public = no
create mask = 0666
directory mask = 0755
```

### 32.11.1.1. 全局设置

在 **/usr/local/etc/smb4.conf** 中添加的描述网络的设置：

`workgroup`\
要服务的工作组的名称。

`netbios name`\
Samba 服务器的 NetBIOS 名称。默认情况下，它与主机的 DNS 名称的第一个组件相同。

`server string`\
将在 `net view` 和一些其他网络工具的输出中显示的字符串，供这些工具显示有关服务器的描述性文本。

`wins support`\
是否让 Samba 充当 WINS 服务器。不要在网络中的多个服务器上启用 WINS 支持。

### 32.11.1.2. 安全设置

**/usr/local/etc/smb4.conf** 中最重要的设置是安全模型和后端密码格式。这些指令控制了以下选项：

`security`\
如果客户端使用与其在 FreeBSD 机器上的用户名相同的用户名，则应使用用户级安全性。`security = user` 是默认的安全策略，它要求客户端先登录才能访问共享资源。

参考 [smb.conf(5)](https://man.freebsd.org/cgi/man.cgi?query=smb.conf\&sektion=5\&format=html) 了解有关 `security` 选项的其他支持设置。

`passdb backend`\
Samba 有几种不同的后端身份验证模型。客户端可以通过 LDAP、NIS+、SQL 数据库或修改过的密码文件进行身份验证。推荐的身份验证方法是 `tdbsam`，它适用于简单的网络，并在此进行介绍。对于较大或更复杂的网络，建议使用 `ldapsam`。`smbpasswd` 是以前的默认方法，现在已不再使用。

### 32.11.1.3. Samba 用户

FreeBSD 用户账户必须映射到 `SambaSAMAccount` 数据库，才能让 Windows® 客户端访问共享。使用 [pdbedit(8)](https://man.freebsd.org/cgi/man.cgi?query=pdbedit\&sektion=8\&format=html) 将现有的 FreeBSD 用户账户映射到 Samba：

```sh
# pdbedit -a -u username
```

本节仅提到了一些最常用的设置。有关可用配置选项的更多信息，请参考 [官方 Samba Wiki](https://wiki.samba.org/)。

## 32.11.2. 启动 Samba

要在启动时启用 Samba，请将以下行添加到 **/etc/rc.conf** 中：

```sh
samba_server_enable="YES"
```

要立即启动 Samba，可以执行：

```sh
# service samba_server start
Performing sanity check on Samba configuration: OK
Starting nmbd.
Starting smbd.
```

Samba 由三个独立的守护进程组成。`nmbd` 和 `smbd` 守护进程都由 `samba_enable` 启动。如果还需要 winbind 名称解析，请设置：

```sh
winbindd_enable="YES"
```

要随时停止 Samba，可以执行：

```sh
# service samba_server stop
```

Samba 是一个功能复杂的软件套件，提供与 Microsoft® Windows® 网络的广泛集成功能。有关超出这里描述的基本配置功能的更多信息，请参考 [https://www.samba.org](https://www.samba.org/)。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://handbook.bsdcn.org/di-32-zhang-wang-luo-fu-wu-qi/32.11.-yong-yu-microsoft-windows-ke-hu-duan-de-wen-jian-he-da-yin-fu-wu-samba.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
