You can configure a network interface from the command line using the basic Linux networking utilities. You configure your network client hosts with the command line by using commands to change your current settings or by editing a number of system files. Two commands, ifconfig and route, are used for network configuration. The netstat command displays information about the network connections.
/sbin/ifconfig ifconfig is used to configure your network interface. You can use it to
• Activate or deactivate your NIC or change your NIC's mode
• Change your machine's IP address, netmask, or broadcast address
• Create an IP alias to allow more than one IP address on your NIC
• Set a destination address for a point-to-point connection
You can change as many or as few of these options as you'd like with a single command. The basic structure for the command is as follows:
ifconfig [network device] options
Table 18.1 shows a subset of ifconfig options and examples of their uses.
Table 18.1. ifconfig Options
Change IP address Change the netmask
Change the broadcast
Take interface down Bring interface up
Set NIC promiscuous Set multicasting mode Enable [disable]
netmask [netmask] broadcast [address] down up (add IP address)
[-]promisc [ifconfig eth0 -promise]
[-]pointopoint eth0 pointopoint
Example ifconfig eth0:0_:[number] 10.10.10.10
ifconfig eth0 10.10.10.12
fconfig eth0 netmask 255.255.255.0
ifconfig eth0 broadcast 10.10.10.255
ifconfig eth0 down ifconfig eth0 up (ifconfig eth0 10.10.10.10)
ifconfig eth0 promisc mode on [off]
ifconfig eth0_0n [off] allmulti [ifconfig eth0 -allmulti]
10.10.10.20 [ifconfig eth0 pointopoint_10.10.10.20]
The ifconfig man page shows other options that enable your machine to interface with a number of network types such as AppleTalk, Novell, IPv6, and others. Again, read the man page for details on these network types.
Promiscuous mode causes the NIC to receive all packets on the network. It is often used to sniff a network. Multicasting mode enables the NIC to receive all multicast traffic on the network.
If no argument is given, ifconfig displays the status of active interfaces. For example, the output of ifconfig, without arguments and one active and configured NIC, looks similar to this: $ ifconfig eth0 Link encap:Ethernet HWaddr 00:0F:EA:B2:53:85
inet addr:192.168.2.5 Bcast:192.168.2.255 Mask:255.255.255.0 inet6 addr: fe80::20f:eaff:feb2:5385/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:471 errors:0 dropped:0 overruns:0 frame:0 TX packets:695 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000
RX bytes:160637 (156.8 KiB) TX bytes:86193 (84.1 KiB) Interrupt:185 Base address:0x6000
lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:19 errors:0 dropped:0 overruns:0 frame:0
TX packets:19 errors:0 dropped:0 overruns:0 carrier:0
The output is easily understood. The inet entry displays the IP address for the interface. up signifies that the interface is ready for use, broadcast denotes that the interface is connected to a network that supports broadcast messaging (ethernet), running means that the interface is operating, and loopback shows which device (lo) is the loopback address. The maximum transmission unit (MTU) on etho is 1500 bytes. This determines the size of the largest packet that can be transmitted over this interface (and is sometimes "tuned" to other values for performance enhancement). MeTRic is a number from 0 to 3 that relates to how much information from the interface is placed in the routing table. The lower the number, the smaller the amount of information.
The ifconfig command can be used to display information about or control a specific interface using commands as listed in Table 18.1. For example, to deactivate the first Ethernet device on a host, use the ifconfig command, the interface name, and the command down like so:
$ sudo ifconfig eth0 down
You can also configure and activate the device by specifying a hostname or IP address and network information. For example to configure and activate ("bring up") the etho interface with a specific IP address, use the ifconfig command like this:
$ sudo ifconfig eth0 192.168.2.9 netmask 255.255.255.0 up
If you have a host defined in your system's /etc/hosts file (see the section "Network Configuration Files" later in this chapter), you can configure and activate the interface according to the defined hostname like this:
$ sudo ifconfig eth0 dogdog.hudson.com up
Read the next section to see how to configure your system to work with your LAN.
The second command used to configure your network is the route command. route is used to build the routing tables (in memory) implemented for routing packets as well as displaying the routing information. It is used after ifconfig has initialized the interface. route is normally used to set up static routes to other networks via the gateway or to other hosts. The command configuration is like this:
$ route [options] [commands] [parameters]
To display the routing table, use the route command with no options. The display will look similar to this:
Kernel IP routing table Destination Gateway
Genmask 255.255.255.0 0.0.0.0
Flags Metric Ref U 0 0
In the first column, Destination is the IP address (or, if the host is in /etc/hosts or /etc/networks, the hostname) of the receiving host. The default entry is the default gateway for this machine. The Gateway column lists the gateway that the packets must go through to reach their destination. An asterisk (*) means that packets go directly to the host. Genmask is the netmask. The Flags column can have several possible entries. In our example, u verifies that the route is enabled and g specifies that Destination requires the use of a gateway. The Metric column displays the distance to the Destination. Some daemons use this to figure the easiest route to the Destination. The Ref column is used by some UNIX flavors to convey the references to the route. It isn't used by Linux. The Use column indicates the number of times this entry has been looked up. Finally, the iface column is the name of the interface for the corresponding entry.
Using the -n option to the route command will give the same information, substituting IP addresses for names and asterisks (*), and looks like this:
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
The route command can add to the table using the add option. With the add option, you can specify a host (-host) or a network (-net) as the destination. If no option is used, the route command assumes that you are configuring the host issuing the command. The most common uses for the route command are to add the default gateway for a host, for a host that has lost its routing table, or if the gateway address has changed. For example, to add a gateway with a specific IP address, you could use the following:
$ sudo route add default gw 184.108.40.206
Note that you could use a hostname instead of an IP address if desired. Another common use is to add the network to the routing table right after using the ifconfig command to configure the interface. Assuming that the 220.127.116.11 entry from the previous examples was missing, replace it using the following command:
$ sudo route add -net 18.104.22.168 netmask 255.255.255.0 dev eth0
You also can use route to configure a specific host for a direct (point-to-point) connection. For example, say that you have a home network of two computers. One of the computers has a modem through which it connects to your business network. You typically work at the other computer. You can use the route command to establish a connection through specific hosts using the following command:
$ sudo route add -host 22.214.171.124 gw 126.96.36.199
The preceding example makes the computer with the modem the gateway for the computer you are using. This type of command line is useful if you have a gateway or firewall connected to the Internet. There are many additional uses for the route command, such as manipulating the default packet size. See the man page for those uses.
The netstat command is used to display the status of your network. It has several parameters that can display as much or as little information as you prefer. The services are listed by sockets (application-to-application connections between two computers). You can use netstat to display the information in Table 18.2.
Table 18.2. netstat Options
-g Displays the multicast groups configured
-i Displays the interfaces configured by ifconfig
Lists a summary of activity for each protocol Gives verbose output, listing both active and inactive sockets Updates output every second (good for testing and troubleshooting) Gives verbose output for active connections only
Displays information from the route cache and is good for looking at past connections
Several other options are available for this command, but they are used less often. As with the route command, the man page can give you details about all options and parameters.
Was this article helpful?