VMware 虚拟机和宿主机共享操作

共享文件夹

参考图片在 VMware 中设置好文件夹共享

VMware 共享文件夹操作

# 先创建一个挂载点目录 /mnt/share
# 然后再将共享目录挂载在 /mnt/share 下面
sudo mkdir /mnt/share && sudo mount -t fuse.vmhgfs-fuse .host:/ /mnt/share -o allow_other
# 将我的 windows 共享目录软链接至 ~/winshare
# 也就是类似 windows 中的快捷方式
# - 访问 ~/winshare 时自动跳转至 /mnt/share/winshare
ln -s /mnt/share/winshare ~/winshare
# 查看 winshare 内容
ls ~/winshare

参考:解决虚拟机找不到共享文件夹问题,
也请关注评论区

修复重启之后共享文件夹失效的问题

问题: 重启之后, 共享文件夹又会无法访问. 这是由于 sudo mount -t fuse.vmhgfs-fuse .host:/ /mnt/share -o allow_other 作用期限仅为未关机之前.

解决办法:

  • 开机或打开终端时自动挂载 .host://mnt,无需每次输入密码,且不要重复挂载。

而要满足上面的条件则需要使用 sudoers 配合 mount 自动执行.

操作步骤:

# 1.  新建一个脚本文件(用于做挂载操作)
sudo nano /usr/local/bin/mount-vmhgfs.sh

文件内容如下:

#!/bin/bash
exec /bin/mount -t fuse.vmhgfs-fuse .host:/ /mnt/ -o allow_other
# 2. 赋予可执行权限
sudo chmod +x /usr/local/bin/mount-vmhgfs.sh
# 3. 配置 sudoers,允许你无密码则可以执行该命令
sudo visudo

在文件最后添加如下的一行代码

[你的默认账户名字] ALL=(ALL) NOPASSWD: /usr/local/bin/mount-vmhgfs.sh
# 例如你叫 unic
# 则是 unic ALL=(ALL) NOPASSWD: /usr/local/bin/mount-vmhgfs.sh
# 4. 在 source 文件中添加执行操作
# 例如我的 source 文件为 ~/.zshrc, 则是在 ~/.zshrc 文件尾部添加下面的内容即可.
# 默认的 shell 的 source 文件为 ~/.bashrc.
if ! mountpoint -q /mnt; then
  sudo /usr/local/bin/mount-vmhgfs.sh
fi

上述流程之后:

  • 安全:仅允许执行你写的特定脚本(挂载共享文件夹);
  • 自动化:登录终端自动挂载;
  • 无需密码:sudo 执行脚本不再弹出密码;

共享剪贴板/拖动文件

我的 VMware 虚拟机总是弹出找不到合适的 VMware tools. 并且我在通过
sudo apt install open-vm-tools 命令安装 open-vm-tools
之后。依旧没有办法实现虚拟机和宿主机共享剪贴板,文件拖动共享的操作。

找到解决办法之后记录于此。

参考图片在 VMware 中设置好文件夹共享

VMware 共享剪贴板/拖动文件

# 请先确保卸载了 open-vm-tools
sudo apt autoremove open-vm-tools
# 安装 open-vm-tools-desktop
sudo apt update && sudo apt install open-vm-tools-desktop -y

上面的命令 sudo apt update && sudo apt install open-vm-tools-desktop -y
如果运行出错了。
则替换为这段代码并且执行:sudo apt update && sudo apt install open-vm-tools-desktop fuse -y

# 等待安装完毕, 安装完毕之后重启机器
sudo reboot # 立刻重启机器