在 Ubuntu 虚拟机 中实现“共享”,常见有 三种场景。下面按最常用情况给你完整说明(以 VirtualBox / VMware 为主,也补充 KVM/QEMU 和 跨系统共享)。
sudo apt update
sudo apt install -y build-essential dkms linux-headers-$(uname -r)
在 VirtualBox 菜单:
设备 → 安装增强功能
挂载并安装:
sudo mount /dev/cdrom /mnt
cd /mnt
sudo ./VBoxLinuxAdditions.run
重启虚拟机。
VirtualBox:
设置 → 共享文件夹 → 添加
sharesudo usermod -aG vboxsf $USER
reboot
默认挂载位置:
/media/sf_share
sudo apt install open-vm-tools open-vm-tools-desktop
VMware:
虚拟机 → 设置 → 选项 → 共享文件夹
Ubuntu 中查看:
vmware-hgfsclient
挂载:
sudo mkdir /mnt/hgfs
sudo vmhgfs-fuse .host:/ /mnt/hgfs
主机 ↔ Ubuntu 直接传文件:
scp file.txt user@ubuntu-ip:/home/user/
查看 Ubuntu IP:
ip a
sudo apt install samba
mkdir ~/share
sudo chmod 777 ~/share
sudo nano /etc/samba/smb.conf
添加:
[share]
path = /home/youruser/share
read only = no
guest ok = yes
重启:
sudo systemctl restart smbd
Windows 访问:
\\ubuntu-ip\share
sudo apt install nfs-kernel-server
sudo nano /etc/exports
添加:
/home/user/share *(rw,sync,no_subtree_check)
sudo exportfs -a
sudo systemctl restart nfs-kernel-server
sudo mount ubuntu-ip:/home/user/share /mnt
⚠️ 需要安装 Guest Additions
<filesystem type='mount' accessmode='passthrough'>
<source dir='/host/path'/>
<target dir='share'/>
</filesystem>
| 场景 | 推荐 |
|---|---|
| Windows ↔ Ubuntu | VirtualBox 共享文件夹 |
| 临时传文件 | SCP |
| Windows 访问 Ubuntu | Samba |
| 多虚拟机 | NFS |
| 剪贴板 | 增强工具 |
1️⃣ 用的是 VirtualBox / VMware / KVM?
2️⃣ 主机是 Windows / macOS / Linux?
3️⃣ 想共享 文件夹 / 网络 / 剪贴板?
我可以直接给你 一步一步针对你环境的命令 ✅