Setting Up TCP/IP on Linux
Dr. Karanjit Siyan shows you the steps you need to take to set up TCP/IP on Linux, from making sure the networking software is correctly installed to using graphical tools to configure network interfaces.
This article is excerpted from Chapter 28 of TCP/IP Unleashed by Karanjit Siyan (Sams Publishing: ISBN 0672323516).
Before configuring TCP/IP on your Linux system, you need to perform a few small steps to ensure that your filesystem is ready. The first step is to make sure the networking software has been installed. You can install the network package through the setup program, as shown in Figure 28.1. Selecting the networking option installs the applications you need to use TCP/IP under Linux.
NOTE
Other distributions, notably RedHat, now have a GUI installation that makes installing the Linux system easier for the novice.
After the network software has been installed, you might have to reboot your system.
Figure 28.1 The Linux setup program lets you install the networking software easily.
Some versions of Linux (notably those that use the 2.0 kernel and many of the latest releases) require a /proc filesystem for networking to function properly. Most Linux kernels that inherently support networking automatically create the /proc filesystem when the operating system is installed, so you shouldn't have to do anything more than make sure it is properly mounted by the kernel. (The /proc filesystem is a quick interface point for the kernel to obtain network information, as well as to help the kernel maintain tables usually kept in the subdirectory /proc/net.) You can verify that your Linux version uses the /proc filesystem by trying to do a "cd /proc" command to change to the /proc directoryt, as shown in Figure 28.2.
Figure 28.2 If you can change into the /proc filesystem and obtain a directory listing, the filesystem exists and TCP/IP can be configured properly.
If you can't change into /proc, it probably doesn't exist (assuming you have access permissions, of course). If the /proc filesystem was not created for you by the Linux installation routine, you have to rebuild the kernel and select the /proc option. Change to the Linux source directory (such as /usr/src/Linux) and run the kernel configuration routine with this command:
make config
If you have X-Windows running and properly configured, you can run the following GUI-based configuration process:
make xconfig
When you are asked whether you want procfs support (or a similarly worded question), answer yes. If you do not get asked about the /proc filesystem support, and the /proc directory is not created on your filesystem, you need to upgrade your kernel to support networking.
The /proc filesystem should be mounted automatically when your Linux system boots. To force the /proc filesystem to be mounted automatically, edit the /etc/fstab file and add a line similar to this (if it isn't already there):
none /proc proc defaults
Another step you should take before configuring TCP/IP is to set the system's hostname. To set the hostname, use this command:
hostname name
name is the system name you want for your local machine. If you have a full domain name assigned to your network and your machine, you can use that name for your system. For example, if your Linux machine is attached to the domain yacht.com and your machine's name is spinnaker, you can set the full domain name using this command:
hostname spinnaker.yacht.com
If you don't have a fully qualified domain name, you can make up your own domain name as long as you are not connected to the Internet. (A made-up domain name does not have any meaning outside your local area network.) Alternatively, you do not have to assign a domain at all for your machine, but can simply enter this short name:
hostname spinnaker
An entry is made in the /etc/hosts file to reflect your machine's name. You should verify that your machine's name appears in that file. You also need to know the IP address assigned to your machine. You should have a unique IP address ready for your Linux machine for use in the configuration process.
One file that you might need to work with if you plan to direct information across many networks is /etc/networks. The /etc/networks file contains a list of all the network names your machine should know about, along with their IP addresses. Applications use this file to determine target networks based on the network name. The /etc/networks file consists of two columns for the symbolic name of the remote network and its IP address. Most /etc/networks files have at least one entry for the loopback driver that should be on every Linux system (the loopback driver is used as a default IP address by some Linux applications and is discussed in more detail later in this chapter). A sample /etc/networks file looks like this:
loopback 127.0.0.0 merlin-net 147.154.12.0 BNR 47.0.0.0
This sample file has two networks entered in it with their network IP addresses. Only the network portion of the IP address is specified, leaving the host component of the IP addresses set to zeros.
Network Interface Access
You need to make the network interface accessible to the operating system and its utilities. This is done with the ifconfig command. When run, ifconfig makes the Network Layer of the kernel work with the network interface by giving it an IP address, then issuing the command to make the interface active. When the interface is active, the kernel can send and receive data through the interface.
You need to set up several interfaces for your machine, including the loopback driver and the Ethernet interface (I assume you are using Ethernet throughout this chapter, but you can use other interfaces). The ifconfig command is used for each interface in order. The syntax of the ifconfig command is
ifconfig interface_type IP_Address
where interface_type is the interface's device driver name (such as lo for loopback, ppp for PPP, and eth for Ethernet). IP_Address is the IP address used by that interface.
After the ifconfig command has been run and the interface is active, you use the route command to add or remove routes to the kernel's routing table. This is necessary to enable the local machine to find other machines. The syntax of the route command is
route add|del IP_Address
where either add or del is used to add or remove a route from the kernel's routing table, and IP_Address is the remote route being affected.
You can display the current contents of the kernel's routing table by entering the route command with no arguments. For example, if your system is set up only with a loopback driver, you will see this:
$ route Kernel Routing Table Destination Gateway Genmask Flags MSS Window Use Iface loopback * 255.0.0.0 U 1936 0 16 lo
NOTE
You can also display the routing table using the following command:
netstat -rn
The -r option displays the routing table and the -n option displays IP addresses instead of symbolic names.
The columns that you should be concerned with are the destination name, which shows the name of the configured target (in this case loopback), the mask to be used (Genmask), and the interface (Iface, in this case /dev/lo). You can force route to display the IP addresses instead of symbolic names by using the -n option:
$ route -n Kernel Routing Table Destination Gateway Genmask Flags MSS Window Use Iface 127.0.0.1 * 255.0.0.0 U 1936 0 16 lo
As mentioned earlier in this section, a typical Linux network configuration includes a loopback interface (which should exist on every machine) and a network interface such as Ethernet. You can set these interfaces up in order.
Setting Up the Loopback Interface
A loopback interface should exist on every machine. It is used by some applications that require an IP address in order to function properly, which may not exist if the Linux system is not configured for networking. The loopback driver is also used as a diagnostic utility by some TCP/IP applications. The loopback interface has the IP address 127.0.0.1, so the /etc/hosts file should have an entry for this interface.
NOTE
A loop back address is any address of the form 127.x.x.x where x is a number from 0 to 255. The address of 127.0.0.1 is commonly used as a loopback address for historical reasons. This was the format in which the loopback address was specified in the /etc/hosts file on UNIX systems.
A loopback driver might have been created by the kernel during software installation, so check the /etc/hosts file for a line similar to this:
localhost 127.0.0.1
If such a line exists, the loopback driver is already in place and you can continue to the Ethernet interface. If you are not sure about the /etc/hosts file, you can use the ifconfig utility to display all the information it knows about the loopback driver. Use this command:
ifconfig lo
You should see several lines of information. If you get an error message, the loopback driver does not exist.
If the loopback interface is not in the /etc/hosts file, you need to create it with the ifconfig command. The following command creates the necessary line in /etc/hosts:
ifconfig lo 127.0.0.1
You can view the specifics of the newly created loopback driver with ifconfig. For example, the following command shows the loopback driver's typical configuration:
$ ifconfig lo lo Link encap: Local Loopback inet addr 127.0.0.1 Bcast {NONE SET] Mask 255.0.0.0 UP BROADCAST LOOPBACK RUNNING MTU 2000 Metric 1 RX packets:0 errors:0 dropped:0 overruns:0 TX packets:0 errors:0 dropped:0 overruns:0
As long as the loopback driver's details are shown as output from the ifconfig command, all is well with that interface. After checking the ifconfig routine, you should add the loopback driver to the kernel routing tables with one of these two commands:
route add 127.0.0.1 route add localhost
It doesn't matter which command you use. As a quick check that all is correct with the loopback driver, you can use the ping command to check the routing. If you issue this command:
ping localhost
you should see output like this:
PING localhost: 56 data bytes 64 bytes from 127.0.0.1: icmp_seq=0. ttl=255 time=1 ms 64 bytes from 127.0.0.1: icmp_seq=1. ttl=255 time=1 ms 64 bytes from 127.0.0.1: icmp_seq=2. ttl=255 time=1 ms 64 bytes from 127.0.0.1: icmp_seq=3. ttl=255 time=1 ms 64 bytes from 127.0.0.1: icmp_seq=4. ttl=255 time=1 ms 64 bytes from 127.0.0.1: icmp_seq=5. ttl=255 time=1 ms 64 bytes from 127.0.0.1: icmp_seq=6. ttl=255 time=1 ms 64 bytes from 127.0.0.1: icmp_seq=7. ttl=255 time=1 ms ^C localhost PING Statistics 7 packets transmitted, 7 packets received, 0% packet loss round-trip (ms) min/avg/max = 1/1/1
The ping command's progress was interrupted by issuing a Ctrl+C. If you get no output from the ping command, the localhost name wasn't recognized. Check the configuration files and route entry again and repeat the ping to the loopback address.
Setting Up the Ethernet Interface
You can follow the same procedure to set up the Ethernet driver. You use ifconfig to tell the kernel about the interface, then add the routes to the remote machines on the network. If the network is attached to your machine, you can test the connections immediately with the ping command.
Set up the Ethernet interface using ifconfig. To make the interface active, use the ifconfig command with the Ethernet device name (usually eth0) and your IP address. For example, use the following command to set up your system with the IP address 147.123.20.1:
ifconfig eth0 147.123.20.1
You don't have to specify the network mask with the ifconfig command because it can deduce the proper value from the IP address. If you want to provide the network mask value explicitly, append it to the command line with the keyword netmask:
ifconfig eth0 147.123.20.1 netmask 255.255.255.0
You can check the interface with the ifconfig command using this Ethernet interface name:
$ ifconfig eth0 eth0 Link encap 10Mps: Ethernet Hwaddr inet addr 147.123.20.1 Bcast 147.123.1.255 Mask 255.255.255.0 UP BROADCAST RUNNING MTU 1500 Metric 1 RX packets:0 errors:0 dropped:0 overruns:0 TX packets:0 errors:0 dropped:0 overruns:0
You might notice in the output that the broadcast address is set based on the local machine's IP address. This is used by TCP/IP to access all machines on the local area network at once. The Maximum Transfer Unit (MTU) size is usually set to the maximum value of 1500 (for Ethernet networks).
Next, you need to add an entry to the kernel routing tables to let the kernel know the local machine's network address. The IP address that is used with the route command to do this is that of the network as a whole, without the local identifier. To set the entire local area network at once, the -net option of the route command is used. In the case of the IP addresses shown earlier, the command is
route add -net 147.123.20.0
This command adds all the machines on the local area network identified by the network address 147.123.20 to the kernel's list of accessible machines. If you didn't do it this way, you would have to manually enter the IP address of each machine on the network. An alternative is to use the /etc/networks file to specify only the network portions of the IP addresses. The /etc/networks file might contain a list of network names and their IP addresses. If you have an entry in the /etc/networks file for a network called foobar_net, you could add the entire network to the routing table with this command:
route add foobar_net
Using the /etc/networks file approach has the security problem that any machine on that network is granted access. This may not be what you want.
After the route has been added to the kernel routing tables, you can try the Ethernet interface. To ping another machine (assuming you are connected to the Ethernet cable, of course), you need either its IP address or its name (which is resolved either by the /etc/hosts file or a service like DNS). The command and output looks like this:
tpci_sco1-45> ping 142.12.130.12 PING 142.12.130.12: 64 data bytes 64 bytes from 142.12.130.12: icmp_seq=0. time=20. ms 64 bytes from 142.12.130.12: icmp_seq=1. time=10. ms 64 bytes from 142.12.130.12: icmp_seq=2. time=10. ms 64 bytes from 142.12.130.12: icmp_seq=3. time=20. ms 64 bytes from 142.12.130.12: icmp_seq=4. time=10. ms 64 bytes from 142.12.130.12: icmp_seq=5. time=10. ms 64 bytes from 142.12.130.12: icmp_seq=6. time=10. ms ^C 142.12.130.12 PING Statistics 7 packets transmitted, 7 packets received, 0% packet loss round-trip (ms) min/avg/max = 10/12/20
If you don't get anything back from the remote machine, verify that the remote is connected and you are using the proper IP address. If all is well there, check the configuration and route commands. If that checks out, try pinging another machine.
After these steps are completed, your Linux system should be able to access any machine on the local area network through TCP/IP. If you are on a small network, that's all you really have to do. On larger networks, or those that implement special protocols or employ gateways, you need to take a few more configuration steps. These steps are covered in the following sections.
If you want to allow a few other machines on the TCP/IP network to access your Linux machine, you can put their names and IP addresses in the /etc/hosts file. Figure 28.3 shows a sample /etc/hosts file with a name and possible variations (such as godzilla and godzilla.tpci), and its IP address. That machine (which can be any operating system running TCP/IP) can now connect to your Linux system using telnet, ftp, or a similar utility. Of course, a user on the remote machine can't log in unless you set up an account for him. If the name of a remote machine is in the /etc/hosts file, you can also telnet or ftp to that machine using either its name or IP address.
Figure 28.3 This /etc/hosts file lets remote machines connect to the Linux server.