# Basic IP Connectivity

Internet Protocol (IP) networking is now among the most common networking technologies in use today. The IP stack under linux is mature, robust and reliable. This chapter covers the basics of configuring a linux machine or multiple linux machines to join an IP network.

This chapter covers a quick overview of the locations of the networking control files on different distributions of linux. The remainder of the chapter is devoted to outlining the basics of IP networking with linux.

These basics are written in a more tutorial style than the remainder of the first part of the book. Reading and understanding IP addressing and routing information is a key skill to master when beginning with linux. Naturally, the next step is to alter the IP configuration of a machine. This chapter will introduce these two key skills in a tutorial style. Subsequent chapters will engage specific subtopics of linux networking in a more thorough and less tutorial manner.

## IP Networking Control Files

Different linux distribution vendors put their networking configuration files in different places in the filesystem. Here is a brief summary of the locations of the IP networking configuration information under a few common linux distributions along with links to further documentation.

### Location of networking configuration files

- RedHat (and Mandrake)

  - Interface definitions `/etc/sysconfig/network-scripts/ifcfg-*`

  - Hostname and default gateway definition `/etc/sysconfig/network`

  - Definition of static routes `/etc/sysconfig/static-routes`

- SuSe (version >= 8.0)

  - Interface definitions `/etc/sysconfig/network/ifcfg-*`

  - Static route definition `/etc/sysconfig/network/routes`

  - Interface specific static route definition `/etc/sysconfig/network/ifroute-*`

- SuSe (version <= 8.0)

  - Interface and route definitions `/etc/rc.config`

- Debian

  - Interface and route definitions `/etc/network/interfaces`

- Gentoo

  - Interface and route definitions `/etc/conf.d/net`

- Slackware

  - Interface and route definitions `/etc/rc.d/rc.inet1`

The format of the networking configuration files differs significantly from distribution to distribution, yet the tools used by these scripts are the same. This documentation will focus on these tools and how they instruct the kernel to alter interface and route information. Consult the distribution's documentation for questions of file format and order of operation.

For the remainder of this document, many examples refer to machines in a hypothetical network. Refer to the example network description for the network map and addressing scheme.

## Reading Routes and IP Information

Assuming an already configured machine named &tristan;, let's look at the IP addressing and routing table. Next we'll examine how the machine communicates with computers (hosts) on the locally reachable network. We'll then send packets through our default gateway to other networks. After learning what a default route is, we'll look at a static route.

One of the first things to learn about a machine attached to an IP network is its IP address. We'll begin by looking at a machine named &tristan; on the main desktop network (192.168.99.0/24).

The machine &tristan; is alive on IP 192.168.99.35 and has been properly configured by the system administrator. By examining the `route` and `ifconfig` output we can learn a good deal about the network to which &tristan; is connected For BSD and UNIX users, the idiom `netstat -rn` may be more familiar than the common `route -n` on a linux machine. Both of these commands provide the same basic information although the formatting is a bit different. For a fuller discussion of these, see either or . For access to all of the routing features of the linux kernel, use `ip route` instead. .

### Sample `ifconfig` output

    [root@tristan]# ifconfig
    eth0      Link encap:Ethernet  HWaddr 00:80:C8:F8:4A:51
              inet addr:192.168.99.35  Bcast:192.168.99.255  Mask:255.255.255.0
              UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
              RX packets:27849718 errors:1 dropped:0 overruns:0 frame:0
              TX packets:29968044 errors:5 dropped:0 overruns:2 carrier:3
              collisions:0 txqueuelen:100
              RX bytes:943447653 (899.7 Mb)  TX bytes:2599122310 (2478.7 Mb)
              Interrupt:9 Base address:0x1000

    lo        Link encap:Local Loopback
              inet addr:127.0.0.1  Mask:255.0.0.0
              UP LOOPBACK RUNNING  MTU:16436  Metric:1
              RX packets:7028982 errors:0 dropped:0 overruns:0 frame:0
              TX packets:7028982 errors:0 dropped:0 overruns:0 carrier:0
              collisions:0 txqueuelen:0
              RX bytes:1206918001 (1151.0 Mb)  TX bytes:1206918001 (1151.0 Mb)

    [root@tristan]# route -n
    Kernel IP routing table
    Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
    192.168.99.0    0.0.0.0         255.255.255.0   U     0      0        0 eth0
    127.0.0.0       0.0.0.0         255.0.0.0       U     0      0        0 lo
    0.0.0.0         192.168.99.254  0.0.0.0         UG    0      0        0 eth0

