Network - General

Network - General


Network Interfaces (/etc/network/interfaces)

/etc/network/interfaces lists network interfaces with information about how they should be configured

If the computer has an Ethernet card, the IP network that is associated with it must be configured, eaither with dhcp, or with static configuration.

DHCP:
The simplest method is dynamic configuration with DHCP, and it requires a DHCP server on the local network which sends configuration settings for the network card. Today`s routers act as a DHCP server, and a gateway/router as well, so it manages the network traffic between the computer and the Internet

DHCP configuration:
auto eth0
iface eth0 inet dhcp
hostname privateraspi


Static:
A “static” configuration must indicate network settings in a fixed manner. This includes at least the IP address and subnet mask; network and broadcast addresses are also sometimes listed. A router connecting to the exterior will be specified as a gateway.

Static configuration:
auto eth0                                <--automatically should be configured durong bot
iface eth0 inet static                   <--it is a static IP configuration
address 192.168.0.3                      <--the IP address configured for eth0
netmask 255.255.255.0                    <--the value of subnetmask valid for that network
broadcast 192.168.0.255                  <--broadcast IP (the highest value on that network)
network 192.168.0.0                      <--network domain (based on subnet mask)
gateway 192.168.0.1                      <--gatway for that network (usually the 1st IP)

A line starting with auto gives a list of interfaces to be automatically configured on boot by ifupdown and its /etc/init.d/networking init script. This will often be eth0, which refers to the first Ethernet card.

==========================================

Name Resolution (/etc/nsswitch.conf)

The mechanism for name resolution in Linux can use various sources of information declared in the /etc/nsswitch.conf file. The entry that involves host name resolution is hosts. By default, it contains "files dns", which means that the system consults the /etc/hosts file first, then DNS servers. (NIS or LDAP servers are other possible sources.)

The command host do not use the standard name resolution mechanism, so it will not take into consideration /etc/nsswitch.conf, and /etc/hosts file either.

# cat /etc/nsswitch.conf
passwd:         compat
group:          compat
shadow:         compat
gshadow:        files

hosts:          files mdns4_minimal [NOTFOUND=return] dns
networks:       files

==========================================

Configuring DNS servers (/etc/resolv.conf)

DNS (Domain Name Service) is a hierarchical service, which maps domain names to IP addresses, and vice-versa. (like www.raspberrypi.org to 93.93.128.133)

In order to access internet addresses with their domain names (without using IP addresses), Pi needs to know which DNS servers to use. The list of DNS servers, (sometimes called nameservers) is stored in /etc/resolv.conf. When DHCP configuration is used, this file is filled in automatically. When IP address has been set manually, you need to fill in the DNS servers in that file. (Normally, this would be the address of your router.)

# cat /etc/resolv.conf
nameserver 212.27.32.176
nameserver 212.27.32.177
nameserver 8.8.8.8

(For home user configurations, it contains usually the IP address of the router, as on the home network, the router manages mostly the DNS server functions as well.)


Configure resolv.conf:
(Here Google’s publicly-accessible nameservers will be used to resolve domain names)

1. add these lines to /etc/resolv.conf

nameserver 8.8.8.8
nameserver 8.8.4.4

2. sudo /etc/init.d/networking restart    <--Restart the networking interface
3. ping -c 1 www.raspberrypi.org          <--test new settings by ping command (or by opening a web browser)


==========================================

IP and hostname mapping locally  (/etc/hosts)

Each line in the /etc/hosts file contains an IP address, the complete name (domain name) and optionally an alias (short name) belonging to that IP address. This file can be edited manually, and it is a good way to keep in a local file the important IP addresses and its DNS names;

# cat /etc/hosts
127.0.0.1   localhost
192.168.0.1 raspi.privatehome.com raspi


If there is no name server on the local network, it is still possible to establish a small table, mapping IP addresses and machine hostnames in the /etc/hosts file, which is available even during network outages or when DNS servers are unreachable,

3 IP address ranges have been created officially (by RFC1918), which are planned to use in local (home) networks. Those are not  to be routed on the Internet:
class-A: 10.0.0.0/8
class-B: 172.16.0.0/16 to 172.31.0.0/16
class-C: 192.168.0.0/24 to 192.168.255.0/24

==========================================

ifconfig


Checking current network configuration and IP addresses with command ifconfig:

• Link encap: The type of encapsulation used by the network, can be Ethernet (it means that this interface will wrap your packets in an Ethernet frame for transmission)

• Hwaddr: The Media Access Control (MAC) address of the network interface, (in hexadecimal). This is unique for every device on the network, which is set at the factory.

• inet addr: The internet protocol (IP) address of the network interface. 

• Bcast: The broadcast address for the network to which the Pi is connected. Any traffic sent to this address will be received by every device on the network.

• Mask: The network mask, which controls the size of the network to which the Pi is connected. For most home users, this will read 255.255.255.0.

• MTU: The maximum transmission unit size, which is how big a single packet of data can be before the system needs to split it into multiple packets.

• RX: Details about the received network traffic, including the number of errors and dropped packets recorded. 

• TX: Details about the transmitted packets. (errors recorded here or at RX, indicate a problem with the network)

• collisions: two systems try to talk at the same time, which requires to retransmit their packets. Small numbers aren’t a problem, but large number indicates a network issue.

• txqueuelen: The length of the transmission queue, which will usually be set to 1000 and rarely needs changing.

• RX bytes, TX bytes: The cumulative amount of traffic the network interface has received and transmitted.

==========================================

routing

Routing table can be checked by netstat command:

pi@raspberrypi:~ $ netstat -nr
Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
0.0.0.0         192.168.1.1     0.0.0.0         UG        0 0          0 eth0
192.168.1.0     0.0.0.0         255.255.255.0   U         0 0          0 eth0


Default gateway can be added:
route add default gw 192.168.1.1 netmask 255.255.255.0 eth0

==========================================

/etc/network/interfaces      network interface configuration
/etc/hostname                contains the hostname (after modification, kernel should be updated)
/proc/sys/kernel/hostname    it shows the actual value of hostname (what is known to the kernel)
/etc/hosts                   each line contains an IP address, the full name and optionally the alias belonging to that IP address
/etc/nsswitch.conf           contains the order to resolve IP and hostname (files dns means, first check /etc/hosts file after DNS)
/etc/resolv.conf             contains the DNS servers


ifconfig                     shows network interfaces with IP addresses and other info
netstat -tupan               shows network activity
                             (t:tcp connections, u:udp conn., p:list processes, a:list listening sockets, n:no dns resol.)

sudo ifdown eth0             turns off network device (eth0) 
sudo ifup eth0               turns on network device (eth0)

==========================================

Manual IP configuration:
(with static IP)

In /etc/network/interfaces this is written:
# Please note that this file is written to be used with dhcpcd
# For static IP, consult /etc/dhcpcd.conf and 'man dhcpcd.conf'


1. add this to the end of /etc/dhcpcd.conf
interface eth0
static ip_address=192.168.1.113/24
static routers=192.168.1.1
static domain_name_servers=192.168.1.1


2. restart network services
sudo /etc/init.d/networking restart  <--restart the networking service, it will read new settings
                                     (this is the same: sudo service networking restart)

==========================================

Setting up hostname 

Each machine can have several network cards, so one single computer can have several names in the domain name system. But to be able to identify a specific machine, a main name (also known as hostname) is stored in the /etc/hostname file. The modifications in this file, are not automatically sent to the kernel, the kernel is updated by the hostname command.

Configuring hostname (boot persistent):

1. edit /etc/hosts                         <--make sure the required hostname exists there (with correct IP)
2. edit /etc/hostname                      <--write hostname there (during boot hostname will be read from this file)
3. hostname <hostname>                     <--it updates kernel with new hostname
4. cat /proc/sys/kernel/hostname           <--get the actual current value for hostname (this is known to the kernel)

No comments:

Post a Comment