Module 14
Module 14
1 Introduction
Having access to the network is a key feature of most Linux systems. Users want to surf the net,
send and receive email and transfer files with other users.
Typically the programs that perform these functions, such as web browsers and email clients, are
reasonably easy to use. However, they all rely on an important feature: the ability of your computer
to communicate with another computer. To have this communication, you need to know how to
configure your system's network.
Linux provides you with several tools to both configure your network as well as monitor how it is
performing.
Net A network is a collection of two or more hosts (computers) that are able to
ork communicate with each other. This communication can be via a wired
connection or wireless.
Clie A client is a host that is accessing a server. When you are working on a
computer surfing the Internet, you are considered to be on a client host.
Rou Also called a gateway , a router is a machine that connects hosts from on
r network to another network. For example, if you work in an office
environment, the computers within the company can all communicate via th
local network created by the administrators. To access the Internet, the
computers would have to communicate with a router that would be used to
forward network communications to the Internet. Typically when you
communicate on a large network (like the Internet), several routers are used
before your communication reaches its final destination.
Hostn Each host on a network could have its own hostname because names a
me more natural for humans to remember than numbers, making it easier for
us to address network packets to another host. Hostnames are translated
into IP addresses before the network packet is sent on the network.
URL A Uniform Resource Locator (URL) , also commonly called a web addre
is used to locate a resource, like a web page, on the internet. It’s what yo
type into your web browser to access a web page. For example,
[Link] . It includes the protocol http:// and the
hostname [Link] .
14.4 IP Addresses
As previously mentioned, hosts address network packets by using the IP address of the
destination machine. The network packet also includes a return address , which is the IP
address of the sending machine.
There are, in fact, two different types of IP addresses: IPv4 and IPv6. To understand why there
are two different types, you need to understand a brief bit of IP addressing history.
For many years, the IP addressing technique that was used by all computers was IPv4. In an
IPv4 address, a total of four 8-bit numbers are used to define the address. This is considered a
32-bit address (4 x 8 = 32). For example:
[Link]
While it seems like there should be plenty of IP addresses to go around, various factors have led
to a problem: the Internet started running out of IP addresses.
This issue encouraged the development of IPv6. IPv6 was officially created in 1998. In an IPv6
network the addresses are much larger, 128-bit addresses that look like this:
2001:0db8:85a3:0042:1000:8a2e:0370:7334
Essentially, this provides for a much larger address pool, so large that running out of addresses
any time in the near future is very unlikely.
It is important to note that the difference between IPv4 and IPv6 isn't just a larger address pool.
IPv6 has many other advanced features that address some of the limitations of IPv4, including
better speed, more advanced package management and more efficient data transportation.
Considering all the advantages, you would think that by now all hosts would be using IPv6.
However, the majority of network-attached devices in the world still use IPv4 (something like
98-99% of all devices).
So, why hasn't the world embraced the superior technology of IPv6?
Nonetheless, most experts agree that IPv6 will eventually replace IPv4, so understanding the
basics of both is recommended for those who work in the IT industry.
Generally speaking, desktop machines use wired networks, while laptops use wireless networks.
Normally a wired machine uses a static IP address, but these can also often be assigned via a
DHCP server. In almost all cases, wireless machines use DHCP since they are almost always
mobile and attached to different networks.
These files may vary depending on the Linux distribution that you are working on.
If the device were configured to be a DHCP client, the BOOTPROTO value would be set to dhcp,
and the IPADDR, GATEWAY and DNS1 values would not be set.
If you want your system to be a DHCP IPv6 client, then add the following setting:
DHCPV6C=yes
You also need to add the following setting to the /etc/sysconfig/network file:
NETWORKING_IPV6=yes
Consider This
The widely accepted method of making changes to a network interface is to take the interface
down using a command such as ifdown eth0, make the desired changes to the configuration
file, and then bring the interface back up and into service with a command such as ifup eth0.
Another less specific method is to restart the system’s networking entirely, with a command such
as service network restart, which takes down ALL interfaces, re-reads all related
configuration files, and then restarts the networking for the system.
Restarting the network service can disrupt much more than just the single interface a user
wanted to change, so use the most limited and specific commands to restart the interface if
possible.
The following example demonstrates how the service command would need to be executed on
a CentOS system:
The address of the DNS server is stored in the /etc/[Link] file. A typical
/etc/[Link] file is automatically generated and looks like the following:
The nameserver setting is often set to the IP address of the DNS server. The following example
uses the host command, which works with DNS to associate a hostname with an IP address.
Note that the example server is associated with the IP address [Link] by the DNS
server:
sysadmin@localhost:~$ host [Link]
[Link] has address [Link]
It is also common to have multiple nameserver settings, in the event that one DNS server isn't
responding.
Files Explanation
/etc/[Link] This file contains the IP addresses of the name servers the
system should consult in any attempt to resolve names to IP
addresses. These servers are often DNS servers. It also ca
contain additional keywords and values that can affect the
resolution process.
Output Omitted...
Output Omitted...
Commands or programs on the system, such as the browser, request a connection with a remote
computer by DNS name. Then the system consults various files in a particular order to attempt to
resolve that name into a usable IP address.
Third, if the local /etc/hosts file doesn’t result in a match, the system will use the configured DNS
server entries contained in the /etc/[Link] file to attempt to resolve the name.
The /etc/[Link] file should contain at least two entries for name servers, such as the
example file below:
nameserver [Link]
nameserver [Link]
3. The DNS resolution system will use the first name server for an attempted lookup of the
name. If that is unavailable, or a timeout period is reached, the second server will then be
queried for the name resolution. If a match is found, it is returned to the system and used
for initiating a connection and is also placed in the DNS cache for a configurable time
period.
Consider This
Two other keywords may appear in the system’s /etc/[Link] file. Although these are
beyond the scope of this course, they are routinely included in default /etc/[Link] files
and so we include explanations of these terms below:
root@localhost:~# ifconfig
eth0 Link encap:Ethernet HWaddr b6:84:ab:e9:8f:0a
inet addr:[Link] Bcast:[Link] Mask:[Link]
inet6 addr: fe80::b484:abff:fee9:8f0a/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:95 errors:0 dropped:4 overruns:0 frame:0
TX packets:9 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:25306 (25.3 KB) TX bytes:690 (690.0 B)
lo Link encap:Local Loopback
inet addr:[Link] Mask:[Link]
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:6 errors:0 dropped:0 overruns:0 frame:0
TX packets:6 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:460 (460.0 B) TX bytes:460 (460.0 B)
The lo device is referred to as the loopback device. It is a special network device used by the
system when sending network-based data to itself.
The ifconfig command can also be used to modify network settings temporarily. Typically
these changes should be permanent, so using the ifconfig command to make such changes is
relatively rare.
The ip command differs from ifconfig in several important manners, chiefly that through its
increased functionality and set of options, it can almost be a one-stop shop for configuration and
control of a system’s networking. The format for the ip command is as follows:
While ifconfig is limited primarily to modification of networking parameters, and displaying the
configuration details of networking components, the ip command branches out to do some of the
work of several other legacy commands such as route and arp.
Note
Linux and Unix commands don’t usually just disappear when they become obsolete; they stick
around as a legacy command, sometimes for many years, as the number of scripts that depend
on those commands, and the amount of muscle memory amongst system administrators, makes
it a good idea to keep them around for compatibility sake.
The ip command can initially appear to be a little more verbose than the ifconfig command,
but it’s a matter of phrasing and a result of the philosophy behind the operation of the ip
command.
In the example below, both the ifconfig command and ip command are used to show all
interfaces on the system.
root@localhost:~# ifconfig
eth0 Link encap:Ethernet HWaddr 00:0c:29:71:f0:bb
inet addr:[Link] Bcast:[Link] Mask:[Link]
inet6 addr: fe80::20c:29ff:fe71:f0bb/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:8506 errors:0 dropped:0 overruns:0 frame:0
TX packets:1201 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:8933700 (8.9 MB) TX bytes:117237 (117.2 KB)
root@localhost:~# route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
[Link] * [Link] U 0 0 0 eth0
default [Link] [Link] UG 0 0 0 eth0
The first highlighted line in the preceding example indicates that any network packet sent to a
machine in the 192.168.0 network is not sent to a gateway machine (the * indicates
no gateway ). The second highlighted line indicates that all other network packets are sent to
the host with the IP address of [Link] (the router).
Some users prefer to display this information with numeric data only, by using the -n option to
the route command. For example, compare the following and focus on where the previous
output displayed the word default:
root@localhost:~# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
[Link] [Link] [Link] U 0 0 0 eth0
[Link] [Link] [Link] UG 0 0 0 eth0
The [Link] refers to all other machines , and is the same as default .
The route command is becoming obsolete in some Linux distributions (deprecated) and is being
replaced with a form of the ip command, specifically ip route or ip route show. Note that the
same information highlighted above can also be found using this command:
root@localhost:~# ip route show
default via [Link] dev eth0 proto static
[Link]/24 dev eth0 proto kernel scope link src [Link]
14.6.4 The ping Command
The ping command can be used to determine if another machine is reachable . If the ping
command can send a network package to another machine and receive a response, then you
should be able to connect to that machine.
By default, the ping command continues sending packages endlessly. To limit how many pings
to send, use the -c option followed by a number indicating how many iterations you desire. The
following examples show ping being limited to 4 iterations.
If the ping command fails, a message stating, Destination Host Unreachable displays:
It is important to note that just because the ping command fails does not mean that the remote
system is unreachable. Some administrators configure their machines (and even entire
networks!) to not respond to ping requests because a server can be attacked by something
called a denial of service attack . In this sort of attack, a server is overwhelmed by a massive
number of network packets. By ignoring ping requests, the server is less vulnerable.
As a result, the ping command may be useful for checking the availability of local machines, but
not always for machines outside of your own network.
Consider This
Many administrators use the ping command with a hostname, and if that fails then use the IP
address to see if the fault is in resolving the device’s hostname. Using the hostname first saves
time; if that ping command is successful, there is proper name resolution, and the IP address is
functioning correctly as well.
For example, to display statistics regarding network traffic, use the -i option to the netstat
command:
root@localhost:~# netstat -i
Kernel Interface table
Iface MTU Met RX-OK RX-ERR RX-DRP RX-OVR TX-OK TX-ERR TX-DRP TX-OVR Flg
eth0 1500 0 137 0 4 0 12 0 0 0 BMRU
lo 65536 0 18 0 0 0 18 0 0 0 LRU
The most important statistics from the output above are the TX-OK and TX-ERR. A high
percentage of TX-ERR may indicate a problem on the network, such as too much network traffic.
To use the netstat command to display routing information, use the -r option:
root@localhost:~# netstat -r
Kernel IP routing table
Destination Gateway Genmask Flags MSS Window irtt Iface
[Link] * [Link] U 0 0 0 eth0
default [Link] [Link] UG 0 0 0 eth0
The netstat command is also commonly used to display open ports . A port is a unique
number that is associated with a service provided by a host. If the port is open, then the service
is available for other hosts.
For example, you can log into a host from another host using a service called SSH . The SSH
service is assigned port #22. So, if port #22 is open, then the service is available to other hosts.
It is important to note that the host also needs to have the services running itself; this means that
the service (in this case the ssh daemon) that allows remote users to log in needs to be started
(which it typically is, for most Linux distributions).
To see a list of all currently open ports, use the following command:
root@localhost:~# netstat -tln
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 [Link]:53 [Link]:* LISTEN
tcp 0 0 [Link]:53 [Link]:* LISTEN
tcp 0 0 [Link]:22 [Link]:* LISTEN
tcp 0 0 [Link]:953 [Link]:* LISTEN
tcp6 0 0 :::53 :::* LISTEN
tcp6 0 0 :::22 :::* LISTEN
tcp6 0 0 ::1:953 :::* LISTEN
As you can see from the output above, port #22 is listening , which means it is open.
In the previous example, -t stands for TCP (recall this protocol from earlier in this chapter), -l
stands for listening (which ports are listening) and -n stands for show numbers, not names .
Sometimes showing the names can be more useful. This can be achieved by dropping the -n
option:
root@localhost:~# netstat -tl
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 [Link].:domain *:* LISTEN
tcp 0 0 localhost:domain *:* LISTEN
tcp 0 0 *:ssh *:* LISTEN
tcp 0 0 localhost:953 *:* LISTEN
tcp6 0 0 [::]:domain [::]:* LISTEN
tcp6 0 0 [::]:ssh [::]:* LISTEN
tcp6 0 0 localhost:953 [::]:* LISTEN
On some distributions you may see the following message in the man page of the netstat
command:
NOTE
This program is obsolete. Replacement for netstat is ss. Replacement for
netstat -r is ip route. Replacement for netstat -i is ip -s link.
Replacement for netstat -g is ip maddr.
While no further development is being done on the netstat command, it is still an excellent tool
for displaying network information. The goal is to eventually replace the netstat command with
commands such as the ss and ip commands. However, it is important to realize that this may
take some time.
In the following example, the dig command is used to determine the IP address of the
[Link] host:
root@localhost:~# dig [Link]
;; Got answer:
;; QUESTION SECTION:
;[Link]. IN A
;; ANSWER SECTION:
;; AUTHORITY SECTION:
;; SERVER: [Link]#53([Link])
Note that the response included the IP address of [Link], meaning that the DNS server
has the IP address to hostname translation information in its database.
If the DNS server doesn't have the requested information, it is configured to ask other DNS
servers. If none of them have the requested information, an error message displays:
root@localhost:~# dig [Link]
; <<>> DiG 9.8.1-P1 <<>> [Link]
The host command can also be used in reverse if an IP address is known, but the domain name
is not.
root@localhost:~# host [Link]
Other options exist to query the various aspects of a DNS such as a CNAME
canonical name -alias :
root@localhost:~# host -t CNAME [Link]
Since many DNS servers store a copy of [Link], SOA Start of Authority records indicate
the primary server for the domain:
root@localhost:~# host -t SOA [Link]
Trying "[Link]"
;; QUESTION SECTION:
;[Link]. IN ANY
;; ANSWER SECTION:
;; ADDITIONAL SECTION:
socket types. Meant to be a replacement for and to be similar in function to the netstat
command, it also shows a lot more information and has more features.
The main reason a user would use the ss command is to view what connections are currently
established between their local machine and remote machines, statistics about those
connections, etc.
Similar to the netstat command, you can get a great deal of useful information from the ss
command just by itself as shown in the example below.
root@localhost:~# ss
Netid State Recv-Q Send-Q Local Address:Port Peer
Address:Port
u_str ESTAB 0 0 * 104741 *
104740
u_str ESTAB 0 0 /var/run/dbus/system_bus_socket 14623 *
14606
u_str ESTAB 0 0 /var/run/dbus/system_bus_socket 13582 *
13581
u_str ESTAB 0 0 /var/run/dbus/system_bus_socket 16243 *
16242
u_str ESTAB 0 0 * 16009 *
16010
u_str ESTAB 0 0 /var/run/dbus/system_bus_socket 10910 *
10909
u_str ESTAB 0 0 @/tmp/dbus-LoJW0hGFkV 15706 *
15705
u_str ESTAB 0 0 * 24997 *
24998
u_str ESTAB 0 0 * 16242 *
16243
u_str ESTAB 0 0 @/tmp/dbus-opsTQoGE 15471 * 15470
The output is very similar to the output of the netstat command with no options. The columns
above are:
Recv-Q Amount of data queued up for being processed having been receiv
Local The address and port of the local host’s portion of the connection
Address
Peer The address and port of the remote host’s portion of the connection
Address
The format of the output of the ss command can change dramatically, given the options
specified, such as the use of the -s option, which displays mostly the types of sockets, statistics
about their existence and numbers of actual packets sent and received via each socket type, as
shown below:
root@localhost:~# ss -s
Total: 1000 (kernel 0)
TCP: 7 (estab 0, closed 0, orphaned 0, synrecv 0, timewait 0/0), ports 0
Consider This
The ss command typically shows many rows of data, and it can be somewhat daunting to try to
find what you want in all that output. Consider sending the output to the less command to make
the output more manageable. Pagers allow the user to scroll up and down, do searches and
many other useful functions inside the parameters of the less command.
While the ss command offers many different options for gathering and displaying information, the
examples above are the most common ones, and anything else would be outside of the scope of
the exam’s objectives at this level.
If you only provide a machine name or IP address to log into, the ssh command assumes you
want to log in using the same username that you are currently logged in as. To use a different
username, use the syntax:
username@hostname
bob@test:~$ exit
logout
Connection to test closed.
root@localhost:~#
Warning
Be careful, if you use the exit command too many times, you will close the terminal window that
you are working in!
After you answer yes, the RSA key fingerprint of the remote machine is stored on your local
system. When you attempt to ssh to this same machine in the future, the RSA key fingerprint
provided by the remote machine is compared to the copy stored on the local machine. If they
match, then the username prompt appears. If they don't match, an error like the following
displays:
sysadmin@localhost:~$ ssh bob@test
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
Someone could be eavesdropping on you right now (man-in-the-middle attack)!
It is also possible that the RSA host key has just been changed.
The fingerprint for the RSA key sent by the remote host is
c2:0d:ff:27:4c:f8:69:a9:c6:3e:13:da:2f:47:e4:c9.
Please contact your system administrator.
Add correct host key in /home/sysadmin/.ssh/known_hosts to get rid of this
message.
Offending key in /home/sysadmin/.ssh/known_hosts:1
RSA host key for test has changed and you have requested strict checking.
Host key verification failed.
This error could indicate that a rogue host has replaced the correct host. Check with the
administrator of the remote system. If the system were recently reinstalled, it would have a new
RSA key, and that would be causing this error.
In the event that this error message is due to a remote machine reinstall, you can remove the
~/.ssh/known_hosts file from your local system (or just remove the entry for that one
machine) and try to connect again:
sysadmin@localhost:~$ cat ~/.ssh/known_hosts
test ssh-rsa
AAAAB3NzaC1yc2EAAAAmIwAAAQEAklOUpkDHrfHY17SbrmTIp/RZ0V4DTxgq9wzd+ohy006SWDSGPA+
nafzlHDPOW7vdI4mZ5ew18KL4JW9jbhUFrviQzM7xlELEVf4h9lFX5QVkbPppSrg0cda3Pbv7kOdJ/M
TyBlWXFCRH+Cv3FXRitBqxiX1nKhXpHAZsMciLq8V6RjsNAQwdsdMFvSlVK/7BA
t5FaiKoAfncM1Q8x3+2V0Ww71/eIFmb1zuUFljHYTprrX88XypNDvjYNby6vw/Pb0rwprz/Tn
mZAW3UX+PnTPI89ZPmNBLuxyrD2cE86Z/il8b+gw3r3+1nJotmIkjn2so1d01QraTlMqVSsbx
NrRFi9wrf+ghw==
sysadmin@localhost:~$ rm ~/.ssh/known_hosts
sysadmin@localhost:~$ ssh bob@test
The authenticity of host ‘test ([Link])’ can’t be established.
RSA key fingerprint is c2:0d:ff:27:4c:f8:69:a9:c6:3e:13:da:2f:47:e4:c9.
Are you sure you want to continue connection (yes/no)? yes
Warning: Permanently added ‘test’ (RSA) to the list of known hosts.
bob@test’s password:
Last login: Fri Oct 4 16:14:39 CDT 2013 from localhost