如何在 Linux 系统中开启和配置网络 # 最简单快捷的方式输入nmtui进入网络管理器持久化配置使用NetworkManagerCentOS/RHEL的配置文件确保配置在重启后仍然有效。检查NetworkManager状态systemctl status NetworkManager如果显示inactive (dead)需要启动服务sudo systemctl start NetworkManagersudo systemctl enable NetworkManager # 设置开机自启1.完全使用nmcli重新配置sudo nmcli connection delete 连接名注连接名可通过 nmcli connection show 查看如eth0、ens33、wlan0无线等。sudo nmcli connection add con-name my-static-eth0 ifname eth0 type ethernet ip4 192.168.1.100/24 gw4 192.168.1.1注ip地址前三位及gw地址根据自己虚拟机配置的子网ip设置/24表示子网掩码中连续的1的位数即255.255.255.0根据虚拟机配置的子网掩码设置因为24个1对应11111111.11111111.11111111.00000000/24→255.255.255.0/16→255.255.0.0适用于较大的网络如192.168.0.0/16/8→255.0.0.0适用于非常大的网络如10.0.0.0/8sudo nmcli connection modify my-static-eth0 ipv4.dns 8.8.8.8sudo nmcli connection up my-static-eth02.禁用传统network服务避免冲突:sudo systemctl stop networksudo systemctl disable network3.查看所有连接配置1.确保你的配置如my-static-eth0存在并且TYPE为Generic或Ethernet。nmcli connection show2.确认ipv4.method是否为manual静态 IP或autoDHCP。3.确认ipv4.addresses、ipv4.gateway、ipv4.dns是否正确。nmcli connection show my-static-eth04.手动激活连接sudo nmcli connection up my-static-eth05.测试连接查看ip addr show测试访问外网 ping 8.8.8.8ping百度测试ping www.baidu.com重启服务器再次测试连接2.扩展使用ifconfig或ip命令避免直接使用ifconfig或ip命令因为它们是临时生效的1. 检查网络接口状态在配置网络之前首先需要确认系统中的网络接口是否存在以及当前状态。查看网络接口baship link show# 或ls /sys/class/net常见的网络接口名称eth0、ens33、wlan0无线等。如果接口显示为DOWN需要手动启用。2. 启用网络接口使用ip或ifconfig如果已安装net-tools命令启用网络接口。使用ip命令bashsudo ip link set 接口名 up# 示例sudo ip link set eth0 up使用ifconfig命令需安装 net-toolsbashsudo ifconfig 接口名 up如果提示ifconfig: command not found可以通过以下命令安装net-toolsbash# Debian/Ubuntusudo apt-get updatesudo apt-get install net-tools# CentOS/RHELsudo yum install net-tools3. 配置网络动态 IP 或静态 IP动态 IPDHCP如果网络环境支持 DHCP可以通过以下命令获取动态 IPbashsudo dhclient 接口名# 示例sudo dhclient eth0静态 IP如果需要配置静态 IP需根据不同的 Linux 发行版编辑对应的网络配置文件。Debian/Ubuntu使用 netplan编辑 netplan 配置文件bashsudo nano /etc/netplan/*.yaml示例配置假设接口为eth0yamlnetwork:version: 2ethernets:eth0:dhcp4: noaddresses: [192.168.1.100/24]gateway4: 192.168.1.1nameservers:addresses: [8.8.8.8, 8.8.4.4]应用配置bashsudo netplan applyCentOS/RHEL使用 NetworkManager使用nmcli配置静态 IPbashsudo nmcli connection modify 连接名 ipv4.addresses 192.168.1.100/24 ipv4.gateway 192.168.1.1 ipv4.dns 8.8.8.8 ipv4.method manual连接名可通过 nmcli connection show 查看如eth0、ens33、wlan0无线等。ipv4.addresses 192.168.1.100/24 \ipv4.gateway 192.168.1.1 \ 192.168.1地址前三位根据自己虚拟机的子网ip设置重启连接sudo nmcli connection down 连接名sudo nmcli connection up 连接名4. 验证网络连接检查接口状态和 IP 地址baship addr show测试网络连通性bashping 8.8.8.8 # 测试外网连通性ping 192.168.1.1 # 测试网关连通性5. 启用无线网络可选扫描可用 Wi-Fibashsudo iwlist 无线接口名 scan# 示例sudo iwlist wlan0 scan连接 Wi-Fibashsudo nmcli device wifi connect SSID password 密码常见问题排查接口未显示检查网卡是否被识别bashlspci | grep -i ethernet # 有线网卡lsusb # 无线网卡确保内核加载了正确的驱动bashlsmod | grep 驱动名无法获取 IP检查 DHCP 服务是否运行bashsystemctl status dhcpd # 传统 DHCP 服务systemctl status NetworkManager # NetworkManager 管理的 DHCP确认防火墙未阻止 DHCP 请求。DNS 解析失败检查/etc/resolv.conf是否包含有效的 DNS 服务器。使用dig或nslookup测试 DNS 解析bashdig example.comnslookup example.com总结通过以上步骤你可以成功开启并配置 Linux 系统的网络。如果仍有问题可以根据具体的错误信息进一步排查。希望本文对你有所帮助

