Linux Networking

Enable Internet access on Linux VM

To enable internet follow the below steps :

  • Open the VirtualBox Manager and select the machine on the left pane.
    linux-network
  • Click the Setting button from the top.
  • Click Network in the left pane in the Settings window.
    linux-network2
  • Select the Bridge Adaptor from the Attached to the drop-down menu.
  • Log in to the virtual machine, become root, and reboot it with the reboot command.

To verify the internet is available and get a response back, run the command:

ping www.google.com

Network Components

Network Components include:

  • IP
  • Subnet Mask
  • Gateway
  • Static vs DHCP
  • Interface
  • Interface MAC address

Network Files and Commands

To configure a machine and bring it online or to the network for communication it should have:

  • Interface detected.
  • The IP address assigned.
  • Interface configuration files are set in: /etc/nsswitch.conf, /etc/hosts, /etc/sysconfig/network, /etc/sysconfig/network-scripts/ifcfg-nic and /etc/resolv.conf.

The network commands are ping, ifconfig, ifup or ifdown, netstat and tcpdump.

NIC Info

rmation

NIC is a Network Interface Card. It is installed on a computer that has a port associated with it. It can have multiple ports and often port is incorrectly referred to as NIC.
linux-network4

To get the information of the NIC, the command is:

ethtool enp0s3

The enp0s3 is the name of the NIC.

We can get all the NIC information by running ifconfig . Other NICs are l0, virb0.

NIC Bonding in Linux

NIC bonding is also known as Network bonding. It is known as the aggregation or combination of multiple NIC into a single bond interface. It gives high availability.

To create NIC bonding, follow the below steps:

  • Open Oracle virtual box and click on Settings.
    linux-network5
  • Click on Network. Go to the Adapter 2 tab. Then select Enable Network Adapter. Select Bridged Adapter from the Attached to drop-down.
    linux-network7
  • Now, we shall have two Adapters 1 and 2 attached.
    linux-network8
  • Click on Start from the Virtual Box.
    linux-network10
  • The virtual box gets started. Log in as root.
    linux-network11
  • Right-click on the desktop to open the terminal.
  • To get the network interfaces, run the command:
    ifconfig​

linux-nic

The network interfaces are enp0s3 and enp0s8.

  • Install bonding driver with the command:
modprobe bonding
  • To list the bonding module-info, the command is:
modinfo bonding
  • Create a new file ifcfg-bond0 in the path /etc/sysconfig/network-scripts, with the command:
vi /etc/sysconfig/network-scripts/ifcfg-bond0
  • Add the parameters in the vi editor in insert mode:
DEVICE=bond0
TYPE=Bond
NAME=bond0
BONDING_MASTER=yes
BOOTPROTO=none
ONBOOT=yes
IPADDR=192.168.1.80
NETMASK=255.255.255.0
GATEWAY=192.168.1.1
BONDING_OPTS=”mode=5 miimon=100”
:wq!
  • Edit the first NIC file ifcfg-enp0s3 in the path /etc/sysconfig/network-scripts, with the command:
vi /etc/sysconfig/network-scripts/ifcfg-enp0s3
  • Delete the content of the file.
  • Add the parameters in the vi editor in insert mode:
TYPE=Ethernet
BOOTPROTO=none
DEVICE=enp0s3
ONBOOT=yes
HWADDR=”MAC from the ifconfig command”
MASTER=bond0
SLAVE=yes
:wq!
  • Create the second NIC File (ifcfg-enp0s8) in the path /etc/sysconfig/network-scripts:
    vi /etc/sysconfig/network-scripts/ifcfg-enp0s8​
  • Add the parameters in the vi editor in insert mode:
    TYPE=Ethernet
    BOOTPROTO=none
    DEVICE=enp0s8
    ONBOOT=yes
    HWADDR=”MAC from the ifconfig command”
    MASTER=bond0
    SLAVE=yes
    :WQ!​
  • Restart the Network Service, with the command:
    systemctl restart network​
  • Test and verify the configuration, with the command:
    ifconfig | more​

The output shall be:
linux-network12
The bond0 is shown as MASTER and enp0s3 and enp0s8 are shown as SLAVE.

  • To view the bond and slave interface details, run the command:
    cat /proc/net/bonding/bond0​

