5. Networking

5.1. Linux networking

  • Overview of network protocols and TCP/IP in Linux.

  • Network configuration in Linux.

  • Netplan on Ubuntu servers.

  • Network settings in Red Hat.

  • Network applications and services.

  • Network calculation with ipcalc

5.2. Computers on the intertnet

  • Network communication allows sending information, transfering files, and managing applications remotely between computers.

  • Networks are organized in a series of layers implemented on each machine.

  • The layers are independent and each layer offers a specific task to the neighboring layers above and below.

  • The communication between the layers is based on defined rules and procedures, called protocols.

  • Data is processed and passed through the layers from above to below, until the bottom layer is reached.

  • At the bottom layer, computers communicate with each other via sending and receiving network datagrams through - cables, switches and routers.

5.3. TCP/IP 5 layer protocol suit

Each layer has its own task:

TCP/IP Protocol Layer


5. Application Layer: ssh, ftp, http …

Data segmentation by the application into Message Transfer Unit (MTU) segments. Typical MTU is 1.5 Kbyte

4. Transport Layer: TCP, UDP, ports

Connection management, quality, and ports to the applications

3. Internet Layer: IP addresses, subnets, routing

Addressing: each computer has a unique address on its network or subnet. Routing between networks.

2. Link Layer: Ethernet

Communication on the local network (subnet) through a switch. Media Access Card (MAC) address unique on the subnet. For example, 48:d2:4f:f4:d:fb, should be unique within a subnet. Address Resolution Protocol (ARP) matches the IP address to MAC on the subnet.

1. Physical Layer: NIC, hubs, switches, routers, cabling, wifi APs and cards

Electronic devices to send and receive signals between computers.

5.4. OSI 7 layer Network Stack Model

TCP/IP Protocol Layer


7. Application Layer: ssh, ftp, http …

Data segmentation by the application into Message Transfer Unit (MTU) segments. Typical MTU is 1.5 Kbyte

6. Presentation Layer: SSL, libz, XDR

Data encryption, compression, and encoding/decoding

5. Session Layer: Remote Procedure Call (RPC)

Responsible for opening, using and closing sessions. Remote procedures.

5.5. TCP/IP and Kernel

The Link, Internet and Transport layers are implemented through kernel.

5.6. Packet Encapsulation

Data is processed in form of packet encapsulation and transmitted/received in form of Ethernet frames.

5.7. Hosts communication via TCP/IP

5.8. Wired connection checkup (Exercise)

  • To see whether your desktop is connected to a network switch and at what speed, use command ethtool.

  • On your desktop, install package ethtool:

apt install ethtool
  • See your network interfaces with command ifconfig or ip a:

ifconfig -a


ip a

The main network interface, connected to the Ethernet, is eno1. Run command below:

ethtool eno1

In the output, among various parameters, it shows

  • Speed: 1000Mb/s,

  • Duplex: Full,

  • Link detected: yes

Which means the cable is connected and the link speed is 1Gb/s speed, full duplex mode.

5.9. Network IP configuration

  • Static configuration

    • IP address, gateway, and Dynamic Name Service (DNS) are configured on the host.

  • Dynamic Host Configuration Protocol (DHCP)

    • The settings are obtained from a DHCP server.

5.10. Environment for IP configuration on Ubuntu and RedHat

ifupdown scripts

Network Manager service

Netplan networkd service Meaning

Old Debian/Ubuntu:

Ubuntu desktop, Debian:

Ubuntu server:

File: /etc/network/interfaces

Commands: nmtui, nmcli




Command: netplan apply

File: /etc/sysconfig/network-scripts/ifcfg-eth0

File: /etc/sysconfig/network-scripts/ifcfg-eth0

Command:ifup eth0

Command: ifup eth0

5.11. Deploy a new VM for exercises

Shutdown kvm1 VM, and clone it into netplan VM:

virsh shutdown kvm1
virt-clone  -o kvm1 -n netplan -f /home/hostadm/KVM/netplan.qcow2

Start netplan. Login to console of netplan:

virsh console netplan

Fix the hostname in file /etc/hostname. Reset the machine ID by running the following commands on netplan:

rm -f /etc/machine-id
rm /var/lib/dbus/machine-id
dbus-uuidgen --ensure=/etc/machine-id
dbus-uuidgen --ensure

Clear the DHCP leased IP settings:

dhclient -r ens3

Execute command reboot on netplan:


5.12. Configuring network settings on Ubuntu server with Netplan (Exercises)

On netplan VM check the IP address settings by running command below:

ip addr

Assign IP address by modifying the content of /etc/netplan/01-netcfg.yaml as follows:

  version: 2
  renderer: networkd
     dhcp4: no
     addresses: []
       addresses: []

Have the settings applied:

netplan apply

Check the IP address again:

ip addr

It should show for ens3 interface now.

See the gateway settings by running command

ip route show

It should show the “default via”

5.13. Network configurations on a Red Hat server (Exercises)

Start CentOS7 VMs:

virsh start CentOS7

Login to console of CentOS7:

virsh console CentOS7

Execute command ip address show to read the IP address:

ip addr show eth0

Try pinging netplan by its IP address, for example:

ping -c 3

Become root on CentOS7 VM

sudo -s

Reconfigure eth0 network interface on CentOS7:

ifconfig eth0 netmask up

Verify the network settings:

ifconfig eth0

Ping netplan by the IP address:

ping -c 3

Reset the network setting to the default on CentOS7 VMs:

ifdown eth0
ifup eth0

Check the updated network settings on the VM:

ifconfig eth0

Static IP configuration on CentOS7 VMs. On CentOS7, edit file /etc/sysconfig/network-scripts/ifcfg-eth0 and put the following settings for the static IP configuration there:


Apply the network settings:

ifdown eth0
ifup eth0

Ping netplan by the IP address:

ping -c 3

Restore the interface settings to DHCP on CentOS7 VMs. On CentOS7, edit file /etc/sysconfig/network-scripts/ifcfg-eth0 and put the following settings for DHCP configuration:


Reset the network settings:

ifdown eth0
ifup eth0

Verify the network settings:

ifconfig eth0

The interface should acquire some IP address on network.

5.14. Internet Control Message Protocol (ICMP) protocol (Exercises)

Implemented on hosts and gateways (routers) for:

Reporting the status of datagram processing. Diagnostics of connection and routing. Reporting errors in the processing of a datagram.

Example: ping

Exercise: command ping.

ping -c 5 capone
ping -c 5 engsoft.rutgers.edu
ping -c 5 google.com

If the systems respond, it means they are reachable on the network. Notice how varies the round-trip time and Time to Live (ttl) for packets between the hosts.

Example: traceroute

Time to leave (TTL) decrement. The TTL value of an IP packet represents the maximum number of IP routers that the packet can go through before being thrown away. You can expect each router in the Internet to decrement the TTL field by exactly one. The default TTL for traceroute on Linux is 30. It can be increased up to 255 maximum by using the -m command option.

Exercise: command traceroute.

sudo /usr/sbin/traceroute -I capone
sudo /usr/sbin/traceroute -I engsoft.rutgers.edu
sudo /usr/sbin/traceroute -I google.com

This shows you all the gateways between the subnets your packet travels towards the destination. Notice the difference in the number of gateways between your desktop and the three remote hosts as well as the round trip times.

5.15. Network services (Exercises)

Make sure virtual machine netplan is running

virsh list

Figure out the IP address of netplan:

virsh domifaddr netplan

Place the IP address and netplan host name into file /etc/hosts on your desktop. netplan IP is

nano /etc/hosts

Make entry in the file:   netplan

ssh to netplan as user hostadm:

ssh hostadm@netplan

Configure SSH for private/public key authentication. For SSH authentication, you can use either RSA or DSA public/private keys besides password. We’ll be using RSA in the exercises below. To generate an RSA key pair, type the following command at a shell prompt on your desktop:

ssh-keygen -t rsa

Accept the default file location of ~/.ssh/id_rsa. Enter a passphrase different from your account password and confirm it by entering it again. The public key is written to ~/.ssh/id_rsa.pub. The private key is written to ~/.ssh/id_rsa. Never distribute your private key to anyone. The contents of ~/.ssh/id_rsa.pub needs to be delivered onto the remote machine to which you want to connect, specifically netplan, into file ~/.ssh/authorized_keys To accomplish the transfer task, here you can use sftp service that comes with ssh.

sftp  netplan
Name (netplan:hostadm): hostadm
sftp> cd .ssh
sftp> lcd .ssh
sftp> put id_rsa.pub authorized_keys
sftp>  quit

