Ubuntu 22.04 安装 Samba 4.15.13 指南
从 0 到 1 在 Ubuntu 22.04 上安装部署 Samba 4.15.13:apt 在线与 deb 离线两种方案、匿名与认证共享配置、smbclient 与 cifs 挂载读写闭环验证、ufw 防火墙放行、升级与卸载清理,全部命令与回显均在双机环境实测。
Ubuntu 22.04 安装 Samba 4.15.13 指南
目标:在一台全新 Ubuntu 22.04 服务器上从 0 到 1 装好 Samba 4.15.13,建起匿名与认证两个共享,并用 Linux 客户端(smbclient 与 cifs 挂载)完成读写、删文件的闭环验证。全部命令与输出均在真实服务器实测,版本号、IP、路径、回显逐行照抄。
一、环境信息
| 项目 | 值 |
|---|---|
| 服务端操作系统 | Ubuntu 22.04.5 LTS(x86_64) |
| 服务端主机名 / IP | fileserver / 10.89.56.10 |
| Samba 版本 | samba 2:4.15.13+dfsg-0ubuntu1.13(smbd --version 显示 Version 4.15.13-Ubuntu) |
| 客户端 | Ubuntu 22.04.5 LTS,主机名 client,IP 10.89.56.20 |
| 客户端工具 | smbclient 2:4.15.13+dfsg-0ubuntu1.13、cifs-utils 2:6.14-1ubuntu0.7 |
| 防火墙 | ufw 0.36.1 |
| 配置文件 | /etc/samba/smb.conf |
| 共享目录 | /srv/samba/public(匿名)、/srv/samba/team(认证) |
| 日志 | /var/log/samba/ |
| 端口 | TCP 445/139、UDP 137/138 |
| 演示账号 | 管理账号 admin、普通账号 dev1(密码均为 q1w2e3r4) |
注意:客户端与服务端处于同一内网网段 10.89.56.0/24;以下所有
ssh/操作均按一台服务端 + 一台 Linux 客户端的双机环境展开。
二、前置条件
- 一台 Ubuntu 22.04 服务器,具备
sudo权限(本文演示直接以 root 操作); - 服务端与客户端网络互通:安装前在客户端确认可达(
ping -c 2 10.89.56.10),Samba 端口为 TCP 445/139; - apt 软件源可用(Ubuntu 22.04 官方仓库/镜像);
- 规划好共享目录:本文使用
/srv/samba/public与/srv/samba/team; - 规划账号:
admin(管理员演示账号)、dev1(普通用户),密码统一为演示密码 q1w2e3r4,生产环境请改用强密码。
三、版本选型说明
先确认仓库里能装到哪个版本:
apt policy samba
实测输出:
samba:
Installed: (none)
Candidate: 2:4.15.13+dfsg-0ubuntu1.13
Version table:
2:4.15.13+dfsg-0ubuntu1.13 500
500 http://archive.ubuntu.com/ubuntu jammy-updates/main amd64 Packages
500 http://security.ubuntu.com/ubuntu jammy-security/main amd64 Packages
2:4.15.5~dfsg-0ubuntu5 500
500 http://archive.ubuntu.com/ubuntu jammy/main amd64 Packages
结论:Ubuntu 22.04 仓库候选版本为 samba 2:4.15.13+dfsg-0ubuntu1.13,即 Samba 4.15.13。作为对照,Samba 官方在 2026-10-26 检索日的最新稳定版是 4.25.1(见《Samba 4.15.13 介绍与选型指南》第七节),4.15 系列上游已停止维护,但发行版仓库版由 Canonical 持续 backport 安全修复直至 Ubuntu 22.04 生命周期结束——生产部署优先选发行版仓库版,这也是本文采用 apt 在线安装作为主线方案的原因。
技术拓展:Samba 上游各系列约 18 个月维护期,4.15 早已进入 EOL;若坚持用上游新版,需要更换发行版、使用第三方仓库或源码编译,本文不展开。
四、方案 A:apt 在线安装(推荐主线)
4.1 更新索引
apt-get update
实测输出(节选):
Hit:2 http://security.ubuntu.com/ubuntu jammy-security InRelease
Hit:3 http://archive.ubuntu.com/ubuntu jammy-updates InRelease
Hit:4 http://archive.ubuntu.com/ubuntu jammy-backports InRelease
Reading package lists...
4.2 安装 samba
DEBIAN_FRONTEND=noninteractive apt-get install -y samba
实测输出(安装尾部节选):
Loaded services file OK.
Weak crypto is allowed
Server role: ROLE_STANDALONE
Done
Adding group `sambashare' (GID 107) ...
Done.
Samba is not being run as an AD Domain Controller: Masking samba-ad-dc.service
Please ignore the following error about deb-systemd-helper not finding those services.
(samba-ad-dc.service masked)
invoke-rc.d: policy-rc.d denied execution of start.
invoke-rc.d: policy-rc.d denied execution of start.
invoke-rc.d: policy-rc.d denied execution of start.
Created symlink /etc/systemd/system/multi-user.target.wants/nmbd.service → /lib/systemd/system/nmbd.service.
Failed to preset unit: Unit file /etc/systemd/system/samba-ad-dc.service is masked.
/usr/bin/deb-systemd-helper: error: systemctl preset failed on samba-ad-dc.service: No such file or directory
Created symlink /etc/systemd/system/multi-user.target.wants/smbd.service → /lib/systemd/system/smbd.service.
/usr/sbin/policy-rc.d returned 101, not running 'start nmbd.service samba-ad-dc.service smbd.service'
Processing triggers for libc-bin (2.35-0ubuntu3.15) ...
Processing triggers for ca-certificates (20260816ubuntu0.22.04.1) ...
Updating certificates in /etc/ssl/certs...
0 added, 0 removed; done.
Running hooks in /etc/ca-certificates/update.d...
done.
解读这段尾部回显:
- 包安装脚本先用 testparm 做了一次自检(
Loaded services file OK.),随后创建sambashare组; - 普通服务器角色(非 AD DC)下自动屏蔽
samba-ad-dc.service,并把nmbd.service、smbd.service加入开机自启(Created symlink ... multi-user.target.wants/...); invoke-rc.d: policy-rc.d denied execution of start.表示当前环境存在/usr/sbin/policy-rc.d策略文件、安装器未立即拉起服务——这在自动化/受限环境常见,不影响安装,装完手动systemctl start smbd即可;常规服务器上没有该策略文件,安装器会直接启动服务。
4.3 验证版本与服务状态
smbd --version
systemctl status smbd --no-pager -l
实测输出(status 节选):
Version 4.15.13-Ubuntu
● smbd.service - Samba SMB Daemon
Loaded: loaded (/lib/systemd/system/smbd.service; enabled; vendor preset: enabled)
Active: active (running) since Mon 2026-10-26 09:08:28 UTC; 6min ago
Docs: man:smbd(8)
man:samba(7)
man:smb.conf(5)
Process: 7700 ExecStartPre=/usr/share/samba/update-apparmor-samba-profile (code=exited, status=0/SUCCESS)
Main PID: 7709 (smbd)
Status: "smbd: ready to serve connections..."
Tasks: 4 (limit: 3172)
Memory: 9.1M
enabled 表示服务已随安装自启动(开机自启);若个别机器处于 disabled 状态,执行:
systemctl enable --now smbd nmbd
4.4 确认监听端口
ss -ltnp | grep -E ":445|:139"
实测输出:
LISTEN 0 50 0.0.0.0:445 0.0.0.0:* users:(("smbd",pid=7709,fd=47))
LISTEN 0 50 0.0.0.0:139 0.0.0.0:* users:(("smbd",pid=7709,fd=48))
LISTEN 0 50 [::]:445 [::]:* users:(("smbd",pid=7709,fd=45))
LISTEN 0 50 [::]:139 [::]:* users:(("smbd",pid=7709,fd=46))
至此服务端安装完成,smbd 已在 TCP 445/139 就绪。
五、方案 B:deb 离线包安装(内网/无外网场景)
适用:服务器不能直接连外网,但有跳板机/内网源可以下载 deb。
5.1 在有外网的机器下载 deb 包
mkdir -p /tmp/debdl && cd /tmp/debdl
apt-get download samba
ls -lh
dpkg -I samba_2%3a4.15.13+dfsg-0ubuntu1.13_amd64.deb | grep -E "Package|Version|Pre-Depends"
实测输出:
Fetched 1193 kB in 2s (521 kB/s)
W: Download is performed unsandboxed as root as file '/tmp/debdl/samba_2%3a4.15.13+dfsg-0ubuntu1.13_amd64.deb' couldn't be accessed by user '_apt'. - pkgAcquire::Run (13: Permission denied)
1.2M samba_2%3a4.15.13+dfsg-0ubuntu1.13_amd64.deb
Package: samba
Version: 2:4.15.13+dfsg-0ubuntu1.13
Pre-Depends: dpkg (>= 1.15.6~), init-system-helpers (>= 1.54~)
注意:root 直接执行
apt-get download会出现上面这条_apt沙箱警告,属正常现象(下载本身已成功),介意的话可先chown _apt下载目录或换用普通用户执行。
5.2 传输到目标机并安装
将 deb 及其依赖包一并拷入目标机(如 /opt/samba-deb/),在目标机执行本地安装(apt 会自动解析本地路径并处理依赖):
apt-get install -y ./samba_2%3a4.15.13+dfsg-0ubuntu1.13_amd64.deb
实测输出(目标机已装同版本时实测):
samba is already the newest version (2:4.15.13+dfsg-0ubuntu1.13).
0 upgraded, 0 newly installed, 0 to remove and 2 not upgraded.
注意:
samba主包只是入口,完整依赖(samba-common-bin、samba-vfs-modules、python3-samba 等)必须一起离线准备,逐个apt-get download后用同一命令安装。
六、方案对比选择建议
| 方案 | 优点 | 缺点 | 适用 |
|---|---|---|---|
| A. apt 在线安装 | 一条命令装齐依赖、后续升级自动 | 需要外网或内网 apt 源 | 默认首选 |
| B. deb 离线安装 | 不依赖外网,版本可控 | 需手动准备全部依赖,升级麻烦 | 隔离网段、安全审计环境 |
七、配置首个共享(匿名 + 认证)
7.1 备份默认配置
cp -a /etc/samba/smb.conf /etc/samba/smb.conf.default
md5sum /etc/samba/smb.conf /etc/samba/smb.conf.default
实测输出:
ae3c484dde7ba4277b4a83b89938f32b /etc/samba/smb.conf
ae3c484dde7ba4277b4a83b89938f32b /etc/samba/smb.conf.default
7.2 写入最小共享配置
Ubuntu 仓库版默认的 smb.conf 带有 [global]、[printers]、[print$] 等示例段(安装后 testparm -s 实测可见 server string = %h server (Samba, Ubuntu)、[printers]/[print$] 段)。用以下配置直接覆盖(两个共享:public 匿名可写、team 认证可写):
cat > /etc/samba/smb.conf <<'EOF'
[global]
workgroup = WORKGROUP
server string = Samba Server %v
security = user
map to guest = Bad User
log file = /var/log/samba/log.%m
max log size = 1000
[public]
comment = Anonymous public share
path = /srv/samba/public
browseable = yes
writable = yes
guest ok = yes
guest only = yes
force create mode = 0666
force directory mode = 0777
[team]
comment = Authenticated team share
path = /srv/samba/team
browseable = yes
writable = yes
guest ok = no
valid users = @smbteam
create mask = 0664
directory mask = 0775
EOF
用 testparm 做语法校验:
testparm -s
实测输出(节选):
Load smb config files from /etc/samba/smb.conf
Loaded services file OK.
Weak crypto is allowed
Server role: ROLE_STANDALONE
# Global parameters
[global]
log file = /var/log/samba/log.%m
map to guest = Bad User
max log size = 1000
security = USER
server string = Samba Server %v
idmap config * : backend = tdb
[public]
comment = Anonymous public share
force create mode = 0666
force directory mode = 0777
guest ok = Yes
guest only = Yes
path = /srv/samba/public
read only = No
[team]
comment = Authenticated team share
create mask = 0664
directory mask = 0775
path = /srv/samba/team
read only = No
valid users = @smbteam
Loaded services file OK. 即校验通过;testparm 对拼写错误的参数会直接警告,务必以它为修改后的第一道检查。
7.3 创建目录、组与账号
mkdir -p /srv/samba/public /srv/samba/team
chmod 777 /srv/samba/public
groupadd smbteam
useradd -M -s /usr/sbin/nologin -G smbteam dev1
useradd -M -s /usr/sbin/nologin -G smbteam admin
chgrp -R smbteam /srv/samba/team
chmod 2775 /srv/samba/team
ls -l /srv/samba/
实测输出:
total 8
drwxrwxrwx 2 root root 4096 Oct 26 07:59 public
drwxrwsr-x 2 root smbteam 4096 Oct 26 07:59 team
7.4 添加 Samba 账号
Samba 账户与系统账户是两套密码库(passdb),useradd 后必须用 smbpasswd 把用户加入 Samba:
smbpasswd -a admin
smbpasswd -a dev1
pdbedit -L
实测输出(两次 smbpasswd 均按提示输入两次新密码):
New SMB password:
Retype new SMB password:
Added user admin.
New SMB password:
Retype new SMB password:
Added user dev1.
admin:1001:
dev1:1000:
7.5 重启服务生效
systemctl restart smbd
systemctl is-active smbd
systemctl is-enabled smbd
实测输出:
active
enabled
八、客户端访问验证
在客户端(10.89.56.20)安装访问工具:
apt-get install -y smbclient cifs-utils
smbclient -V
实测输出:
Version 4.15.13-Ubuntu
8.1 匿名浏览共享列表
smbclient -L //10.89.56.10 -N
实测输出:
Sharename Type Comment
--------- ---- -------
public Disk Anonymous public share
team Disk Authenticated team share
IPC$ IPC IPC Service (Samba Server 4.15.13-Ubuntu)
SMB1 disabled -- no workgroup available
8.2 认证浏览共享列表
smbclient -L //10.89.56.10 -U admin%q1w2e3r4
实测输出:
Sharename Type Comment
--------- ---- -------
public Disk Anonymous public share
team Disk Authenticated team share
IPC$ IPC IPC Service (Samba Server 4.15.13-Ubuntu)
SMB1 disabled -- no workgroup available
8.3 匿名上传验证(smbclient)
在客户端准备测试文件并上传:
echo "hello from client $(date)" > /tmp/hello.txt
smbclient //10.89.56.10/public -N -c "put /tmp/hello.txt hello.txt; ls"
实测输出:
putting file /tmp/hello.txt as \hello.txt (45.9 kb/s) (average 45.9 kb/s)
Try "help" to get a list of possible commands.
. D 0 Mon Oct 26 08:00:55 2026
.. D 0 Mon Oct 26 07:59:31 2026
hello.txt A 47 Mon Oct 26 08:00:55 2026
464118080 blocks of size 1024. 336461396 blocks available
注意:若写
smbclient //10.89.56.10/public -N -c "put /tmp/hello.txt",smbclient 会把/tmp/hello.txt当成远端文件名而实测报NT_STATUS_OBJECT_PATH_NOT_FOUND opening remote file \tmp\hello.txt;put必须显式给出远端文件名(或用交互模式)。
服务端侧验证文件属主与权限:
ls -l /srv/samba/public/
cat /srv/samba/public/hello.txt
stat -c "%U:%G %a" /srv/samba/public/hello.txt
实测输出:
total 4
-rwxrw-rw- 1 nobody nogroup 47 Oct 26 08:00 hello.txt
hello from client Mon Oct 26 08:00:41 UTC 2026
nobody:nogroup 766
注意:
guest only = yes的匿名上传文件属主为 nobody;force create mode = 0666之外多出的 x 位来自 Samba 默认的map archive = yes(把 DOS archive 属性映射为属主执行位),属正常现象。
8.4 cifs 挂载验证(team 共享)
mkdir -p /mnt/team
mount -t cifs //10.89.56.10/team /mnt/team -o username=dev1,password=q1w2e3r4
mount | grep team
实测输出:
//10.89.56.10/team on /mnt/team type cifs (rw,relatime,vers=3.1.1,cache=strict,upcall_target=app,username=dev1,uid=0,noforceuid,gid=0,noforcegid,addr=10.89.56.10,file_mode=0755,dir_mode=0755,soft,nounix,serverino,mapposix,reparse=nfs,nativesocket,symlink=native,rsize=4194304,wsize=4194304,bsize=1048576,echo_interval=60,actimeo=1,closetimeo=1)
协商协议为 vers=3.1.1,说明 SMB3 协议族默认协商生效。挂载后写入、查看文件:
echo "team file by dev1" > /mnt/team/readme.md
touch "/mnt/team/季度报表-Q3.xlsx"
ls -l /mnt/team/
实测输出:
total 4
-rwxr-xr-x 1 root root 18 Oct 26 08:01 readme.md
-rwxr-xr-x 1 root root 0 Oct 26 08:02 季度报表-Q3.xlsx
中文文件名在 SMB3 传输下无需额外字符集参数即可正常读写。服务端侧确认属主与权限:
ls -l /srv/samba/team/
stat -c "%U:%G %a %n" /srv/samba/team/readme.md
umask
实测输出:
total 4
-rw-rw-r-- 1 dev1 smbteam 18 Oct 26 08:01 readme.md
dev1:smbteam 664 /srv/samba/team/readme.md
0022
create mask = 0664 生效:经 cifs 写入的文件在服务端呈 664,属主为 cifs 会话用户 dev1、属组 smbteam。对照 8.3 节:smbclient 上传的文件带 DOS archive 属性(map archive 映射出属主 x 位,呈 766),而 cifs 内核客户端写入不带 archive 属性,呈 664——客户端形态不同,落盘权限不同。删除文件并卸载:
rm /mnt/team/readme.md
ls /mnt/team/
umount /mnt/team
ls -l /srv/samba/team/
实测输出:
季度报表-Q3.xlsx
total 0
-rw-rw-r-- 1 dev1 smbteam 0 Oct 26 08:02 季度报表-Q3.xlsx
8.5 负向验证
三组"应该失败"的访问,确认权限模型符合预期:
smbclient //10.89.56.10/team -N -c ls # 匿名访问认证共享
smbclient //10.89.56.10/team -U dev1%badpass -c ls # 密码错误
smbclient //10.89.56.10/team -U nosuchuser%q1w2e3r4 -c ls # 不存在的用户
实测输出(=== 为三次实测输出的分隔):
tree connect failed: NT_STATUS_ACCESS_DENIED
===
session setup failed: NT_STATUS_LOGON_FAILURE
===
tree connect failed: NT_STATUS_ACCESS_DENIED
- 匿名被拒:
guest ok = no在 tree connect(连接共享)阶段拦截,报tree connect failed: NT_STATUS_ACCESS_DENIED; - 密码错误:会话建立阶段即失败,报
session setup failed: NT_STATUS_LOGON_FAILURE; - 不存在用户:
map to guest = Bad User将其映射为 guest,随后同样在 tree connect 阶段被valid users = @smbteam拒绝——两条参数的联动在《Samba 4.15.13 配置参数详解》5.1 节有逐项实验。
8.6 Windows 客户端访问
Windows 侧无需装任何软件:文件管理器地址栏输入 \\10.89.56.10\team,弹出认证框后输入用户名 dev1 与密码即可;命令行持久映射用:
net use Z: \\10.89.56.10\team /user:dev1 密码
Windows 不同版本的提示语与交互略有差异,以实际环境为准。
九、防火墙配置
Ubuntu 默认未启用 ufw;启用并放行 Samba(系统自带应用配置文件):
apt-get install -y ufw
cat /etc/ufw/applications.d/samba
ufw --force enable
ufw allow Samba
ufw status
实测输出:
[Samba]
title=LanManager-like file and printer server for Unix
description=The Samba software suite is a collection of programs that implements the SMB/CIFS protocol for unix systems, allowing you to serve files and printers to Windows, NT, OS/2 and DOS clients. This protocol is sometimes also referred to as the LanManager or NetBIOS protocol.
ports=137,138/udp|139,445/tcp
Firewall is active and enabled on system startup
Rule added
Rule added (v6)
Status: active
To Action From
-- ------ ----
Samba ALLOW Anywhere
Samba (v6) ALLOW Anywhere (v6)
放行后客户端复测共享访问正常(smbclient -L 列表与 8.2 节一致),说明 ufw 应用配置覆盖了 Samba 全部端口(UDP 137/138 + TCP 139/445)。
十、升级与多版本说明
10.1 检查升级
apt-get install --only-upgrade samba
实测输出:
samba is already the newest version (2:4.15.13+dfsg-0ubuntu1.13).
0 upgraded, 0 newly installed, 0 to remove and 2 not upgraded.
10.2 多版本并存的现实
同一台 Ubuntu 22.04 上不能与仓库版并存安装上游其他 Samba 版本(会覆盖 /usr/bin 下同名程序)。需要其他版本时,换发行版/新宿主机或源码编译部署;日常升级跟随 apt update && apt upgrade 即可,安全补丁由 Canonical backport。
十一、常见问题
- smbd active 但客户端连不上 445:按顺序查
ss -ltnp | grep 445(监听是否存在)、ufw status(是否放行 Samba)、smbclient -L //本机IP -N(本机自测)。 - 登录报 NT_STATUS_LOGON_FAILURE:系统用户没有加入 Samba 密码库,执行
smbpasswd -a 用户名后重试(见 7.4)。 - 认证通过但写不进共享(NT_STATUS_ACCESS_DENIED):先分清两层——smb.conf 的
valid users/read only决定 SMB 层放不放行,Linux 目录权限决定 smbd 进程能不能写文件;testparm -s逐项核对共享段。 - 挂载报 mount error(112): Host is down:多为协议协商失败或端口被防火墙拦截;确认服务端
server min protocol与客户端vers=选项(Samba 4.11+ 默认拒绝 SMB1)。 - 中文文件名乱码:Samba 4.15 默认
unix charset = UTF-8,服务端与客户端文件系统均应为 UTF-8;经 cifs 挂载传输时默认按 UTF-8 协商(实测见 8.4),出现乱码时检查客户端 locale 与挂载参数iocharset=utf8。
十二、卸载与清理
DEBIAN_FRONTEND=noninteractive apt-get remove --purge -y samba
实测输出(节选):
(Reading database ... 10910 files and directories currently installed.)
Removing samba (2:4.15.13+dfsg-0ubuntu1.13) ...
/usr/sbin/policy-rc.d returned 101, not running 'stop nmbd.service samba-ad-dc.service smbd.service'
invoke-rc.d: policy-rc.d denied execution of stop.
invoke-rc.d: policy-rc.d denied execution of stop.
invoke-rc.d: policy-rc.d denied execution of stop.
Processing triggers for libc-bin (2.35-0ubuntu3.15) ...
(Reading database ... 10713 files and directories currently installed.)
Purging configuration files for samba (2:4.15.13+dfsg-0ubuntu1.13) ...
dpkg: warning: while removing samba, directory '/var/lib/samba/printers/x64' not empty so not removed
dpkg: warning: while removing samba, directory '/var/lib/samba/printers/W32X86' not empty so not removed
Processing triggers for ufw (0.36.1-4ubuntu0.1) ...
清理依赖与数据目录:
apt-get autoremove -y
rm -rf /srv/samba
实测输出(autoremove 节选):
Removing tdb-tools (1.4.5-2build1) ...
Processing triggers for libc-bin (2.35-0ubuntu3.15) ...
验证终态:
systemctl is-active smbd
ss -ltn | grep -cE ":445|:139"
which smbclient
ls /etc/samba
实测输出:
inactive
0
/usr/bin/smbclient
gdbcommands
smb.conf
smb.conf.default
tls
smbd 二进制已随包移除(which smbd 无输出)、服务 inactive、445/139 监听数为 0;smbclient 属于独立的 smbclient 包,未被连带删除(/usr/bin/smbclient 仍在)。/etc/samba/ 下的 smb.conf、smb.conf.default 与 passdb(/var/lib/samba/private/passdb.tdb)由 samba-common 包保留——若确认不再回滚,可执行 apt-get remove --purge -y samba-common 并删除上述残留目录彻底清场。
十三、参考链接
- Samba 官网 https://www.samba.org/ (检索日期 2026-10-26)
- Ubuntu 手册:Samba 服务 https://ubuntu.com/server/docs/service-samba (检索日期 2026-10-26)
- smb.conf 手册(Ubuntu 22.04 仓库版随附 man smb.conf) https://www.samba.org/samba/docs/current/man-html/smb.conf.5.html (检索日期 2026-10-26)
- Samba 官方 Wiki:Setting up Samba as a Standalone Server https://wiki.samba.org/index.php/Setting_up_Samba_as_a_Standalone_Server (检索日期 2026-10-26)
- 本站相关文档:Samba 4.15.13 介绍与选型指南