Downloading Files and Apps

The command wgetis used to download files in Linux.

For example, to download Putty, login in as root, and run the command:

wget https://the.earth.li/~sgtatham/putty/latest/putty-0.74.tar.gz

curl and ping Commands

The curl and ping commands are used to troubleshoot issues in the server. The curl is used with URL and it gets the HTTP content. It checks if both the server and page is up.

curl www.google.com

The command ping is to communicate with an IP address.

ping www.google.com

To download Putty with curl, login in as root, and run the command:

curl -O https://the.earth.li/~sgtatham/putty/latest/putty-0.74.tar.gz

File Transfer Protocol in Linux

File Transfer Protocol

The file transfer protocol is used to transfer a file from one server to another. A protocol is a group of guidelines used by computers to interact. The FTP port is 21.

For FTP, we should have a client machine and a server machine. The FTP server machine should have ftpd service running at Port 21.

To install and configure FTP on the remote server machine, the steps are :

  • Become a root by running the command:
    su -​
  • Verify if ftp is already installed, with the command:
    rpm –qa | grep ftp​
  • If not available, check the internet access with the command:
    ping www.google.com​
  • If the ping command is getting the response, then install the vsftpd package with the command:
    yum install vsftpd​
  • Now, verify if ftp is installed, with the command:
    rpm –qa | grep ftp​​

The output shall be:
linux-ftp

  • Do the configuration within the vsftpd.conf file in the location /etc/vsftpd with the command:
    vi /etc/vsftpd/vsftpd.conf​
  • As the vi editor opens, search for the /anonymous and set the parameter:
    anonymous_enable=NO​
  • Search for the /ascii and uncomment the lines:
    ascii_upload_enable=YES
    ascii_download_enable=YES​
  • Search for the /ftpd and uncomment the line:
    ftpd_banner=Welcome to blah FTP service​
This step is optional and we can set any value for the ftpd_banner parameter.
  • Go to the file end (pressing SHIFT+G) and add the line and save:
    use_localtime=YES​
    :wq!
  • Start the vsftpd service with the command:
    systemctl start vsftpd​
  • Enable the vsftpd service with the command:
    systemctl enable vsftpd​
  • Stop the firewalld service with the command:
    systemctl stop firewalld​
  • Disable the firewalld service with the command:
    systemctl disable firewalld​
  • Add a user (if the user does not exist) with the command:
    useradd username​

To install the FTP client on the client machine, the steps are :

  • Become a root by running the command:
    su -​
  • Verify if FTP is already installed, with the command:
    ftp​
  • If not available, check the internet access with the command:
    ping www.google.com​
  • If the ping command is getting the proper response, then install the ftp package with the command:
    yum install ftp​
  • Exit from the root account to the user account with the command:
    exit​
  • Create a file in the home directory, with the command:
    touch kruger​
  • Add some content to it, with the command:
    ls -ltr > kruger​
  • Log in to the remote server machine and check the IP address, with the command:
    ifconfig​

    linux-ftpserveripaddress

The IP address of the server: 192.168.1.58.

  • To transfer the file, run the command:
    ftp 192.168.1.58​

The output shall be: linux-ftptransfer

  • Enter the username and password.
  • To transfer the file in binary mode, run the command:
    bin​
  • To show the progress of transfer with a # mark, run the command:
    hash​
  • Finally, to transfer the file, run the command:
    put kruger​

The output shall be:

linux-ftpput

  • Exit from the FTP with the command:
    bye​
  • Now, login to the remote server machine to check the file kruger with the command:
    ls -ltr​

Secure Transfer Protocol in Linux

The Secure Transfer Protocol enables to move files securely from a local to a remote host. This is just like FTP and also has security and authentication features.

A protocol is a group of guidelines used by computers to interact. The SCP port is 22.

For SCP, we should have a client machine and a server machine. The SCP server machine should have ssh service running at Port 22.

To install and configure SCP on the remote server machine, the steps are :

  • Login as yourself.
  • Create a file named jack with the command:
    touch jack​
  • To transfer the file to the home directory, run the command:
    scp jack [email protected]:/home/saby​

Here, saby is the user, and 192.168.1.58 is the IP address.

  • Once the file is transferred, enter the username and password.

Now, login to the remote server machine to check the file jack with the command:

