在 VirtualBox 中的 CentOS 联网,通常有两种常见场景:NAT(能上网但不能被外部访问) 和 桥接(和宿主机在同一局域网)。下面按步骤说明。
ip addr
一般会看到 eth0 或 enp0s3
dhclient
或
nmcli con up eth0
测试:
ping www.baidu.com
nmcli device status
nmcli con up <连接名>
或者使用 DHCP:
dhclient
编辑网卡配置(示例):
vi /etc/sysconfig/network-scripts/ifcfg-eth0
示例(DHCP):
BOOTPROTO=dhcp
ONBOOT=yes
示例(静态):
BOOTPROTO=static
ONBOOT=yes
IPADDR=192.168.1.100
NETMASK=255.255.255.0
GATEWAY=192.168.1.1
DNS1=8.8.8.8
重启网络:
systemctl restart network
dhclient -r
dhclient
systemctl stop firewalld
建议安装 VirtualBox Guest Additions,提升兼容性。
| 需求 | 推荐方式 |
|---|---|
| 只上网 | NAT |
| 局域网访问 | 桥接 |
| 固定 IP | 桥接 + 静态 |
如果你告诉我 CentOS 版本(7 / 8 / Stream) 和 宿主机系统(Windows / macOS / Linux),我可以给你更精确的步骤。