ARTICLE DETAIL

资讯详情

深耕网站视觉设计与运营推广的一线实战洞察。

如何在 Linux 系统中开启和配置网络

如何在 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 系统的网络。如果仍有问题可以根据具体的错误信息进一步排查。希望本文对你有所帮助
返回列表