BLOG

Configure static IP address in Ubuntu

If you don't set static IP in Ubuntu Server's installer he trying receive IP by DHCP protocol.

Ethernet interfaces are identified by the system using predictable network interface names. These names can appear as eno1 or enp0s25. However, in some cases an interface may still use the kernel eth# style of naming. Documentation.

 

 

# Detecting interfaces

 

Use "ip address" or "ip a" command for finding your network interfaces:

user@localhost:~$ ip address
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: ens18: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether bc:24:11:25:01:7a brd ff:ff:ff:ff:ff:ff
    altname enp0s18
    inet 10.0.255.97/16 metric 100 brd 10.0.255.255 scope global dynamic ens18
       valid_lft 6081sec preferred_lft 6081sec
    inet6 fe80::be24:11ff:fe25:17a/64 scope link
       valid_lft forever preferred_lft forever

 

Open /etc/netplan/00-installer-config.yaml file for show your default settings:

# This is the network config written by 'subiquity'
network:
  ethernets:
    ens18:
      dhcp4: true
  version: 2

 

 

# Setting static IP

 

Since Ubuntu Server 22.04 deprecated "gateway4" parameter and structure of /etc/netplan/00-installer-config.yaml file changed. New network configuration file's structure:

# This is the network config written by 'subiquity'
network:
  version: 2
  renderer: networkd
  ethernets:
    ens18:
      addresses:
        - 10.0.255.97/16
      routes:
        - to: default
          via: 10.0.0.1
      nameservers:
          search: [mydomain, otherdomain]
          addresses: [10.0.0.1, 8.8.8.8, 8.8.4.4]

 

For Ubuntu Server before 22.04 /etc/netplan/00-installer-config.yaml file's stucture should be like this:

# This is the network config written by 'subiquity'
network:
  ethernets:
    ens18:
      dhcp4: no
      dhcp6: no
      addresses: [10.0.255.97/16, ]
      gateway4: 10.0.0.1
      nameservers:
        addresses: [10.0.0.1, 8.8.8.8, 8.8.4.4]
  version: 2

 

WARNING: if you set old configurations in Ubuntu Server 22.04 or higher you get this error:

** (generate:1681): WARNING **: 08:48:03.456: `gateway4` has been deprecated, use default routes instead.
See the 'Default routes' section of the documentation for more details.

 

 

# Reload Netplan

 

The network configuration abstraction renderer. Netplan is a utility for easily configuring networking on a linux system. You simply create a YAML description of the required network interfaces and what each should be configured to do. From this description Netplan will generate all the necessary configuration for your chosen renderer tool. Official site.

Netplan reads network configuration from /etc/netplan/*.yaml which are written by administrators, installers, cloud image instantiations, or other OS deployments. During early boot, Netplan generates backend specific configuration files in /run to hand off control of devices to a particular networking daemon.

Netplan currently works with these supported renderers:
- NetworkManager
- Systemd-networkd

 

Now apply new configurations:

sudo netplan apply

Top button