你说的“CentOS 怎么连 VirtualBox 网”,一般是指 在 VirtualBox 里运行的 CentOS 怎么联网 / 怎么和主机通信。下面按常见场景给你说明,基本覆盖 99% 的情况。
| 模式 | 能否上外网 | 能否访问主机 | 主机能否访问虚拟机 |
|---|---|---|---|
| NAT | ✅ | ❌(默认) | ❌ |
| 桥接(Bridged) | ✅ | ✅ | ✅ |
| Host-Only | ❌ | ✅ | ✅ |
| NAT + Host-Only | ✅ | ✅ | ✅(需配置) |
虚拟机 → 设置 → 网络
适配器1:
启用网络连接 ✅
连接方式:NAT
ip addr
一般网卡是 eth0 或 enp0s3
dhclient
# 或
nmcli connection up eth0
测试:
ping 8.8.8.8
✅ 能 ping 通就说明联网成功
很多 CentOS 默认网卡是关闭的
cd /etc/sysconfig/network-scripts/
ls
vi ifcfg-eth0
改成:
ONBOOT=yes
BOOTPROTO=dhcp
保存后:
systemctl restart network
# 或
nmcli connection reload
网络 → 适配器1
连接方式:桥接网卡
名称:你的物理网卡(Wi-Fi 或 Ethernet)
dhclient
ip addr
你会得到一个和主机同网段的 IP
例如:
主机可以直接:
ssh root@192.168.1.20
适合 不想连外网,只做实验
VirtualBox:
主机网络管理器 → 创建 Host-Only 网络
CentOS:
BOOTPROTO=static
IPADDR=192.168.56.10
NETMASK=255.255.255.0
ip addr
没 IP → 网卡没启 / DHCP 没拿到
cat /etc/resolv.conf
应有:
nameserver 8.8.8.8
我可以直接给你一份精准配置命令。