Powered By GitBook
109.2. Basic network configuration
Weight: 4
Description: Candidates should be able to view, change and verify configuration settings on client hosts.
Key Knowledge Areas:
    Manually and automatically configure network interfaces
    Basic TCP/IP host configuration
    Setting a default route
Terms and Utilities:
    /etc/hostname
    /etc/hosts
    /etc/nsswitch.conf
    ifconfig
    ifup
    ifdown
    ip
    route
    ping
In this tutorial, we learn hat make network configurations persistent in linux.

ifconfig

ifconfig ( interface configuration) utility is used to configure, or view the configuration of, a network interface via command line interface(CentOS6).
1
ifconfig [...OPTIONS] [INTERFACE]
Copied!
The “ifconfig” command with no option, displays current network configuration information:
2
[[email protected] ~]# ifconfig
3
eth0 Link encap:Ethernet HWaddr 00:0C:29:6D:D2:C5
4
inet addr:172.16.43.137 Bcast:172.16.43.255 Mask:255.255.255.0
5
inet6 addr: fe80::20c:29ff:fe6d:d2c5/64 Scope:Link
6
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
7
RX packets:235444 errors:0 dropped:0 overruns:0 frame:0
8
TX packets:81562 errors:0 dropped:0 overruns:0 carrier:0
9
collisions:0 txqueuelen:1000
10
RX bytes:309940884 (295.5 MiB) TX bytes:5027974 (4.7 MiB)
11
12
lo Link encap:Local Loopback
13
inet addr:127.0.0.1 Mask:255.0.0.0
14
inet6 addr: ::1/128 Scope:Host
15
UP LOOPBACK RUNNING MTU:65536 Metric:1
16
RX packets:2964 errors:0 dropped:0 overruns:0 frame:0
17
TX packets:2964 errors:0 dropped:0 overruns:0 carrier:0
18
collisions:0 txqueuelen:0
19
RX bytes:386271 (377.2 KiB) TX bytes:386271 (377.2 KiB)
Copied!
the lo is the loopback adapter , with ip address 127.0.0.1 which is used by operating system for its own internal communications.
ifconfig option
description
-a
display all the interfaces available, even if they are down.
-s
display a short list (like netstat -i)
-v
be more verbose for some error conditions
We can use ifconfig to change network configurations, but this will require root access:
ifconfig command
description
ifconfig eth0 172.16.43.155
Assign a IP Address
ifconfig eth0 netmask 255.255.255.224
Assign a Netmask
ifconfig eth0 172.16.43.155 netmask 255.255.255.224
Assign a IP, Netmask
ifconfig can be used to turn an interface up and down ,again this will need root access:
    ifconfig interface up: used to activate the driver for the given interface
    ifconfig interface down: used to deactivate the driver for the given interface
If you're still using ifconfig, you're living in the past! ifconfig command is deprecated and replaced by ip command.

ifup , ifdown

Same as previous commands, ifup and ifdown are used to enable and disable an interface.
1
[[email protected] ~]# ifdown eth0
2
Device state: 3 (disconnected)
3
[[email protected] ~]# ifup eth0
4
Active connection state: activating
5
Active connection path: /org/freedesktop/NetworkManager/ActiveConnection/10
6
state: activated
7
Connection activated
Copied!

gateway

One benefit of chopping ip addresses into classes and subnets is controlling broad casts.This happens using subnetmask. But what is two compuyers from two different networks wants to communicate with each other?
A gateway is a node or a router that acts as an access point to passes network data from local networks to remote networks.
the address which is used as a gateway to reach to other networks outside our local network is called default gateway. There are different ways to set gateway.

Network Configuration files

The bad news is that all configurations that we have done using ifconfig command are not persistent and got vanished whe system restarts ot the interface turns up and down. So we should find out a way to make our setting persistent and the only way we can achieve that by using network configuration files.
Unfortunately the network configuration files in linux are placed in different places and it depend on the distribution which we are talking about.

RedHat Based systems

