686 字
CentOS 虚拟机网络连接配置指南

适用环境#

  • 虚拟化平台: VMware Workstation / VirtualBox / Hyper-V
  • 系统版本: CentOS 7.x / CentOS 8.x / CentOS Stream
  • 网络模式: NAT(推荐) / 桥接(Bridged)

一、虚拟机网络模式设置#

1. VMware Workstation#

  1. 右键虚拟机 → 设置网络适配器
  2. 选择模式:
    • NAT 模式:共享宿主机 IP,可访问外网
    • 桥接模式:独立 IP,与宿主机同局域网
  3. 勾选 已连接启动时连接

2. VirtualBox#

  1. 选中虚拟机 → 设置网络
  2. 连接方式 选择:
    • NAT(默认)
    • 桥接网卡(需选择宿主机物理网卡)
  3. 高级中启用 电缆已连接

二、CentOS 系统内配置#

1. 查看网络接口名称#

Terminal window
ip addr # 或 nmcli device status

2. 动态 IP(DHCP 自动获取)#

Terminal window
# CentOS 7/8 通用命令
nmcli connection modify "有线连接 1" ipv4.method auto
nmcli connection up "有线连接 1"
# 或直接重启网络服务
systemctl restart NetworkManager

3. 静态 IP(手动配置)#

Terminal window
# 修改网络配置文件(CentOS 7)
vi /etc/sysconfig/network-scripts/ifcfg-ens33
# 修改以下参数
BOOTPROTO=static
IPADDR=192.168.1.100 # 自定义 IP
NETMASK=255.255.255.0
GATEWAY=192.168.1.1 # 宿主机网关
DNS1=8.8.8.8
ONBOOT=yes # 开机自启
# 重启网络服务
systemctl restart network
# CentOS 8+ 使用 nmcli
nmcli connection modify ens33 ipv4.addresses 192.168.1.100/24
nmcli connection modify ens33 ipv4.gateway 192.168.1.1
nmcli connection modify ens33 ipv4.dns "8.8.8.8 114.114.114.114"
nmcli connection modify ens33 ipv4.method manual
nmcli connection up ens33

三、验证网络连通性#

Terminal window
# 查看 IP 地址
ip addr show ens33
# 测试内网连通性
ping 192.168.1.1 # 网关地址
# 测试外网连通性
ping baidu.com
# 检查 DNS 解析
nslookup baidu.com

四、防火墙与 SELinux 配置#

1.临时关闭防火墙#

Terminal window
systemctl stop firewalld # 停止防火墙
systemctl disable firewalld # 禁止开机启动(可选)

2.开放特定端口(示例:开放 80 端口)#

Terminal window
firewall-cmd --zone=public --add-port=80/tcp --permanent
firewall-cmd --reload

3. 禁用 SELinux(可选)#

Terminal window
setenforce 0 # 临时关闭
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config # 永久关闭

五,常见问题解决#

问题现象解决方案
无法获取 IP1. 检查虚拟机网络模式设置
2. 确认 DHCP 服务正常
3. 重启 NetworkManager 服务
能 ping IP 但不能域名1. 检查 /etc/resolv.conf DNS 配置
2. 禁用防火墙或添加 DNS 规则
宿主机无法访问虚拟机1. 检查防火墙是否放行端口
2. 桥接模式下确认 IP 在同一网段
网络服务启动失败1. 检查配置文件语法
2. 使用 journalctl -xe 查看日志

六,拓展配置#

1.多网卡配置#

Terminal window
# 创建绑定接口
nmcli connection add type bond con-name bond0 ifname bond0 mode active-backup
# 添加从属网卡
nmcli connection add type bond-slave ifname ens33 master bond0
nmcli connection add type bond-slave ifname ens34 master bond0
# 激活配置
nmcli connection up bond0

2. 网络速度测试#

Terminal window
# 安装 speedtest-cli
pip3 install speedtest-cli
# 执行测速
speedtest

文档版本: v1.3
最后更新: 2023-10-07
参考链接:


💬 评论