📜  netplan static ip (1)

📅  最后修改于: 2023-12-03 15:03:09.834000             🧑  作者: Mango

Netplan Static IP

Netplan is a network configuration tool introduced in Ubuntu 17.10 as a replacement for the traditional ifupdown tool. It uses YAML syntax to define network interfaces and their configuration. Static IP configuration is one of the common use cases solved by Netplan.

Basic Syntax for Static IP

To configure a static IP address for a network interface, you need to define the following in a YAML file:

  • Interface name
  • IPv4 address and subnet mask
  • Default gateway
  • DNS servers

Here's an example YAML configuration file for a static IP configuration:

network:
  version: 2
  renderer: networkd
  ethernets:
    enp0s3:
      addresses: [192.168.1.10/24]
      gateway4: 192.168.1.1
      nameservers:
        addresses: [8.8.8.8, 8.8.4.4]

In the above configuration file:

  • version defines the Netplan configuration file version.
  • renderer specifies the renderer to use. In this case, we're using networkd.
  • ethernets is a map of network interfaces.
  • enp0s3 is the name of the network interface to configure.
  • addresses defines the IP address and subnet mask in CIDR notation.
  • gateway4 specifies the default gateway.
  • nameservers defines DNS server addresses.

Once you have created the configuration file, you can apply it with the following command:

sudo netplan apply
Additional Options

Here are some additional options that you can use in a Netplan configuration file:

IPv6 Address Configuration

To configure an IPv6 address for a network interface, you can use the following syntax:

ethernets:
  enp0s3:
    addresses:
      - 192.168.1.10/24
      - "2001:db8::2/64"
Static Routes

To define static routes, you can use the routes option:

ethernets:
  enp0s3:
    addresses: [192.168.1.10/24]
    routes:
      - to: 0.0.0.0/0
        via: 192.168.1.1
Link-level Settings

To configure link-level settings for a network interface, you can use the optional section:

ethernets:
  enp0s3:
    addresses: [192.168.1.10/24]
    optional:
      mtu: 1500
Conclusion

Netplan provides an easy and efficient way to configure static IP addresses and other network settings in Ubuntu. With the help of YAML syntax, you can easily define complex network configurations.