9.11 Introduction to Network Interface Configuration
You’ve now seen all of the basic elements that go into the lower levels of a network stack: the physical layer, the network (internet) layer, and the Linux kernel’s network interfaces. In order to combine these pieces to connect a Linux machine to the internet, you or a piece of software must do the following:
- Connect the network hardware and ensure that the kernel has a driver for it. If the driver is present,
ip address showincludes an entry for the device, even if it hasn’t been configured. - Perform any additional physical layer setup, such as choosing a network name or password.
- Assign IP address(es) and subnets to the kernel network interface so that the kernel’s device drivers (physical layer) and internet subsystems (internet layer) can talk to each other.
- Add any additional necessary routes, including the default gateway.
When all machines were big stationary boxes wired together, this was relatively straightforward: the kernel did step 1, you didn’t need step 2, and you’d do step 3 with the old ifconfig command and step 4 with the old route command. We’ll briefly look at how to do this with the ip command.
9.11.1 Manually Configuring Interfaces
We’re now going to see how to set up interfaces manually, but we won’t go into too much detail because doing so is rarely needed and error-prone. This is typically something you’d only do when experimenting with your system. Even when configuring, you may wish to use a tool such as Netplan to build the configuration in a text file instead of using a series of commands as shown next.
You can bind an interface to the internet layer with the ip command. To add an IP address and subnet for a kernel network interface, you’d do this:
# ip address add address/subnet dev interface
Here, interface is the name of the interface, such as enp0s31f6 or eth0. This also works for IPv6, except that you need to add parameters (for example, to indicate link-local status). If you’d like to see all of the options, see the ip-address(8) manual page.
9.11.2 Manually Adding and Deleting Routes
With the interface up, you can add routes, which is typically just a matter of setting the default gateway, like this:
# ip route add default via gw-address dev interface
The gw-address parameter is the IP address of your default gateway; it must be an address in a locally connected subnet assigned to one of your network interfaces.
To remove the default gateway, run:
# ip route del default
You can easily override the default gateway with other routes. For example, say your machine is on subnet 10.23.2.0/24, you want to reach a subnet at 192.168.45.0/24, and you know that the host at 10.23.2.44 can act as a router for that subnet. Run this command to send traffic bound for 192.168.45.0 to that router:
# ip route add 192.168.45.0/24 via 10.23.2.44
You don’t need to specify the router in order to delete a route:
# ip route del 192.168.45.0/24
Before you go crazy with routes, you should know that configuring routes is often more complicated than it appears. For this particular example, you also have to make sure that the routing for all hosts on 192.163.45.0/24 can lead back to 10.23.2.0/24, or the first route you add is basically useless.
Normally, you should keep things as simple as possible, setting up local networks so that their hosts need only a default route. If you need multiple subnets and the ability to route between them, it’s usually best to configure the routers acting as the default gateways to do all of the work of routing between different local subnets. (You’ll see an example in Section 9.21.)