Linux 系统中网络接口配置主要分为临时配置重启后失效和永久配置写入配置文件。以下是具体方法和命令一、临时配置立即生效重启后失效1. 使用ifconfig传统工具# 查看所有网络接口ifconfig-a# 配置 IP 地址和子网掩码sudoifconfigeth0192.168.1.100 netmask255.255.255.0# 启用/禁用接口sudoifconfigeth0 upsudoifconfigeth0 down# 添加虚拟接口别名sudoifconfigeth0:0192.168.1.101 netmask255.255.255.02. 使用ip命令现代推荐工具# 查看网络接口和 IP 地址ipaddr showipa# 添加 IP 地址sudoipaddradd192.168.1.100/24 dev eth0# 删除 IP 地址sudoipaddr del192.168.1.100/24 dev eth0# 启用/禁用接口sudoiplinkseteth0 upsudoiplinkseteth0 down# 添加默认网关sudoiprouteadddefault via192.168.1.1# 查看路由表iproute show二、永久配置写入配置文件重启后生效1.CentOS / RHEL / Fedora 系列编辑网卡配置文件如/etc/sysconfig/network-scripts/ifcfg-eth0# 静态 IP 配置示例DEVICEeth0BOOTPROTOstaticONBOOTyesIPADDR192.168.1.100NETMASK255.255.255.0GATEWAY192.168.1.1DNS18.8.8.8DNS28.8.4.4重启网络服务sudosystemctl restart network2.Ubuntu / Debian 系列旧版编辑/etc/network/interfaces# 静态 IP 配置示例auto eth0 iface eth0 inet static address192.168.1.100 netmask255.255.255.0 gateway192.168.1.1 dns-nameservers8.8.8.88.8.4.4重启网络服务sudosystemctl restart networking3.Ubuntu 18.04使用 Netplan编辑/etc/netplan/01-netcfg.yamlnetwork:version:2renderer:networkdethernets:eth0:dhcp4:noaddresses:[192.168.1.100/24]gateway4:192.168.1.1nameservers:addresses:[8.8.8.8,8.8.4.4]应用配置sudonetplan apply三、使用 NetworkManager 工具图形化或命令行1. 使用nmtui文本界面工具sudonmtui通过菜单界面配置网络。2. 使用nmcli命令行工具# 查看连接状态nmcli connection show# 配置静态 IPsudonmcli connection modify eth0 ipv4.addresses192.168.1.100/24sudonmcli connection modify eth0 ipv4.gateway192.168.1.1sudonmcli connection modify eth0 ipv4.dns8.8.8.8,8.8.4.4sudonmcli connection modify eth0 ipv4.method manual# 激活连接sudonmcli connection up eth0四、DNS 配置编辑/etc/resolv.confnameserver8.8.8.8 nameserver8.8.4.4五、网络故障排查常用命令# 测试网络连通性ping8.8.8.8pingwww.baidu.com# 追踪路由路径traceroute8.8.8.8# 查看网络连接状态ss-tulnnetstat-tuln# 查看网络接口统计信息ip-slinkshow eth0总结临时配置使用ifconfig或ip命令适合快速调试。永久配置根据发行版编辑对应配置文件CentOS/RHEL 用ifcfg-*Ubuntu 用Netplan或interfaces。推荐工具现代系统推荐使用ip命令和Netplan/NetworkManager进行管理。配置完成后建议使用ping和ip a命令验证网络连通性和 IP 设置。