Ubuntu 17.10 Server static IP netplan - how to set netmask

Ubuntu 17.10 Server uses the package netplan instead of /etc/network/interfaces.

I have created the /etc/netplan/01-netcfg.yaml

Like described here: Ubuntu 17.10 will not accept static IP

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

The default netmask in netplan is: 255.255.255.0

How can I change/set the netmask e.g. 255.255.255.1?


Netmask cannot be 255.255.255.1. Netmask for class C addresses can be:

Prefix size         | Subnet mask   
/24                 | 255.255.255.0 
/25                 | 255.255.255.128
/26                 | 255.255.255.192
/27                 | 255.255.255.224
/28                 | 255.255.255.240
/29                 | 255.255.255.248
/30                 | 255.255.255.252

Based on this you configuration in .yaml can be

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

Or some appropriate netmask.

Please keep in mind that ip address of host and gateway must be in the same subnet.


You set the netmask with CIDR notation in the addresses, so /24 is 255.255.255.0, /25 is 255.255.255.128, /28 is 255.255.255.240, etc.

Here is a working example from https://netplan.io/examples

network:
  version: 2
  renderer: networkd
  ethernets:
    enp2s0:
      addresses:
        - 10.10.10.2/24
      dhcp4: no
      gateway4: 10.10.10.1
      nameservers:
          addresses: [8.8.8.8, 1.1.1.1]

Or like this:

network:
  version: 2
  renderer: networkd
  ethernets:
    enp2s0:
      addresses: [10.10.10.2/25]
      dhcp4: no
      gateway4: 10.10.10.1
      nameservers:
          addresses: [8.8.8.8, 1.1.1.1]

Or with aliases:

network:
  version: 2
  renderer: networkd
  ethernets:
    enp2s0:
      addresses: [10.10.10.2/25, 10.10.10.3/25]
      gateway4: 10.10.10.1
      nameservers:
          addresses: [8.8.8.8, 1.1.1.1]

And this also works:

network:
  version: 2
  renderer: networkd
  ethernets:
    enp2s0:
      addresses:
        - 10.10.10.2/25
        - 10.10.10.3/25
      gateway4: 10.10.10.1
      nameservers:
          addresses: [8.8.8.8, 1.1.1.1]

255.255.255.0 is decimal representation of IPv4 netmask for masking out 24 of 32 bits.

11111111.11111111.11111111.00000000 (there are 24 masking bits and 8 unmasking bits).

255.255.255.1 in binary is 11111111.11111111.11111111.00000001 (there are 25 masking bits and 7 unmasking bits).

Such bitmask is not valid for netmask as it has 'hole' of unmasking bits ('0') between masking bits ('1').

11111111.11111111.11111111.10000000 (binary) = 255.255.255.128 (decimal) is valid and represent 25 masking bits (/25)

  • https://en.wikipedia.org/wiki/Mask_(computing)
  • https://en.wikipedia.org/wiki/Subnetwork