新装Linux系统网络不通?老司机手把手教你从诊断到修复,一篇全搞定

admin 2026-06-16 04:49:25 网络安全文章 来源:ZONE.CI 全球网 0 阅读模式

文章总结: 该文档详细介绍了新装Linux系统网络不通的故障排查与修复方法。文章从故障现象分析入手,系统性地阐述了硬件识别、驱动加载、NetworkManager服务状态、网卡启用、IP地址获取等关键排查步骤,并提供了具体的命令操作和修复方案。文档还包含生产环境操作规范和预防措施,具有很高的实用价值。 综合评分: 75 文章分类: 安全运营,解决方案


cover_image

新装Linux系统网络不通?老司机手把手教你从诊断到修复,一篇全搞定

原创

刘军军 刘军军

运维星火燎原

2026年6月12日 00:42 山西

在小说阅读器读本章

去阅读

目录

  1. 故障概述
  2. 故障现象
  3. 排查思路总览
  4. 详细排查步骤
  5. 修复方案
  6. 验证测试
  7. 生产环境操作规范
  8. 预防措施
  9. 命令速查表

1. 故障概述

1.1 问题背景

在新安装的Linux系统(特别是openEuler、CentOS等企业级发行版)中,系统安装完成后经常遇到网络未正确配置的情况。这会导致:

  • 无法执行系统更新(dnf updateyum update
  • 无法安装软件包
  • 无法访问外部网络资源
  • 远程管理受阻(SSH无法连接)

1.2 典型场景

| 场景 | 描述 | | — | — | | 虚拟机安装 | VMware、VirtualBox、KVM等虚拟化平台安装的系统 | | 物理服务器 | 裸金属服务器首次开机配置 | | 云环境 | 私有云环境中的镜像部署 | | 离线安装 | 使用ISO镜像安装后未配置网络 |


2. 故障现象

2.1 常见报错信息

# 1. 软件源访问失败
[root@localhost ~]# dnf update
Failed to download metadata for repo 'openeuler'
Error: Failed to download metadata for repo 'openeuler'

# 2. 网络连接超时
[root@localhost ~]# dnf install vim
CentOS Linux 8 - AppStream                      0.0 B/s | 0 B     00:02
Errors during downloading metadata for repo 'appstream':
  - Curl error (-28): Timeout was reached

# 3. IP地址获取失败
[root@localhost ~]# ip a
1: lo: <LOOPBACK,UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
&nbsp; &nbsp; link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
&nbsp; &nbsp; inet 127.0.0.1/8 scope host lo
&nbsp; &nbsp; &nbsp; &nbsp;valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST> mtu 1500 qdisc mq state DOWN group default qlen 1000
&nbsp; &nbsp; link/ether 52:54:00:12:34:56 brd ff:ff:ff:ff:ff:ff
&nbsp; &nbsp; # 注意:eth0 没有 inet 地址,state 为 DOWN

2.2 故障表现

| 症状 | 说明 | | — | — | | ip a 无物理网卡IP | 网卡未获取到IP地址 | | ip a 网卡状态为DOWN | 网卡未启用 | | ping www.baidu.com 超时 | DNS解析正常但网络不通 | | ping 8.8.8.8 超时 | 物理网络连接问题 | | dnf install 失败 | 软件源无法访问 |


3. 排查思路总览

3.1 排查流程图

3.2 排查优先级

| 优先级 | 检查项 | 耗时 | 重要性 | | — | — | — | — | | 1 | 网卡硬件识别 | 1分钟 | ⭐⭐⭐⭐⭐ | | 2 | 网卡驱动加载 | 1分钟 | ⭐⭐⭐⭐⭐ | | 3 | NetworkManager服务 | 1分钟 | ⭐⭐⭐⭐⭐ | | 4 | 网卡启用状态 | 30秒 | ⭐⭐⭐⭐ | | 5 | IP地址获取 | 30秒 | ⭐⭐⭐⭐ | | 6 | DNS配置 | 1分钟 | ⭐⭐⭐ | | 7 | 软件源配置 | 2分钟 | ⭐⭐⭐ |


4. 详细排查步骤

4.1 Step 1: 检查网卡硬件识别

4.1.1 查看网卡硬件信息

# 方法1:使用lspci查看PCI设备列表
[lspci |&nbsp;grep-i&nbsp;ethernet]
00:03.0 Ethernet controller: Red Hat, Inc. Virtio network device (rev&nbsp;01)

# 方法2:使用ip link查看网络接口
[ip link show]
1: lo: <LOOPBACK,UP> mtu&nbsp;65536&nbsp;qdisc noqueue state UNKNOWN
&nbsp; &nbsp; link/loopback&nbsp;00:00:00:00:00:00 brd&nbsp;00:00:00:00:00:00
2: eth0: <BROADCAST,MULTICAST> mtu&nbsp;1500&nbsp;qdisc noop state DOWN
&nbsp; &nbsp; link/ether&nbsp;52:54:00:12:34:56 brd ff:ff:ff:ff:ff:ff
3: ens33: <BROADCAST,MULTICAST> mtu&nbsp;1500&nbsp;qdisc noop state DOWN
&nbsp; &nbsp; link/ether&nbsp;00:0c:29:ab:cd:ef brd ff:ff:ff:ff:ff:ff

# 方法3:查看系统网络设备
[cat /proc/net/dev]
Inter-| &nbsp; Receive &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;| &nbsp;Transmit
&nbsp;face |bytes &nbsp; &nbsp;packets errs drop fifo frame compressed multicast|bytes &nbsp; &nbsp;packets errs drop fifo frame compressed colls carrier
&nbsp; &nbsp; lo: &nbsp;1234&nbsp; &nbsp; &nbsp;10&nbsp; &nbsp;&nbsp;0&nbsp; &nbsp;&nbsp;0&nbsp; &nbsp;&nbsp;0&nbsp; &nbsp; &nbsp;0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0&nbsp; &nbsp; &nbsp;1234&nbsp; &nbsp; &nbsp;10&nbsp; &nbsp;&nbsp;0&nbsp; &nbsp;&nbsp;0&nbsp; &nbsp;&nbsp;0&nbsp; &nbsp; &nbsp;0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0
&nbsp; eth0: &nbsp; &nbsp; &nbsp;0&nbsp; &nbsp; &nbsp;&nbsp;0&nbsp; &nbsp;&nbsp;0&nbsp; &nbsp;&nbsp;0&nbsp; &nbsp;&nbsp;0&nbsp; &nbsp; &nbsp;0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;0&nbsp; &nbsp; &nbsp;&nbsp;0&nbsp; &nbsp;&nbsp;0&nbsp; &nbsp;&nbsp;0&nbsp; &nbsp;&nbsp;0&nbsp; &nbsp; &nbsp;0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0

4.1.2 故障判断

| 情况 | 判断 | 处理 | | — | — | — | | lspci 能看到网卡 | 硬件正常 | 进入Step 2 | | lspci 看不到网卡 | 硬件问题 | 检查虚拟机配置或物理网卡 | | ip link 无eth0/ens33 | 内核未识别 | 加载驱动或检查配置 |

4.1.3 常见网卡名称

| 环境 | 常见网卡名 | 说明 | | — | — | — | | VMware | ens33, eth0 | 准虚拟化网卡 | | KVM | virtio_net | VirtIO驱动 | | VirtualBox | enp0s3 | PCI总线命名 | | 物理服务器 | em1, p1p1 | 品牌服务器命名 |


4.2 Step 2: 检查内核模块(驱动)

4.2.1 检查驱动模块状态

# 查看已加载的网络相关模块
[lsmod |&nbsp;grep-E"net|eth|e1000|virtio|bridge"]
virtio_net &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;12345&nbsp;&nbsp;0
virtio_blk &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;6789&nbsp;&nbsp;0
virtio_pci &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;2345&nbsp;&nbsp;0
virtio_ring &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;11111&nbsp;&nbsp;2&nbsp;virtio_net,virtio_blk
virtio &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;1234&nbsp;&nbsp;4&nbsp;virtio_net,virtio_blk,virtio_pci,virtio_ring

# 查看特定模块信息
[modinfo virtio_net]
filename: &nbsp; &nbsp; &nbsp; /lib/modules/5.10.0-xxx.el8.x86_64/kernel/drivers/net/virtio_net.ko.xz
version: &nbsp; &nbsp; &nbsp; &nbsp;1.0.0
license: &nbsp; &nbsp; &nbsp; &nbsp;GPL
description: &nbsp; &nbsp;Virtio network driver
author: &nbsp; &nbsp; &nbsp; &nbsp; Rusty Russell
parm: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; csum:bool
parm: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; gso:bool

# 检查模块是否加载
[modprobe&nbsp;-c&nbsp;|&nbsp;grep&nbsp;virtio_net]
# 如果没有输出,说明模块未加载

4.2.2 常见虚拟化驱动

| 驱动名称 | 适用环境 | 性能 | | — | — | — | | virtio_net | KVM, QEMU | 最佳(准虚拟化) | | e1000 | VMware, VirtualBox | 一般(模拟驱动) | | vmxnet3 | VMware | 较好(VMware专用) | | ixgbevf | VMware vSphere | 较好(SR-IOV) |

4.2.3 手动加载驱动

# 如果驱动未加载,手动加载
[modprobe virtio_net]

# 设置开机自动加载
[echo&nbsp;"virtio_net"&nbsp;> /etc/modules-load.d/virtio_net.conf]

# 验证加载
[lsmod |&nbsp;grep&nbsp;virtio_net]
virtio_net &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;12345&nbsp;&nbsp;0

4.3 Step 3: 检查NetworkManager服务

4.3.1 查看服务状态

# 查看NetworkManager服务状态
[systemctl status NetworkManager]
● NetworkManager.service&nbsp;-&nbsp;Network Manager
&nbsp; &nbsp;Loaded: loaded (/usr/lib/systemd/system/NetworkManager.service; enabled; vendor preset: enabled)
&nbsp; &nbsp;Active: active (running) since Wed&nbsp;2024-01-1510:30:00 CST; 1h 30min ago
&nbsp; &nbsp; &nbsp;Docs: man:NetworkManager(8)
&nbsp; &nbsp;Main PID:&nbsp;1024&nbsp;(NetworkManager)
&nbsp; &nbsp; &nbsp; Tasks:&nbsp;3&nbsp;(limit:&nbsp;32768)
&nbsp; &nbsp; &nbsp;Memory:&nbsp;8.5M
&nbsp; &nbsp; &nbsp; &nbsp; CPU: 123ms
&nbsp; &nbsp; &nbsp;CGroup: /system.slice/NetworkManager.service
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;└─1024 /usr/sbin/NetworkManager&nbsp;-n--config-dir=/etc/NetworkManager

# 如果服务未运行,状态会显示为
# Active: inactive (dead)

4.3.2 启动NetworkManager服务

# 启动服务
[systemctl&nbsp;start&nbsp;NetworkManager]

# 设置开机自启
[systemctl enable NetworkManager]

# 重新加载systemd配置
[systemctl daemon-reload]

4.3.3 服务状态说明

| 状态 | 说明 | 处理 | | — | — | — | | active (running) | 服务正常运行 | 检查网络配置 | | inactive (dead) | 服务未启动 | 启动服务 | | failed | 服务启动失败 | 查看日志排查原因 | | masked | 服务被禁用 | 取消mask后启动 |


4.4 Step 4: 检查网卡启用状态

4.4.1 使用nmcli命令检查

# 查看所有网络连接
[nmcli con show]
NAME &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;UUID &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;TYPE &nbsp; &nbsp; &nbsp;DEVICE
System eth0 &nbsp; &nbsp; &nbsp; &nbsp; 5fb06bd0-0bb0-7ffb-45f2-d9f72e3935f1 &nbsp;ethernet &nbsp;eth0
ens33 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 6d4f3a2b-8c9e-4d5e-9f1a-2b3c4d5e6f7a &nbsp;ethernet &nbsp;--

# 查看网络设备状态
[nmcli dev status]
DEVICE &nbsp;TYPE &nbsp; &nbsp; &nbsp;STATE &nbsp; &nbsp; &nbsp;CONNECTION
eth0 &nbsp; &nbsp;ethernet &nbsp;connected &nbsp;System eth0
ens33 &nbsp; ethernet &nbsp;disconnected &nbsp;--

# 查看具体网卡详情
[nmcli dev show eth0]
GENERAL.DEVICE: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; eth0
GENERAL.TYPE: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ethernet
GENERAL.HWADDR: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;52:54:00:12:34:56
GENERAL.MTU: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;1500
GENERAL.STATE: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;100&nbsp;(connected)
GENERAL.CONNECTION: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System eth0
GENERAL.CON-PATH: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; /org/freedesktop/NetworkManager/ActiveConnection/1
IP4.ADDRESS[1]: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;192.168.1.100/24
IP4.GATEWAY: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;192.168.1.1
IP4.DNS[1]: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;8.8.8.8
IP4.DNS[2]: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;114.114.114.114

4.4.2 常见问题及处理

# 问题1:网卡未启用
# 解决:启用网卡
nmcli con up&nbsp;"System eth0"
# 或
nmcli dev connect eth0

# 问题2:网卡显示"unavailable"
# 解决:检查硬件连接和驱动
nmcli dev&nbsp;set&nbsp;eth0 managed&nbsp;yes

# 问题3:网卡显示"unmanaged"
# 解决:修改NetworkManager配置
nmcli dev&nbsp;set&nbsp;eth0 managed&nbsp;yes

4.4.3 使用传统ip命令(备选)

# 启用网卡
ip link&nbsp;set&nbsp;eth0 up

# 查看网卡状态
ip link show eth0

# 查看IP地址
ip addr show eth0

# 添加IP地址(临时)
ip addr add&nbsp;192.168.1.100/24 dev eth0

# 删除IP地址
ip addr del&nbsp;192.168.1.100/24 dev eth0

4.5 Step 5: 检查DNS配置

4.5.1 查看DNS配置文件

# 查看DNS配置
[cat /etc/resolv.conf]
# Generated by NetworkManager
nameserver&nbsp;8.8.8.8
nameserver&nbsp;114.114.114.114
nameserver&nbsp;192.168.1.1

# 如果文件为空或无nameserver,DNS未配置

4.5.2 测试DNS解析

# 测试DNS解析
nslookup www.baidu.com
Server: &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;8.8.8.8
Address: &nbsp; &nbsp; &nbsp; &nbsp;8.8.8.8#53

Non-authoritative answer:
Name: &nbsp; www.baidu.com
Address:&nbsp;220.181.38.149
Name: &nbsp; www.baidu.com
Address:&nbsp;220.181.38.150

# 或使用dig命令
dig www.baidu.com

4.5.3 DNS故障排查

# 检查DNS服务器是否可达
ping-c38.8.8.8

# 检查DNS端口是否可达
telnet8.8.8.8&nbsp;53

# 查看DNS服务状态
systemctl status systemd-resolved

4.6 Step 6: 检查软件源配置

4.6.1 查看软件源配置

# 查看所有repo源
dnf repolist all
# 或
yum repolist all

# 输出示例
repo id &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;repo name &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;status
openeuler &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;openEuler &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;enabled
update &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; openEuler Update &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;enabled
source&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;openEuler Source &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;disabled
debuginfo &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;openEuler Debuginfo &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; disabled

4.6.2 查看具体repo文件

# 查看openEuler基础源配置
cat&nbsp;/etc/yum.repos.d/openEuler.repo

[openEuler]
name=openEuler
baseurl=https://repo.openeuler.org/openEuler-24.03-LTS/OS/$basearch/
enabled=1
gpgcheck=0
priority=1

4.6.3 测试软件源连接

# 测试网络到软件源
curl-I&nbsp;https://repo.openeuler.org

# 如果无法访问,检查是否需要代理
curl-I--proxy&nbsp;http://proxy.example.com:8080 https://repo.openeuler.org

# 清理缓存并重建
dnf clean all
dnf makecache

5. 修复方案

5.1 方案一:使用DHCP自动获取IP(推荐)

适用于内网环境,配置简单:

# 1. 启用网卡并设置为自动获取IP
nmcli con mod&nbsp;"System eth0"&nbsp;ipv4.method auto
nmcli con mod&nbsp;"System eth0"&nbsp;ipv4.gateway&nbsp;""
nmcli con mod&nbsp;"System eth0"&nbsp;ipv4.addresses&nbsp;""
nmcli con mod&nbsp;"System eth0"&nbsp;ipv4.dns&nbsp;""

# 2. 激活连接
nmcli con down&nbsp;"System eth0"
nmcli con up&nbsp;"System eth0"

# 3. 验证
ip addr show eth0
# 应该能看到类似:inet 192.168.1.100/24

5.2 方案二:配置静态IP

适用于需要固定IP的生产环境:

# 1. 配置静态IP地址
nmcli con mod&nbsp;"System eth0"&nbsp;ipv4.method manual
nmcli con mod&nbsp;"System eth0"&nbsp;ipv4.addresses&nbsp;192.168.1.100/24
nmcli con mod&nbsp;"System eth0"&nbsp;ipv4.gateway&nbsp;192.168.1.1
nmcli con mod&nbsp;"System eth0"&nbsp;ipv4.dns&nbsp;"8.8.8.8,114.114.114.114"

# 2. 配置DNS搜索域(可选)
nmcli con mod&nbsp;"System eth0"&nbsp;ipv4.dns-search&nbsp;"example.com"

# 3. 激活连接
nmcli con down&nbsp;"System eth0"
nmcli con up&nbsp;"System eth0"

# 4. 验证
ip addr show eth0
ping&nbsp;-c&nbsp;3&nbsp;192.168.1.1

5.3 方案三:更换软件源(内网/离线环境)

5.3.1 企业内网镜像源

# 1. 备份原配置文件
cp&nbsp;/etc/yum.repos.d/openEuler.repo /etc/yum.repos.d/openEuler.repo.bak

# 2. 创建新的repo文件
cat&nbsp;> /etc/yum.repos.d/openEuler.repo <<&nbsp;'EOF'
[openEuler]
name=openEuler&nbsp;-&nbsp;Local Mirror
baseurl=http://192.168.1.200/repo/openEuler-24.03-LTS/OS/x86_64/
enabled=1
gpgcheck=0
priority=1

[update]
name=openEuler Update&nbsp;-&nbsp;Local Mirror
baseurl=http://192.168.1.200/repo/openEuler-24.03-LTS/Update/x86_64/
enabled=1
gpgcheck=0
priority=1
EOF

# 3. 清理并重建缓存
dnf clean all
dnf makecache

5.3.2 配置HTTP代理

# 1. 设置全局代理(所有服务使用)
cat&nbsp;>> /etc/environment <<&nbsp;'EOF'
export&nbsp;http_proxy=http://proxy.example.com:8080
export&nbsp;https_proxy=http://proxy.example.com:8080
export&nbsp;no_proxy="localhost,127.0.0.1,.example.com"
EOF

# 2. 为dnf单独设置代理
cat&nbsp;>> /etc/dnf/dnf.conf <<&nbsp;'EOF'
proxy=http://proxy.example.com:8080
proxy_username=your_username
proxy_password=your_password
EOF

# 3. 测试代理
dnf makecache

5.3.3 使用阿里云镜像源(公网)

# 备份原配置
cp&nbsp;/etc/yum.repos.d/openEuler.repo /etc/yum.repos.d/openEuler.repo.bak

# 创建阿里云镜像配置
cat&nbsp;> /etc/yum.repos.d/openEuler.repo <<&nbsp;'EOF'
[openEuler]
name=openEuler
baseurl=https://mirrors.aliyun.com/openEuler/openEuler-24.03-LTS/OS/x86_64/
enabled=1
gpgcheck=0
priority=1
EOF

# 重建缓存
dnf clean all
dnf makecache

6. 验证测试

6.1 网络连通性测试

# 1. 测试网关连通性
ping&nbsp;-c&nbsp;3&nbsp;192.168.1.1

# 2. 测试DNS解析
ping&nbsp;-c&nbsp;3&nbsp;www.baidu.com

# 3. 测试公网IP
ping&nbsp;-c&nbsp;3&nbsp;8.8.8.8

# 4. 测试端口连通性
telnet&nbsp;repo.openeuler.org&nbsp;443
curl&nbsp;-I&nbsp;https://repo.openeuler.org

6.2 软件源测试

# 1. 列出可用软件包
dnf list available | head&nbsp;-20

# 2. 搜索软件包
dnf search&nbsp;vim

# 3. 安装测试包
dnf install&nbsp;-yvim-q

# 4. 系统更新测试
dnf update&nbsp;--dry-run

6.3 完整验证脚本

#!/bin/bash
# network_diagnosis.sh - 网络故障诊断脚本

echo&nbsp;"=========================================="
echo&nbsp;"Linux网络故障诊断报告"
echo&nbsp;"时间:&nbsp;$(date '+%Y-%m-%d %H:%M:%S')"
echo&nbsp;"主机名:&nbsp;$(hostname)"
echo&nbsp;"=========================================="
echo&nbsp;""

echo&nbsp;"【1. 网卡硬件检查】"
echo&nbsp;"PCI设备列表(以太网控制器):"
lspci |&nbsp;grep-i&nbsp;ethernet
echo&nbsp;""

echo&nbsp;"【2. 内核模块检查】"
echo&nbsp;"已加载的网络模块:"
lsmod |&nbsp;grep-E"net|eth| virtio"
echo&nbsp;""

echo&nbsp;"【3. 网络接口状态】"
ip&nbsp;-brief&nbsp;addr show
echo&nbsp;""

echo&nbsp;"【4. NetworkManager状态】"
systemctl is-active NetworkManager
echo&nbsp;""

echo&nbsp;"【5. DNS配置】"
cat&nbsp; /etc/resolv.conf
echo&nbsp;""

echo&nbsp;"【6. 软件源状态】"
dnf repolist
echo&nbsp;""

echo&nbsp;"【7. 网络连通性测试】"
echo&nbsp;"网关连通性:"
ping&nbsp;-c&nbsp;2&nbsp;-W&nbsp;2&nbsp;192.168.1.1&nbsp;2>&1 | tail&nbsp;-2
echo&nbsp;""
echo&nbsp;"DNS解析测试:"
nslookup www.baidu.com&nbsp;2>&1 |&nbsp;grep-A1"^Name:"&nbsp;||&nbsp;echo"DNS解析失败"
echo&nbsp;""

echo&nbsp;"【8. 测试系统更新】"
dnf check-update&nbsp;-q2>&1 | head&nbsp;-5
echo&nbsp;""

echo&nbsp;"=========================================="
echo&nbsp;"诊断完成"
echo&nbsp;"=========================================="

7. 生产环境操作规范

7.1 操作前准备

| 步骤 | 操作 | 说明 | | — | — | — | | 1 | 记录当前配置 | ip addr > /tmp/ip_before.txt | | 2 | 备份配置文件 | cp /etc/sysconfig/network-scripts/ifcfg-eth0 /tmp/ | | 3 | 创建恢复点 | VMware快照或物理服务器断电准备 | | 4 | 通知相关人员 | 告知维护窗口 |

7.2 操作中注意事项

⚠️ &nbsp;重要提示:
1. 远程操作时,确保有备用连接方式(如ILO/DRAC/IPMI)
2. 修改网络配置前,确保当前会话不会断开
3. 使用screen或tmux保持会话
4. 每步操作后立即验证

7.3 操作步骤模板

# ========== 生产环境网络配置修改流程 ==========

# 第一阶段:备份与准备
echo&nbsp;"=== 阶段1: 备份配置 ==="
cp&nbsp;/etc/resolv.conf /etc/resolv.conf.bak.$(date +%Y%m%d%H%M)
cp&nbsp;/etc/NetworkManager/system-connections/*.nmconnection /tmp/&nbsp;2>/dev/null
ip addr > /tmp/ip_before.txt
ip route > /tmp/route_before.txt

# 第二阶段:验证当前状态
echo&nbsp;"=== 阶段2: 验证当前状态 ==="
ip addr show eth0
ping&nbsp;-c&nbsp;2&nbsp;192.168.1.1

# 第三阶段:执行修改
echo&nbsp;"=== 阶段3: 执行修改 ==="
nmcli con mod&nbsp;"System eth0"&nbsp;ipv4.addresses&nbsp;192.168.1.100/24
nmcli con mod&nbsp;"System eth0"&nbsp;ipv4.gateway&nbsp;192.168.1.1
nmcli con mod&nbsp;"System eth0"&nbsp;ipv4.dns&nbsp;"8.8.8.8"

# 第四阶段:重启服务
echo&nbsp;"=== 阶段4: 重启服务 ==="
nmcli con down&nbsp;"System eth0"
sleep2
nmcli con up&nbsp;"System eth0"

# 第五阶段:验证结果
echo&nbsp;"=== 阶段5: 验证结果 ==="
ip addr show eth0
ping&nbsp;-c&nbsp;3&nbsp;www.baidu.com
dnf makecache

echo&nbsp;"=== 完成 ==="

8. 预防措施

8.1 系统安装时的网络配置

| 步骤 | 操作 | 说明 | | — | — | — | | 1 | 安装时配置网络 | 打开”网络与主机”配置 | | 2 | 设置主机名 | 使用规范的主机名命名 | | 3 | 配置网络 | 设置IP、网关、DNS | | 4 | 启用网络 | 确保网络服务开机启动 |

8.2 标准化配置

8.2.1 创建网络配置模板

# /etc/NetworkManager/system-connections/eth0.nmconnection
[connection]
id=eth0
uuid=5fb06bd0-0bb0-7ffb-45f2-d9f72e3935f1
type=ethernet
autoconnect-priority=-999
interface-name=eth0

[ethernet]
mac-address-blacklist=

[ipv4]
method=manual
address1=192.168.1.100/24,192.168.1.1
dns=8.8.8.8;114.114.114.114;
dns-search=
route1=0.0.0.0/0,192.168.1.1

[ipv6]
method=disabled

[proxy]

8.2.2 设置权限

chmod&nbsp;600&nbsp;/etc/NetworkManager/system-connections/eth0.nmconnection
chown&nbsp;root:root /etc/NetworkManager/system-connections/eth0.nmconnection

8.3 监控与告警

| 监控项 | 阈值 | 告警方式 | | — | — | — | | 网卡状态 | state=DOWN | 邮件+短信 | | 网络丢包率 | >1% | 邮件 | | DNS解析失败 | 连续3次 | 邮件+短信 | | 软件源不可用 | 连续2次 | 邮件 |

8.4 定期巡检

# 每周巡检脚本
#!/bin/bash
LOGFILE="/var/log/network_health.log"

echo&nbsp;"=== 网络健康检查&nbsp;$(date)&nbsp;==="&nbsp;>>&nbsp;$LOGFILE

# 检查网络连通性
ping&nbsp;-c&nbsp;3&nbsp;192.168.1.1 >>&nbsp;$LOGFILE2>&1
ping&nbsp;-c&nbsp;3&nbsp;www.baidu.com >>&nbsp;$LOGFILE2>&1

# 检查软件源
dnf check-update >>&nbsp;$LOGFILE2>&1

# 检查DNS
nslookup www.baidu.com >>&nbsp;$LOGFILE2>&1

# 定期清理日志
find&nbsp;/var/log&nbsp;-name"network_health.log"-mtime+30-delete

9. 命令速查表

9.1 网络状态查看

| 命令 | 作用 | 示例 | | — | — | — | | ip addr | 查看IP地址 | ip addr show eth0 | | ip link | 查看网卡状态 | ip link set eth0 up | | ip route | 查看路由表 | ip route show | | ss -tlnp | 查看监听端口 | ss -tlnp \| grep 22 | | netstat -rn | 查看路由表 | netstat -rn | | ip neigh | 查看ARP表 | ip neigh show |

9.2 NetworkManager命令

| 命令 | 作用 | 示例 | | — | — | — | | nmcli dev status | 查看设备状态 | nmcli dev status | | nmcli con show | 查看连接列表 | nmcli con show | | nmcli con up | 激活连接 | nmcli con up "System eth0" | | nmcli con down | 关闭连接 | nmcli con down "System eth0" | | nmcli con mod | 修改连接 | nmcli con mod "eth0" ipv4.addresses 192.168.1.100/24 | | nmcli dev connect | 连接设备 | nmcli dev connect eth0 | | nmtui | 文本界面配置 | nmtui |

9.3 DNS相关命令

| 命令 | 作用 | 示例 | | — | — | — | | nslookup | DNS查询 | nslookup www.baidu.com | | dig | 详细DNS查询 | dig www.baidu.com | | host | 简单DNS查询 | host www.baidu.com | | cat /etc/resolv.conf | 查看DNS配置 | cat /etc/resolv.conf |

9.4 软件源管理

| 命令 | 作用 | 示例 | | — | — | — | | dnf repolist | 列出所有源 | dnf repolist | | dnf list | 列出软件包 | dnf list available | | dnf search | 搜索软件包 | dnf search nginx | | dnf install | 安装软件包 | dnf install vim | | dnf update | 更新软件包 | dnf update | | dnf clean all | 清理缓存 | dnf clean all | | dnf makecache | 重建缓存 | dnf makecache |

9.5 服务管理

| 命令 | 作用 | 示例 | | — | — | — | | systemctl status NetworkManager | 查看NM状态 | systemctl status NetworkManager | | systemctl start NetworkManager | 启动NM | systemctl start NetworkManager | | systemctl enable NetworkManager | 开机自启 | systemctl enable NetworkManager | | journalctl -u NetworkManager -f | 查看NM日志 | journalctl -u NetworkManager -f |


附录

A. 常见故障处理流程图

B. 配置文件路径速查

| 文件/目录 | 说明 | | — | — | | /etc/NetworkManager/ | NetworkManager配置目录 | | /etc/NetworkManager/system-connections/ | 网卡连接配置 | | /etc/resolv.conf | DNS配置文件 | | /etc/hosts | 主机名解析 | | /etc/sysconfig/network | 网络全局配置 | | /etc/sysconfig/network-scripts/ | 网卡脚本配置 | | /etc/dnf/dnf.conf | DNF全局配置 | | /etc/yum.repos.d/ | 软件源配置 |


免责声明:

本文所载程序、技术方法仅面向合法合规的安全研究与教学场景,旨在提升网络安全防护能力,具有明确的技术研究属性。

任何单位或个人未经授权,将本文内容用于攻击、破坏等非法用途的,由此引发的全部法律责任、民事赔偿及连带责任,均由行为人独立承担,本站不承担任何连带责任。

本站内容均为技术交流与知识分享目的发布,若存在版权侵权或其他异议,请通过邮件联系处理,具体联系方式可点击页面上方的联系我

本文转载自:运维星火燎原 刘军军 刘军军《新装Linux系统网络不通?老司机手把手教你从诊断到修复,一篇全搞定》

评论:0   参与:  0