58人参与 • 2025-11-07 • ar
在 centos 9 虚拟机中进行网络配置,可根据不同的网络连接模式(如桥接模式、nat 模式等)来操作,以下以常见的 nat 模式为例,详细介绍配置步骤。
打开虚拟机软件(如 vmware workstation 或 virtualbox),设置 centos 9 虚拟机的网络连接模式为 nat 模式。
centos 9 使用 networkmanager 来管理网络连接,网络配置文件通常位于 /etc/sysconfig/network-scripts/ 目录下,文件名一般以 ifcfg- 开头,后面跟着网络接口名称(如 eth0、ens33 等)。
打开终端,使用以下命令查看当前网络接口名称:
ip link show
假设网络接口名称为 ens33。
使用文本编辑器(如 vim 或 nano)打开对应的网络配置文件:
sudo vim /etc/sysconfig/network-scripts/ifcfg-ens33
修改配置文件内容,确保以下参数正确设置:
type=ethernet proxy_method=none browser_only=no bootproto=dhcp # 使用 dhcp 自动获取 ip 地址 defroute=yes ipv4_failure_fatal=no ipv6init=yes ipv6_autoconf=yes ipv6_defroute=yes ipv6_failure_fatal=no ipv6_addr_gen_mode=stable-privacy name=ens33 uuid=xxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx # 保持原有的 uuid device=ens33 onboot=yes # 开机自动启用网络接口
保存并退出编辑器。如果使用 vim,按 esc 键,输入 :wq 并回车;如果使用 nano,按 ctrl + x,然后按 y 确认保存,最后回车。
重启网络服务
修改配置文件后,需要重启网络服务使配置生效:
sudo systemctl restart networkmanager
验证网络连接
使用以下命令检查网络连接是否正常:
ping www.baidu.com
如果能够正常收到响应,说明网络连接配置成功。
手动配置静态 ip 地址
如果需要手动配置静态 ip 地址,可以将 bootproto 参数修改为 static,并添加以下参数:
ipaddr=192.168.1.100 # 静态 ip 地址 netmask=255.255.255.0 # 子网掩码 gateway=192.168.1.1 # 网关地址 dns1=8.8.8.8 # dns 服务器地址
修改完成后,保存文件并重启网络服务:
sudo systemctl restart networkmanager
在 centos 9 虚拟机中使用桥接模式进行网络配置,可让虚拟机直接连接到物理网络,就像一台独立的物理设备一样,以下是详细的配置步骤。
vmware workstation:
virtualbox:
确定网络接口名称:
打开终端,使用以下命令查看当前网络接口名称:
ip link show
假设网络接口名称为 ens33。
编辑配置文件:
使用文本编辑器(如 vim 或 nano)打开对应的网络配置文件:
sudo vim /etc/sysconfig/network-scripts/ifcfg-ens33
自动获取 ip 地址(dhcp 方式):
若希望通过 dhcp 自动获取 ip 地址,确保配置文件内容如下:
type=ethernet proxy_method=none browser_only=no bootproto=dhcp defroute=yes ipv4_failure_fatal=no ipv6init=yes ipv6_autoconf=yes ipv6_defroute=yes ipv6_failure_fatal=no ipv6_addr_gen_mode=stable-privacy name=ens33 uuid=xxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx # 保持原有的 uuid device=ens33 onboot=yes
手动配置静态 ip 地址:
如果需要手动配置静态 ip 地址,将 bootproto 参数修改为 static,并添加以下参数:
type=ethernet proxy_method=none browser_only=no bootproto=static defroute=yes ipv4_failure_fatal=no ipv6init=yes ipv6_autoconf=yes ipv6_defroute=yes ipv6_failure_fatal=no ipv6_addr_gen_mode=stable-privacy name=ens33 uuid=xxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx # 保持原有的 uuid device=ens33 onboot=yes ipaddr=192.168.1.100 # 静态 ip 地址,需与物理网络在同一网段 netmask=255.255.255.0 # 子网掩码 gateway=192.168.1.1 # 网关地址,通常是路由器的 ip 地址 dns1=8.8.8.8 # dns 服务器地址 dns2=8.8.4.4 # 备用 dns 服务器地址
注意,ipaddr、netmask、gateway 和 dns 等参数需要根据你的实际网络环境进行修改。
保存并退出配置文件
重启网络服务
修改配置文件后,需要重启网络服务使配置生效:
sudo systemctl restart networkmanager
验证网络连接
使用以下命令检查网络连接是否正常:
ping www.baidu.com
若能正常收到响应,说明网络连接配置成功。你还可以使用 ip addr show 命令查看当前网络接口的 ip 地址信息。
以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。
您想发表意见!!点此发布评论
版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。
发表评论