How to configure Static IP on Ubuntu

neotam Avatar

How to configure Static IP on Ubuntu

Hosts may obtain the IP address configuration automatically from DHCP server in such a case IP address acquired by host may be different if not each time it is reconnected to the network but occasionally if same IP address is leased to different host by DHCP server. If you really want to keep IP address of your machine is static, for suppose if the host acts as the server. This article will help you configure static IP on Ubuntu Servers and Desktops

Note: Since Ubuntu 17.10 default network management tool is “Netplan” where prior it was “ifconfig”

Netplan tool uses the YAML for configuration files which are with extension .yaml. Neplan can generate the configuration for the chosen renderer tool. Currently supported renderers are “NetworkManager’ and “Systemd-networkd” where “systemd-networkd” is used for servers without GUI

Configure Static IP on Ubuntu

First step is to identify the network interface identifier (or name/label)

ip link

Above command “ip link” spits out the network information such as interface name, mac address, IP address and subnet mask etc

$ ip link
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: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP mode DEFAULT group default qlen 1000
    link/ether 08:00:27:b1:9d:67 brd ff:ff:ff:ff:ff:ff
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP mode DEFAULT group default qlen 1000
    link/ether 08:00:27:4b:46:b7 brd ff:ff:ff:ff:ff:ff

Note: Netplan configuration files are stored in the directory /etc/netplan .

All files in the netplan directory may differ system to system. These are typically named as “01-netcfg.yaml”

To edit these file(s) open in your favourite text editor vim or nano

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

Configuration typically looks like

network:
  version: 2
  renderer: NetworkManager
  ethernets:
    ens3:
      dhcp4: yes

Edit this configuration to add configuration for new interfaces

network:
  version: 2
  renderer: NetworkManager
  ethernets:
    enp0s3:
      dhcp4: no
      addresses:
        - 192.168.4.5/24
      gateway4: 192.168.121.1
      nameservers:
          addresses: [8.8.8.8, 1.1.1.1]

You can also specify the addresses as list

network:
  version: 2
  renderer: networkd
  ethernets:
    enp0s3:
     dhcp4: no
     addresses: [192.168.4.5/24]
     gateway4: 192.168.4.1
     nameservers:
       addresses: [8.8.8.8,8.8.4.4]

Note: gateway4 configuration options is deprecated, instead use routes

network:
  version: 2
  renderer: networkd
  ethernets:
    enp0s3:
      dhcp4: false
      dhcp6: false
     addresses:
      - 192.168.4.5/24
     routes:
      - to: default
        via: 192.168.4.1
     nameservers:
       addresses: [192.168.4.1]

Gotcha, mind of YAML syntax, once done where spaces and tabs matter

sudo netplan apply

You can also verify if changes are applied

ip addr show dev enp0s3

Where, enp0s3 is the network device



For Debian based Linux distributions that still use ifconfig as network management tool, here is the way to assign static IP

Configuration is located at “/etc/network/interfaces”. Open this file with text editor

vim /etc/network/interfaces

For example, to configure static IP for the interface eth0

# The loopback network interface
auto lo
iface lo inet loopback
 
# The primary network interface
auto eth0
iface eth0 inet static
 address 192.168.4.5
 netmask 255.255.255.0
 gateway 192.168.4.1
 dns-domain example.com
 dns-nameservers 8.8.8.8

To restart the networking service

sudo systemctl restart networking.service

To query the status of networking daemon

systemctl status networking.service 

Leave a Reply

Your email address will not be published. Required fields are marked *