Linux虚拟机中 编译Linux源码 记录
为Linux虚拟机设置共享文件夹:
解决Ubuntu虚拟机共享文件夹问题
Ubuntu中无法找到之前设置的共享文件夹。可能是因为VMware共享文件夹未设置为自动挂载。在这种情况下,建议采取以下措施:
-
确保安装了VMware Tools或open-vm-tools。
-
在/etc/fstab中添加自动挂载选项,或者使用vmware-hgfs服务。
-
检查共享文件夹是否启用,并确保FUSE模块加载。
-
可以更改/tools.conf文件启用HGFS挂载。
每次重启虚拟机后看不到 HGFS 共享,其实最常见的原因就是——你之前都是手动用 vmhgfs-fuse
挂载的,这个挂载只在本次会话生效,重启后就没了。
如果把 /mnt/hgfs
删掉了,系统在启动时根本找不到挂载点,也不会自动挂载。
两种持久化的方案,任选其一:
方案一:在 /etc/fstab
中写入自动挂载
-
重新创建挂载点
sudo mkdir -p /mnt/hgfs sudo chown root:root /mnt/hgfs sudo chmod 755 /mnt/hgfs
-
编辑
/etc/fstab
sudo nano /etc/fstab
在末尾追加一行(把
SharedData
换成你在 VMware 里给共享取的 Name):.host:/SharedData /mnt/hgfs fuse.vmhgfs-fuse defaults,allow_other,uid=1000,gid=1000,nofail 0 0
-
allow_other,uid=1000,gid=1000
:普通用户可读写 -
nofail
:即使挂载失败也不中断启动 -
如果你有多个共享,就为每个写一行,挂载到不同目录(如
/mnt/hgfs/Share1
)
-
-
测试挂载
sudo mount -a ls /mnt/hgfs
重启后也会自动挂载。
方案二:用 systemd 服务单元自动挂载
如果你不想改 fstab,可以写一个小的 systemd 服务,让它开机后自动执行挂载命令。
-
创建 unit 文件
sudo tee /etc/systemd/system/hgfs-mount.service > /dev/null << 'EOF' [Unit] Description=Mount VMware Shared Folders (HGFS) After=network.target[Service] Type=oneshot ExecStart=/usr/bin/vmhgfs-fuse .host:/SharedData /mnt/hgfs -o allow_other,uid=1000,gid=1000 RemainAfterExit=yes[Install] WantedBy=multi-user.target EOF
-
确保挂载点存在
sudo mkdir -p /mnt/hgfs
-
启用并启动服务
sudo systemctl daemon-reload sudo systemctl enable --now hgfs-mount.service
-
验证
systemctl status hgfs-mount.service ls /mnt/hgfs
额外检查
-
VMware 设置:虚拟机设置 → Options → Shared Folders → Always enabled 并且列表中有你的共享。
-
open-vm-tools:确保装了
open-vm-tools-desktop
并且open-vm-tools
服务已启动,否则会挂载失败:sudo apt install open-vm-tools-desktop fuse sudo systemctl enable --now open-vm-tools
-
/etc/fuse.conf
:确认去掉了#user_allow_other
前的注释,否则allow_other
不生效。
按以上任意方案配置一次,之后每次开机重启,Ubuntu 下就能自动看到并访问 VMware 里的共享文件夹了。