21 TCP/IP configuration How much work would a network net if a network could net work?
SuSE 7.x and before
21.2 Configuring IP
ifconfig
ifconfig is used to configure the network interfaces controlled by the kernel. The bootup sequence will call ifconfig to set up the initial configuration. After this ifconfig is only used for debugging and system tuning.
ifconfig without any further parameters displays the current interfaces and their settings. If you have a network card configured, you will two interfaces. eth0 is the first ethernet interface. lo is the loopback interface, which is used for when the machine needs to talk to
itself.
foobar:~ $ /sbin/ifconfig #abbreviated output
eth0 Link encap:Ethernet HWaddr 00:E9:98:99:70:91 inet addr:192.168.0.100 Bcast:192.168.0.255
Mask:255.255.0.0 ...
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0 ...
To change settings using ifconfig, the following syntax is used. The changes take effect immediately. If you do not supply a netmask or a broadcast address, the kernel will guess values for you (sometimes better than at other times).
foobar:~ # ifconfig eth0 192.168.0.100 netmask 255.255.255.0 broadcast 192.168.0.255
foobar:~ # ifconfig eth0
eth0 Link encap:Ethernet HWaddr 00:E9:98:99:70:91 inet addr:192.168.0.100 Bcast:192.168.0.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:135 dropped:0 overruns:0 carrier:270 collisions:0 txqueuelen:100
RX bytes:0 (0.0 b) TX bytes:0 (0.0 b) Interrupt:10 Base address:0x4000
ifconfig can also take down a running interface. When you do this, it will disapear from the output of ifconfig.
foobar:~ # ifconfig eth0 down
foobar:~ # ifconfig
...
foobar:~ # ifconfig eth0 up
foobar:~ # ifconfig
... route
When an interface is configured by ifconfig, the kernel automatically adds a route to the local network, based on the netmask value supplied. It remains only to configure routes to networks that are not local. In the case of a workstation, this generally means adding a single routing entry for the default gateway. All traffic not destined for the local network is forwarded to that machine. route with no options the active routes on the system22. foobar:~ # route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.0.0 * 255.255.255.0 U 0 0 0 eth0
21 TCP/IP configuration LPI 102 Course Notes 143
route add
To add a route the command is route add, with the following syntax:
route add [-net | -host] DEST [gw GATEWAY] [netmask MASK] [dev DEVICE]
The kernel adds a route to the destination host or the network with the given netmask, via the specific gateway, which is directly reachable on the device. The usage of route add is one of the following: • route add default ... – set the default route • route add host ... – set up a route to a specific host • route add net ... – set up a route to a network This changes the route for outgoing packets. Whether packets will return to you or not depends on the routing tables on the remote machine and intermediate routers. Here we say that the route to the world is via the gateway 192.168.0.1, connected to eth0.
foobar:~ # route add default gw 192.168.0.1 dev eth0
foobar:~ # route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.0.0 * 255.255.255.0 U 0 0 0 eth0
default 192.168.0.1 0.0.0.0 UG 0 0 0 eth0
To add a route to a single host, you use the host switch.
Here we adjust our routing table so that packets destined for the host 192.168.0.62 will be submitted to the gateway 192.168.0.4 for further processing.
foobar:~ # route add -host 192.168.0.62 gw 192.168.0.4 dev eth0
foobar:~ # route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface 192.168.0.62 192.168.0.4 255.255.255.255 UGH 0 0 0 eth0 192.168.0.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0 0.0.0.0 192.168.0.1 0.0.0.0 UG 0 0 0 eth0
To add a route to a network the net switch is used.
foobar:~ # route add -net 172.16.88.0 netmask 255.255.255.0 gw 192.168.0.5 dev eth0
foobar:~ # route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.0.62 192.168.0.4 255.255.255.255 UGH 0 0 0 eth0
192.168.0.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
172.16.88.0 192.168.0.5 255.255.255.0 UG 0 0 0 eth0
0.0.0.0 192.168.0.1 0.0.0.0 UG 0 0 0 eth0
route del
Routes can be deleted in the same way they were added, except that route del is used in the place of route add.
In this example, we realise that the gateway for the network 172.16.88.0/24 was incorrect. The way to fix this is to remove the route, and add a correct route.
foobar:~ # route del -net 172.16.88.0 netmask 255.255.255.0
foobar:~ # route add -net 172.16.88.0 netmask 255.255.255.0 gw 192.168.0.6
foobar:~ # route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface 192.168.0.62 192.168.0.4 255.255.255.255 UGH 0 0 0 eth0 192.168.0.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0 172.16.88.0 192.168.0.6 255.255.255.0 UG 0 0 0 eth0 0.0.0.0 192.168.0.1 0.0.0.0 UG 0 0 0 eth0
In the last example, the route add and route del commands did not entirely specify the route (leaving out gw and dev or both). The version of route that was used was smart enough to figure out what was intended. The route man page contains a number of examples of how the command is used.
Here we change the default gateway from 192.168.0.1 to 192.168.0.10. We remove the original default route, and add the new default route.
foobar:~ # route del default gw 192.168.0.1
foobar:~ # route add default gw 192.168.0.10
foobar:~ # route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface 192.168.0.62 192.168.0.4 255.255.255.255 UGH 0 0 0 eth0 192.168.0.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0 172.16.88.0 192.168.0.6 255.255.255.0 UG 0 0 0 eth0 0.0.0.0 192.168.0.10 0.0.0.0 UG 0 0 0 eth0
21.3 Configuring name resolution
Name resolution involves translating names (such as www.w3c.org) into network addresses21 TCP/IP configuration LPI 102 Course Notes 145 (such as 172.31.98.251). Linux supports a number of name resolution methods, but the most commonly used ones are: • files – look up names in /etc/hosts • dns – look up names by querying the DNS server(s) listed in /etc/resolv.conf. The full list varies between systems. foobar:/lib $ cd /lib/ foobar:/lib $ ls libnss*
libnss_compat.so.2 libnss_hesiod.so.2 libnss_winbind.so libnss_dns.so.2 libnss_nis.so.2 libnss_winbind.so.2 libnss_files.so.2 libnss_nisplus.so.2 libnss_wins.so.2
The file that configures which name resolution mechanism is to be used is /etc/nsswitch.conf. Older applications (based on libc5) use /etc/host.conf. It is good practice to have the same information in both files to avoid surprises.
foobar:~ $ cat /etc/host.conf
order hosts, bind multi on
“hosts, bind” in the above text means that first /etc/hosts is checked, then the DNS servers are tried, as configured in /etc/resolv.conf. The “multi on” causes the resolver to return all the names of hosts, rather than just the first one found.
Of the many entries in nsswitch.conf, the hosts entry is the one that configures the resolving of host names.
foobar:~ $ cat /etc/nsswitch.conf
passwd: compat group: compat hosts: files dns networks: files dns services: files //snip /etc/hosts
For each IP address, /etc/hosts lists the IP address, the name, and optionally a number of aliases.
foobar:~ $ cat /etc/hosts
# IP-Address Full-Qualified-Hostname Short-Hostname 127.0.0.1 localhost
192.168.0.100 foobar.example.com foobar
In the usual case, the hosts file contains a localhost entry for the loopback address and an entry for the local machine (sometimes using the IP address 127.0.0.2 if there is no fixed IP address). The entry for the local machine determines the fullyqualified host name (in the above case “foobar” maps to “foobar.example.com”).
/etc/resolv.conf
resolv.conf determines how DNS resolution proceeds. There are only two entries.
foobar:~ $ cat /etc/resolv.conf
# domain example.com # an alternative to "search" nameserver 192.168.0.2
nameserver 196.25.1.1
The search entry specifies that a query for a name like foomatic will actually generate queries for foomatic.example.com. The DNS query will be sent to each of the DNS servers until an answer is received.
/etc/networks
/etc/networks is similar to /etc/hosts, but lists names for networks. This information can be used by the output of route to display descriptions of network names. It is not unusual for this file to be empty.
foobar:~ # route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface 10.0.0.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0 172.19.18.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0 192.168.0.0 0.0.0.0 255.255.0.0 U 0 0 0 eth0 0.0.0.0 10.0.0.20 0.0.0.0 UG 0 0 0 eth0
foobar:~ # cat /etc/networks
loopback 127.0.0.0 worknet 10.0.0.0 homenet 172.19.18.0 sparenet 192.168.0.0 foobar:~ # route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface worknet * 255.255.255.0 U 0 0 0 eth0 homenet * 255.255.255.0 U 0 0 0 eth0 sparenet * 255.255.0.0 U 0 0 0 eth0 default 10.0.0.20 0.0.0.0 UG 0 0 0 eth0 The method used for network name lookups is actually determined by the “networks” entry in /etc/nsswitch.conf.
foobar:~ # grep networks /etc/nsswitch.conf
networks: files dns