CHAPTER 6
Networks consist of a set of devices (two or more) such as computers, printers, and other related equipment that are connected by either physical cable or wireless links. The purpose of this is to share and distribute information among the connected devices.
This section focuses on managing and configuring a network on the command line.
Ethernet networking interfaces refer to a circuit board or cards installed in a personal computer or workstation as a network client. These allow a computer or mobile device to connect to a local area network (LAN) using Ethernet protocol as the transmission mechanism.
Ethernet interfaces are identified by Ubuntu using the naming convention of ethX, where X represents a numeric value. The first Ethernet interface is typically identified as eth0, the second as eth1, and so on.
The following command can be used to quickly identify all available Ethernet interfaces.
Code Listing 83
$ ifconfig -a | grep -i eth |
The output generated by the command should look like the following snippet.
Code Listing 84
eth0 Link encap:Ethernet HWaddr 00:15:c5:4a:16:5a |
The previous snippet shows that only one Ethernet interface is available. In this case, eth0 identifies the interface. The hexadecimal digits shown after the HWaddr abbreviation correspond to the MAC (Media Access Control) address. This address is a unique identifier assigned to the network interface when it is manufactured. The manufacturer “burns” this address into the interface circuit. A MAC address is formed by six sets of two digits each. This address never changes.
It’s also possible to obtain detailed info about Ethernet interfaces. For doing this, the lshw command must be used, like in the following snippet.
Code Listing 85
$ sudo lshw –-class network |
The previous example shows the extended info for the eth0 interface along with driver details, bus information, and all supported capabilities. The output for the command should look like the following snippet.
Code Listing 86
*-network description: Ethernet interface product: BCM4401-B0 100Base-TX vendor: Broadcom Corporation physical id: 0 bus info: pci@0000:03:00.0 logical name: eth0 version: 02 serial: 00:15:c5:4a:16:5a size: 10MB/s capacity: 100MB/s width: 32 bits clock: 33MHz capabilities: pm pcix bus_master cap_list rom … configuration: autonegotiation = on broadcast = yes … resources: irq:17 memory:ef9fe000-ef9fffff |
The user can view the configuration values for any network adapter using the ethtool. The following command shows the configuration information for the eth0 adapter.
Code Listing 87
$ sudo ethtool eth0 |
The output should look like the following snippet.
Code Listing 88
Settings for eth0: Supported ports: [ TP ] Supported link modes: 10baseT/Half 10baseT/Full 100baseT/Half 100baseT/Full 1000baseT/Half 1000baseT/Full Supports auto-negotiation: Yes Advertised link modes: 10baseT/Half 10baseT/Full 100baseT/Half 100baseT/Full 1000baseT/Half 1000baseT/Full Advertised auto-negotiation: Yes Speed: 1000Mb/s Duplex: Full Port: Twisted Pair PHYAD: 1 Transceiver: internal Auto-negotiation: on Supports Wake-on: g Wake-on: d Current message level: 0x000000ff (255) Link detected: yes |
It’s also possible to gather statistics about network adapters. This can be useful for troubleshooting received (rx) and transmitted (tx) traffic issues. These statistics can be obtained by running the following command, in this case for the eth0 adapter.
Code Listing 89
$ sudo ethtool –-S eth0 |
The command output should look like the following example.
Code Listing 90
NIC statistics: tx_packets: 148683 rx_packets: 179489 tx_errors: 0 rx_errors: 0 rx_missed: 0 align_errors: 0 tx_single_collisions: 0 tx_multi_collisions: 0 unicast: 116884 broadcast: 25361 multicast: 61674 tx_aborted: 0 tx_underrun: 0 |
From the previous output the user can see a list of transmitted (tx) packets, received (rx) packets, and packet errors.
Another useful task that you can do with the ethtool command is identify network cards in the system. This applies in cases where there is more than one adapter attached to the computer and the user wants to know which physical card is a recorder for a particular ethX logical device. The following example shows how to do this.
Code Listing 91
$ sudo eththool -p eth0 5 |
In the previous snippet, the –p flag tells ethtool that it has to perform a physical identification for the eth0 logical adapter. The number 5 at the end of the line corresponds to the number of seconds that the physical device LED will blink. Now, the user only has to look at the back of the computer to know which physical adapter is attached to the eth0 logical device.
IP addressing is the process of assigning an IP address to each device participating in a computer network that uses the Internet Protocol of communication.
An IP address is a unique numeric label that identifies a device on the Internet or a local network, allowing the device to be recognized by other connected systems.
IP addresses are defined as 32-bit numbers under the system known as Internet Protocol Version 4 (IPv4), which is the most widely used today. However, because of the growth of the Internet, a depletion of available addresses has been foreseen, and a new version of IP (IPv6) has emerged. This new version was developed in 1995 and uses 128 bits for the address. IP version 6 addresses have been deployed since the mid-2000s.
This section will focus on IPv4 addressing and will describe the appropriate commands for successfully doing it.
IPv4 addresses are represented in dot-decimal notation, which consists of four decimal numbers separated by dots, where each number ranges from 0 to 255. Each part represents a group of 8 bits (octet) of the address, for example, 172.16.254.1.
Since 1981, the Classful Network architecture is used for IP addressing under IPv4. This method divides the address space into five classes. Each class, based on the first four bits of the address, defines either a network size, i.e. number of hosts for unicast addresses (classes A,B,C), or a multicast network (class D). The fifth class is reserved for experimental purposes.
The following table summarizes the classes defined for universal unicast (sending messages to a unique destination) addressing.
Classful Network Architecture
Class | A | B | C |
|---|---|---|---|
Leading bits | 0 | 10 | 110 |
Size of network number bit field | 8 | 16 | 24 |
Size of rest bit field | 24 | 16 | 8 |
Number of networks | 128 (27) | 16,384 (214) | 2,097,152 (221) |
Addresses per network | 16,777,216 (224) | 65,536 (216) | 256 (28) |
Start address | 0.0.0.0 | 128.0.0.0 | 192.0.0.0 |
End address | 127.255.255.255 | 191.255.255.255 | 223.255.255.255 |
This design was intended to assign a unique IP address to a particular computer or device in a global end-to-end connectivity environment. However, this is not always necessary in the case of private networks because these networks are not always connected to the Internet. The computers or devices connected to these networks don’t require global unique IP addresses, so three non-overlapping ranges of IPv4 addresses for private networks were reserved in RFC 1998 of the IETF (Internet Engineering Task Force). These addresses are not used on the Internet. The following table show these ranges.
Private IPv4 Network Ranges
Start | End | Number of addresses | |
|---|---|---|---|
24-bit block (/8 prefix, 1 x A) | 10.0.0.0 | 10.255.255.255 | 16,777,216 |
20-bit block (/12 prefix, 16 x B) | 172.16.0.0 | 172.31.255.255 | 1,048,576 |
16-bit block (/16 prefix, 256 x C) | 192.168.0.0 | 192.168.255.255 | 65,536 |
These blocks are available for any user, but network administrators typically divide a block into subnets. For example, many small office or home office routers automatically use a default address range from 192.168.0.0 to 192.168.0.255.
There are several commands in Ubuntu used to manage private IP addressing. One of them is ifconfig. This command allows the user to verify the IP address configuration for a particular network adapter.
Code Listing 92
$ ifconfig eth0 |
The previous example shows the address configuration for the eth0 adapter. The following snippet shows the output display after execution of the command.
Code Listing 93
eth0 Link encap:Ethernet HWaddr 00:15:c5:4a:16:5a inet addr:10.0.0.100 Bcast:10.0.0.255 Mask:255.255.255.0 inet6 addr: fe80::215:c5ff:fe4a:165a/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:466475604 errors:0 dropped:0 overruns:0 frame:0 TX packets:403172654 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:2574778386 (2.5 GB) TX bytes:1618367329 (1.6 GB) Interrupt:16 |
A temporary IP address for a network adapter can be configured with the ifconfig command, like in the following example.
Code Listing 94
$ sudo ifconfig eth0 192.168.0.25 netmask 255.255.255.0 |
In this case, the IP address assigned will be lost after the computer is rebooted or powered off and powered on again. To configure the system to assign a specific IP address for the eth0 network adapter, the file /etc/network/interfaces must be edited. The following example shows the lines that must be added to the file to assign a static IP address.
Code Listing 95
auto eth0 iface eth0 inet static address 192.168.0.25 netmask 255.255.255.0 gateway 192.168.0.1 |
Then, this IP address will be always assigned every time the system is powered on.
On the other hand, if the user wants the system to assign IP addresses dynamically, the following lines must be added to the /etc/network/interfaces file.
Code Listing 96
auto eth0 iface eth0 inet dhcp |
What is a netmask?
Looking at the lines of the /etc/network/interfaces file, when it is configured to assign a specific IP address for the network adapter, one of these lines refers to a parameter named netmask. A netmask is a 32-bit number used to divide an IP address into subnets and specify the network’s available hosts. Two bits are always assigned automatically in a netmask. For example, in 255.255.225.0, 0 is the assigned network address, and in 255.255.255.255, 255 is the assigned broadcast address. The 0 and 255 are always assigned and cannot be used.
The following table shows a netmask and its binary conversion.
Netmask Example
Netmask | 255. | 255. | 255. | 255 |
|---|---|---|---|---|
Binary | 11111111 | 11111111 | 11111111 | 11111111 |
Netmask length | 8 | 16 | 24 | 32 |
The netmask length can be obtained by counting out the bits in the binary conversion. The previous example is a 32-bit address. However, this address is a broadcast address (used to distribute signals across a network) and does not allow any hosts (computers or other network devices) to be connected to it.
The commonly used netmask, which is 24-bit, is shown in the following table.
Netmask Commonly Used
Netmask | 255. | 255. | 255. | 0 |
|---|---|---|---|---|
Binary | 11111111 | 11111111 | 11111111 | 00000000 |
Netmask length | 8 | 16 | 24 | -- |
The previous netmask allows 2,097,150 networks or 254 different hosts with an IP range from 192.0.1.x to 223.255.254.x. These are plenty of addresses for one network.
The following table shows the commonly used network classes and the netmask that is associated to each one of them.
Network Classes and Their Netmasks
Class | Netmask length | Number of networks | Number of hosts | Netmask |
|---|---|---|---|---|
A | 8 | 126 | 16,777,214 | 255.0.0.0 |
B | 16 | 16,382 | 65,534 | 255.255.0.0 |
C | 24 | 2,097,150 | 254 | 255.255.255.0 |
According to the previous table, all private networks created in most scenarios belong to Class C networks. So the netmask used for these networks is 255.255.255.0, which appears in the /etc/network/interfaces file.
What is a gateway?
One line of the /etc/network/interfaces makes a reference to a gateway. This is a device (node) in a computer network that acts as a key stopping point for data on its way to or from other networks. Thanks to gateways, the Internet is available to users of private networks.
In a company network, the gateway is usually a router, which takes traffic from any workstation to the outside network that is serving up webpages. In home networks, the gateway is the modem (or modem-router combo) that is provided by the Internet Service Provider (ISP).
For the example, in the /etc/network/interfaces file shown previously, the gateway device indicated for that function has an IP address of 192.168.0.1. Thus, if the IP address 192.168.0.3 has been assigned to the device that will serve as a gateway, then this IP address must be indicated beside the gateway parameter of the /etc/network/interfaces file.
If the user doesn’t want to reboot the system to apply the /etc/network/interfaces file modifications, the user needs to manually disable the network interface first, and then manually enable it again to apply those changes. To manually disable the eth0 network interface, the following command must be used.
Code Listing 97
$ sudo ifdown eth0 |
Then, the following command will enable the eth0 interface.
Code Listing 98
$ sudo ifup eth0 |
For IPv4, the loopback interface is a range of addresses starting from 127.0.0.1 to 127.255.255.254. All these addresses represent the local computer system. For most purposes, it’s necessary to use only one IP address, and this is 127.0.0.1. This IP points to the hostname localhost. The localhost is commonly used when the user wants to point to the local computer through a network service, such as a web server.
By default, Ubuntu Server identifies the loopback interface with the logical name lo. Its configuration can be viewed using the ifconfig command, as in the following example.
Code Listing 99
$ ifconfig lo |
The file /etc/network/interfaces has the lines responsible for automatically configuring the loopback interface. It’s not recommended to change these lines. Here’s an example:
Code Listing 100
auto lo iface lo inet loopback |
DHCP (Dynamic Host Configuration Protocol) is a network service that enables a device or computer to assign network settings automatically to other hosts attached to the network. This device is commonly known as a DHCP server.
The other hosts in the network are configured as DHCP clients and have no control over the settings they will receive from the DHCP server, and the configuration for those settings is transparent to the computer’s user.
The most common settings provided by a DHCP server to the clients are:
The advantage of using DHCP servers is that changes to the network are made only in the DHCP server, and all network hosts will be reconfigured the next time they poll the DHCP server via their DHCP clients. Also, integrating new devices into the network is easier because the DHCP server will handle IP address availability, and conflicts with IP allocation will be reduced.
In the previous section, I explained that one of the common settings provided by a DHCP server is the IP address of a DNS server.
A DNS (Domain Name Server) is a computer that maintains a directory of domain names and translates them to IP addresses. This is necessary because, although domain names are easy for people to remember, computer or network devices access websites based on IP addresses.
When a user types a web address (e.g., www.google.com), the Domain Name Server specified by the DHCP server looks for the web address entered and translates that name into a machine-friendly IP address (201.165.116.170), then directs the user’s Internet connection to the correct website.
To install DHCP server in Ubuntu, the following command must be issued.
Code Listing 101
$ sudo apt-get install isc-dhcp-server |
After the installation of the DHCP service, the file /etc/default/isc-dhcp-server must be edited to specify which network interfaces should listen to the service. The file should look like the following snippet.
Code Listing 102
# Defaults for isc-dhcp-server initscript # sourced by /etc/init.d/isc-dhcp-server # installed at /etc/default/isc-dhcp-server by the maintainer scripts # Path to dhcpd's config file (default: /etc/dhcp/dhcpd.conf). # DHCPD_CONF=/etc/dhcp/dhcpd.conf # On what interfaces should the DHCP server (dhcpd) serve DHCP requests? # Separate multiple interfaces with spaces, e.g., "eth0 eth1" INTERFACES="eth0" |
In the previous example, the DHCP server is configured to use the eth0 network interface to listen for all DHCP requests coming from all DHCP clients in the network.
DCHP configuration can be done by editing the /etc/dhcp/dhcpd.conf file. The following sample shows a group of configuration settings.
Code Listing 103
# minimal sample /etc/dhcp/dhcpd.conf default-lease-time 600; max-lease-time 7200; subnet 192.168.1.0 netmask 255.255.255.0 { range 192.168.1.150 192.168.1.200; option routers 192.168.1.254; option domain-name-servers 192.168.1.1, 192.168.1.2; option domain-name "mydomain.example"; } |
This configuration will make sure that the DHCP server gives clients an IP address from 192.168.1.150 to 192.168.1.250. It will lease an IP address for 600 seconds if the client doesn’t ask for a specific timeframe. Otherwise, the maximum (allowed) lease will be 7,200 seconds. The server will also suggest the client to use 192.168.1.254 as a default gateway, and 192.168.1.1 and 192.168.1.254 as its DNS servers.
NTP (Network Time Protocol) is a protocol for synchronizing time over a network. The client requests the current time from a server and uses it to set its own clock.
Behind the previous description, there is a lot of complexity—there are tiers of NTP servers, with the tier one NTP servers connected to atomic clocks, and tier two and three servers spreading the load of actually handling requests across the Internet. Also, the client software is a lot more complex than you might think—it has to factor out communication delays, and adjust the time in a way that doesn’t upset all the other processes that run on the server. Luckily, all that complexity is hidden from the user.
Ubuntu Server comes with the ntpdate command as standard, and will run it once at boot time to set up the server time according to Ubuntu’s NTP server, as shown in the following example.
Code Listing 104
$ ntpdate -s ntp.ubuntu.com |
In case that ntpdate command wasn’t installed during Ubuntu Server’s installation process, the user can install it by typing the following.
Code Listing 105
$ sudo apt-get install ntpdate |
As mentioned previously, ntpdate is run once at the server’s boot time, which may lead to inconsistent logs or other kinds of time-related issues. Fortunately, Ubuntu Server has a daemon (service) which calculates the drift of the system clock and adjusts it continuously. This daemon is ntpd, and it can be installed issuing the following command.
Code Listing 106
$ sudo apt-get install ntp |
The /etc/ntp.conf file must be edited in order to change NTP configuration. The user can add or remove server lines. The following snippet shows the default NTP configuration file.
Code Listing 107
# Use servers from the NTP Pool Project. Approved by Ubuntu Technical Board # on 2011-02-08 (LP: #104525). See http://www.pool.ntp.org/join.html for # more information. server 0.ubuntu.pool.ntp.org server 1.ubuntu.pool.ntp.org server 2.ubuntu.pool.ntp.org server 3.ubuntu.pool.ntp.org |
If any change is made to the /etc/ntp.conf file, the user has to reload the ntpd service.
Code Listing 108
$ sudo service ntp reload |
An understanding of networking is critical when installing and configuring a computer as a server. In this order of things, Ubuntu Server has a series of commands that allow you to configure the server in the network.
This chapter started with an overview of Ethernet interfaces and how they can be managed in the system. A list of all available interfaces can be obtained with the ifconfig –a | grep –i eth command. Also, detailed information for a specific network adapter can be shown with the sudo lshw –class network command. Finally, the ethtool command allows the user to view the adapter settings, gather statistics about an adapter, and identify one when there are several network adapters attached to the system.
Network connections can’t be made without IP addressing; that is, without assigning an IP address to any device that is connected in the network. An IP address is a 32-bit numeric value defined under the IPv4 protocol, which identifies a device and an address for its location in the network. The IPv4 protocol divides the networks into five classes, labeled A, B, C, D, and E. For private networks (built at a home or office), the classes A, B, and C are used. The ifconfig command is used to manage IP addressing in Ubuntu Server, allowing the user to assign a specific IP address to the computer, a netmask, and a gateway in order to connect to external networks. This kind of IP addressing is known as static IP addressing. Also, Ubuntu Server allows dynamic (automatic) IP addressing. To establish dynamic or static addressing by default, the /etc/network/interfaces file must be edited.
Dynamic IP addressing is possible because of the DHCP protocol. This protocol allows a device in the network to take charge of assigning IP addresses to other devices connected in the network. The device in which the DHCP protocol resides is known as a DHCP server. Ubuntu can be used as a DHCP server by installing the isc-dhcp-server service. Once the service is installed, it can be configured by editing the /etc/default/isc-dhcp-server and the /etc/dhcp/dhcpd.conf files.
Finally, it is possible to synchronize the system’s clock with a remote server’s time. To do this, the NTP (Network Time Protocol) must be used. Ubuntu comes with the ntpdate command and ntpd service to manage NTP. Configuration for NTP is done by editing the /etc/ntp.conf file.