For the moment, ignore the loopback interface (lo) and concentrate on the Ethernet interface. Examine the output of the `ifconfig` command. We can learn a great deal about the IP network to which we are connected simply by reading the `ifconfig` output. For a thorough discussion of `ifconfig`, see .

The IP address active on &tristan; is 192.168.99.35. This means that any IP packets created by &tristan; will have a source address of 192.168.99.35. Similarly any packet received by &tristan; will have the destination address of 192.168.99.35. When creating an outbound packet &tristan; will set the destination address to the server's IP. This gives the remote host and the networking devices in between these hosts enough information to carry packets between the two devices.

Because &tristan; will advertise that it accepts packets with a destination address of 192.168.99.35, any frames (packets) appearing on the Ethernet bound for 192.168.99.35 will reach &tristan;. The process of communicating the ownership of an IP address is called ARP. Read for a complete discussion of this process.

This is fundamental to IP networking. It is fundamental that a host be able to generate and receive packets on an IP address assigned to it. This IP address is a unique identifier for the machine on the network to which it is connected.

Common traffic to and from machines today is unicast IP traffic. Unicast traffic is essentially a conversation between two hosts. Though there may be routers between them, the two hosts are carrying on a private conversation. Examples of common unicast traffic are protocols such as HTTP (web), SMTP (sending mail), POP3 (fetching mail), IRC (chat), SSH (secure shell), and LDAP (directory access). To participate in any of these kinds of traffic, &tristan; will send and receive packets on 192.168.99.35.

In contrast to unicast traffic, there is another common IP networking technique called broadcasting. Broadcast traffic is a way of addressing all hosts in a given network range with a single destination IP address. To continue the analogy of the unicast conversation, a broadcast is more like shouting in a room. Occasionally, network administrators will refer to broadcast techniques and broadcasting as "chatty network traffic".

Broadcast techniques are used at the Ethernet layer and the IP layer, so the cautious person talks about Ethernet broadcasts or IP broadcast. Refer to , for more information on a common use of broadcast Ethernet frames.

IP Broadcast techniques can be used to share information with all partners on a network or to discover characteristics of other members of a network. SMB (Server Message Block) as implemented by Microsoft products and the `samba` package makes extensive use of broadcasting techniques for discovery and information sharing. Dynamic Host Configuration Protocol (DHCP) also makes use of broadcasting techniques to manage IP addressing.

