Thursday, December 12, 2019

How to set a static IP using terminal in Ubuntu

First lets find the version you are running:


lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 18.04.3 LTS
Release: 18.04
Codename: bionic


Now lets see what our interface name is:


ip link show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP mode DEFAULT group default qlen 1000
    link/ether 00:50:56:af:9d:c8 brd ff:ff:ff:ff:ff:ff


Ok so the version and interface information we need is in red.

Now let's create the file we need.  First, let's back up the original.


cp /etc/netplan/01-network-manager-all.yaml /etc/netplan/01-network-manager-all.yaml.bak


Ok now edit your 01-network-manager-all.yaml with your favorite editor.

vi /etc/netplan/01-network-manager-all.yaml

The following is the working config for the version and interface listed above:

# This file describes the network interfaces available on your system
# For more information, see netplan(5).
network:
  version: 2
  renderer: networkd
  ethernets:
    ens160:
      dhcp4: no
      dhcp6: no
      addresses: [10.10.10.100/24]
      gateway4: 10.10.10.1
      nameservers:
        addresses: [10.10.10.1,8.8.8.8,8.8.4.4]
Ok let's break down the file:

First thing, formatting matters.  If it is not formatted correctly, it will fail.

ens160 is the interface name.  You can see the links I used to figure this out, theirs was called ens33.

addresses is the static IP of the box.  You set this to what you want it to be.

addresses is your DNS servers.  If you have a local dns server, put that first.

Now that all that is done, lets apply it.

netplan apply
You can verify this by looking in the gui or ifconfig.

Sources:  Link Link Link

No comments:

Post a Comment

ShareThis