Ubuntu 18.04 Server必须使用netplan命令配置IP地址

Ubuntu 18.04 Server必须使用netplan命令配置IP地址_第1张图片

新出的Ubuntu服务器18.04版本修改了IP地址配置程序, Ubuntu和Debian的软件架构师删除了以前的ifup/ifdown命令和/etc/network/interfaces配置文件, 改为使用/etc/netplan/01-netcfg.yamlsudo netplay apply命令管理IP地址.

这个改动影响了我公司基于Ubuntu 16.04 LTS构建的网关产品的运行环境.
对开发人员来说, 从16.04迁移到18.04会很麻烦.


本文来自以下网址:

  • https://linuxconfig.org/how-to-configure-static-ip-address-on-ubuntu-18-04-bionic-beaver-linux

Ubuntu 18.04 Server

To configure a static IP address on your Ubuntu 18.04 server you need to modify a relevant netplan network configuration file within /etc/netplan/ directory.

For example you might find there a default netplan configuration file called 01-netcfg.yaml or 50-cloud-init.yaml with a following content instructing the networkd deamon to configure your network interface via DHCP:

# This file describes the network interfaces available on your system
# For more information, see netplan(5).
network:
  version: 2
  renderer: networkd
  ethernets:
    enp0s3:
      dhcp4: yes

To set your network interface enp0s3 to static IP address 192.168.1.222 with gateway 192.168.1.1 and DNS server as 8.8.8.8 and 8.8.4.4 replace the above configuration with the one below.

WARNING:

You must adhere to a correct code indent for each line of the block. In other words the prefix number of spaces for each line is important. Othersiwe you may end up with an error message similar to: Invalid YAML at //etc/netplan/01-netcfg.yaml line 7 column 6: did not find expected key

# This file describes the network interfaces available on your system
# For more information, see netplan(5).
network:
  version: 2
  renderer: networkd
  ethernets:
    enp0s3:
     dhcp4: no
     addresses: [192.168.1.222/24]
     gateway4: 192.168.1.1
     nameservers:
       addresses: [8.8.8.8,8.8.4.4]

Once ready apply changes with:
$ sudo netplan apply
In case you run into some issues execute:
$ sudo netplan --debug apply


Netplan and systemd-networkd

ifupdown (including the familiar ifup and ifdown utilities) has been replaced by Netplan. Netplan is a simplified interface for configuring Linux networking, where YAML files in /etc/netplan are used to generate configuration information for either NetworkManager or — in the case of new Ubuntu Server installations – systemd-networkd.

The ip link set command is a replacement for ifup and ifdown. You can learn more about it in the How To Configure Network Interfaces and Addresses section of our IPRoute2 Tools tutorial.

For more information on configuring Netplan, see the official documentation. Details on how to use and configure systemd-networkd are available in the systemd-networkd.service and systemd.network man pages.

The command networkctl can output a summary of your network devices:

networkctl

OutputIDX LINK             TYPE               OPERATIONAL SETUP
  1 lo               loopback           carrier     unmanaged
  2 eth0             ether              routable    configured

Run the command with the status flag and it will print the state of each IP address on the system:

networkctl status

Output●        State: routable
       Address: 192.0.2.10 on eth0
                203.0.113.241 on eth0
                2001:DB8:68be:caff:fe4c:c963 on eth0
       Gateway: 203.0.113.1 (ICANN, IANA Department) on eth0
           DNS: 203.0.113.2
                203.0.113.3

参考资料: https://www.digital 和 谐 ocean.com/community/tutorials/what-s-new-in-ubuntu-18-04
(访问上述URL请手动删除“和 谐”字样)

你可能感兴趣的:(Ubuntu 18.04 Server必须使用netplan命令配置IP地址)