WIN10上搭建samba服务器和Ubuntu虚拟机共享文件
本文和网上描述不同的一点主要在于,samba服务器搭建在Windows 10系统上,而非Ubuntu中。通过Ubuntu中配置在/etc/fstab中自动挂载命令或通过systemd脚本,将Windows中的共享文件夹开机自动挂载到Ubuntu系统中,从而实现文件共享。
Windows搭建Samba服务
启用Samba功能
win+r中执行命令optionalfeatures,然后做如图选择:
重启设备。
创建访问账户
win+r中执行命令compmgmt.msc,添加用户share,如图所示:
启用文件共享
省略
Ubuntu虚拟机挂载
手动挂载
sudo mount -t cifs //192.168.100.1/E ~/windows_share -o username=share,password=18159676,rw,file_mode=0777,dir_mode=0777,uid=1000,gid=1000
自动挂载
方案一:在/etc/fstab中自动挂载
在/etc/fstab中自动挂载:
$ cat /etc/fstab
# /etc/fstab: static file system information.
#
# Use 'blkid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#
# <file system> <mount point> <type> <options> <dump> <pass>
# / was on /dev/sda2 during curtin installation
//192.168.100.1/E /home/blduan/windows_share cifs defaults,user=share,password=18159676,rw,file_mode=0777,dir_mode=0777,uid=1000,gid=1000, 0 0
方案二:使用systemd脚本自动挂载
使用systemd脚本自动挂载:
systemd的mount脚本可以用来实现挂载,并且可以设置开机启动,比/etc/fstab好的地方在于如果出现错误,不会导致无法开机。
在/etc/systemd/system目录下增加脚本文件,文件名要以.mount结尾,mount单元的名称必须根据其文件系统挂载点路径命名(以连字符分隔目录名称)。比如挂载/home/xxx/windows_share目录下,则文件名需要为home-xxx-windows_share.mount。
[Unit]
Description=mount samba
After=network-online.target systemd-networkd-wait-online.service
Requires=network-online.target systemd-networkd-wait-online.service
Before=remote-fs.target
# 增加启动失败自动重试 StartLimitIntervalSec=60
StartLimitBurst=5
[Mount]
What=//192.168.100.1/d
Where=/home/blduan/windows_share
Type=cifs
Options=username=samba,password=samba,uid=1000,gid=1000,rw,file_mode=0777,dir_mode=0777
TimeoutSec=15
[Install]
WantedBy=multi-user.target
创建完成之后执行命令进行挂载和开机自动挂载:systemctl start home-xxx-windows-share.mount和systemctl enable home-xxx-windows_share.mount。
方案二存在问题:使用systemd的mount脚本进行挂载有极大可能失败,因为其依赖于network-online.target和systemd-networkd-wait-online.service,这两个服务是检测网络配置是否完成。如果是通过DHCP配置网络,则需要完成DHCP交互并设置完IP才可以。
其中网络配置如下:
# /etc/netplan/50-cloud-init.yaml
# This file is generated from information provided by the datasource. Changes
# to it will not persist across an instance reboot. To disable cloud-init's
# network configuration capabilities, write a file
# /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg with the following:
# network: {config: disabled}
network:
ethernets:
internet:
addresses: [192.168.100.129/24]
dhcp4: false
dhcp6: true
# optional: true
version: 2
这里的IPv4采用的是静态IP,IPv6采用DHCPv6方式配置。之前出现挂载失败的原因是通过DHCPv6获取IPv6地址时一直检测地址重复,配置IPv6地址失败,进而导致systemd-networkd-wait-online.service检测失败,然后无法挂载。
解决掉IPv6 DAD问题之后,systemd-networkd-wait-online.service的状态如下:
$ systemctl status systemd-networkd-wait-online.service
● systemd-networkd-wait-online.service - Wait for Network to be Configured
Loaded: loaded (/usr/lib/systemd/system/systemd-networkd-wait-online.service; enabled; preset: enabled)
Drop-In: /run/systemd/system/systemd-networkd-wait-online.service.d
└─10-netplan.conf
Active: active (exited) since Wed 2025-09-17 08:56:47 CST; 45s ago
Docs: man:systemd-networkd-wait-online.service(8)
Process: 977 ExecStart=/lib/systemd/systemd-networkd-wait-online -i internet:degraded (code=exited, status=0/SUCCESS)
Process: 1109 ExecStart=/lib/systemd/systemd-networkd-wait-online --any -o routable -i internet (code=exited, status=0/SUCCESS)
Main PID: 1109 (code=exited, status=0/SUCCESS)
CPU: 20ms
Sep 17 08:56:46 ubuntu24 systemd[1]: Starting systemd-networkd-wait-online.service - Wait for Network to be Configured...
Sep 17 08:56:47 ubuntu24 systemd[1]: Finished systemd-networkd-wait-online.service - Wait for Network to be Configured.
$ systemctl status home-blduan-windows_share.mount
● home-blduan-windows_share.mount - mount samba
Loaded: loaded (/etc/systemd/system/home-blduan-windows_share.mount; enabled; preset: enabled)
Active: active (mounted) since Wed 2025-09-17 08:56:47 CST; 10min ago
Where: /home/blduan/windows_share
What: //192.168.100.1/e
Tasks: 0 (limit: 9379)
Memory: 8.0K (peak: 1.7M)
CPU: 9ms
CGroup: /system.slice/home-blduan-windows_share.mount
Sep 17 08:56:47 ubuntu24 systemd[1]: Mounting home-blduan-windows_share.mount - mount samba...
Sep 17 08:56:47 ubuntu24 systemd[1]: Mounted home-blduan-windows_share.mount - mount samba.
问题排查
Windows系统升级之后Ubuntu虚拟机无法连接
Windows 11先是修复了24H2的系统错误之后又升级到了25H2之后,重新启动了Ubuntu24.02虚拟机发现连不上Windows 11共享的samba服务。
报错信息如下:
mount: /home/blduan/windows_share: mount(2) system call failed: Operation now in progress.
dmesg(1) may have more information after failed mount system call.
[ 823.739971] CIFS: VFS: Error connecting to socket. Aborting operation.
[ 823.740007] CIFS: VFS: cifs_mount failed w/return code = -115
ping检测畅通,确保网络没有问题。
然后检测端口,发现Windows 11升级之后445端口在域和公用网络上没有打开,因此初步确认是这个问题。
在“控制面板”->“系统和安全”->“Windows Defender 防火墙“中的”入栈规则“中打开445端口的相关规则。
- 原文作者:生如夏花
- 原文链接:https://DBL2017.github.io/post/%E5%B7%A5%E5%85%B7%E4%BD%BF%E7%94%A8/win10%E4%B8%8A%E6%90%AD%E5%BB%BAsamba%E6%9C%8D%E5%8A%A1%E5%99%A8%E5%92%8Cubuntu%E8%99%9A%E6%8B%9F%E6%9C%BA%E5%85%B1%E4%BA%AB%E6%96%87%E4%BB%B6/
- 版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 4.0 国际许可协议进行许可,非商业转载请注明出处(作者,原文链接),商业转载请联系作者获得授权。


