Configuring Basic Networking

LPIC-1 [ Yaser Rahmati | یاسر رحمتی ]

Computer networking

  • Computer networking is how we get data from one computer system to another.

  • To help simplify things, computer networks are often described as layered systems.

  • Different layers play different roles in the process of getting the data from one network device to another.

  • Network models :

    1. OSI

    2. TCP/IP

1. Physical

The physical layer consists of the hardware required to connect your Linux system to the network.

A.Wired network connections

  • Copper cables

  • Fiber-optic cables

    • Faster speed

    • Longer distance

B.Wireless networking

  • Small office and home network

  • Using radio signals to transmit the data between the network device and network access point

2. Network

  • Controls how data is sent between connected network device both in :

    • local network

    • across Internet

  • To connect a Linux system to an IP network you will need 4 pieces of information :

    1. IP

    2. Hostname

    3. Default Router

    4. Netmask

3. IP

  • In an IP network, each network device is assigned a unique 32-bit address.

  • Dotted-decimal notation :

    • IP addresses are split into four 8-bit values, represented by decimal numbers, with a period between each value.

  • IP addresses are split into 2 sections :

    1. network address

      • All devices on the same physical network have the same network address.

    2. host address

      • Each device on the same network must have a unique host address.

  • Specific subnetwork ranges are reserved for private IP networks :

Class

First IP

Last IP

A

10.0.0.0

10.255.255.255

B

172.16.0.0

172.31.255.255

C

192.168.0.0

192.168.255.255

4. Hostname

  • With all of these IP addresses, it can be impossible trying to remember just what servers have what addresses.

  • The Domain Name Server (DNS) assigns a name to hosts on the network

  • With DNS, each network address is assigned a domain name (such as linux.org) and each host in the network is assigned a unique host name

    • HOST.Domain : shadrach.example.org

5. Default router

  • To connect different physical networks together you can use a router

  • Usually, a network will contain a single router to forward packets to an upper-level network. This is called default router or default gateway.

  • Example :

Item

IP

Host Address

192.168.20.5

Netmask

255.255.255.0

Default Gateway

192.168.20.1

6. Netmask value

  • Distinguishes between the network and host address portions

  • Using 1 bit to show which bits of the 32-bit IP address are used by the network and 0 bits to show which bits represent the host address

Class

Default Subnet Mask

Network Bits

A

255.0.0.0

8

B

255.255.0.0

16

C

255.255.255.0

24

7. DHCP

  • Dynamic Host Configuration Protocol

  • With DHCP, the client communicates with a DHCP server on the network using a temporary address

  • Each time the client reboots, it may receive a different IP address

  • The DHCP server can tells the client :

    1. IP address

    2. Netmask address

    3. Default gateway

    4. DNS server

8. Transport

  • The transport layer helps get the data to the correct application contained on the host by using ports

  • Two common transport protocols are used in the IP networking world :

    1. Transmission Control Protocol (TCP)

    2. User Datagram Protocol (UDP)

TCP

UDP

Secure

Insecure

Connection-oriented

Connection-less

Slow

Fast

Guaranteed transmission

Not guarantee

Used by critical application

Used by real-time application

Flow control

No flow control

20 bits header

8 bits header

Acknowledgment mechanism

None acknowledgment

3-way handshake (SYN, SYN_ACK, ACK)

No handshake

9. Application

  • This is where the network programs process the data sent across the network and then return a result

  • Most network applications behave using the client/server paradigm

Network Configuration Files

  • /etc/hosts

  • /etc/resolv.conf

  • /etc/sysconfig/network

  • /etc/sysconfig/network-scripts/ifcfg-interface-name

1. Configuring Network Features

There are 5 pieces of information you need to configure in Linux system :

  1. The host address

  2. The network subnet address

  3. The default router

  4. The system host name

  5. A DNS server address for resolving host names

2. Three Ways of Configuration

  1. Manually editing network configuration files

  2. Using a graphical tool included with your Linux distribution

  3. Using command-line tools