ls -ltr

Remote Synchronization in Linux

The rsync is a utility to transfer and synchronize files efficiently within the computer or to a remote computer by comparing the modification and size of the files. It is faster than ftp or SCP.

This is mostly used to back up the files and directories from one server to another. The rsync server machine should have ssh with default service running at Port 22.

For rsync, we should have a client machine and a server machine.

To install and configure rsync on the client machine, the steps are :

  • Become a root by running the command:
    su -​
  • Verify if rsync is already installed, with the command:
    rpm –qa | grep rsync
  • If not available, check the internet access with the command:
    ping www.google.com​
  • If the ping command is getting the proper response, then install the rsync package with the command:
    yum install rsync
  • Now, again verify if rync is installed, with the command:
    rpm –qa | grep rsync​​
  • Exit from the root account to the user account with the command:
    exit​

To rsync a file in on a local machine in the home directory the steps are :

  • Tar files in a file(backup.tar) and keep it in the location /home/saby, with the command:
tar cvf backup.tar /home/saby
  • Create another directory in the location /tmp where the backup.tar file shall be transferred with the command:
    mkdir /tmp/backups​
  • To rsync the backup.tar file to the directory /tmp/backups, the command is:
    rsync -zvh backup.tar /tmp/backups/​backup.tar

The output shall be:

linux-rsynctransfer

To rsync the home directory to the /tmp/backups the command is:

rsync -azvh /home/saby /tmp/backups/backup.tar

To rsync the backup.tar file to a remote server machine the steps are :

  • Go to the remote machine and check the IP address, with the command:
    ifconfig​

The output shall be:

linux-rsyncremoteip

  • Create a directory backups in the location /tmp with the command:
    mkdir /tmp/backups​
  • Go back to the client machine and run the command:
    rsync -avz backup.tar [email protected]:/tmp/backups​

Here, saby is the user, and 192.168.1.58 is the server IP address.

  • Now, login to the remote server machine to check the file backup.tar in the location /tmp/backups with the command:
    ls -ltr​

To rsync a file from a remote machine the steps are :

  • Open the remote machine, and create a file with the command:
    touch serverfile​
  • Open the client machine, and pull the serverfile to the /tmp/backups directory with the command:
    rsync [email protected]:/home/saby/serverfile /tmp/backups​

System Updates in Linux

System Updates and Repos

The system updates and repository command for CentOS is yum and for other Linux distribution is apt-get.

The yum command downloads and installs packages from the repositories that are in Linux systems. It needs to go online and install the package. The configuration files for yum are present in the /etc/yum.repos.d.

Another system updates and repos command is rpm. The rpm command is used when a package is already downloaded in the system and we have to install it locally. It does not require an internet connection.

Both yum and rpm commands are executed from the root user.

To install the NTP package, the command is:

yum install ntp

To check the packages already available, the command is:

rpm -qa

To install a package.rpm available in the location /tmp, the command is:

rpm -ihv /tmp/package.rpm

To remove the package.rpm, the command is:

rpm -e package.rpm

To remove the bind package with yum, the command is:

yum remove bind

System Updates/Patch Management

There are two types of upgrades in Linux - major version(5, 6, 7) and minor version(7.3 to 7.4). The major version updates can not be done by the yum command but the minor version can be done.

To install an update with the answer yes to every question during the installation process, the command is:

yum update -y

To delete the old packages, the command is:

yum upgrade

To preserve the old packages, the command is.

