Ubuntu Network Configuration Guide

I. Introduction

insert image description here

Starting from Ubuntu 17.10 Artful, Netplan replaces ifupdown as the default configuration utility, and network management is changed to netplan. The configuration of fixing IP from /etc/network/interfaces is no longer used. The configuration is written in /etc/netplan/01-network-manager-all.yaml or a similarly named yaml file; after the release of Ubuntu 18.04, Ubuntu and Debian removed the previous ifup/ifdown Command and /etc/network/interfaces configuration file, instead use ip link set or /etc/netplan/01-netcfg.yaml template and sudo netplan apply command to realize network management.

Netplan, short for Abstract Network Configuration Descriptor, is the default network configuration tool in Ubuntu 17.10 and later. It is a command-line tool that helps users easily configure network interfaces, including static or dynamic IP addresses, gateways, DNS, etc., for configuring Linux networks. It uses configuration files in YAML format, which makes configuration files easy to read and edit, and can be managed through a version control system. In addition, Netplan also supports renderer-based network configuration, which means that according to different network configuration requirements, through the netplan command, you only need to use a YAML file to describe the configuration required for each network interface, and then you can apply the effective network of the configuration. Netplan currently supports the following two network management tools/engines: NetworkManager and Systemd-networkd, which are described by the renderer keyword in the YAML file, networkd and NetworkManager, but actually it does not care whether the underlying management tool is NetworkManager or networkd . If the server version is installed, networkd is used for management by default, and NetworkManager will not be installed and the nmcli interface will not be provided. If the desktop version is installed, NetworkManager is used by default for management.

insert image description here

A major advantage of Netplan is that it can help users reduce some common network configuration problems, such as inconsistent network naming conventions, changes in network interface names, and more. The main reason why Ubuntu developers decided to introduce Netplan is to improve the network configuration experience and consistency of the Ubuntu system. It provides a modern, easy-to-read and edit configuration file in YAML format, which can easily manage multiple network interfaces and configuration parameters, and supports multiple configuration engines. Using Netplan, users can easily configure multiple network interfaces and can specify their default routes, thereby improving network performance and security. The Ubuntu system can automatically detect network interface and hardware changes, and automatically update configuration files, avoiding errors and inconvenience caused by users manually modifying network configuration files.

Attachments: official documents , network introduction , Netplan , Netplan Design , Netplan

2. Network configuration

2.1 Netplan

insert image description here

