setup DHCP and static IP for ubuntu.

find IP address, can use the ip command which is part of the iproute2 package that is usually installed by default.

ip addr show

This command displays a list of all network interfaces on your system along with their IP addresses and other information.

xenial

Type the following command to edit the network configuration file:

sudo vim /etc/network/interfaces

blow is DHCP setting:

auto ens160
iface ens160 inet dhcp

blow is static IP setting:

1
2
3
4
5
6
auto ens160
iface ens160 inet static
    address 192.168.1.20
    netmask 255.255.255.0
    gateway 192.168.1.1
dns-nameservers 192.168.1.1

Apply the changes by running the following command:

sudo /etc/init.d/networking restart

bionic & focal

Type the following command to edit the network configuration file:

sudo /etc/netplan/00-installer-config.yaml

blow is DHCP setting:

1
2
3
4
5
6
network:
  ethernets:
    ens160:
      dhcp4: true
  version: 2
  renderer: networkd

blow is static IP setting:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
network:
  ethernets:
    ens160:
      addresses:
      - 192.168.1.25/24
      - "2001:1::1/64"
      gateway4: 192.168.1.1
      gateway6: "2001:1::2"
      nameservers:
        addresses:
        - 8.8.8.8
        - 8.8.4.4
  version: 2
  renderer: networkd

Apply the changes by running the following command:

sudo netplan apply

jammy

Type the following command to edit the network configuration file:

sudo vim /etc/netplan/00-installer-config.yaml

blow is DHCP setting:

1
2
3
4
network:
  ethernets:
    enp1s0:
      dhcp4: true

blow is static IP setting:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
network:
  ethernets:
    enp1s0:
      dhcp4: no
      addresses: [192.168.1.102/24]
      nameservers:
        addresses: [223.5.5.5, 119.29.29.29]
      routes:
        - to: 0.0.0.0/0
          via: 192.168.1.1
  version: 2
  renderer: networkd

Apply the changes by running the following command:

sudo netplan apply