linux-网络配置,关联 network,interface,Debian,ifupdown

Debian

Ubuntu

Network interfaces are typically initialized in

“networking.service” 初始化 lo interface
“NetworkManager.service” 初始化其他 interfaces

传统的 ifupdown 包通过 /etc/network/interfaces 文件来配置网络。

netplan 配置过程

  1. 修改 /etc/netplan/ 下的配置文件
  2. sudo netplan apply

配置文件示例

示例:为interface ens3 设置DHCP

1
2
3
4
5
6
network:
  version: 2
  renderer: networkd
  ethernets:
    ens3:
      dhcp4: yes

示例:为interface ens3 设置静态地址

1
2
3
4
5
6
7
8
9
10
11
12
/etc/netplan/01-netcfg.yaml
network:
  version: 2
  renderer: networkd
  ethernets:
    ens3:
      dhcp4: no
      addresses:
        - 192.168.121.221/24
      gateway4: 192.168.121.1
      nameservers:
          addresses: [8.8.8.8, 1.1.1.1]

示例:设置bridge

1
2
3
4
5
6
7
8
9
10
11
network:
  version: 2
  renderer: networkd
  ethernets:
    enp3s0:
      dhcp4: no
  bridges:
    br0:
      dhcp4: yes
      interfaces:
        - enp3s0

CentOS

修改网络配置

  1. 修改 /etc/sysconfig/network-scripts/ifcfg-<interface-name>
  2. systemctl restart network

实例

  • DHCP 实例

DHCP configration for eth0 (stored in /etc/sysconfig/network-scripts/ifcfg-eth0 file):

1
2
3
4
5
6
7
8
9
DEVICE="eth0"
ONBOOT=yes
NETBOOT=yes
UUID="41171a6f-bce1-44de-8a6e-cf5e782f8bd6"
IPV6INIT=yes
BOOTPROTO=dhcp
HWADDR="00:08:a2:0a:ba:b8"
TYPE=Ethernet
NAME="eth0"
  • static ip 实例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# static IP address on CentOS 7 or RHEL 7#
HWADDR=00:08:A2:0A:BA:B8
TYPE=Ethernet
BOOTPROTO=none
# Server IP #
IPADDR=192.168.2.203
# Subnet #
PREFIX=24
# Set default gateway IP #
GATEWAY=192.168.2.254
# Set dns servers #
DNS1=192.168.2.254
DNS2=8.8.8.8
DNS3=8.8.4.4
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
# Disable ipv6 #
IPV6INIT=no
NAME=eth0
# This is system specific and can be created using 'uuidgen eth0' command #
UUID=41171a6f-bce1-44de-8a6e-cf5e782f8bd6
DEVICE=eth0
ONBOOT=yes