Labs / Network Basics / Neighbours & joining

Lab 4 of 4 · Neighbours & joining

Linux networking: bring a blank machine online by hand

Four settings, typed one at a time, then written down to survive a restart.

Start this lab 6 steps

The idea

Leo's new machine has a network card, a cable and nothing else. No address, no route, no way to turn a name into an address, and no DHCP server on this network to hand them over.

Set it up by hand, one piece at a time, and watch each piece open up more of the network. Then make it last, because most of what you type here is gone at the next restart.

Four settings, all empty
Four settings, all empty
The mask says who is near
The mask says who is near
Two lines, two ways to go
Two lines, two ways to go
One page, four settings
One page, four settings
Where a name goes
Where a name goes
What a restart keeps
What a restart keeps

Steps

  1. Read a blank machine

    A machine needs four things to work on a network: an address, a mask that says which addresses are near, a way out to other networks, and a way to turn names into addresses. DHCP hands all four over, as you saw in the lab on it. Servers and routers often set them by hand, because their addresses must not change, and you fall back on the same skill when DHCP fails.

    Start by reading what Leo's machine has. Linux keeps the first three in the kernel, the core of the system, and ip is the tool that reads and changes them.

    leo:~# ip -br link
    leo:~# ip -br -4 addr
    leo:~# ping -c 1 10.0.1.20
  2. Switch on the card and take an address

    Two things are missing before Leo can reach even the next desk: a card that is on, and an address. Linux leaves a card off until something turns it on. ip link set dev eth0 up does that, and ip addr add puts an address on it.

    Older guides use ifconfig and route for this. Those tools are obsolete, and many systems no longer install them, so learn ip.

    The /24 matters as much as the number. It is the mask, and it tells the machine which addresses are near. /24 says the first three numbers, 10.0.1, name the network, so every address that starts with 10.0.1. is near.

    Near means Leo can reach them himself, with no router. Leave the mask off and Linux takes /32, a network of one machine, and Noah is out of reach.

    Both commands print nothing when they work. That is the Linux habit: quiet on success, loud on failure. So an empty screen here is the result you want.

    Leo takes 10.0.1.10, the address he was given for this machine. Nothing checks it. Linux accepts any address you type, free or not, and the second Practice problem shows what that costs.

    leo:~# ip link set dev eth0 up
    leo:~# ip addr add 10.0.1.10/24 dev eth0
    leo:~# ping -c 1 10.0.1.20
  3. Give it a way out

    Leo can reach his own network now and nothing past it. Zoe's machine sits behind gate, and Leo's machine doesn't know that yet.

    The routing table holds that knowledge. It is a list of rules, each one saying where messages for some set of addresses should go.

    You have read one before, on a machine somebody else set up. This time you see where each line comes from. Leo hasn't typed a single route, so what is in the table?

    Read it first. Then add the line that matters most: a default route. It hands every message that isn't for a near address to one router, his default gateway.

    default matches every address, so that one line covers the whole world beyond his own network.

    Leo's gateway is gate, at 10.0.1.2. On a real network that address comes from whoever runs it, or from ip route on a machine that already works.

    leo:~# ip route
    leo:~# ip route add default via 10.0.1.2
    leo:~# ping -c 1 10.0.2.10
  4. Give it names

    Leo can reach any address now. People don't type addresses, though, and the page he wants is at zoe.lab.

    When a program meets a name, the system asks a DNS server for the address. Linux learns which server to ask from one plain text file, /etc/resolv.conf.

    The file takes a line that starts with nameserver, then the server's address. On this network gate answers names too, at 10.0.1.2. DHCP normally writes this file for you, and here you write it yourself.

    So far you have changed the kernel with commands. This setting is different: you edit a file, and that is how most Linux settings work. They are plain text, mostly under /etc, and every one of them is there to read.

    Try the name first and read how it fails. Then edit the file.

    leo:~# curl -sS -m 5 http://zoe.lab/
    leo:~# curl -sS -m 5 http://zoe.lab/
  5. Check the list that comes first

    Before DNS existed, every machine on the early internet kept its own list of names and addresses. Linux still has that list, /etc/hosts, and Leo's own name is in it. Each line is an address, then a name.

    The list stayed because it is handy. It lets you try a new server under its real name before the name goes live, or name the few machines on a small network with no DNS server at all.

    The list can also disagree with DNS. Noah keeps a test copy of Zoe's site, so Leo adds a line that points zoe.lab at Noah's machine. The list then says one thing, and DNS still says another.

    Ask both ways: curl for the page, and nslookup for the address.

    leo:~# curl -sS -m 5 http://zoe.lab/
    leo:~# nslookup zoe.lab
  6. Make it last

    Most of what you typed lives in the kernel's memory. The card being on, the address and the route all vanish when the machine stops. /etc/resolv.conf is a file, so it stays.

    The lab now wipes the rest, the way a restart does. To last, those settings go in a file the machine reads as it starts. Linux comes in many versions, called distributions, and this is the part of network configuration where they differ.

    • Alpine and Debian: /etc/network/interfaces, read by ifup.
    • Ubuntu: settings files in /etc/netplan/.
    • Fedora and Red Hat: NetworkManager, a background program you drive with nmcli.

    This machine runs Alpine, and its file needs five lines. auto eth0 means bring the card up at start. iface eth0 inet static means eth0, IPv4, fixed settings.

    The three lines under it are the address, the mask and the gateway you typed. ifup eth0 reads the file, and starting the machine does the same. -v prints what it does.

    leo:~# ip -br -4 addr
    leo:~# ifup -v eth0
    leo:~# curl -sS -m 5 http://zoe.lab/

Run these steps on real machines

Keep reading

Learn it on real machines.

Every lab is a small network of real Linux machines in your browser. Sign in with Google or email to start.

Start this lab