Thursday, March 07, 2019

Setting a static IP on Ubuntu server 18

Since ubuntu now uses netplan (more info here -> https://netplan.io) which uses yaml to define and create the required networks. The legacy way of making these changes via /etc/network/interfaces is now deprecated.

See below:

#sudo cat /etc/network/interfaces

# ifupdown has been replaced by netplan(5) on this system.  See
# /etc/netplan for current configuration.
# To re-enable ifupdown on this system, you can run:
#    sudo apt install ifupdown


1. Let's start by confirming our interface names:

#ifconfig -a

ens160: flags=4163  mtu 1500
        inet 172.1.2.3  netmask 255.255.255.0  broadcast 172.1.2.255
        inet6 fe80::250:56ff:fea8:38f8  prefixlen 64  scopeid 0x20
        ether 00:50:56:a8:38:f9  txqueuelen 1000  (Ethernet)
        RX packets 1545  bytes 955401 (955.4 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 827  bytes 101663 (101.6 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0


2. Now identify the config file netplan is using:

#ls -la  /etc/netplan/
total 12
drwxr-xr-x  2 root root 4096 Mar  6 06:15 .
drwxr-xr-x 90 root root 4096 Mar  6 06:16 ..
-rw-r--r--  1 root root  409 Mar  6 06:15 50-cloud-init.yaml


Note the interface name is  "ens160" taken from 1. above.

For our example we are going to set the static IP to 172.1.2.3 with a subnet mask of 255.255.255.0 and gateway of 172.1.2.1

3. Make the necessary changes as shown below:

#sudo vi /etc/netplan/50-cloud-init.yaml

# This file is generated from information provided by
# the datasource.  Changes to it will not persist across an instance.
# To disable cloud-init's network configuration capabilities, write a file
# /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg with the following:
# network: {config: disabled}
network:
    ethernets:
        ens160:
            addresses: [172.1.2.3/24]
            gateway4:   172.1.2.1
            dhcp4: no
            nameservers:
              addresses: [1.1.1.1,2.2.2.2]

    version: 2

4. Lastly apply the configuration
#sudo netplan apply








No comments: