ip: network configuration on Ubuntu

Introduction

Network connection configuration is the most important step when working with Ubuntu, and it can raise many questions for a beginner.

In this article you can find answers to some of them.

To navigate network administration better, namely such concepts as IP address types, masks, subnets and so on, I recommend studying the article

«Computer network basics»

Installation

The ip command on Ubuntu is available from the iproute2 utility, which can be installed with the command

apt install -y iproute2

ip

To get the list of interfaces you need to use the command

ip a

The output will be something like

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: enp3s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether 30:f9:ed:d9:ea:d3 brd ff:ff:ff:ff:ff:ff inet 192.0.2.131/24 brd 192.0.2.255 scope global enp3s0 valid_lft forever preferred_lft forever inet6 fe80::32f9:edff:fed9:ead3/64 scope link valid_lft forever preferred_lft forever 3: wlp1s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000 link/ether b8:76:3f:f5:c4:1f brd ff:ff:ff:ff:ff:ff inet 192.168.43.4/24 brd 192.168.43.255 scope global dynamic noprefixroute wlp1s0 valid_lft 2800sec preferred_lft 2800sec inet6 fe80::ba76:3fff:fef5:c41f/64 scope link noprefixroute valid_lft forever preferred_lft forever

Previously interfaces were called eth0, eth1 and so on, now they have more varied names.



1: The first interface is lo it points to the local machine and has the IP 127.0.0.1/8



2: The second interface enp3s0 is my Ethernet connection to another PC.

In this example the computer with Ubuntu(1) is connected to the computer with Windows (2) directly through an Ethernet cable

Through this connection I log in to computer number 1 over SSH.

You can see the IP of the Ubuntu computer above 192.0.2.131/24 , while the Windows machine has the IP 192.0.2.130/24 it is naturally not visible here.

Read how to configure this IP below



3: The third interface wlp1s0 is the connection to a local Wi-Fi from a mobile phone. The IP is obtained dynamically 192.168.43.4/24

DHCP

If you need to obtain an ip automatically

iface eth0 inet dhcp

Static IP

First you need to find out the interface names

ip a

Then you can use the ip utility or configure via netplan

ip

An example of assigning the IP address 10.0.2.16/24 to the enp0s31f6 interface

sudo ip addr add 10.0.2.16/24 dev enp0s31f6

netplan

You need to go into the directory

/etc/netplan/

and see what the settings file is called

cd /etc/netplan
ls -la

Let us edit the file 01-network-manager-all.yaml

sudo vi 01-network-manager-all.yaml

The most important thing is to watch the indentation. For example, the - before IP must be exactly under the second letter d in the word addresses.

Indentation must be done with a space, not a tab

network: version: 2 renderer: networkd ethernets: ens32: addresses: - 172.16.120.74/16 nameservers: addresses: [127.0.0.53, 172.16.1.5]

nameservers: are not always needed, you can first not specify them and see whether everything suits you.

The new settings can be applied with the command

sudo netplan apply

You can check the result by running ip a again

ip a

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: enp3s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000 link/ether 30:f9:ed:d9:ea:d3 brd ff:ff:ff:ff:ff:ff inet 192.0.2.131/24 brd 192.0.2.255 scope global enp3s0 valid_lft forever preferred_lft forever 3: wlp1s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000 link/ether b8:76:3f:f5:c4:1f brd ff:ff:ff:ff:ff:ff inet 192.168.43.4/24 brd 192.168.43.255 scope global dynamic noprefixroute wlp1s0 valid_lft 2822sec preferred_lft 2822sec inet6 fe80::4deb:2d14:d1e8:8c7f/64 scope link noprefixroute valid_lft forever preferred_lft forever

ENABLE the enp0s25 interface:

ip link set dev enp0s25 up

DISABLE the enp0s25 interface:

ip link set dev enp0s25 down

Change the Gateway

Deleting the old gateway

route delete default

Adding a new gateway

route add default gw IP_Address

If there are many old gateways, you need to specify which exact IP to delete and from which interface

route delete default gw 10.0.0.1 eth0

For creation you can also specify the exact data

route add default gw 192.168.0.1 eth0

Change the netmask

You can learn what a netmask or subnet mask is in the article «Computer networks»

Find out your mask

ip r

default via 10.6.0.1 dev eth0
10.6.0.0/8 dev eth0 src 10.6.1.68

Delete the old one

ip address del 10.6.1.100/8 dev eth0

Add a new one

ip address add 10.6.1.68/22 dev eth0

Article author: Andrei Olegovich

Banner image