Manually Editing Network Configuration Files

Different distribution uses different configuration files to define the network settings :

Distribution

File / Directory

Debian-based

/etc/network/interfaces file

Red Hat-based

/etc/sysconfig/network-scripts directory

OpenSUSE

/etc/sysconfig/network file

Assumption : Red Hat-based Distribution

1. Manually editing network configuration files

The file name format of the network interface configuration file is /etc/sysconfig/network-scripts/ifcfg-#.


[root@localhost ~]# ls /etc/sysconfig/network-scripts/
ifcfg-ens33      ifdown-post      ifup-bnep   ifup-routes
ifcfg-ens33.bak  ifdown-ppp       ifup-eth    ifup-sit
ifcfg-lo         ifdown-routes    ifup-ippp   ifup-Team
ifdown           ifdown-sit       ifup-ipv6   ifup-TeamPort
ifdown-bnep      ifdown-Team      ifup-isdn   ifup-tunnel
ifdown-eth       ifdown-TeamPort  ifup-plip   ifup-wireless
ifdown-ippp      ifdown-tunnel    ifup-plusb  init.ipv6-global
ifdown-ipv6      ifup             ifup-post   network-functions
ifdown-isdn      ifup-aliases     ifup-ppp    network-functions-ipv6

You'll need to define the network settings in 2 separate files :

  • First. The first file defines the network and netmask addresses in a file named after the network interface name (such as ens33)

[root@localhost ~]# cd /etc/sysconfig/network-scripts/
[root@localhost network-scripts]# vi ifcfg-ens33
TYPE="Ethernet"
BOOTPROTO="none"
DEFROUTE="yes"
IPV4_FAILURE_FATAL="no"
IPV6INIT="yes"
IPV6_AUTOCONF="yes"
IPV6_DEFROUTE="yes"
IPV6_FAILURE_FATAL="no"
NAME="ens33"
UUID="e8a1ed0f-050f-4fea-98c4-18baca5c3ddf"
DEVICE="ens33"
ONBOOT="yes"
IPADDR="192.168.1.141"
PREFIX="24"
GATEWAY="192.168.1.1"
DNS1="8.8.8.8"
IPV6_PEERDNS="yes"
IPV6_PEERROUTES="yes"
IPV6_PRIVACY="no"

TYPE=device_type: The type of network interface device

BOOTPROTO=protocol: Where protocol is one of the following:

  • none: No boot-time protocol is used.

  • bootp: Use BOOTP (bootstrap protocol).

  • dhcp: Use DHCP (Dynamic Host Configuration Protocol).

ONBOOT=answer: Where answer is one of the following:

  • yes: This interface is activated at boot time.

  • : This interface is not activated at boot time.

HWADDR=MAC-address: The hardware address of the Ethernet device

IPADDRN=address: The IPv4 address assigned to the interface

PREFIXN=N: Length of the IPv4 netmask value

GATEWAYN=address: The IPv4 gateway address assigned to the interface. Because an interface can be associated with several combinations of IP address, network mask prefix length, and gateway address, these are numbered starting from 0.

DNSN=address: The address of the Domain Name Servers (DNS)

DOMAIN=DNS_search_domain: The DNS search domain

  • Second. The second file required is the network file, which defines the host name and default gateway.

To permanently change the static host name, change the HOSTNAME directive in the /etc/sysconfig/network file. For example:

vi /etc/sysconfig/network
NETWORKING=yes
HOSTNAME=lpictest
GATEWAY=192.168.1.1

Graphical Tool

For this option, you must have Desktop installed on your CentOS system. Now, login to your CentOS Desktop system and open settings windows on your machine as showing in the below screen.

In the left sidebar click on the Network tab. After that click icon to open setting for your systems network interface as shown in the below screen.

Here is the IP address of your desktop system.

Using command-line tools

  1. NetworkManager Tools

  2. Legacy Tools

NetworkManager Tools

Application or Tool

