Network in linux - TopLearnLinux

 

Networking:

    It is a connection between two or more machines to communicate with each other.

    The basic requirements for Networking are:

  • NIC (Network Interface Controller or Card)
  • Media
  • Topology
  • Protocol
  • IP Addresses

    NIC (Network Interface Controller or Card):

    A network interface controller (also known as a network interface card, network adapter, LAN adapter and by similar terms) is a computer hardware component that connects a computer to a computer network. Each NIC will be having a unique MAC addresses (Media Access Control address) to avoid conflicts between same NIC adapters. In Linux these NIC adapter is represented by the word “eth”. Example if there are two Ethernet adapters in the system then it will be denoted as eth0, eth1, etc.

    Media:

    Media is the medium via which two different computer’s NIC card will be connected. The best example for media is Cable. Example RJ 45, CAT 5 etc.

    Topology:

    Topology is the scheme or design in which the computers in the network will be connected to each other. Example for topology is Bus, Ring, star, mesh, tree topologies. The following pictures explain it better.

    Protocol:

    A network protocol defines rules and conventions for communication between network devices. Protocols for computer networking all generally use packet switching techniques to send and receive messages in the form of packets.

    Network protocols include mechanisms for devices to identify and make connections with each other, as well as formatting rules that specify how data is packaged into messages sent and received. Some protocols also support message acknowledgement and data compression designed for reliable and/or high-performance network communication. Hundreds of different computer network protocols have been developed each designed for specific purposes and environments.

    Example for Protocols are TCP/IP (Transmission Control Protocol), UDP (User Datagram Protocol), HTTP. The most widely and regularly used protocols for transferring data are TCP and UDP. Let’s analyze some basic differences between TCP/IP and UDP.

    TCP/IP UDP
    Transmission Control Protocol User Datagram Protocol
    It is connection Oriented Connectionless
    Reliable Non-Reliable
    TCP Acknowledgement will be sent/received No Acknowledgement for UDP
    Slow Communication Faster Communication
    Protocol Number for TCP is 6 Protocol Number for UDP is 17
    HTTP, FTP, SMTP uses TCP DNS, DHCP uses UDP

    IP ADDRESS:

    An IP address can be thought of as being similar to a phone number. Just as every person who communicates with a telephone is using a phone with a unique phone number, every computer that is on the Internet has a unique IP address. Not only on internet but within an organization every computer is assigned an IP address so that they can communicate with each other. Basically IP addressing is very deep concept. To understand the concept of IP address we need to understand some important aspect of IP Address which is

  • IP Address Classes
  • Subnet mask
  • Gateway

    IP Address Classes:

    The IP addresses are further broken down into classes. These classes are A, B, C, D, E and their possible ranges can be seen in Figure below.

    Class Start End Default Subnet Mask CIDR
    Class A 0.0.0.0 127.255.255.255 255.0.0.0 /8
    Class B 128.0.0.0 191.255.255.255 255.255.0.0 /16
    Class C 192.0.0.0 223.255.255.255 255.255.255.255 /24
    Class D (Multicast) 224.0.0.0 239.255.255.255
    Class E (Reserved) 240.0.0.0 255.255.255.255
  • Validating Network Configuration - TopLearnLinux

     

    Displaying IP Addresses :


    Command :

    $ip addr show ens160

    IP Command is used to show statistics about network performance. Received (RX) and Transmitted (TX) packets, errors, and dropped counters can be used to identify network issues caused by congestion, low memory and over runs.

    Command:

    $ip -s link show ens160

    Troubleshooting routing :

    This command is used to show the routing informations

    Command :

    $ip route

    Ping command is used to test connectivity

    Command :

    $ping 192.168.43.2

    To trace the path to a remote host, use either traceroute or tracepath. Both commands can be used to trace a network path with UDP Packets.

    Troubleshooting ports and Services :

    SS command is used to display socket statistics. the ss command is meant to replace to the older tool netstat.

    Command :

    $ss -ta

    Create Simple Calculator using Linux Script without Coding

    $vim calc.sh

    #!/bin/bash

    echo "Enter the first number :"
    read FN
    echo " Choose the operation :"
    echo "1. add"
    echo "2. sub"
    echo "3. multipication"
    echo "4. divide"
    read opr

    echo "Enter the second number :"
    read SN

    if [ $opr = "1" ]
    then
        echo "Answer:" $((FN + SN))


    elif [ $opr = "2" ]
    then
        echo "Answer :" $((FN - SN))


    elif [ $opr = "3" ]
    then
        echo "Answer :" $((FN * SN))


    elif [ $opr = "4" ]
    then
        echo "Answer :" $((FN / SN))  

      
    elif [ $opr != "" ]
    then
        echo "Please enter correct operator"

    fi
    exit

    Output:

    User enter the first number :

    20








    then user choose the operator :

    3







    Enter the second number :

    5






    Finally answer is :

    100

    Group Management Commands in Linux - Top Learn Linux

      Creating a Group with default options :

    • To create a group the syntax is
    • #groupadd <name for the group>
    • #groupadd admingrp

    Let see admingrp group created and that group id is 1012.

    Creating a group with user specified group id (GID)
    • #groupadd <option> <name for the group>
    • #groupadd -g 615 admin
    • Verify it in /etc/group


    Let see admin group and and user mentioned group id is 615 created .

    Modifying the properties of the group :
    • To modify the group properties the syntax is
    • #groupmod <option> <arguments> <group name>
    • -g to change the group id
    • -o to override the previous assigned id, if it matches with the new one.
    • -n to change the group name
    • Changing the GID of the group
    • #groupmod –g 700 admin
    • Verify it in /etc/group

    Let see the groupid is changed.

    Changing the name of the group:
    • The syntax for changing the group name is
    • #groupmod –n <new name > < existing name >
    • #groupmod –n newadmin admin



    Adding and Removing Members to a Group :
    • Adding the members to the group is to add users to the group. To add the members to the group the syntaxes are
    • To add single user to the group
    • #usermod –G <group name > < user name>
    • #usermod –G newadmin toplearnlinux











    Adding multiple single or multiple users to the group with various attributes
    • #gpasswd < option> <arguments> <group name>
    Options:
    • -M For Adding Multiple users to a group
    • -A for Adding a group Administrator
    • -a for Adding a single user to a group
    • -d removing a user from a group
    • #gpasswd –M <user>,<user>,<user> <group>
    • #gpasswd –M admin2,admin3,admin4 newadmin
















    Adding a single user using gpasswd :
    • #gpasswd –a admintl newadmin 
    (verify it in /etc/group)
















    Making a user as a administrator:
    • #gpasswd –A admintl newadmin
    (verify it in /etc/gshadow)










    Removing a user from the group :
    • #gpasswd –d admin2 newadmin















    User and Group Management Commands in Linux - Top Learn Linux

    Creating a user:

    The syntax for creating a user in Linux is 

    Let’s create a user with default attributes.

    • When no option is used with useradd command the options like UID, GID, home dir and shell will be assigned default.
    • #useradd <username>
    • #useradd student




    Observe that the uid, gid, home dir, and shell is assigned automatically.

    • # useradd <option> <username>
    • options are
    • -u user id
    • -G Secondary group id
    • -g primary group id
    • -d home directory
    • -c comment
    • -s shell


    The syntax is as follows to rename by a UID (user ID):

    • Create a user with following attributes
    • Name = student
    • uid=1331




    Now look at changes the user id , userid = student id = 1331 changed.


    Set password to user :
    • As a root user we can assign any password to any user
    • The syntax for assigning a password is
    • #passwd to assign password to current user ( the one with which you have logged in, if it is root then root’s password will be changed
    • #passwd <user name> to assign a password to a specific user, only root can assign password to other user.



    Modifying the user’s attribute
    • After creating a user if we need to modify the attributes of user like changing uid, changing secondary group id or adding a comment, locking or unlocking the user account, can be done by following command
    • Syntax. # usermod <options> <username>
    • all the options which are used with useradd command can be used and also the following,
    • -l to change login name
    • -L to LOCK account
    • -U to UNLOCK account
    • ex. # usermod -l newname oldname (changing the name of the user)
    • ex. # usermod -L newname to lock the user account
    • ex. # usermod -U newname to unlock the user account
    • Note: - when an account is locked it will show! (Exclamation mark) in /etc/shadow file.

    Locking and unlocking a user account:
    • To lock a user a/c use the following
    • #usermod –L < user name>
    • #usermod –L student
    • Verify it in /etc/shadow file, it shows exclamation mark before user a/c or try login as student

    Unlocking a user account:
    • Unlock the above a/c
    • #usermod –U < user name >
    • #usermod –U student
    • Verify it in /etc/shadow file, it shows exclamation mark before user a/c or try login as student


    • Observe in both pictures that once the account is unlocked the exclamation is gone.

    Password parameters:
    • For any user we can set the parameters for the password, like min and max password age, password expiration warnings and a/c expiration date etc.
    • To view the advanced parameters of the user, use
    • #chage -l < user name>
    • #chage -l student


    • Last password change: When the password was change last time.
    • Password expires: Password expiry date
    • Password inactive: After password expiry grace period before the account gets locked.
    • Account expires: Date on which the account expires.
    • Minimum number of days b/w password change: once the password is changed, it cannot be changed until a min period of specified date. [0] means never.
    • Max number of days b/w password change: After changing the password how long it will be valid for.
    • Number of days of warning before password expires: start of warnings to change the password, no. of days before the password expires.

    Changing the password parameters:
    • Changing of the password parameters can be done by two ways.
    • #chage <user name >
    • #chage <option> <value> <username>
    • Let’s see the first method and then the other.
    • To set the password parameters of a user “student” to
    • Min password age : 25 days
    • Max password age: 30 days
    • Password expiration warnings: 2 days before password expires
    • Password inactive [-1]: 0 same day account is locked after password expiry.
    • A/C expiration date: 2022-12-31 (dec 31st 2022)


    • The second method is for, if you want to change a particular field of password aging policy
    • #chage <option> <value> <username>
    • The options which can be used are as follows
    • -m for Min password age
    • -M for Max password age
    • -d for last time the password is changed.
    • -W Password expiration warnings
    • -I Password inactive [-1 means inactive].
    • -E A/C expiration date

    Deleting a User:
    • To delete a user the syntax used is
    • #userdel <username> it will only delete the user but home directory will be there. To delete the user with its home directory use the following command.
    • #userdel –r < user name >
    • #userdel –r student


    • tail command is used to showing bottom of the files .
    • tail -2 is mentioned show only bottom two lines.


    Top 20 + Important Commands with Examples - Basic Commands for Linux

     Creating files :

    cat (Concatenate) command is used to create a file and to display and modify the contents of a file.

    Example :



    cat -> command is used to create a file.

    file1 -> line is user mentioned a file name.




    Ctrl+d (To save the file)

    To display the content of the file :

    # cat (filename)




    To append the data in the already existing file :

    # cat >> (filename)


    Ctrl+d (to save the changes)

    Creating multiple files at same time using touch command :

    #touch (filename) (filename) (filename)

    #touch file1 file2 file3



    check the files use # ls command.





    check the files permissions , user and group details use # ll command.






    Creating a Directory:

    #mkdir (dir name)

    #mkdir folder1




    check the files permissions , user and group details use # ll command.








    Copying files into directory:

    #cp (source filename) (destination directory)

    #cp file1 folder1

    #cd command is used to change directory.







    Moving files from one location to other:

    #mv (filename) (Destination directory)

    #mvfile1 folder1











    Moving a Directory from one location to another location :

    #mv (dir name) (destination dir name)
    #mv folder1 folder2












    Renaming a File :

    #mv (old name) (new name)

    #mv file1 newfile1








    Renaming a Directory :

    Command for renaming the directory.

    #mv (old name) (new name)

    #mv folder1 newfolder1






    Removing a File :

    #rm filename

    #rm file2






    Removing an Empty directory :

    #rmdir (dirname)

    #rmdir folder2










    Filter commands:

    less:

    less command can be used to read the contents of a text file.

    #less





    more:

    more command can be used to read the contents of a text file.

    #more





    head:

    head command used to show top 10 lines of a text file.

    #head


    tail:

    tail command used to show last 10 lines of a text file.

    #tail


    Network in linux - TopLearnLinux

      Networking: It is a connection between two or more machines to communicate with each other. The basic requirements for Net...