In Redhat , CentOS, and fedora the files are located at /etc/sysconfig/network-scripts/ .
1
[[email protected] ~]# ls /etc/sysconfig/network-scripts/
2
ifcfg-eth0 ifdown-ippp ifdown-sit ifup-ib ifup-post init.ipv6-global
3
ifcfg-lo ifdown-ipv6 ifdown-tunnel ifup-ippp ifup-ppp net.hotplug
4
ifdown ifdown-isdn ifup ifup-ipv6 ifup-routes network-functions
5
ifdown-bnep ifdown-post ifup-aliases ifup-isdn ifup-sit network-functions-ipv6
6
ifdown-eth ifdown-ppp ifup-bnep ifup-plip ifup-tunnel
7
ifdown-ib ifdown-routes ifup-eth ifup-plusb ifup-wireless
8
9
Copied!
and the default gateway is configured via file: /etc/sysconfig/network
1
[[email protected] Desktop]# cat /etc/sysconfig/network
2
NETWORKING=yes
3
HOSTNAME=server1
4
GATEWAY=172.16.43.2
Copied!
lets take a look at eth0 configuration file:
1
[[email protected] ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0
2
DEVICE="eth0"
3
BOOTPROTO=none
4
IPV6INIT="yes"
5
NM_CONTROLLED="yes"
6
ONBOOT="yes"
7
TYPE="Ethernet"
8
UUID="8f5774e8-607e-4629-8d54-a9cc4d12e851"
9
IPADDR=172.16.43.127
10
PREFIX=24
11
GATEWAY=172.16.43.2
12
DEFROUTE=yes
13
IPV4_FAILURE_FATAL=yes
14
IPV6_AUTOCONF=yes
15
IPV6_DEFROUTE=yes
16
IPV6_FAILURE_FATAL=no
17
NAME="System eth0"
18
HWADDR=00:0C:29:6D:D2:C5
19
DNS1=8.8.8.8
20
IPV6_PEERDNS=yes
21
IPV6_PEERROUTES=yes
22
LAST_CONNECT=1582381005
Copied!

Debian Based Systems

In debian based systems line Ubuntu , ... the main network configuration file location is /etc/network/interfaces and there is no separated file for gateway configuration :
1
auto lo
2
iface lo inte loopback
3
4
auto eth0
5
#ifconfig eth0 inet dhcp
6
iface eth0 inet static
7
address 172.16.43.135
8
netmask 255.255.255.0
9
gateway 172.16.43.2
10
dns-nameservers 8.8.8.8
Copied!
there might be /etc/network/interfaces.d directory for configuration files.
ifdown and ifup commands use this configuration file.

DNS configuration file

/etc/resolv.conf:

As you have noticed DNS configuration is in the same file that interface configuration is located but there is another place in linux which contains DNS information /etc/resolv.conf :
1
[[email protected] Desktop]# cat /etc/resolv.conf
2
# Generated by NetworkManager
3
nameserver 8.8.8.8
Copied!
Again setting in this file are not permanent and it is not recommended to change this file by hand , except for temporary tests.

hostname

Hostname is the program that is used to either set or display the current host, domain or node name of the system (we are usiong centOS).
1
[[email protected] Desktop]# hostname
2
server1
3
[[email protected] Desktop]# hostname centos6-1
4
[[email protected] Desktop]# hostname
5
centos6-1
Copied!
The new hostname will appear if you open a new terminal but get vanished if you restart the system. To configure hostname permanently there are a couple of other places which should be changed
    1.
    /etc/hostname (Ubuntu) OR /etc/sysconfig/network (CentOS)
    2.
    /etc/hosts (both Ubuntu , CentOS)

/etc/hostname

/etc/hostname contains name of the machine and is one of the configuration files that should be modified in order to make a new hostname persistent in Debian based systems.(ubuntu16 here)
1
[email protected]:~# cat /etc/hostname
2
ubuntu16-1
Copied!

/etc/sysconfig/network

Another place which contains hostname and should be changed in RedHat based systems to have persistent hostname(CentOS6)
1
[[email protected] Desktop]# cat /etc/sysconfig/network
2
NETWORKING=yes
3
HOSTNAME=server1 ###<---- change it to centos6-1 in our ex
4
GATEWAY=172.16.43.2
Copied!

/etc/hosts

The /etc/hosts is an operating system file that translate hostnames or domain names to IP addresses , it do the same thing that DNS do. We can using for testing purposes or when DNS server is absent. Do not forget that it has a highr priority than DNS, (means that operating system first look inside /etc/hosts file to gain the ip address of a host, if it wasn't successful then it would query DNS Server).
1
[[email protected] Desktop]# cat /etc/hosts
2
127.0.0.1 centos6-1 localhost localhost.localdomain localhost4 localhost4.localdomain4
3
::1 server1 localhost localhost.localdomain localhost6 localhost6.localdomain6
4
Copied!
For making new hostname permanent it is another file which should be modified.

route

All network devices, whether they are hosts, routers, or other types of network nodes such as network attached printers, need to make decisions about where to route TCP/IP data packets. The routing table provides the configuration information required to make those decisions. the route command is used to view and make changes to the kernel routing table.
route command make temporary setting, use config files instead!
Running route command without any options displays the routing table entries:
1
[[email protected] Desktop]# route
2
Kernel IP routing table
3
Destination Gateway Genmask Flags Metric Ref Use Iface
4
172.16.43.0 * 255.255.255.0 U 1 0 0 eth0
5
default 172.16.43.2 0.0.0.0 UG 0 0 0 eth0
Copied!
This shows us how the system is currently configured. If a packet comes into the system and has a destination in the range 172.16.43.0 through 172.16.43.255, then it is forwarded to the gateway *, which is 0.0.0.0 — a special address which represents an invalid or non-existant destination. So, in this case, our system will not route these packets.
If the destination is not in this IP address range, it is forwarded to the default gateway (in this case, 172.16.43.2, and that system will determine how to forward the traffic on to the next step towards its destination.
By default route command displays the host name in its output. We can request it to display the numerical IP address using -n option:
1
[[email protected] Desktop]# route -n
2
Kernel IP routing table
3
Destination Gateway Genmask Flags Metric Ref Use Iface
4
172.16.43.0 0.0.0.0 255.255.255.0 U 1 0 0 eth0
5
0.0.0.0 172.16.43.2 0.0.0.0 UG 0 0 0 eth0
Copied!
The following route add command will set the default gateway as 172.16.43.2:
1
[[email protected] ~]# route add default gw 172.16.43.1
2
3
Kernel IP routing table
4
Destination Gateway Genmask Flags Metric Ref Use Iface
5
172.16.43.0 * 255.255.255.0 U 0 0 0 eth0
6
link-local * 255.255.0.0 U 1002 0 0 eth0
7
default 172.16.43.1 0.0.0.0 UG 0 0 0 eth0
8
default 172.16.43.2 0.0.0.0 UG 0 0 0 eth0
Copied!
use route del for deleting:
1
[[email protected] ~]# route del default gw 172.16.43.1
2
3
Kernel IP routing table
4
Destination Gateway Genmask Flags Metric Ref Use Iface
5
172.16.43.0 * 255.255.255.0 U 0 0 0 eth0
6
link-local * 255.255.0.0 U 1002 0 0 eth0
7
default 172.16.43.2 0.0.0.0 UG 0 0 0 eth0
Copied!
Kernel maintains the routing cache information to route the packets faster. We can list the kernel’s routing cache information by using the -C flag.
netstat -rn also shows routing table.

ip

This command replaces old good and now deprecated ifconfig command, however, ifconfig command is still works and available for most of the Linux distributions.
It can be used to assign and remove addresses , bring interfaces up or down, manipulate routing, and many more things.
1
ip [ OPTIONS ] OBJECT { COMMAND | help }
Copied!
ip command example
description
ip address show
show all IP addresses associated on all network devices
ip address show eth0
view the information of any particular interface
ip addr add 192.168.50.5/24 dev eth0
Assign a IP Address to Specific Interface
ip addr del 192.168.50.5/24 dev eth0
Remove an IP Address
ip link show
Display Network Interface(s)
ip link set eth0 up
Enable Network Interface
ip link set eth0 down
Disable Network Interface
ip route show
Show routing table information
ip route add 10.10.20.0/24 via 192.168.50.100 dev eth0
Add static route
ip route del 10.10.20.0/24
Remove static route
All the above settings will be lost after a system restart. use config files instead.

ping

The ping command is one of the most used utilities for troubleshooting, testing, and diagnosing network connectivity issues.
Ping works by sending one or more ICMP (Internet Control Message Protocol) Echo Request packages to a specified destination IP on the network and waits for a reply. When the destination receives the package, it will respond back with an ICMP echo reply.
1
[[email protected] ~]# ping 8.8.8.8 -c3
2
PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
3
64 bytes from 8.8.8.8: icmp_seq=1 ttl=128 time=45.0 ms
4
64 bytes from 8.8.8.8: icmp_seq=2 ttl=128 time=47.0 ms
5
64 bytes from 8.8.8.8: icmp_seq=3 ttl=128 time=40.2 ms
6
7
--- 8.8.8.8 ping statistics ---
8
3 packets transmitted, 3 received, 0% packet loss, time 2043ms
9
rtt min/avg/max/mdev = 40.262/44.107/47.040/2.851 ms
Copied!
With the ping command, we can determine whether a remote destination IP is active or inactive. You can also find the round-trip delay in communicating with the destination and check whether there is a packet loss.
ping command switch
description
-n
Numeric output only.do not try to resolve hostname
-i interval
Wait interval seconds between sending each packet
-I interface
Set source address to specified interface address
-a
Audible ping
for ipv6 environment use ping6 command.

/etc/nsswitch

This file determines where the system finds things like host names, passwords, and protocol numbers:
Here’s a snippet from a sample /etc/nsswitch.conf file
1
passwd: files nis
2
group: files nis
3
4
hosts: files dns myhostname
Copied!
In this example, user information (the passwd and group services) come first from “files” (like /etc/passwd or /etc/group), and if no entries are found there, a query to an NIS server (configured elsewhere) will be used.
Host information first comes from /etc/hosts (files), then a DNS server (dns), and if neither of those work, at least a fallback of “myhostname” so that the local machine has some name.
The non-complexity comes in the “and if that doesn’t work” rule. When multiple services are listed, they’re tried in order, and a sevice either succeeds or fails. If it fails, the next is tried, etc.
that's all.
.
.
.

Consistent network device naming

Red Hat Enterprise Linux provides methods for consistent and predictable network device naming for network interfaces. These features change the name of network interfaces on a system in order to make locating and differentiating the interfaces easier.
Traditionally, network interfaces in Linux are enumerated as eth[0123…], but these names do not necessarily correspond to actual labels on the chassis. Modern server platforms with multiple network adapters can encounter non-deterministic and counter-intuitive naming of these interfaces. This affects both network adapters embedded on the motherboard (Lan-on-Motherboard, or LOM) and add-in (single and multiport) adapters.
In Red Hat Enterprise Linux, udev supports a number of different naming schemes. The default is to assign fixed names based on firmware, topology, and location information. This has the advantage that the names are fully automatic, fully predictable, that they stay fixed even if hardware is added or removed (no re-enumeration takes place), and that broken hardware can be replaced seamlessly. The disadvantage is that they are sometimes harder to read than the eth0 or wlan0 names traditionally used. For example: enp5s0.
    for disabling that (how ever it is not recommanded Add both net.ifnames=0 and biosdevname=0 as kernel parameter values to the GRUB_CMDLINE_LINUX variable )
.
.
.
.
Last modified 1yr ago