Courses3 levels
Level 1 · Foundations11 courses
Level 2 · Core5 courses
Level 3 · Advanced5 courses
Labs8 topics
Library Practice exams AI Range Pricing Start free

Part 3 · Networking and hardening · Lesson 02 · 45 minutes

Configure addresses, routes and names

After this lesson you can
  • Add and remove an address, bring an interface up and down, and change routes
  • Set a local name, a DNS server and the host name
  • Make a network setting permanent with netplan
Lab files of this lesson (2)

The lab folder as it stands after this lesson. Unzip it in ~/krlab.

More

The zip holds every file that the course has printed up to here. To start this lesson with the files of the earlier lessons only, take the zip of the lesson before. Type the code yourself first: you learn more.

Scenario

You must connect a new test server to a second network, 198.51.100.0/24, and give another machine on it a short name. Your team lead warns you: "Do this on the practice system first. A wrong default route cuts you off from the machine." You build the setting on a dummy interface, which is a virtual interface that carries no real traffic, so a mistake cannot cut you off. Then you make it survive a reboot.

Recall

Which command shows the route that the kernel picks for one address, without sending a packet?

ip route get ADDRESS. It prints the interface, the gateway (via) and the source address that the kernel would use.

ss shows "0.0.0.0:8011" for a server. Who can connect?

Any machine that can reach one of your addresses. A listener on 127.0.0.1 is private to the machine.

The idea

The kernel holds the live network state: addresses, routes, the state of each link. The command ip changes that state at once, and it is lost at the next reboot. To keep a setting, you write it into a configuration file that a network manager reads at boot. On Ubuntu Server that file is netplan YAML. Netplan only writes the settings for a backend (systemd-networkd on a server, NetworkManager on a desktop), and the backend applies them to the kernel.

