在 Windows 11 宿主机上,通过 VMware 运行 CentOS 8 虚拟机,将主机文件夹共享给虚拟机;部署 Nginx 搭建内网文件下载服务,实现内网所有设备浏览器在线浏览、下载宿主机共享文件夹内资料。
| 组件 | 版本/配置 |
|---|---|
| 宿主机 | Windows 11(内网网段:192.168.3.0/24) |
| 虚拟机 | CentOS 8 最小化安装(无GUI桌面) |
| 虚拟化软件 | VMware Workstation |
| 共享文件夹名 | WinShare |
| 服务组件 | Nginx 静态文件服务 |
| 最终网络模式 | 虚拟机网卡 桥接模式 |
实际以个人设备参数为准
执行 dnf update -y 报错:
Could not resolve host: mirrors.aliyun.com
无法解析软件源域名,下载软件包失败。
系统默认 DNS 配置异常,无法外网域名解析。
|
1 2 3 |
echo "" > /etc/resolv.conf echo "nameserver 223.5.5.5" >> /etc/resolv.conf echo "nameserver 8.8.8.8" >> /etc/resolv.conf |
|
1 2 |
systemctl restart NetworkManager ping www.baidu.com |
|
1 |
dnf update -y |
|
1 |
dnf install -y nginx |
|
1 2 |
systemctl enable nginx systemctl start nginx |
|
1 |
systemctl status nginx |
正常状态:active (running)
|
1 2 3 4 5 6 7 8 9 |
# 关闭并禁用防火墙 systemctl stop firewalld systemctl disable firewalld
# 临时关闭SELinux setenforce 0
# 永久关闭SELinux sed -i 's/^SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config |
执行 ls /mnt/hgfs/ 无任何输出,看不到 Windows 共享目录。
CentOS8 最小化无GUI,不会自动挂载 VMware 共享目录;且默认挂载目录非空,直接 fstab 挂载会冲突报错。
|
1 |
dnf install -y open-vm-tools |
|
1 |
mkdir -p /mnt/hgfs |
|
1 |
vmhgfs-fuse .host:/ /mnt/hgfs -o allow_other -o nonempty |
|
1 |
ls /mnt/hgfs/ |
正常输出:WinShare
|
1 2 3 4 5 6 7 |
# 清理旧冲突配置 sed -i '/vmhgfs-fuse/d' /etc/fstab # 写入标准挂载配置 echo ".host:/ /mnt/hgfs fuse.vmhgfs-fuse defaults,allow_other,nonempty 0 0" >> /etc/fstab # 重载系统配置 + 测试挂载 systemctl daemon-reload mount -a |
|
1 |
cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak |
|
1 |
vi /etc/nginx/nginx.conf |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
user nginx; worker_processes auto; error_log /var/log/nginx/error.log; pid /run/nginx.pid;
include /usr/share/nginx/modules/*.conf;
events { worker_connections 1024; }
http { log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; types_hash_max_size 2048;
include /etc/nginx/mime.types; default_type application/octet-stream;
server { listen 80; server_name _;
root /mnt/hgfs/WinShare; index index.html;
autoindex on; autoindex_exact_size off; autoindex_localtime on;
location / { try_files $uri $uri/ =404; } } } |
|
1 2 |
nginx -t systemctl restart nginx |
Nginx 运行正常、共享目录挂载正常、防火墙全关,但访问宿主机IP无法打开文件页面。
Nginx 部署在虚拟机内,若为NAT模式,直接访问宿主机IP不会自动转发80端口到虚拟机;请求无法抵达Nginx服务。
|
1 |
ip a |
| 问题现象 | 排查处理方向 |
|---|---|
| dnf 无法解析域名 | 配置公共DNS,测试外网ping连通性 |
| /mnt/hgfs 为空 | 用带 nonempty 参数手动挂载,配置fstab开机自挂 |
| 页面403/404 | 检查Nginx root路径、共享目录挂载状态 |
| 浏览器打不开 | 虚拟机网卡改用桥接模式,访问虚拟机IP而非宿主机IP |