Saturday, 26 May 2018

Linux Fundamentals for Networking- Part 1

  • Reasons for Windows popularity: 1. Dedicated support 2. Compatibility with legacy softwares 3. Established 4. Windows- specific development tools and applications 5. Windows is used for Server load-balancing
  • Linux Architecture:
  • Fun fact: Arista's EOS is a pure Linux OS, we can enter linux shell using #bash command
  • ps command gives us a list of process IDs
  • To see all process ps -ef
    • The UID  tells which user started the process
    • PID is process ID
    • PPID is parent process ID
    • For example, BGP is PID 1768 and the parent PID is 1767 which is proc_mgr (which is init)
  • Any application related configs are copied to /etc directory
  • In proc directory, the state of running process including address space, memory, etc
  • Devices are there in /dev directory
  • To make directory, mkdir. To remove directory rm -rf
  • To create a file, we use touch command $touch filename.doc
  • > : redirection. Saves the o/p to file.
  • >> : apphend
  • Wildcard- * and ? . For example, if we want all files that end with .txt, we can use $ls *.txt
  • Inside bash in EOS, to see the flash contents: $cd /mnt/flash. Then ls -l
  • Linux Distributions:
    • "Linux" properly refers to the OS kernel.
    • By "different flavors of linux", you are referring to different Linux distributions. A "distribution" is a complete operating system -- a particular packaging of the kernel with various other utilities, services and shells that provide the rest of what users expect from an operating system.
    • The differences between distributions are in the particulars of
  • what a distribution is designed to accomplish,
  • which versions of what tools it includes to achieve that goal, and
  • how it chooses to handle configuration and packaging.

    • A distribution aimed at desktop users typically includes the X-window system, a desktop environment, a printing subsystem, networking components, etc. One designed for servers often doesn't include X or a desktop environment, but adds services that aren't needed on a desktop (webserver, database, network filesystems, etc).
    • Kill -<signal number> <pid>

    signal #
    Usage
    1
    HUP (hang up)
    2
    INT (interrupt)
    3
    QUIT (quit)
    6
    ABRT (abort)
    9
    KILL (non-catchable, non-ignorable kill)
    14
    ALRM (alarm clock)
    15
    TERM (software termination signal)


    • Ls command in Linus:
      • ls syntax : $ ls [options] [file or dir]
      • ls command options:
    option
    description
    list all files including hidden file starting with '.'
    ls --color
    colored list [=always/never/auto]
    ls -d
    list directories - with ' */'
    ls -F
    add one char of */=>@| to enteries
    ls -i
    list file's inode index number
    list with long format - show permissions
    list long format including hidden files
    list long format with readable file size
    list with long format with file size
    list in reverse order
    list recursively directory tree
    list file size
    sort by file size
    sort by time & date
    ls -X
    sort by extension name
    ls -ltr
    Sort by latest file created
    • Ls Command Examples:
      • You can press the tab button to auto complete the file or folder names.
      • List directory Documents/Books with relative path:
    $ ls Documents/Books
    • List directory /home/user/Documents/Books with absolute path.
    $ ls /home/user/Documents/Books
    • List root directory:
    $ ls /
    • List parent directory:
    $ ls ..
    • List user's home directory (e.g: /home/user):
    $ ls ~
    • List with long format:
    $ ls -l
    • Show hidden files:
    $ ls -a
    • List with long format and show hidden files:
    $ ls -la
    • Sort by date/time:
    $ ls -t
    • Sort by file size:
    $ ls -S
    • List all subdirectories:
    $ ls *
    • Recursive directory tree list:
    $ ls -R
    • List only text files with wildcard:
    $ ls *.txt
    • ls redirection to output file:
    $ ls > out.txt
    • List directories only:
    $ ls -d */
    • List files and directories with full path:
    $ ls -d $PWD/*

    • PS Command:
      • The ps command displays active processes.
      • The syntax for the ps command is: ps [options]
      • Various Options are:

    -a
    list the processes of all users on the system rather than just those of the current user (except group leaders)
    -u
    provide detailed information about each process
    -x
    adds to the list processes that have no controlling terminal, such as daemons
    -c
    Displays scheduler data.
    -d
    Displays all processes with the exception of session leaders.
    -e
    Displays all processes.
    -f
    Displays a full listing.
    -glist
    Displays data for the list of group leader IDs.
    -j
    Displays the process group ID and session ID.
    -l
    Displays a long listing
    -plist
    Displays data for the list of process IDs.
    -slist
    Displays data for the list of session leader IDs.
    -tlist
    Displays data for the list of terminals.
    -ulist
    Displays data for the list of usernames.

    • A common and convenient way of using ps to obtain much more complete information about the processes currently on the system is to use the following:
    ps -aux | less    
    Note that each of the ‘a’ , ‘u’ , ‘x’ are separate options
    The aux gives details of all process and less makes it scrollable
    • An alternative set of options for viewing all the processes running on a system is
    ps -ef | less
    • Note that the ‘-’ is optional. So, we can also use ps aux | less
    • The processes shown by ps can be limited to those belonging to any given user by piping the output through grep, a filter that is used for searching text.
    For example, processes belonging to a user with a username adam can be displayed with the following:
    ps -ef | grep adam
    • The -l option generates a long listing, and when used together with the -e and -f options creates a table with 15 columns:
    ps -efl
    • Sorting the PS Output:
      •      CODE NORMAL   HEADER
              %C pcpu     %CPU
              %G group    GROUP
              %P ppid     PPID
              %U user     USER
              %a args     COMMAND
              %c comm     COMMAND
              %g rgroup   RGROUP
              %n nice     NI
              %p pid      PID
              %r pgid     PGID
              %t etime    ELAPSED
              %u ruser    RUSER
              %x time     TIME
              %y tty      TTY
              %z vsz      VSZ
      • Example: ps -ef --sort=user | less or ps -ef --sort=pid | less
      • Example: to see by startup time ps -ef --sort=time | less

    • Filters:
      • Some useful ones are the commands awk, grep, sed, spell, and wc.
      • Filters usually works with Linux pipes.
      • Grep:
        • grep scans the output line per line, searching for matching patterns.
        • It is case-sensitive. To make it insensitive, use -i attribute
        • Some examples: suppose we want to know which files in a certain directory have been modified in February:
    jenny:~> ls -la | grep Feb
    • Important options of the grep command
    -e (Used to specify a pattern)
    -i (Case insensitive search)
    -c (Print a count of matching lines)
    -v (invert search - Returns lines that do not match, instead of lines that match.)
    -w (Matches only when the input text consists of full words)
    -x (Should match the entire line)
    --color (Colorize output)
    -l (Instead of normal output, prints just the names of input files containing the pattern)
    -L (Prints the names of input files that contain no matches)
    -o (Prints only the text that matches, instead of the whole line of input)
    -q (Suppresses output. Useful in finding the exit status (0 for success if a match is found, 1 for no match found, 2 if the program cannot run because of an error)).
    • "^" character is used as anchor, to find the lines which begin which the following text. Example – “^RedHat”
    • "$" character is used as anchor, to find the lines which end with the preceding text. Example – "RHEL5$"
    • To search for lines which contains numbers, use RegEx expression range "[0-9]"
    • To search for lines which contain small case letters, use RegEx expression range "[a-z]"
    • To search for lines which contain caps letters, use RegEx expression range "[A-Z]"