相关新闻

最新新闻

KMS智能激活工具:5分钟搞定Windows和Office永久激活终极指南

KMS智能激活工具:5分钟搞定Windows和Office永久激活终极指南

KMS智能激活工具:5分钟搞定Windows和Office永久激活终极指南 【免费下载链接】KMS_VL_ALL_AIO Smart Activation Script 项目地址: https://gitcode.com/gh_mirrors/km/KMS_VL_ALL_AIO 还在为Windows系统弹出激活提示而烦恼吗?Office突然变成只读…

2026/8/11 19:11:31
从源码到应用:csvtomd核心函数详解与自定义开发指南

从源码到应用:csvtomd核心函数详解与自定义开发指南

从源码到应用:csvtomd核心函数详解与自定义开发指南 【免费下载链接】csvtomd 📝📊 Convert your CSV files into Markdown tables. 项目地址: https://gitcode.com/gh_mirrors/cs/csvtomd csvtomd是一款轻量级实用工具,能…

2026/8/11 19:11:31
RedCloud-OS开发者指南:贡献代码前必须了解的架构设计

RedCloud-OS开发者指南:贡献代码前必须了解的架构设计

RedCloud-OS开发者指南:贡献代码前必须了解的架构设计 【免费下载链接】RedCloud-OS RedCloudOS is a Cloud Adversary Simulation Operating System for Red Teams to assess the Cloud Security of Leading Cloud Service Providers (CSPs) 项目地址: https://g…

2026/8/11 19:11:31
.NET Core Serverless 实战:Azure Functions 与 AWS Lambda 部署指南

.NET Core Serverless 实战:Azure Functions 与 AWS Lambda 部署指南

.NET Core Serverless 实战:Azure Functions 与 AWS Lambda 部署指南Serverless 架构让开发者专注于业务逻辑,无需管理服务器。本文将对比 Azure Functions 和 AWS Lambda 两大主流 Serverless 平台,并给出 .NET Core 项目的完整部署方案。一…

2026/8/11 19:11:31
为什么React开发者应该拥抱Functional CSS?10个令人惊叹的优势解析

为什么React开发者应该拥抱Functional CSS?10个令人惊叹的优势解析

为什么React开发者应该拥抱Functional CSS?10个令人惊叹的优势解析 【免费下载链接】react-functional-css-protips :sunglasses: Functional CSS - The Good, The Bad, and Some Protips for React.js Users 项目地址: https://gitcode.com/gh_mirrors/re/react-…

2026/8/11 19:11:31
技术实战中代价最高的错误:数据库事务、缓存与分布式系统避坑指南

技术实战中代价最高的错误:数据库事务、缓存与分布式系统避坑指南

最近在整理巡回赛技术复盘时,发现一个普遍现象:很多团队在技术选型和架构设计上投入巨大,却在一些看似基础的“技术操作”上反复踩坑,导致线上故障、性能瓶颈甚至数据丢失。这些错误往往不是高深算法或复杂架构的问题,…

2026/8/11 19:06:31