The IP broadcast address is, usually, correctly derived from the IP address and network mask although it can be easily be set explicitly to a different address. Because the broadcast address is used for autodiscovery (e.g, SMB under some protocols, an incorrect broadcast address can inhibit a machine's ability to participate in networked communication An incorrect broadcast address often highlights a mismatch of the configured IP address and netmask on an interface. If in doubt, be sure to use an IP calculator to set the correct netmask and broadcast addresses. .

The netmask on the interface should match the netmask in the routing table for the locally connected network. Typically, the route and the IP interface definition are calculated from the same configuration data so they should match perfectly.

If you are at all confused about how to address a network or how to read either the traditional notation or the CIDR notation for network addressing, see one of the CIDR/netmask references in .

### Sending Packets to the Local Network

We can see from the output above that the IP address 192.168.99.35 falls inside the address space 192.168.99.0/24. We also note that the machine &tristan; will route packets bound for 192.168.99.0/24 directly onto the Ethernet attached to eth0. This line in the routing table identifies a network available on the Ethernet attached to eth0 ("Iface") by its network address ("Destination") and size ("Genmask").

    Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
    192.168.99.0    0.0.0.0         255.255.255.0   U     0      0        0 eth0

Every host on the 192.168.99.0/24 network should share the network address and netmask specified above. No two hosts should share the same IP address.

Currently, there are two hosts connected to the example desktop network. Both &tristan; and &masq-gw; are connected to 192.168.99.0/24. Thus, 192.168.99.254 (&masq-gw;) should be reachable from &tristan;. Success of this test provides evidence that &tristan; is configured properly. N.B., Assume that the network administrator has properly configured &masq-gw;. Since the default gateway in any network is an important host, testing reachability of the default gateway also has a value in determining the proper operation of the local network.

The `ping` tool, designed to take advantage of Internet Control Message Protocol (ICMP), can be used to test reachability of IP addresses. For a command summary and examples of the use of `ping`, see .

#### Testing reachability of a locally connected host with `ping`

    [root@tristan]# ping -c 1 -n 192.168.99.254
    PING 192.168.99.254 (192.168.99.254) from 192.168.99.35 : 56(84) bytes of data.

    --- 192.168.99.254 ping statistics ---
    1 packets transmitted, 0 packets received, 100% packet loss
    PING 192.168.99.254 (192.168.99.254) from 192.168.99.35 : 56(84) bytes of data.
    64 bytes from 192.168.99.254: icmp_seq=0 ttl=255 time=238 usec

    --- 192.168.99.254 ping statistics ---
    1 packets transmitted, 1 packets received, 0% packet loss
    round-trip min/avg/max/mdev = 0.238/0.238/0.238/0.000 ms

### Sending Packets to Unknown Networks Through the Default Gateway

In , we verified that hosts connected to the same local network can reach each other and, importantly, the default gateway. Now, let's see what happens to packets which have a destination address outside the locally connected network.

Assuming that the network administrator allows ping packets from the desktop network into the public network, `ping` can be invoked with the record route option to show the path the packet travels from &tristan; to &wan-gw; and back.

#### Testing reachability of non-local hosts

    [root@tristan]# ping -R -c 1 -n 205.254.211.254
    PING 205.254.211.254 (205.254.211.254) from 192.168.99.35 : 56(84) bytes of data.

    --- 205.254.211.254 ping statistics ---
    1 packets transmitted, 0 packets received, 100% packet loss
    PING 205.254.211.254 (205.254.211.254) from 192.168.99.35 : 56(84) bytes of data.
    64 bytes from 205.254.211.254: icmp_seq=0 ttl=255 time=238 usec
    RR:     192.168.99.35
            205.254.211.179
            205.254.211.254
            205.254.211.254
            192.168.99.254
            192.168.99.35

    --- 192.168.99.254 ping statistics ---
    1 packets transmitted, 1 packets received, 0% packet loss
    round-trip min/avg/max/mdev = 0.238/0.238/0.238/0.000 ms

As the packet passes through the IP stack on &tristan;, before hitting the Ethernet, &tristan; adds its IP to the list of IPs in the option field in the header.

This is &masq-gw;'s public IP address.

Our intended destination! (Anybody know why there are two entries in the record route output?)

This is &masq-gw;'s private IP address.

And finally, &tristan; will add its IP to the option field in the header of the IP packet just before the packet reaches the calling `ping` program.

By testing reachability of the local network 192.168.99.0/24 and an IP address outside our local network, we have verified the basic elements of IP connectivity.

To summarize this section, we have:

- identified the IP address, network address and netmask in use on &tristan; using the tools `ifconfig` and `route`

- verified that &tristan; can reach its default gateway

- tested that packets bound for destinations outside our local network reach the intended destination and return

### Static Routes to Networks

Static routes instruct the kernel to route packets for a known destination host or network to a router or gateway different from the default gateway. In the example network, the desktop machine &tristan; would need a static route to reach hosts in the 192.168.98.0/24 network. Note that the branch office network is reachable over an ISDN line. The ISDN router's IP in &tristan;'s network is 192.168.99.1. This means that there are two gateways in the example desktop network, one connected to a small branch office network, and the other connected to the Internet.

Without a static route to the branch office network, &tristan; would use &masq-gw; as the gateway, which is not the most efficient path for packets bound for &morgan;. Let's examine why a static route would be better here.

If &tristan; generates a packet bound for &morgan; and sends the packet to the default gateway, &masq-gw; will forward the packet to &isdn-router; as well as generate an ICMP redirect message to &tristan;. This ICMP redirect message tells &tristan; to send future packets with a destination address of 192.168.98.82 (&morgan;) directly to &isdn-router;. For a fuller discussion of ICMP redirect, see .

The absence of a static route has caused two extra packets to be generated on the Ethernet for no benefit. Not only that, but &tristan; will eventually expire the temporary route entry If the machine is a linux machine, then the temporary route entry is stored in the routing cache. Consult for more information on the routing cache. for 192.168.98.82, which means that subsequent packets bound for &morgan; will repeat this process It is quite reasonable to ignore ICMP redirect messages from unknown hosts on the Internet, but ICMP redirect messages on a LAN indicate that a host has mismatched netmasks or missing static routes. .

To solve this problem, add a static route to &tristan;'s routing table. Below is a modified routing table (see to learn how to change the routing table).

#### Sample routing table with a static route

    [root@tristan]# route -n
    Kernel IP routing table
    Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
    192.168.99.0    0.0.0.0         255.255.255.0   U     0      0        0 eth0
    192.168.98.0    192.168.99.1    255.255.255.0   UG    0      0        0 eth0
    127.0.0.0       0.0.0.0         255.0.0.0       U     0      0        0 lo
    0.0.0.0         192.168.99.254  0.0.0.0         UG    0      0        0 eth0

According to this routing table, any packets with a destination address in the 192.168.98.0/24 network will be routed to the gateway 192.168.99.1 instead of the default gateway. This will prevent unnecessary ICMP redirect messages.

These are the basic tools for inspecting the IP address and the routes on a linux machine. Understanding the output of these tools will help you understand how machines fit into simple networks, and will be a base on which you can build an understanding of more complex networks.

## Changing IP Addresses and Routes

This section introduces changing the IP address on an interface, changing the default gateway, and adding and removing a static route. With the knowledge of `ifconfig` and `route` output it's a small step to learn how to change IP configuration with these same tools.

### Changing the IP on a machine

For a practical example, let's say that the branch office server, &morgan;, needs to visit the main office for some hardware maintenance. Since the services on the machine are not in use, it's a convenient time to fetch some software updates, after configuring the machine to join the LAN.

Once the machine is booted and connected to the Ethernet, it's ready for IP reconfiguration. In order to join an IP network, the following information is required. Refer to the network map and appendix to gather the required information below.

- An unused IP address (Use 192.168.99.14.)

- netmask (What's your guess?)

- IP address of the default gateway (What's your guess?)

- network address The network address can be calculated from the IP address and netmask. Refer to . Especially handy is the variable length subnet mask RFC, RFC 1878. (What's your guess?)

- The IP address of a name resolver. (Use the IP of the default gateway here Many networks are configured with the name resolution services on a publicly connected host. See . . )

#### `ifconfig` and `route` output before the change

    [root@morgan]# ifconfig eth0
    eth0      Link encap:Ethernet  HWaddr 00:80:C8:F8:4A:53
              inet addr:192.168.98.82  Bcast:192.168.98.255  Mask:255.255.255.0
              UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
              RX packets:0 errors:0 dropped:0 overruns:0 frame:0
              TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
              collisions:0 txqueuelen:100
              RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b)
              Interrupt:9 Base address:0x5000

    [root@morgan]# route -n
    Kernel IP routing table
    Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
    192.168.98.0    0.0.0.0         255.255.255.0   U     0      0        0 eth0
    127.0.0.0       0.0.0.0         255.0.0.0       U     0      0        0 lo
    0.0.0.0         192.168.98.254  0.0.0.0         UG    0      0        0 eth0

The process of readdressing for the new network involves three steps. It is clear in , that &morgan; is configured for a different network than the main office desktop network. First, the active interface must be brought down, then a new address must be configured on the interface and brought up, and finally a new default route must be added. If the networking configuration is correct and the process is successful, the machine should be able to connect to local and non-local destinations.

#### Bringing down a network interface with `ifconfig`

    [root@morgan]# ifconfig eth0 down

This is a fast way to stop networking on a single-homed machine such as a server or workstation. On multi-homed hosts, other interfaces on the machine would be unaffected by this command. This method of bringing down an interface has some serious side effects, which should be understood. Here is a summary of the side effects of bringing down an interface.

#### Side effects of bringing down an interface with `ifconfig`

- all IP addresses on the specified interface are deactivated and removed

- any connections established to or from IPs on the specified interface are broken It is possible for a linux box which meets the following three criteria to maintain connections and provide services without having the service IP configured on an interface. It must be functioning as a router, be configured to support non-local binding and be in the route path of the client machine. This is an uncommon need, frequently accomplished by the use of transparent proxying software.

- all routes to any destinations through the specified interface are removed from the routing tables

- the link layer device is deactivated

The next step, bringing up the interface, requires the new networking configuration information. It's a good habit to check the interface after configuration to verify settings.

#### Bringing up an Ethernet interface with `ifconfig`

    [root@morgan]# ifconfig eth0 192.168.99.14 netmask 255.255.255.0 up
    [root@morgan]# ifconfig eth0
    eth0      Link encap:Ethernet  HWaddr 00:80:C8:F8:4A:53
              inet addr:192.168.99.14  Bcast:192.168.99.255  Mask:255.255.255.0
              UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
              RX packets:0 errors:0 dropped:0 overruns:0 frame:0
              TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
              collisions:0 txqueuelen:100
              RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b)
              Interrupt:9 Base address:0x5000

The second call to `ifconfig` allows verification of the IP addressing information. The currently configured IP address on eth0 is 192.168.99.14. Bringing up an interface also has a small set of side effects.

#### Side effects of bringing up an interface

- the link layer device is activated

- the requested IP address is assigned to the specified interface

- all local, network, and broadcast routes implied by the IP configuration are added to the routing tables

Use `ping` to verify the reachability of other locally connected hosts or skip directly to setting the default gateway.

### Setting the Default Route

It should come as no surprise to a close reader (hint), that the default route was removed at the execution of `ifconfig eth0 down`. The crucial final step is configuring the default route.

#### Adding a default route with `route`

    [root@morgan]# route -n
    Kernel IP routing table
    Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
    192.168.99.0    0.0.0.0         255.255.255.0   U     0      0        0 eth0
    127.0.0.0       0.0.0.0         255.0.0.0       U     0      0        0 lo
    [root@morgan]# route add default gw 192.168.99.254
    [root@morgan]# route -n
    Kernel IP routing table
    Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
    192.168.99.0    0.0.0.0         255.255.255.0   U     0      0        0 eth0
    127.0.0.0       0.0.0.0         255.0.0.0       U     0      0        0 lo
    0.0.0.0         192.168.99.254  0.0.0.0         UG    0      0        0 eth0

The routing table on &morgan; should look exactly like the initial routing table on &tristan;. Compare the routing tables in and .

These changes to the routing table on &morgan; will stay in effect until they are manually changed, the network is restarted, or the machine reboots. With knowledge of the addressing scheme of a network, and the use of `ifconfig` and `route` it's simple to readdress a machine on just about any Ethernet you can attach to. The benefits of familiarity with these commands extend to non-Ethernet IP networks as well, because these commands operate on the IP layer, independent of the link layer.

### Adding and removing a static route

Now that &morgan; has joined the LAN at the main office and can reach the Internet, a static route to the branch office would be convenient for accessing resources on that network.

A static route is any route entered into a routing table which specifies at least a destination address and a gateway or device. Static routes are special instructions regarding the path a packet should take to reach a destination and are usually used to specify reachability of a destination through a router other than the default gateway.

As we saw above, in , a static route provides a specific route to a known destination. There are several pieces of information we need to know in order to be able to add a static route.

- the address of the destination (192.168.98.0)

- the netmask of the destination (255.255.255.0)

  - EITHER the IP address of the router through which the destination (192.168.99.1) is reachable

  - OR the name of the link layer device to which the destination is directly connected

#### Adding a static route with `route`

    [root@morgan]# route -n
    Kernel IP routing table
    Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
    192.168.99.0    0.0.0.0         255.255.255.0   U     0      0        0 eth0
    127.0.0.0       0.0.0.0         255.0.0.0       U     0      0        0 lo
    0.0.0.0         192.168.99.254  0.0.0.0         UG    0      0        0 eth0
    [root@morgan]# route add -net 192.168.98.0 netmask 255.255.255.0 gw 192.168.99.1
    [root@morgan]# route -n
    Kernel IP routing table
    Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
    192.168.99.0    0.0.0.0         255.255.255.0   U     0      0        0 eth0
    192.168.98.0    192.168.99.1    255.255.255.0   UG    0      0        0 eth0
    127.0.0.0       0.0.0.0         255.0.0.0       U     0      0        0 lo
    0.0.0.0         192.168.99.254  0.0.0.0         UG    0      0        0 eth0

shows how to add a static route to the 192.168.98.0/24 network. In order to test the reachability of the remote network, ping any machine on the 192.168.98.0/24 network. Routers are usually a good choice, since they rarely have packet filters and are usually alive.

Because a more specific route is always chosen over a less specific route, it is even possible to support host routes. These are routes for destinations which are single IP addresses. This can be accomplished with a manually added static route as below.

#### Removing a static network route and adding a static host route

    [root@morgan]# route del -net 192.168.98.0 netmask 255.255.255.0 gw 192.168.99.1
    [root@morgan]# route add -net 192.168.98.42 netmask 255.255.255.255 gw 192.168.99.1
    [root@morgan]# route add -host 192.168.98.42 gw 192.168.99.1
    SIOCADDRT: File exists
    [root@morgan]# route -n
    Kernel IP routing table
    Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
    192.168.99.0    0.0.0.0         255.255.255.0   U     0      0        0 eth0
    192.168.98.42   192.168.99.1    255.255.255.255 UGH   0      0        0 eth0
    127.0.0.0       0.0.0.0         255.0.0.0       U     0      0        0 lo
    0.0.0.0         192.168.99.254  0.0.0.0         UG    0      0        0 eth0

This should serve as an illustration that there is no difference to the kernel in selecting a route between a host route and a network route with a host netmask. If this is a surprise or is at all confusing, review the use of netmasks in IP networking. Some collected links on general IP networking are available in .

## Conclusion

This chapter has introduced the simplest uses of `ifconfig` and `route` to view and alter the IP configuration of a host. To reiterate the minimum requirements to create an IP network between two machines:

### Requirements for Two Hosts on the Same Ethernet to Communicate Using IP

- Each host must have a good connection to the Ethernet. Verify a good connection to the Ethernet with `mii-tool`, documented in .

- Each host must share IP network space. Practically, this means that each host should have the same network address, netmask, and broadcast address Technically, the two hosts simply need to have routes to each other, but we are discussing the simplest case here, so we'll leave this for a discussion of shared media. .

- Each host must have a unique IP address.

- Neither host must block the other's IP packets. (Host based packet filtering may hinder connections!)

This concludes the tour of basic host networking and IP layer configuration as well as some basic tools available to the linux user. For further documentation on these tools, other tips, tricks, and more advanced content, keep reading!