How to Configure Networking on Ubuntu Servers
Without a network connection, a server can’t function. Here’s what you need to know to configure networks on Ubuntu from the command line.
Ubuntu ships with some graphical utilities to configure network devices, but there are some topics that server administrators especially need to master—and that includes knowing how to configure network devices from the command line.
To manage your Ubuntu Server network from the command line, it is important to know concepts such as Ethernet interfaces, IP addressing, bridging, and name resolution. Here’s a primer.
Mục Lục
Managing Ethernet Interfaces
An Ethernet networking interface is the 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 will be sorted like this.
Logical Naming in Ethernet Interfaces
To view the available Ethernet interfaces, run the ifconfig command:
ifconfig -a | grep eth
eth0: flags=4098 mtu 1500
With the lshw command, you can define all available network interfaces on your system. Below you will see an example command. This example lshw 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.
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 in the distribution version you are using, you can install it using the following command:
sudo apt install
ethtool
After the ethtool installation is complete, you can see a sample output about eth0:
sudo ethtool eth0
You should remember that the changes you make with the ethtool command are temporary. If you want to keep these 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
Ubuntu 20.04 has a slight difference from older versions. Ubuntu versions after 18.04 now have /etc/netplan/ instead of /etc/network/interfaces. With this folder, it is possible to make changes to the network interface. It is also straightforward to use since it will contain a YAML file.
For example, using Netplan, you can configure the static IP as follows:
- Enter the /netplan folder using the following command and look at the file inside:
ls /etc/netplan
00-installer-config
.yaml
You may also encounter a file like network-manager-all.yaml here.
- 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 code editor:
sudo vim /etc/netplan/00-installer-config.yaml
- Since this is a “.yaml” file, it will be 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
What Is IP Addressing?
There are some useful commands for making temporary network configurations in GNU/Linux. 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. These configurations will be lost when you reboot your system.
First, you can handle the ifconfig command. For example, imagine you want to configure an IP address temporarily. 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. However, this is a non-permanent configuration. Below is a related example of this in use:
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
For dynamic address assignment, configure your Ubuntu server to use DHCP. 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
The ifdown command can be used to manually deactivate the interface. This command initiates the DHCP broadcast procedure while also closing the interface.
sudo ifdown eth0
Static IP Assignment
You may 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.
Loopback
You may have seen the lo statement when you used 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 should be configured automatically by two lines in the /etc/network/interfaces file by default. Here are two examples of default lines:
auto lo
iface lo inet loopback
What Is Name Resolution?
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.
How to Configure DNS Client
The resolvconf framework is used to monitor these changes and automatically update the settings. Manual changes to the /etc/resolv.conf file has 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 could look like this:
iface eth0 inet static
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 here, 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
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
What 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. The following example is related to this.
First, install the bridge-utils package:
sudo apt install
bridge-utils
After installation, open the /etc/network/interfaces file:
Of course, you must enter the appropriate values for your physical interface and network. Then bring up the bridge:
sudo ifup br0
You now have a new bridge interface.
Is Networking on Ubuntu Server Useful?
Ubuntu servers are generally available to someone with basic Linux knowledge. However, basic Linux 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, Ubuntu servers work in high performance and harmony with almost all systems and all platforms. 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.