一、Debian / Ubuntu

Ubuntu 18.04+ (Netplan)

配置文件位置:
/etc/netplan/*.yaml

静态IP配置示例:

network:
  version: 2
  renderer: networkd
  ethernets:
    eth0:
      addresses:
        - 192.168.1.100/24
      routes:
        - to: default
          via: 192.168.1.1
      nameservers:
        addresses:
          - 8.8.8.8
          - 8.8.4.4

DHCP配置示例:

network:
  version: 2
  renderer: networkd
  ethernets:
    eth0:
      dhcp4: true

生效命令:
netplan apply

Debian / Ubuntu 16.04及以下

配置文件位置:
/etc/network/interfaces

静态IP配置示例:

auto eth0
iface eth0 inet static
    address 192.168.1.100
    netmask 255.255.255.0
    gateway 192.168.1.1
    dns-nameservers 8.8.8.8 8.8.4.4

DHCP配置示例:

auto eth0
iface eth0 inet dhcp

生效命令:
systemctl restart networking

ifdown eth0 && ifup eth0

二、CentOS / RHEL / Rocky Linux

CentOS 7 / RHEL 7 / Rocky Linux 8+

配置文件位置:
/etc/sysconfig/network-scripts/ifcfg-eth0

静态IP配置示例:

TYPE=Ethernet
BOOTPROTO=static
NAME=eth0
DEVICE=eth0
ONBOOT=yes
IPADDR=192.168.1.100
NETMASK=255.255.255.0
GATEWAY=192.168.1.1
DNS1=8.8.8.8
DNS2=8.8.4.4

DHCP配置示例:

TYPE=Ethernet
BOOTPROTO=dhcp
NAME=eth0
DEVICE=eth0
ONBOOT=yes

生效命令:
CentOS 7
systemctl restart network

CentOS 8+ / Rocky Linux

nmcli connection reload
nmcli connection up eth0


systemctl restart NetworkManager

CentOS 9 / RHEL 9 (使用 nmcli 或 keyfile)

配置文件位置:
/etc/NetworkManager/system-connections/eth0.nmconnection

使用 nmcli 配置静态IP:

nmcli connection modify eth0 ipv4.addresses 192.168.1.100/24
nmcli connection modify eth0 ipv4.gateway 192.168.1.1
nmcli connection modify eth0 ipv4.dns "8.8.8.8 8.8.4.4"
nmcli connection modify eth0 ipv4.method manual
nmcli connection up eth0

使用 nmcli 配置 DHCP:

nmcli connection modify eth0 ipv4.method auto
nmcli connection up eth0

三、Fedora

配置文件位置:
/etc/sysconfig/network-scripts/ifcfg-*
或 (新版)
/etc/NetworkManager/system-connections/*.nmconnection

配置方法与 RHEL/CentOS 相同,推荐使用 nmcli:
查看连接
nmcli connection show

配置静态IP

nmcli con mod eth0 ipv4.addresses 192.168.1.100/24
nmcli con mod eth0 ipv4.gateway 192.168.1.1
nmcli con mod eth0 ipv4.dns "8.8.8.8 8.8.4.4"
nmcli con mod eth0 ipv4.method manual
nmcli con up eth0

四、Arch Linux

方式1:systemd-networkd

配置文件位置:
/etc/systemd/network/eth0.network

静态IP配置示例:

[Match]
Name=eth0

[Network]
Address=192.168.1.100/24
Gateway=192.168.1.1
DNS=8.8.8.8 8.8.4.4

DHCP配置示例:

[Match]
Name=eth0

[Network]
DHCP=yes

生效命令:

systemctl enable systemd-networkd
systemctl start systemd-networkd

方式2:netctl

配置文件位置:
/etc/netctl/eth0-static

静态IP配置示例:

Description='A basic static ethernet connection'
Interface=eth0
Connection=ethernet
IP=static
Address=('192.168.1.100/24')
Gateway='192.168.1.1'
DNS=('8.8.8.8' '8.8.4.4')

DHCP配置示例:

Description='A basic dhcp ethernet connection'
Interface=eth0
Connection=ethernet
IP=dhcp

生效命令:

netctl enable eth0-static
netctl start eth0-static

五、openSUSE / SUSE

配置文件位置:
/etc/sysconfig/network/ifcfg-eth0

静态IP配置示例:

BOOTPROTO='static'
IPADDR='192.168.1.100/24'
STARTMODE='auto'
NAME='Ethernet Card 0'

网关配置:
/etc/sysconfig/network/routes

default 192.168.1.1 - -

DNS配置:
/etc/resolv.conf
或使用 YaST 配置

DHCP配置示例:

BOOTPROTO='dhcp'
STARTMODE='auto'
NAME='Ethernet Card 0'

生效命令:
systemctl restart wicked

wicked ifreload all

六、通用工具:NetworkManager (nmcli)

大多数现代发行版都支持 NetworkManager,使用 nmcli 可以统一管理。

查看信息

查看所有连接
nmcli connection show

查看设备状态
nmcli device status

创建新连接(静态IP)

nmcli connection add type ethernet con-name eth0 ifname eth0
nmcli connection modify eth0 ipv4.addresses 192.168.1.100/24
nmcli connection modify eth0 ipv4.gateway 192.168.1.1
nmcli connection modify eth0 ipv4.dns "8.8.8.8 8.8.4.4"
nmcli connection modify eth0 ipv4.method manual
nmcli connection up eth0

创建新连接(DHCP)

nmcli connection add type ethernet con-name eth0 ifname eth0
nmcli connection modify eth0 ipv4.method auto
nmcli connection up eth0

修改现有连接

nmcli connection modify eth0 ipv4.addresses 192.168.1.200/24
nmcli connection modify eth0 ipv4.gateway 192.168.1.1
nmcli connection modify eth0 ipv4.dns "8.8.8.8 8.8.4.4"
nmcli connection up eth0

删除连接

nmcli connection delete eth0

七、DNS 配置

通用方法

配置文件:
/etc/resolv.conf

nameserver 8.8.8.8
nameserver 8.8.4.4
search example.com

systemd-resolved (Ubuntu 18.04+, Arch等)

配置文件:
/etc/systemd/resolved.conf

或创建链接
ln -sf /run/systemd/resolve/resolv.conf /etc/resolv.conf

八、快速参考表

发行版配置文件位置生效命令
Ubuntu 18.04+/etc/netplan/*.yamlnetplan apply
Debian / Ubuntu 16.04-/etc/network/interfacessystemctl restart networking
CentOS 7 / RHEL 7/etc/sysconfig/network-scripts/ifcfg-*systemctl restart network
CentOS 8+ / Rocky/etc/sysconfig/network-scripts/ifcfg-*nmcli con reload
Fedora/etc/sysconfig/network-scripts/ifcfg-*nmcli con up eth0
Arch (systemd)/etc/systemd/network/*.networksystemctl restart systemd-networkd
Arch (netctl)/etc/netctl/*netctl start profile
openSUSE/etc/sysconfig/network/ifcfg-*systemctl restart wicked

九、常用命令

查看IP地址
ip addr show

ifconfig

查看路由
ip route show

route -n

临时配置IP(重启失效)

ip addr add 192.168.1.100/24 dev eth0
ip route add default via 192.168.1.1

临时启用/禁用网卡

ip link set eth0 up
ip link set eth0 down

标签: none