yum update

    Advance Package Management

    The advance package management involves the following:

    • Package installation.
    • Package upgrade.
    • Package delete.
    • View package details.
    • Identify the source and location of the package.
    • Package configuration files.

    To check if the package ksh is installed, the command is:

    rpm -qa | grep ksh

    To install all packages related to ksh, the command is:

    yum install ksh*

    To remove all packages related to ksh, the command is:

    yum remove ksh*

    To download the ksh package, the command is:

    wget http://mirror.centos.org/centos/7/os/x86_64/Packages/ksh-20120801-142.el7.x86_64.rpm

    To verify if the package ksh is downloaded, run the command:

    ls -ltr

    The output shall be:

    linux-package

    To get information about the ksh package, the command is:

    rpm -qi ksh-20120801-137.e17.x86_64

    To remove the package related ksh with rpm, the command is:

    rpm -e ksh-20120801-137.e17.x86_64

    To list configuration files of ksh, the command is:

    rpm -qc ksh-20120801-137.e17.x86_64

    To get the package of ksh command, the steps are:

    • Find the path of the ksh command, with the command:
      which ksh​

    The output:

    linux-package1

    • To get the package of the ksh, the command is:
      rpm -qf ​/usr/bin/ksh

    Rollback Patches and Updates

    An update or a patch may have compatibility issues. If we are using a virtual machine, we can easily rollback to the older version by restoring to the snapshot of the machine.

    If we are using a physical machine, we do not have the snapshot option. We can do the followings:

    • Rollback a package or patch.
    • Rollback an update.
    Downgrading a system to a lower version is not a good practice because it may cause instability in the system.

    To rollback a package or patch follow the steps :

    • Become root with the command:
      su -​
    • Install a package, for example, screen, with the command:
      yum install screen​
    • Verify the screen package is installed with the command:
      rpm -qa | grep screen​
    • To get the history of all the packages or patch, run the command:
      yum history​

    The output shall be: linux-packagerollback

    Note the task ID - 17.

    • To remove that package having ID - 17, the command is:
      yum history undo 17​

    To rollback an update follows the steps :

    • Become root with the command:
      su -​
    • To update, the command is:
      yum update​
    • To get a history of all updates, the command is:
      yum history​

    The output shall be: linux-updateerollback

    Note the task ID - 19.

    • To remove that update having ID - 19, the command is:
      yum history undo 19​

    SSH & Telnet Connection

    Telnet & SSH

    Telnet is an unsecured connection between computers whereas ssh is a secured connection.

    There are two types of packages in almost all the services:

    • Client package.
    • Server package.

    By default, Linux does not install telnet, to install it run the command:

    yum install telnet

    To check if ssh is already installed, run the command:

    ssh

    To connect to localhost or any IP address, run the command:

    ssh hostname

    To come out of that session, run the command:

    exit

    To check the status of sshd, run the command:

    ps -ef | grep sshd

    To stop the sshd service, run the command:

    systemctl stop sshd

    To start the sshd service, run the command:

    systemctl start sshd

    To check if the sshd has started, run the command:

    systemctl status sshd

    Tracing Network Traffic

    The command traceroute is used to monitor the journey of a packet of information from the source to the destination. It helps to identify if a node is down when there is a data loss within the network.

    It also determines the slow points which affect the network traffic.

    To get the network traffic of google.com, run the command:

    traceroute www.google.com

    Open Image through Command Line

    We can open images from the command line.

    First of all, have an image in any location say Desktop.

    Become a root with the command:

    su -

    Install the package ImageMagick with the command:

    yum install ImageMagick -y

    To confirm if ImageMagick is installed, run the command:

    rpm -qa | grep ImageM

    To display the image, run the command:

    display imagefilename

    SSH Keys - Access Remote Server Without Password

    We need to access a remote machine with SSH keys because of the below reasons:

    • Repetitive logins.
    • Automation through scripts.

    The keys can be generated at the:

    • user level.
    • root

    For SSH keys, we should have a client machine and a server machine.

    To access a remote server without a password, follow the steps :

    • Login to the client machine and become root, with the command:
      su -​
    • Note the IP address with the command:
      ifconfig​

    linux-sship

    The IP address is 192.168.1.95.

    • Go to the server machine and become root with the command:
      su -​
    • Note the IP address of the server machine with the command:
      ifconfig​

    linux-sshipserver

    The IP address is 192.168.1.58.

    • From the client machine, run the command:
      ssh-keygen​

    The key gets generated. linux-sshkey

    • Copy the key generated from the client machine to the server with the command:
      ssh-copy-id [email protected]
    • Login to the server machine from the client with the command:
      ssh [email protected]
    • To come out of the server machine session, run the command:
      exit​
    • To get the last login information of the server from the client machine, run the command:
      ssh –l root 192.168.1.58​
    About Author :

    Myself Debomita Bhattacharjee, an IT employee with 6+ years of experience in Software industry. My area of interest is Automation testing and Front End Development.

    Comment / Suggestion Section
    Point our Mistakes and Post Your Suggestions