netplan reads the YAML configuration file from the /etc/netplan/ folder, and updates the back-end network mapping file according to the configuration in it; in the early stage of system startup, netplan will read the configuration file, and read /{lib,etc,run}/netplan/*.yaml according to the specification of "network renderer". The yml file under /run/netplan will be mapped to /etc/netplan, and the files in other directories will be mapped to /lib/netplan.
insert image description here

Description of configuration items:

network: This tag is the root tag of the configuration file, representing the configuration of the entire network. The configuration of multiple network interfaces can be defined under this tab, as well as some global settings.

version: This tag is used to specify the version number of Netplan. The current version is 2.

renderer: This tag is used to specify the configuration method of the network interface, and supports networkd and NetworkManager two renderers.

ethernets: This tag is used to define the configuration of the Ethernet interface.

bridges: This tag is used to define the configuration of the bridge interface.

vlans: This tag is used to define the configuration of the VLAN interface.

bonds: This label is used to define the configuration of the NIC bonding interface.

wifis: This tag is used to define the configuration of the Wi-Fi interface.

access-points: This tag is used to define the access point configuration of the Wi-Fi interface.

nameservers: This tag is used to define the configuration of DNS resolvers.

routes: This tag is used to define the configuration of the routing table.

dhcp4 and dhcp6: This label is used to define DHCP configuration, including whether to use DHCP and DHCP options.

For more reference or execution netplan help: netplan , yaml configuration instructions , netplan-dbus , netplan-get , netplan-try

Note: NM can also be used, and managed = true in the /etc/NetworkManager/NetworkManager.conf configuration file, and restart the NetworkManager service: sudo service network-manager restart.

2.2 Common commands for network configuration

insert image description here
1) View basic network information

sudo ipconfig    #ip a s
#或lshw命令会提供对指定设备更详细的信息
sudo lshw -class network
sudo apt-get install netplan.io
sudo apt-get install net-tools

insert image description here
insert image description here

2) View network status and configuration parameters

sudo ethtool eth4//The output is as follows, the command can be viewed: automatic write, full duplex, port, boot self-evoke and other attributes

sudo ethtool eth4
Settings for eth4:
    Supported ports: [ FIBRE ]
    Supported link modes:   10000baseT/Full
    Supported pause frame use: No
    Supports auto-negotiation: No
    Supported FEC modes: Not reported
    Advertised link modes:  10000baseT/Full
    Advertised pause frame use: No
    Advertised auto-negotiation: No
    Advertised FEC modes: Not reported
    Speed: 10000Mb/s
    Duplex: Full
    Port: FIBRE
    PHYAD: 0
    Transceiver: internal
    Auto-negotiation: off
    Supports Wake-on: d
    Wake-on: d
    Current message level: 0x00000014 (20)
                   link ifdown
    Link detected: yes

3) IP address configuration

sudo ip addr add 10.1.2.5/24 dev enp0s25#Configure static ip for the specified network card, which will take effect immediately after configuration, and multi-temporary user configuration network
ip link set dev enp0s25 up#Enable network card
ip link set dev enp0s25 down#Shutdown network card
ip address show dev enp0s25#Verify
sudo ip route add default via 10.1.2.1#Configure default gateway
ip route show#Verify route
ip addr flush eth0#Reset network card configuration, this command will not clear the content of /etc/resolv.conf,
ip route add <IP段>/<掩码> via <网关> dev <网卡名>#Configure static route, temporarily take effect

4) The DHCP configuration of the network card is carried out with the help of the /etc/netplan/99_config.yaml configuration file, as shown below. After completion, sudo netplan apply can take effect.

network:
  version: 2
  renderer: networkd    #初始指定底层使用的网络管理工具,这里使用networkd
  ethernets:
    enp3s0:
      dhcp4: true

Note: If it is a mobile device with a wireless network, it is recommended to use NetworkManager. You can switch the environment network and modify the WiFi password through NetworkManager; if it is a non-mobile device (or a virtual machine that does not need a wireless network), network is recommended.

5) Static ip configuration through /etc/netplan/99_config.yaml: Note that tabs cannot be used for indentation

network:
  version: 2
  renderer: networkd
  ethernets:
    eth0:
      addresses:
        - 10.10.10.2/24
      routes:
        - to: default
          via: 10.10.10.1
      nameservers:
          search: [mydomain, otherdomain]
          addresses: [10.10.10.1, 1.1.1.1]

Note: The netplan in Ubuntu Bionic 18.04 LTS does not understand the "to:default" syntax for specifying the default route above. In this case, you can use the old gateway4:10.10.10.1 to specify instead of the entire routes: statement block.

6) Wireless wifi configurationinsert image description here

2.3 Netplan yml configuration

1) Customize the name of the network card for a specific network card: use the match field to filter, and the set-name field to set the name of the network card

network:
  version: 2
  renderer: networkd
  ethernets:
    eth_lan0:
      dhcp4: true
      match:
        macaddress: 00:11:22:33:44:55
      set-name: eth_lan0

2.4 NSS:Name Service Switch

The order in which the system resolves hostnames to IP addresses is controlled by the name service switch (NSS) configuration file /etc/nsswitch.conf. An example of /etc/nsswitch.conf is as follows:

hosts: files mdns4_minimal [NOTFOUND=return] dns mdns4

3. Configuration example

3.1 Multiple NICs

insert image description here

Guess you like

Origin blog.csdn.net/ximenjianxue/article/details/131849453