Configuring networking in Ubuntu has always been a pain. But using these three methods plus using the GUI should help network any fresh Ubuntu machine without using a GUI.
Netplan is one of the newer methods. Netplan uses YAML notation meaning that it is very picky about formatting. You may simply copy paste the example and write in your desired addresses.
Using netplan, you are able to configure the IP address, DNS server, and default route. The basic structure is as follows:
Note that the 'dhcp4' field decides whether to use DHCP or not. If set to 'true', i.e. you want to use DHCP, you may omit the other lines. If set to 'false', you may omit the 'dhcp4' line and instead just put the IP address. Both of these examples are provided in the Quick Reference.
Also note that the version field cannot be ommitted. Because of the way the file works, it needs to know which version of the file to look for. Without this field, netplan will not be able to interpret the file properly.
You can commit your config with a full reboot or by running sudo netplan apply.
/etc/network/interfaces is one of the older methods of configuring IP addressing. The formatting is not precise and only requires white space. In fact, if you wanted, you could collapse each interface into their own line, but we're not doing that here. The basic structure is as follows:
`iface` denotes an interface description, the name denotes the name of the interface, and inet denotes an IPv4 address. I do want to write an article about how to do this with IPv6 in the future, but for now, just stick to IPv4. Using `static` denotes a statically defined address, and `auto` denotes a DHCP address.
This config can be committed by rebooting or using `sudo /etc/network/interfaces.d restart`.
The `ip` command replaced the older `ifconfig` for a lot of things, and it includes the ability to set temporary IP addresses, as well as overload a single interface with multiple IP addresses. The biggest problem with IP though is that these interfaces are temporary; they do not survive a reboot. To commit a new IP address permanently, use one of the other two methods presented previously.
The `ip` command is a pain to look through the documentation for, but I will flag only three main comamnds to know here.
As you might expect, these commands configure an IP address and an IP route respectively. The `ip route` command can be used to, for example, configure a default gateway, such as in the quick reference example. The `ip address` command can be used to manage an interface's IP address, including setting an IP address, adding multiple IP addresses, or deleting IP addresses. To overload an interface with multiple IP addresses, you can do something like the following:
However, there's very little reason to do this. Because of the way the `ip` command works, however, you may find that you have accidentally configured multiple IP addresses on an interface, as shown below. Simply delete the old IP address.
As noted in the quick reference, you cannot configure DNS or DHCP using the `ip` command. Instead, you may perform a DHCP request using `dhclient <interface name>`. This will allow your interface to grab a DHCP address from a server, but you will have to rerun it everytime you want a new lease.
DNS servers can be configured using `/etc/resolv.conf`. Simply add the IP addresses preceeded by `nameserver`. If your file tells you not to edit the file, you should first delete the file, then make a new file. An example file is provided below.
Like the rest of the commands in this section, this is a live change, so no commit is needed. Unlike the rest of the commands in this section, this will survive a reboot.