Ubuntu servers come with some utilities for network configurations. However, there are some Ubuntu network configuration topics that server administrators need to master. That includes knowing how to configure network devices from the command line.
To manage your Ubuntu Server network configuration from the command line, it is important to know concepts such as Ethernet interfaces, IP addressing, bridging, and name resolution. Here's a primer.
Managing Ethernet Interfaces on Ubuntu
An Ethernet networking interface is a circuit board with an Ethernet port that enables your computer to establish an Ethernet connection. Ethernet interfaces have a simple naming convention. The first Ethernet interface is typically eth0. Then comes eth1. All additional interfaces are named like this.
Logical Naming in Ethernet Interfaces
To view the available Ethernet interfaces, run the following ifconfig command:
ifconfig -a | grep eth
# Output
eth0: flags=4098 mtu 1500
With the lshw command, you can define all the network interfaces you need for configuring networks on Ubuntu. Below you will see an example command. This command will display bus information, driver details, and all its supported capabilities as a single Ethernet interface:
sudo lshw -class network
You can use the file /etc/udev/rules.d/70-persistent-net.rules to configure the logical names for the interface. To control which interface gets which logical name, you will need the physical MAC addresses of the interfaces.
You can find the line that matches the physical MAC address and change NAME=ethA to whatever you want. Reboot your system immediately afterward to save the changes.
Configuring Ethernet Interface on Ubuntu 18.04 and Earlier
With the ethtool program, you can view settings such as auto-negotiation, duplex mode, and port speed. If ethtool is not installed on your server, you can install it using the following command:
sudo apt install ethtool
After the installation is complete, you can see the sample output for eth0:
sudo ethtool eth0
You should remember that the changes you make with the ethtool command are temporary. If you want to keep those settings, you must add the desired ethtool command to a boot statement in the /etc/network/interfaces file.
For example, you want the interface named eth0 to have a connection speed of 500MB/s running in duplex mode. To configure this permanently, you can edit the /etc/network/interfaces file as follows:
The configuration you've seen above also works with other methods like DHCP, even if it's a static method interface.
Configure Ethernet Interface on Ubuntu 20.04 and Later
The path you need to follow for ethernet network configuration in Ubuntu 20.04 is slightly different from older Ubuntu versions. Newer Ubuntu releases (after 18.04) now have /etc/netplan/ instead of /etc/network/interfaces.
With this folder, it is possible to make changes to the ethernet network interface. It is also straightforward to use since it contains a YAML file.
Using Netplan, you can configure the static IP as follows:
-
Enter the /netplan folder using the following command and look at the files inside:
ls /etc/netplan
# Output
00-installer-config.yaml -
Create a backup of this file with the following command:
sudo cp /etc/netplan/00-installer-config.yaml 00-installer-config.yaml.copy
-
Then, open the file with your favorite text editor:
sudo vim /etc/netplan/00-installer-config.yaml
-
Since this is a YAML file, it's easy to make the necessary edits. Edit the file the way you want. Here is an example:
-
After configuring your file as above, save and exit. Apply the necessary changes with the following command:
sudo netplan apply
-
You can now check the changes using the following command:
ip addr
IP Addressing on Ubuntu
There are several useful Linux commands for making temporary network configurations. Commands such as ip, ifconfig, and route will help you with these configurations. These commands configure parameters that take immediate impact but are not permanent. This means any configurations you make will be lost once you reboot your system.
First, let's take a look at the ifconfig command. Imagine you want to configure an IP address temporarily. To do this, simply change the IP address and subnet mask to suit your network needs.
If you wish to use the route command to specify the default gateway, use the following command as an example:
route add default gw 10.0.0.1 eth0
To test this setting, run the following command:
route -n
Sometimes you need DNS for temporary network configuration. For this, you can add the DNS server IP addresses to the /etc/resolv.conf file. Configuring this file directly can be a concern. But this is a non-permanent Ubuntu network configuration step. Below is a related example of this in use:
# vim /etc/resolv.conf
nameserver 8.8.8.8
nameserver 8.8.4.4
If you no longer need the configurations you made, you can use the following command to flush them:
ip addr flush eth0
Clearing the IP configuration with the above command does not apply to the /etc/resolv.conf file. You have to manually remove the information contained in this file and restart your system.
Dynamic IP Assignment on Ubuntu
Set your Ubuntu 20.04 network configuration to use DHCP for dynamic address assignment. To do this, you must add the DHCP method to the inet address family declaration for the appropriate interface in the /etc/network/interfaces file.
auto eth0
iface eth0 inet dhcp
You may also manually activate the interface using the ifup command, which begins the DHCP operation through dhclient.
sudo ifup eth0
You can use the ifdown command to manually deactivate the interface. This command initiates the DHCP broadcast procedure while also closing the interface.
sudo ifdown eth0
Static IP Assignment on Ubuntu
You can update the /etc/network/interfaces file again to set up your Ubuntu server with a static IP address assignment. In this file, you may add your static method to the inet address family for the relevant interface.
As with dynamic IP assignments, you can manually enable or disable the interface with the ifup and ifdown commands.
Configuring the Loopback Interface
You may have seen the lo statement when using the ifconfig command. The lo expression here is loopback and uses the IP address 127.0.0.1 by default.
ifconfig lo
The loopback interface is configured automatically because of the following two lines in the /etc/network/interfaces file:
auto lo
iface lo inet loopback
Name Resolution on Ubuntu
Name resolution is the process of converting IP addresses to hostnames. There are, however, a few things you need to know about DNS and static hostname records for name resolution on Ubuntu.
How to Configure DNS Client on Ubuntu
The resolvconf framework is used to monitor these changes and automatically update the settings. Manual changes to the /etc/resolv.conf file have an impact on resolvconf. To overcome this, utilize DHCP client hooks and /etc/network/interfaces:
/etc/resolv.conf -> ../run/resolvconf/resolv.conf
Add the IP addresses of the nameservers available for configuration to the /etc/network/interfaces file. If your network has multiple subdomains to search, you can use them as well. Your file should look like this:
iface eth0 inet static
# Output
address 192.168.2.2
netmask 255.255.255.0
gateway 192.168.2.1
dns-search test.com market.test.com support.test.com dns-nameservers 192.168.2.25 192.168.8.11
If you ping the host with a server, the queries for the FQDN (Fully Qualified Domain Name) will be based on your domain name order. In this example, they are test.com, market.test.com, and support.test.com, respectively.
What Are Static Hostnames?
Static hostnames are associated with the /etc/hosts file. Inputs in the hosts file take precedence for DNS. If your system is trying to resolve a hostname, it will browse the /etc/hosts file. If it finds any match there, it won't try to look it up in DNS.
Here's an example of a hosts file with FQDNs:
127.0.0.1 localhost
127.0.1.1 muo-server
10.0.0.11 server1 server1.test.com vpn
10.0.0.12 server2 server2.test.com mail
10.0.0.13 server3 server3.test.com www
NSS Configurations on Ubuntu
The /etc/nsswitch.conf file controls the NSS (Name Service Switch). Here, the order in which your system chooses methods to resolve hostnames to IP addresses is controlled. Here is an example of /etc/nsswitch.conf:
hosts: files mdns4_minimal [NOTFOUND=return] dns mdns4
- files: First try to resolve static hostnames in the hosts file
- mdns4_minimal: Try to resolve using Multicast DNS
- [NOTFOUND=return]: If the Multicast DNS result returns a NOTFOUND response, do not attempt to continue.
- dns: Legacy unicast DNS query
- mdns4: Multicast DNS query
You can change the hosts: string to whatever you want to change the order of these methods.
hosts: files dns [NOTFOUND=return] mdns4_minimal mdns4
Bridging on Ubuntu
Another aspect that you should not ignore for your Ubuntu server configuration is bridging. It is very convenient to communicate between multiple interfaces when there are multiple scenarios.
As an example, you might consider that you want to use a bridge on a system with a single interface to allow virtual machines to access the outside network directly.
First, install the bridge-utils package:
sudo apt install bridge-utils
After installation, open the /etc/network/interfaces file:
Enter the appropriate values for your physical interface and network. Then bring up the bridge with:
sudo ifup br0
You now have a new bridge interface set up on your Ubuntu Server.
Is Networking on Ubuntu Server Useful?
Ubuntu servers are generally available to everyone, even if you've rudimentary Linux knowledge. However, basic knowledge may not be enough, especially in today's world where security problems are increasing. Nonetheless, it certainly makes sense to take advantage of the power of Linux.
Most importantly, if the Ubuntu server network configuration is done correctly, it works with almost all systems with high performance and compatibility. It is also highly compatible with popular products such as Microsoft Hyper-V and VMware. Many multi-user websites and online multiplayer games use Ubuntu servers.