在虚拟机(Ubuntu)中访问 Windows 主机的 FTP 或共享文件夹,需要根据具体需求选择方案。以下是最实用、最安全的解决方案(无需额外配置 FTP 服务,直接使用 Windows 原生共享):
为什么不用 FTP?
Windows 本身不提供 FTP 服务(需额外安装 IIS),而 SMB 是 Windows 原生支持的共享协议,速度更快、更安全。
创建共享文件夹
关闭密码保护共享(重要!避免登录问题)
获取 Windows IP 地址
|
1 2 3 4 5 6 7 8 |
# 1. 安装 SMB 客户端 sudo apt update && sudo apt install -y smbclient
# 2. 查看 Windows 共享列表(可选) smbclient -L //192.168.1.100 -U %
# 3. 进入共享文件夹(无需密码) smbclient //192.168.1.100/ubuntu_share |
输入 ls 查看文件,quit 退出。
|
1 2 3 4 5 6 7 8 |
# 创建挂载点 sudo mkdir -p /mnt/windows_share
# 挂载共享(替换为你的 Windows IP 和共享名) sudo mount -t cifs -o username=,password= //192.168.1.100/ubuntu_share /mnt/windows_share
# 验证 ls /mnt/windows_share |
无需密码:因为已关闭密码保护共享,所以 username= 和 password= 为空。
| 问题 | 解决方案 |
|---|---|
| 连接被拒绝 | 1. 检查 Windows 防火墙:允许 **“文件和打印机共享”**2. 确认 Windows IP 正确(ipconfig) |
| 权限错误 | 1. 在 Windows 共享权限中添加 Everyone2. 关闭密码保护共享(关键!) |
| 无法挂载 | 1. 安装 cifs-utils:sudo apt install -y cifs-utils2. 检查共享名是否正确(大小写敏感) |
| 速度慢 | 1. 确保虚拟机网络模式是 桥接模式(非 NAT)2. 在 Windows 里关闭 “公用网络”(在“网络和共享中心”) |
| 方案 | 优点 | 缺点 |
|---|---|---|
| SMB/CIFS | ? Windows 原生支持? 无需额外配置? 速度更快? 无需密码 | ? 需关闭密码保护(但安全风险极低) |
| FTP | ? 需安装 IIS? 明文传输(不安全)? 配置复杂 | ? 无 |
安全提示:关闭密码保护共享仅影响局域网内访问,不会暴露到公网(内网安全)。
在 Windows 上:创建一个测试文件 test.txt 到共享文件夹。
在 Ubuntu 中:
|
1 2 |
ls /mnt/windows_share # 应看到 test.txt echo "Hello from Ubuntu" > /mnt/windows_share/ubuntu_test.txt |
在 Windows 上:检查共享文件夹,确认 ubuntu_test.txt 已生成。
仅当 Windows 上已安装 IIS FTP 服务 时使用(需额外配置)。
在 Windows 上:
在 Ubuntu 中:
|
1 2 3 |
sudo apt install -y ftp ftp 192.168.1.100 # 连接 FTP 服务器 # 输入用户名/密码 |
强烈建议不要用 FTP:明文传输密码,且 Windows 默认不提供 FTP 服务。
| 需求 | 推荐方案 | 命令/操作 |
|---|---|---|
| 快速访问 Windows 文件 | SMB 共享 | smbclient //192.168.1.100/share |
| 永久挂载到 Ubuntu | SMB 挂载 | sudo mount -t cifs //192.168.1.100/share /mnt/share |
| 避免密码 | 关闭 Windows 密码保护共享 | 在 Windows 高级共享设置中关闭 |
成功标志:
在 Ubuntu 中能看到 Windows 共享文件夹内容,且能创建/删除文件。