Where a network setting lives
/etc/netplan/*.yamlyour permanent wishBackendnetworkd or NetworkManagerKernelthe live state that ip shows/etc/netplan/*.yamlyour permanent wishBackendnetworkd or NetworkManagerKernelthe live state that ip shows

Names have their own order. The file /etc/hosts is read first. A DNS server answers next, and the system takes its address from /etc/resolv.conf. On Ubuntu that file points to 127.0.0.53, the small local resolver of systemd-resolved, which forwards the question to the DNS servers of each link. resolvectl shows and changes those servers. The host name is the name of this machine.

How the settings are kept on the common families:

SystemPermanent network settingsTool
Ubuntu/etc/netplan/*.yamlnetplan apply
Any, with systemd/etc/systemd/network/*.networknetworkctl reload
Desktop LinuxNetworkManager connectionsnmcli, nmtui
Older Debian/etc/network/interfacesifup, ifdown
Older RHEL and CentOS/etc/sysconfig/network-scripts/ifcfg-*ifup, ifdown

Worked example

Every step that changes the system needs administrator rights, so you type sudo in front of it.

1. Make a dummy interface

ip link add NAME type dummy creates a virtual interface, and ip link set NAME up brings it up. ip -br link show NAME prints its state.

sudo ip link add krdummy0 type dummy
sudo ip link set krdummy0 up
ip -br link show krdummy0
krdummy0         UNKNOWN        86:07:51:0c:8e:0b <BROADCAST,NOARP,UP,LOWER_UP>

The state UNKNOWN is normal for a dummy: it has no cable to report on. The link is up.

2. Add and remove an address

ip addr add ADDRESS/LENGTH dev NAME gives the interface an address. A second address on the same network becomes secondary. ip addr del removes one.

sudo ip addr add 198.51.100.10/24 dev krdummy0
sudo ip addr add 198.51.100.11/24 dev krdummy0
ip -4 addr show krdummy0
sudo ip addr del 198.51.100.11/24 dev krdummy0
ip -br addr show krdummy0
3: krdummy0: <BROADCAST,NOARP,UP,LOWER_UP> mtu 1500 qdisc noqueue state UNKNOWN group default qlen 1000
    inet 198.51.100.10/24 scope global krdummy0
       valid_lft forever preferred_lft forever
    inet 198.51.100.11/24 scope global secondary krdummy0
       valid_lft forever preferred_lft forever
krdummy0         UNKNOWN        198.51.100.10/24 fe80::c4e:6dff:fee2:8070/64

3. Bring the interface down and up

When a link goes down, its routes go with it. The address stays on the interface.

sudo ip link set krdummy0 down
ip -br link show krdummy0
ip route show dev krdummy0
sudo ip link set krdummy0 up
ip route show dev krdummy0
krdummy0         DOWN           86:07:51:0c:8e:0b <BROADCAST,NOARP>
198.51.100.0/24 proto kernel scope link src 198.51.100.10

The first route command prints nothing, because a down link has no routes. After up, the kernel adds the route of the network by itself.

4. Add, change and remove routes

ip route add NETWORK via GATEWAY sends the traffic for a network to a gateway. The gateway must be reachable on a directly connected network. ip route replace changes a route, and ip route del removes it.

sudo ip route add 203.0.113.0/24 via 198.51.100.1
ip route get 203.0.113.9
203.0.113.9 via 198.51.100.1 dev krdummy0 src 198.51.100.10 uid 1000
    cache

The default route is the route for everything else, and a wrong change of it cuts a real machine off. So add a spare default route with a high metric:

A spare default route
ip route add defaultthe route for everything elsevia 198.51.100.1send it to this gatewaydev krdummy0out of this interfacemetric 500the lowest metric winsip route add defaultthe route for everything elsevia 198.51.100.1send it to this gatewaydev krdummy0out of this interfacemetric 500the lowest metric wins

The kernel uses the route with the lowest metric, so the spare is never used while the main one works.

sudo ip route add default via 198.51.100.1 dev krdummy0 metric 500
sudo ip route replace default via 198.51.100.2 dev krdummy0 metric 500
ip route show default
ip route get 8.8.8.8
default via 192.0.2.1 dev eth0 proto dhcp src 192.0.2.10 metric 100
default via 198.51.100.2 dev krdummy0 metric 500
8.8.8.8 via 192.0.2.1 dev eth0 src 192.0.2.10 uid 1000
    cache

ip route get 8.8.8.8 sends no packet. It only asks the kernel which route it would pick, and it still picks eth0. Remove both routes:

sudo ip route del default via 198.51.100.2 dev krdummy0 metric 500
sudo ip route del 203.0.113.0/24

Do not run ip route replace default on your main interface over a remote session. Use a console, or use netplan try (step 8), which rolls back.

5. Give another system a name

The file /etc/hosts maps an address to names, one line each. tee -a appends the text that it reads to a file, and it runs as root here because sudo stands in front of it. getent hosts then resolves the name the way a program does.

echo "198.51.100.20 files.lab files" | sudo tee -a /etc/hosts
getent hosts files.lab
getent hosts files
198.51.100.20 files.lab files
198.51.100.20   files.lab files
198.51.100.20   files.lab files

The first line is the text that tee echoes. It has three parts, and every name on the line works:

One line of /etc/hosts
198.51.100.20the addressfiles.labthe full namefilesa short name, same address198.51.100.20the addressfiles.labthe full namefilesa short name, same address

The machine 198.51.100.20 does not exist here, so only the name is tested, not a connection.

6. Read and set the DNS servers

resolvectl status shows the DNS servers per link. resolvectl dns LINK ADDRESS sets a server for one link, and resolvectl domain LINK NAME sets its search domain. resolvectl revert LINK undoes both. These changes are live only: they vanish at a reboot.

resolvectl dns
sudo resolvectl dns krdummy0 198.51.100.53
sudo resolvectl domain krdummy0 lab
resolvectl status krdummy0
sudo resolvectl revert krdummy0
ls -l /etc/resolv.conf
Global:
Link 2 (eth0): 192.0.2.1
Link 3 (krdummy0):
Link 3 (krdummy0)
    Current Scopes: DNS
         Protocols: +DefaultRoute -LLMNR -mDNS -DNSOverTLS DNSSEC=no/unsupported
Current DNS Server: 198.51.100.53
       DNS Servers: 198.51.100.53
        DNS Domain: lab
lrwxrwxrwx 1 root root 39 Nov 18 07:50 /etc/resolv.conf -> ../run/systemd/resolve/stub-resolv.conf

/etc/resolv.conf is a link to a file that systemd-resolved writes. Do not edit it. A program always asks 127.0.0.53, and the resolver chooses the real server.

7. Read and set the host name

hostnamectl shows the host name, and hostnamectl set-hostname NAME sets it. The change is permanent: it goes into /etc/hostname.

OLD=$(hostname)
sudo hostnamectl set-hostname krlab-net
hostname
cat /etc/hostname
sudo hostnamectl set-hostname "$OLD"
krlab-net
krlab-net

The last command puts the old name back. If the old name is also in a line of /etc/hosts, change that line too, or sudo prints a warning.

8. Make the setting permanent with netplan

Write one YAML file, 60-krlab.yaml. The key dummy-devices makes the interface at boot, and addresses and routes hold what you typed with ip.

60-krlab.yaml

network:
  version: 2
  dummy-devices:
    krdummy0:
      addresses: [198.51.100.10/24]
      routes:
        - to: 203.0.113.0/24
          via: 198.51.100.1

Save it in your lab folder (or unzip the lab files of the course, go into the folder linux, and run cp -R 3-advanced/02-configure-addresses-routes-and-names/. ~/krlab/linux-advanced/; sh kr-lab.sh 3-advanced/02-configure-addresses-routes-and-names ~/krlab/linux-advanced does the same), and copy it to /etc/netplan. Netplan warns about a file that others can read, so set mode 600. netplan generate writes the backend files and finds errors, and netplan apply applies them. sudo netplan try applies them for 120 seconds and rolls back if you do not confirm: use it on a remote machine.

sudo cp 60-krlab.yaml /etc/netplan/60-krlab.yaml
sudo chmod 600 /etc/netplan/60-krlab.yaml
sudo netplan generate
sudo netplan apply
ip route show dev krdummy0
198.51.100.0/24 proto kernel scope link src 198.51.100.10
203.0.113.0/24 via 198.51.100.1 proto static

Both routes now come from the file, not from your typing. The file for a real interface with a fixed address looks like this. It was checked with netplan generate and not applied:

network:
  version: 2
  ethernets:
    eth0:
      dhcp4: false
      addresses: [192.0.2.10/24]
      routes:
        - to: default
          via: 192.0.2.1
      nameservers:
        addresses: [192.0.2.53]
        search: [lab]

9. Clean up

Remove the file, apply, and delete the interface (netplan does not delete a virtual interface that it created). Then take the line out of /etc/hosts. sed -i '/pattern/d' deletes, in place, every line that holds the pattern.

sudo rm /etc/netplan/60-krlab.yaml
sudo netplan apply
sudo ip link del krdummy0
sudo sed -i '/files\.lab/d' /etc/hosts

Your turn

Build a second small network. There are no steps here: use what the worked example showed.

  1. Make the dummy interface krdummy1, bring it up, and give it the addresses 203.0.113.77/24 and 203.0.113.78/24. Then remove 203.0.113.77/24.
  2. Add a route to the network 198.18.0.0/24 through the gateway 203.0.113.1. This network is new, so the route cannot clash with the worked example.
  3. Give the name printer.lab to 203.0.113.50 in /etc/hosts.
  4. Set the host name to krlab-net, and read it back.

Now bring krdummy1 down and read its state with ip -br link show krdummy1.

Last, put the old host name back, delete krdummy1, and remove the printer.lab line from /etc/hosts.

Check yourself

You add an address with "ip addr add" and the machine reboots. What is left?

Nothing. ip changes only the live state of the kernel. Put the address into /etc/netplan/*.yaml (or the file of your network manager) to keep it.

Why is a second default route with metric 500 safe, and a "replace" of the main default route is not?

The kernel uses the lowest metric, so the route with metric 500 is only a spare, and a mistake in it changes nothing. A replace of the main default route changes the path of all traffic at once. On a remote session, a wrong gateway cuts the session.

You edited /etc/resolv.conf and the change is gone after a short time. Why?

On Ubuntu the file is a link that systemd-resolved rewrites. Set DNS servers in the netplan file (nameservers), or with resolvectl for a test.

What does the line "172.16.0.9 db" in /etc/hosts do to a program that connects to "db"?

The resolver reads /etc/hosts first, so the program connects to 172.16.0.9 and DNS is never asked. This is useful for a lab, and dangerous if an attacker can write the file.

Field notes

  • Use netplan try for any change of a remote machine. If you lose the session, the old setting returns after the time-out.
  • ip changes are for tests. A setting that is not in a file is a setting that the next reboot erases, and a surprise for the next administrator.
  • /etc/hosts is also a defence tool: pointing a known bad name to 127.0.0.1 stops a program on this machine from reaching it. An attacker also uses it, so a changed /etc/hosts is a sign to investigate.
  • On a desktop, NetworkManager owns the interfaces. nmcli connection show lists its profiles, and a manual ip change may be overwritten. Do not run two managers on one interface.
  • gateway4 in older netplan files is deprecated. Use routes with to: default.