Description

nmcli

A command-line tool which enables users and scripts to interact with NetworkManager. Note that nmcli can be used on systems without a GUI such as servers to control all aspects of NetworkManager. It has the same functionality as GUI tools.

nmtui

A simple curses-based text user interface (TUI) for NetworkManager

1. Configure IP Networking With nmtui

  • The nmtui tool is used in a terminal window.

  • It is contained in the NetworkManager-tui package, but it is not installed along with NetworkManager by default.

  • To install NetworkManager-tui

yum install NetworkManager-tui
  • To verify that NetworkManager is running :

[root@localhost ~]# systemctl status NetworkManager
● NetworkManager.service - Network Manager
   Loaded: loaded (/usr/lib/systemd/system/NetworkManager.service; enabled; vendor preset: enabled)
   Active: active (running) since Wed 2020-04-08 21:32:42 +0430; 11min ago
     Docs: man:NetworkManager(8)
 Main PID: 4695 (NetworkManager)
   CGroup: /system.slice/NetworkManager.service
           ├─4695 /usr/sbin/NetworkManager --no-daemon
           └─4976 /sbin/dhclient -d -q -sf /usr/libexec/nm-dhcp-helper -pf /v...

Apr 08 21:43:33 localhost.localdomain dhclient[5613]: DHCPDISCOVER on ens37 t...
Apr 08 21:43:38 localhost.localdomain dhclient[5613]: DHCPDISCOVER on ens37 t...
Apr 08 21:43:48 localhost.localdomain dhclient[5613]: DHCPDISCOVER on ens37 t...
Apr 08 21:43:57 localhost.localdomain NetworkManager[4695]: <warn>  [15863660...
Apr 08 21:43:57 localhost.localdomain NetworkManager[4695]: <info>  [15863660...
Apr 08 21:43:57 localhost.localdomain NetworkManager[4695]: <info>  [15863660...
Apr 08 21:43:57 localhost.localdomain NetworkManager[4695]: <info>  [15863660...
Apr 08 21:43:57 localhost.localdomain NetworkManager[4695]: <info>  [15863660...
Apr 08 21:43:57 localhost.localdomain NetworkManager[4695]: <warn>  [15863660...
Apr 08 21:43:57 localhost.localdomain NetworkManager[4695]: <info>  [15863660...
Hint: Some lines were ellipsized, use -l to show in full.
[root@localhost ~]#
  • Start the nmtui tool:

[root@localhost ~]# nmtui
  • The text user interface appears.

  • Press enter after choosing ” Edit a connection”. Now you can see all network interfaces, choose one and click “Edit“.

DHCP configuration

  1. Choose “Automatic” in IPv4 CONFIGURATION.

  2. Choose Automatic Connect check box.

  3. Press OK and quit Network manager.

Now Restart network service by typing below command.

systemctl restart network

Static configuration

  1. Choose “Manual” in IPv4 CONFIGURATION.

  2. Add IP Address with Subnet , Gateway and DNS server .

  3. Choose Automatic Connect check box.

  4. Press OK and quit Network manager.

Now Restart network service by typing below command.

systemctl restart network

2. Legacy Tools

  1. ethtool

  2. ifconfig

  3. iwconfig

  4. route

  5. ip

A. ethtool

Displays Ethernet settings for a network interface

[root@localhost ~]# ethtool ens33
Settings for ens33:
        Current message level: 0x00000007 (7)
                               drv probe link
        Link detected: yes

B. ifconfig

  • The ifconfig command has been deprecated and thus missing by default on CentOS Linux.

[root@localhost ~]# ifconfig
-bash: /usr/sbin/ifconfig: No such file or directory
  • Using yum command install net-tools package:

[root@localhost ~]# yum install net-tools
  • The ifconfig command is now available.

[root@localhost ~]# ifconfig
  • Display information of all network interfaces

[root@localhost ~]# ifconfig -a
  • View network settings of specific interface

[root@localhost ~]# ifconfig ens33
  • How to enable an network interface

[root@localhost ~]# ifconfig ens33 up
OR
[root@localhost ~]# ifup ens33
  • How to assign a IP address to network interface

[root@localhost ~]# ifconfig ens33 172.16.25.125
  • How to assign a netmask to network interface

[root@localhost ~]# ifconfig ens33 netmask 255.255.255.224
  • How to assign a broadcast to network interface

[root@localhost ~]# ifconfig ens33 broadcast 172.16.25.63
  • How to assign a IP, netmask and broadcast to network interface

[root@localhost ~]# ifconfig ens33 172.16.25.125 netmask 255.255.255.224 broadcast 172.16.25.63
  • How to enable promiscuous mode

[root@localhost ~]# ifconfig ens33 promisc

What happens in normal mode, when a packet received by a network card, it verifies that the packet belongs to itself. If not, it drops the packet normally, but in the promiscuous mode is used to accept all the packets that flows through the network card.

  • How to disable promiscuous mode

[root@localhost ~]# ifconfig ens33 -promisc

C. route

The route command can show or change the routing system. As you saw on default gateway section, routing is responsible to send your packets to their correct destination. For checking the current route you can issue

$ route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         gateway         0.0.0.0         UG    600    0        0 wlp3s0
192.168.1.0     *               255.255.255.0   U     600    0        0 wlp3s0

and for temporary adding a default route, youc an do:

route add default gw 192.168.1.1

D. The iproute2 package

  • Most of the legacy command-line network tools have been replaced with the newer iproute2 package.

The main utility in the iproute2 package is ip command.

  • The ip command is more robust than the old ifconfig command.

  • Display the current network settings :

[root@centhos ~]# ip address show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
       
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UNKNOWN group default qlen 1000
    link/ether 00:0c:29:97:a5:ab brd ff:ff:ff:ff:ff:ff
    inet 192.168.136.136/24 brd 192.168.136.255 scope global noprefixroute dynamic ens33
       valid_lft 1628sec preferred_lft 1628sec
    inet6 fe80::515a:26c:40f:6050/64 scope link noprefixroute
       valid_lft forever preferred_lft forever
       
3: ens34: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UNKNOWN group default qlen 1000
    link/ether 00:0c:29:97:a5:b5 brd ff:ff:ff:ff:ff:ff
[root@centhos ~]#
  • Specify the host address and netmask values for the interface :

[root@centhos ~]# ip address add 172.16.11.10/24 dev ens34
[root@centhos ~]# ip add show

[ ... ]
       
3: ens34: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UNKNOWN group default qlen 1000
    link/ether 00:0c:29:97:a5:b5 brd ff:ff:ff:ff:ff:ff
    inet 172.16.11.10/24 scope global ens34
       valid_lft forever preferred_lft forever
[root@centhos ~]#
  • Set the default router for network interface

[root@centhos ~]# ip route add default via 172.16.11.1 dev ens34
  • Make the network interface active :

[root@centhos ~]# ip link set ens34 up
[root@centhos ~]# ip add show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000

[ ... ]

3: ens34: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UNKNOWN group default qlen 1000
    link/ether 00:0c:29:97:a5:b5 brd ff:ff:ff:ff:ff:ff
    inet 172.16.11.10/24 scope global ens34
       valid_lft forever preferred_lft forever
[root@centhos ~]#

E. DNS config file

As you saw, we were able to set the DNS configuration in network interface config files. But this is not the only way. There is another file which contains this data: /etc/resolv.conf.

[root@centhos ~]# cat /etc/resolv.conf
# Generated by NetworkManager
search localdomain kelaspar.ir
nameserver 192.168.136.2
nameserver 8.8.8.8
nameserver 217.218.127.127
[root@centhos ~]#

Website : www.yaser-rahmati.ir

Instagram : https://www.instagram.com/yaser.rahmati/

Linkedin : https://www.linkedin.com/in/yaserrahmati/

Last updated