在 VirtualBox 里的 CentOS 联网,常见有 3 种网络模式,设置方法如下(以 VirtualBox + CentOS 7/8 为例):
✅ 适合:只是想让 CentOS 能上外网
一般 NAT 默认就能用(DHCP)
ip addr
如果没有 IP:
dhclient
测试:
ping www.baidu.com
✅ 适合:需要 IP 固定、SSH、局域网访问
cd /etc/sysconfig/network-scripts/
ls
编辑网卡(如 ifcfg-enp0s3):
vi ifcfg-enp0s3
示例(DHCP):
BOOTPROTO=dhcp
ONBOOT=yes
或静态 IP:
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
✅ 适合:本地开发、不联外网
ls /sys/class/net
systemctl stop NetworkManager
systemctl restart network
systemctl stop firewalld
| 需求 | 模式 |
|---|---|
| 只上网 | NAT |
| 远程 SSH | 桥接 |
| 本地测试 | Host-Only |
如果你告诉我:
我可以给你精确配置文件。