在 VirtualBox 里的 CentOS 设置网络,常见有 3 种方式,先说结论,再给具体操作。
特点
适合
✅ 一般不需要改配置
特点
设置步骤
CentOS 内一般自动获取 IP:
ip addr
如果不能上网:
dhclient
特点
设置步骤
vboxnet0CentOS 配置静态 IP(示例):
vi /etc/sysconfig/network-scripts/ifcfg-enp0s3
BOOTPROTO=static
IPADDR=192.168.56.10
NETMASK=255.255.255.0
ONBOOT=yes
systemctl restart network
ip addr
常见:eth0、enp0s3
vi /etc/sysconfig/network-scripts/ifcfg-enp0s3
DHCP 示例
BOOTPROTO=dhcp
ONBOOT=yes
静态 IP 示例
BOOTPROTO=static
IPADDR=192.168.1.100
NETMASK=255.255.255.0
GATEWAY=192.168.1.1
DNS1=8.8.8.8
ONBOOT=yes
重启网络:
systemctl restart network
nmcli connection show
nmcli connection modify eth0 ipv4.method manual ipv4.addresses 192.168.1.100/24 ipv4.gateway 192.168.1.1 ipv4.dns 8.8.8.8
nmcli connection up eth0
❌ 没网卡 / 没 IP
✅ 检查 VirtualBox 是否装了 增强功能(Guest Additions)
❌ 能 ping IP 不能 ping 域名
✅ DNS 没设:
DNS1=8.8.8.8
❌ SSH 连不上
✅ 确认:
systemctl status sshd
firewall-cmd --add-service=ssh --permanent
firewall-cmd --reload
| 用途 | 推荐 |
|---|---|
| 学习 / 上网 | NAT |
| 开发 / 访问 | 桥接 |
| 本地集群 | Host-Only + NAT |
如果你告诉我:
我可以直接给你 一套可用配置。