Command cd in the sftp> shell above is for stepping into the directory, .ssh, on the remote host, netplan. Command lcd is for stepping into the directory, .ssh, on the local desktop.

Now try to ssh to netplan. You should be prompted to enter your passphrase.

The ssh-agent can be used to store your passphrase so that you do not have to enter it each time you make a ssh or scp connection. At a shell prompt on the desktop, type the following command:

exec /usr/bin/ssh-agent $SHELL

Then type the command:


and enter your passphrase(s). If you have more than one key pair configured, you will be prompted for each one. When you log out, your passphrase(s) will be forgotten. You must execute these two commands each time you log in to a virtual console or open a terminal window.

Run a remote command over ssh, for example:

ssh netplan "uname -a"

Copy files from your desktop to netplan and vise versa using scp command:

scp netplan:/etc/hosts .
touch somef.txt
scp somef.txt netplan:/home/$USER

Syncronizing directories between remote hosts by using rsync. This tool lets you copy files and directories between a local host and a remote host. Install rsync on both your desktop and netplan:

apt-get install rsync

Creat a directory tree and copy it over to netplan with rsync command.

mkdir -p dir1/dir2/dir3
rsync  -avz dir1 netplan:/home/$USER

Option a stands for archive (preserve links and timestamps); v is for verbose and z is for data compression when sending-receiving.

5.16. Send/receive data with netcat (Exercises)

Netcat is a very useful tool to connect to any TCP and UDP port on a remote host, and send/receive data.

Start both, netplan and CentOS7 VMs:

virsh start netplan
virsh start CentOS7

Login to console of netplan:

virsh console netplan

Bring up another command prompt on the desktop and login to CentOS7

virsh console CentOS7

On each VM, execute command ip addr show to read the IP address:

ip addr show eth0

Become root on both the VMs

sudo -s


On Ubuntu (server)

On CentOS7


Install netcat: apt-get install netcat

yum install nmap-ncat


Start netcat as a server, listening on port UDP/8080:

Connect to UDP/8080 port on netplan,

netcat -l -u 8080

ncat -u 8080


When the session is over, type Ctrl-C to stop the netcat server.

Start typing text. Hit ENTER key. The text should appear on the both terminals. Press Ctrl+D to close the UDP connection.


Start the server on TCP/8080 to write into a file, outputfile.txt

Send file /etc/hosts to TCP/8080 on netplan VM via netcat

netcat -l 8080 > outputfile.txt

ncat  8080 < /etc/hosts


After the file is received, the connection closes itself. Check the content of the file:

less outputfile.txt

5.17. Subnets and routing

Example: Host A can communicate with both the hosts - B and C. Hosts B and C can’t communicate with each other. What is the problem?
Answer: the routing (gateway) is not defined on Host B.

5.18. Network calculation, gateway and routing

  • Computers connected to the same switch are on the same network/subnet and communicate with each other directly through the switch.

  • For computers located on the different networks/subnets, gateway/router is needed.

  • To define the local subnet, netmask is used.

  • Subnet mask defines the network and the host parts of the IP address

Network address = Host address (logical AND) Netmask:




IP address

11000000 10010100 00000101 00001010


11111111 11111111 11111111 00000000

Network address

11000000 10010100 00000101 00000000

  • The network address is the smallest address on the network:

11000000 10010100 00000101 00000000 =

  • The broadcast address is the largest address on the network:

11000000 10010100 00000101 11111111 =

  • Max number of hosts on the subnet: 254 = 256 - 2 The gateway address should be on the same subnet with the host

5.19. IP calculator exercises

Network IP calculations with ipcalc On the desktop, install ipcalc by using APT:

apt-get install ipcalc

Run ipcalc for network address with subnet mask


See the output for Address, Netmask, Network, HostMin, HostMax, Broadcast, Hosts/Net. Notice the same results for the same network and the different representation of the netmask:


Notice the same results for Netmask, Network, HostMin, HostMax, Broadcast, Hosts/Net if using the different IP addresses within the same network in ipcalc, for example:


Run ipcalc for subnets (networks) and


Notice the values for HostMin and HostMax in both the cases. By looking at the ranges [HostMin, HostMax], you can see, for example, that IP address belongs to the first subnet and to the second. You can verify that by running ipcalc on the IP addresses above and then comparing the Network values: