Chapter 5. Basic Network Configuration

Table of Contents

The ifconfig command
The loopback interface
Understanding the Dynamic Host Configuration Protocol (DHCP)
DHCP offers the following benefits:

This chapter details the process of basic network configuration. Linux has the ability to alter networking information on the fly and in almost all cases, no reboot is necessary. If you're brave enough, you can also delve into the /proc file system that will allow you to change various settings on the fly too- but I'm running away from myself. While this is a boon for many people familiar with other operating systems, it can also be a problem. Often, a system administrator wishing to make the changes quickly may well make them dynamically, forgetting however to make them permanent by modifying the relevant files. On a system reboot, the old settings return to haunt you. So a little word of warning - ensure your dynamic changes are made permanent by updating the files.

The ifconfig command

Network card addresses can be assigned using the ifconfig command. This commands has the ability to bring a network interface card up, take it down and reconfigure it. Let 's start by examining your current network settings.

ifconfig -a
            

The loopback interface

You will notice that at least one network connection is present on your system - the "lo" interface. This is the loopback interface and is common to all systems connected to a network. The loopback interface is a virtual interface and never goes down. It 's primary function is to allow the functioning of the TCP/IP stack on the OS without the presence of a network card.

The IP address assigned to the loopback address (lo) is 127.0.0.1 - a class A address. Try to do an echo test to the loopback interface using the ping command:

ping 127.0.0.1
                

You should get a response. Since this is a virtual interface, you have had to do nothing to ensure that it is up. Of course, just because the loopback interface is up does not mean you can begin talking to others on your local network. For that you need a real network card.

If you do not see an interface called "eth0", it probably means you have not loaded the drivers for your network card. If this is the case, you will need to determine what type of network card (and chip set) you have. It is out of the scope of the course to go into the details here. Instead check our Donald Beckers site - he did most of the coding of network drivers for Linux (http://www.scyld.com/network).

Once you have and interface, you can type:

ifconfig eth0
                

This will show you the Ethernet device 0 - the first network interface card. My output shows:

eth0      Link encap:Ethernet  HWaddr 00:01:03:8C:FB:01  
          inet addr:172.16.1.2  Bcast:172.16.1.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:4537 errors:0 dropped:0 overruns:0 frame:0
          TX packets:4824 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:100 
          RX bytes:1822888 (1.7 Mb)  TX bytes:370681 (361.9 Kb)
          Interrupt:10 Base address:0xe800 
                

From this I can see the following:

the IP address on this card is 172.16.1.2 (inet addr)

the broadcast address 172.16.1.255 (Bcast),

the subnet mask, 255.255.255.0 (Mask),

the interface is up (UP and RUNNING),

it is a broadcast interface (BROADCAST), other technologies like token ring were not broadcast devices,

the maximum transmission unit (MTU) of 1500 bytes. This is the maximum for Ethernet, token ring has a higher MTU,

the number of hops to get to this interface (METRIC),

the number of packets transmitted (TX packets),

the number of packets received (RX packets),

the number of received and transmitted bytes ({RX,TX} bytes,

and finally, the interrupt consumed by this device as well as the base address.

The ifconfig command can be used for more than simply showing your network interface card information. It is also the tool you use to bring a network card up (make it active on the network), or to take it down (deactivate it). To activate and deactivate your network card you can:

ifconfig eth0 [up|down]
                

Additionally, you can change the network address on the fly as we discussed earlier. So here is the general syntax for the ifconfig command:

SYNTAX:
ifconfig interface [aftype] options | address ...
                

The options earlier were up or down, but ifconfig can additionally take an address:

ifconfig eth0 10.0.0.1
                

In the absence of other options such as "netmask" or "promisc", the interface is brought up with the address 10.0.0.1 and the standard class A subnet mask for the 10 network namely 255.0.0.0

Adding the word "netmask" allows us to define a network mask for this interface:

ifconfig eth0 10.0.0.1 netmask 255.255.255.0
                

Some of the less well-known things you can do with this command is change your hardware (MAC) address. Try this:

ifconfig eth0 down
ifconfig eth0 hw ether BA:D1:da:d1:20:04
ifconfig eth0 up
ifconfig eth0
                

Bingo, you've changed your hardware (MAC) address. (Bad Dad 2004!)

I've referred to dangers of setting these setting your IP address on-the-fly versus setting it statically. We will focus now on configuring the interface statically - in other words, through a reboot but not relying on a protocol such as DHCP to issue us an IP address.

As with everything in Linux, networking configuration is stored in a file. This time, it 's in /etc/network/interfaces. Typical entries will look as follows:

iface eth0	inet static
		address 192.168.0.10
		netmask 255.255.255.0
		gateway 192.168.0.1
                

This will configure the interface to the IP address 192.168.0.10, with the default gateway being 192.168.0.1 (we'll talk about the default gateway shortly). Assuming you had two interface cards in your hosts, the first using a static address and the second using a DHCP address, you could have two separate entries in the interfaces file as follows:

iface eth0	inet static
		address 192.168.0.10
		netmask 255.255.255.0
		gateway 192.168.0.1
iface eth1 inet DHCP
                
[Note] Note

on RedHat and SuSE things differ slightly. On both RedHat and SuSE there is not interfaces file in /etc/network. Everything lives in a directory /etc/sysconfig/network-scripts. Within this directory there are files named according to the interface you are configuring. In the above examples under RedHat and SuSE, there would be two file in the /etc/sysconfig/network-scripts directory namely ifcfg-eth0 and ifcfg-eth1. These files would contain information relating directly to the configuration of the interfaces. I've included the contents of a RedHat ifcfg-eth0 file below.

For a dynamic IP configuration using DHCP:

DEVICE=eth0
ONBOOT=yes
BOOTPROTO=DHCP
                    

Once you have set your IP address statically, it is always a good idea to test it out by forcing a network restart. In Debian, this is done using the ifup and ifdown scripts. These can be used by issuing them on the command line:

ifdown eth0; ifup eth0
                

This will stop and restart out interface. Naturally we would need to do this for every interface we have, so presumably we could get smart (or lazy) and write a loop that could do this for us:

    for i in `seq 0 1`
do
    ifdown eth$i;ifup eth$1
done
                

In fact Debian is a lot smarter than either of the commercial Linux distributions. If you use Debian you do not need to create a for loop like this one to restart your Network interfaces, but there is not time to delve into this here (for more information on magic network configuration, visit http://www.debian.org/doc/manuals/reference/ch-gateway.en.html#s-net-magic-reconf )

[Note] Note

if you don't understand this for-loop, hang in there and do the Shell Scripting course.

[Note] Note

In both RedHat and SuSE, the commands to stop and restart daemons/services differ. In RedHat, you would issue the command:

service network restart
                    

SuSE, you would run:

rcnetwork restart
                    

At this point, our network interface is up and able to transmit and receive packets over the network. One of the ways of testing this is by getting a friend to "ping" your interface, assuming they are on the same network as you. Another means of testing the interface is by you "pinging" someone else on your network. I'll assume for now there is another host on your network with the IP address 192.168.0.45. Ping the host using:

ping 192.168.0.45
                

As long as you get a response from the host you are pinging, your network interface is up and A1 OK. We'll spend some time later in the course discussing ping as well as other tools that are invaluable in solving some network related problems.

[Note] Note

What response SHOULD you be getting back if everything is fine? I have included some output below from pinging my default gateway (172.16.1.1) on my network in my office:

ping -c 5 172.16.1.1
PING 172.16.1.1 (172.16.1.1) from 172.16.1.20 : 56(84) bytes of data.
64 bytes from 172.16.1.1: icmp_seq=1 ttl=64 time=0.334 ms
64 bytes from 172.16.1.1: icmp_seq=2 ttl=64 time=0.329 ms
64 bytes from 172.16.1.1: icmp_seq=3 ttl=64 time=0.322 ms
64 bytes from 172.16.1.1: icmp_seq=4 ttl=64 time=0.324 ms
64 bytes from 172.16.1.1: icmp_seq=5 ttl=64 time=0.321 ms

--- 172.16.1.1 ping statistics ---
5 packets transmitted, 5 received, 0% loss, time 3999ms
rtt min/avg/max/mdev = 0.321/0.326/0.334/0.004 ms
                    

As you can see from the second-last line, 5 packets were transmitted and 5 packets were received, so there was no packet loss. We'll discuss troubleshooting your network in much more detail later in this course.

The final thing to deal with in this chapter is the default gateway. So, what 's the default gateway? This is the default route out of your network. Still do not understand? I know, now I'm not talking about routes! Well the concept of a route is actually quite simple. If I were to ask you which exit you would use when leaving the stadium after a scintillating cricket match, you would probably reply that you would leave by the exit closest to your seat. So, this would be the default gate on your way home - your default gate way. With IP packets, it is no different. The route (or path) they will follow to leave your network will go through a particular host - the default gateway. In other words, if the host you are trying to talk to (for email, web or other service) does not reside on your local network, your packets will be channeled (routed) to the default gateway.

Setting the default gateway is done using the "route" command. Without going into all the options of the route command, we could add a default route using the command:

route add default gw 192.168.0.1
                

Consult the route(8) manual pages for additional options. In this case, I'm adding the route to the gateway (or host) 192.168.0.1. It is the responsibility of this host to ensure that the packet is routed out of your network.

Again this is a dynamic setting, so we need to make it static using some configuration file. From our earlier discussions on configuration of the default gateway automatically, you simply have to add the gateway to the interfaces file (in /etc/network) in the form described above. On restarting the network interface, your default gateway is set, even across reboots. Once the default gateway is set, a simple stop an restart of the network interface will enable this.

[Note] Note

while stopping and restarting the interface is not essential since the same can be accomplished using the route command, you may well wish to restart the interface to ensure that all your dynamic settings have been written to files which will ensure things are honkey-dorey even after a reboot.