Linux for DevOps

·

10 min read

Introduction of Linux:-

Linux is an operating system(similar to Windows and macOS) BUT What is an operating system? To define this we can say:-

"OS is a set of programs which manages all the resources of the computer system. It is an intermediary between the user and hardware."

Suppose you want to run the Zoom application on a computer. How will you do it? A computer is a dumb machine. who will be the intermediator between the User(you) and the Computer? So we will provide OS to the Computer so that you(the user) can interact with it. OS knows that Zoom will require this much CPU, this much RAM or this much Storage.

so we can say "Linux is an operating system" and it does the following things:-

  1. Linux does task scheduling.

  2. It does memory management.

  3. It provides security.

  4. It interacts with different software.

  5. It does file management etc.

Why do DevOps & Cloud professionals use Linux over the other operating systems?

In real-world scenarios, while interacting with production-level servers, maximum servers are installed with Linux because it is open source, very secure, very fast, and very easy to use. so becomes reliable for engineers. It also comes in different flavors Each flavor (distribution) bundles with different software packages and configurations to serve specific purposes or user preferences. for example:-

✔️Ubuntu: Great for beginners, desktop users, and servers; known for its user-friendly interface.

✔️Fedora: Often preferred by developers; focuses on the latest features and technologies.

✔️Debian: Stable and reliable; popular for servers and embedded systems.

✔️CentOS: Derived from Red Hat Enterprise Linux (RHEL); widely used for servers and enterprise environments.

✔️Kali Linux: Designed for cybersecurity professionals and penetration testing.

A small history of Linux and Unix:-

The Story starts with Unix. Unix was born in the 1960s when some intelligent engineers wanted to make a new kind of operating system in AT&T Bell Labs. Unix laid the foundation for modern operating systems with its multitasking, multiuser capabilities, and hierarchical file system. The engineers wanted something flexible, reliable, and powerful. Unix became popular in the 1970s and 1980s, especially in big companies and universities. It made computers work smoothly. Even today, many big computers and servers use Unix because it's so good at what it does. macOS which is offered by Apple is Unix-based OS. But Unix is paid and it is not open source.

Again In the same year 1980s, Richard Stallman launched the GNU Project, aiming to develop a free Unix-like operating system. Stallman and his team developed many essential components of an operating system, such as the GNU Compiler Collection (GCC), GNU C Library (glibc), and GNU core utilities. But one thing was missing the kernel code So in 1991, a young engineer named **Linus Torvalds(**Finland, USA) released the Linux kernel as open-source under the GNU General Public License (GPL). Torvalds' kernel combined with the GNU components formed the complete operating system known as GNU/Linux or simply Linux. He named his creation Linux, combining his name "Linus" with "Unix." Linux was inspired by Unix, and it shared many of its features, but it was open to everyone. He shared Linux with the world for free, and soon, other smart people from all over the world joined in to help make it even better.

Linux File System Architecture:-

Filesystem Hierarchy Structure (FHS) in Linux – TecAdmin

✔️ In Linux, everything, even hardware programs, is treated like a file. These files are kept in folders, and these folders are organized in a tree-like structure. This organization is called the File System Hierarchy.

✔️ Linux uses a simple tree-like structure with one main root folder. The Root Directory(folder) is represented with / (forward slash) It is a top-level directory in Linux. OR,

✔️ In Linux, the main starting point is called the root directory, represented by a forward slash (/). All other directories in Linux start from this root directory.

✔️/root

-->It is the home directory for the root user (superuser).

✔️/bin → User Binaries

-->Contain binary executables.

-->Common Linux commands you need to use in single-user modes are located under this directory.

-->Commands used by all the users of the system are located here.

✔️/sbin → SystemBinaries

-->Just like /bin, /sbin also contains binary executables.

-->But, the Linux commands located under this directory are used typically by the system aministrator, for system maintenance purposes.

-->For example: iptables, reboot, fdisk, ifconfig, swapon.

✔️/dev → Device Files.

-->it contains hardware device files.

--> These include terminal devices, usb, or any device attached to the system.For example: /dev/tty1, /dev/usbmon

✔️/var→ Variable Files .

-->The variable data files such as log files are located in the /var directory. File contents that tend to grow are located in this directory. This includes:-

◦ /var/log: System log files generated by OS and other applications.

◦ /var/lib: Contains database and package files.

◦ /var/mail: Contains Emails.

◦ /var/tmp: Contains Temporary files needed for reboot.

✔️/media→ Removable Media Devices.

-->The /media directory contains subdirectories where removable media devices inserted into the computer are mounted.

✔️/usr → User Binaries.

-->The /usr directory contains applications and files used by users, as opposed to applications and files used by the system.

✔️/mnt→ Mount Directory.

-->This directory is used to mount a file system temporarily.

✔️/etc → Configuration files.

--> It contains all configuration files of the server.

-->The core configuration files are stored in the /etc directory. It controls the behavior of an operating system or application. This directory also contains startup and shutdown program scripts that are used to start or stop individual programs.

✔️/boot → Boot Loader Files .

-->The /boot directory contains the files needed to boot the system.The GRUB boot loader's files and your Linux kernels are stored here.

✔️/opt → Optional Applications.

-->The opt directory is used for installing the application software from thirdparty vendors that are not available in the Linux distribution. Usually, the software code is stored in the opt directory and the binary code is linked to the bin directory so that all users can run that software.

✔️/home → Home Directory.

-->It contains the secondary user's home directory.

✔️/tmp → Temporary Files.

--> Directory that contains temporary files created by system and user.

**-->**Files under this directory are deleted when system is rebooted.

Linux Architecture:-

Linux Architecture and Fundamentals

Linux is an operating system, similar to Windows or macOS as we discussed above, but it is built on a different architecture. The main components of this architecture are:-Application, Shell, Kernel, Hardware, Utilities.

✔️Kernel:- At the core of the architecture is the kernel, which acts as an middleman between software applications(web browsers, or games) and the hardware of the system. It is the foundation or backbone of everything else. It manages system resources such as CPU, memory, and devices, and provides essential services like process management, memory management, file system management, and device drivers.

✔️Shell:- The shell is a command-line interface (CLI) through which users interact with the Linux system or we can say the "shell is like the interface between the user and the kernel". It interprets user commands and executes them by interacting with the kernel. Common shells in Linux include Bash (Bourne Again Shell), Zsh (Z Shell), and Fish (Friendly Interactive Shell).

✔️User Space:-User space refers to the area of memory where user programs run. When we run a program or command in Linux, it executes in user space, separated from the kernel. User space includes applications, utilities, and services that users interact daily.

Start with basic Linux commands:-

🔴File System Navigation and Manipulation:-

⭐pwd → it shows the present working directory.

⭐ls → It shows available files and directories in the present working directory.

⭐uname → it shows the name of the kernel (OS).

⭐uname -r → it shows a version of the kernel.

⭐cd → it use to change directory.

⭐clear → it use for clear screen #whoami → it shows the currently login user name.

⭐history → it shows list of previously used commands.

⭐date → it shows time and date.

Let us Learn in depth.

  1. ls: List files and directories.

    • Example: List files and directories in the current directory.

        ls
      
  2. cd: Change directory.

    • Example: Change to the "Projects" directory.

        cd Projects
      
  3. pwd: Print working directory.

    • Example: Display the current working directory.

        pwd
      
  4. mkdir: Create directories.

    • Example: Create a new directory named "DevOps".

        mkdir DevOps
      
  5. touch: Create files.

    • Example: Create a new file named "Dockerfile".

        touch Dockerfile
      
  6. cp: Copy files and directories.

    • Example: Copy a file named "Sumit.txt" to a directory named "backup".

        cp Sumit.txt backup/
      
  7. mv: Move a file to a directory.

    • Example: Move a file named "myfile.txt" to a directory named "metadata".

        mv myfile.txt metadata/
      
  8. rm: Remove files and directories.

    • Example: Remove a file named "myfile.txt".

        rm myfile.txt
      
  9. cat: Display file contents.

    • Example: Display the contents of a file named "sumit.txt".

        cat sumit.txt
      
  10. more and less: Display file contents with paging.

    • Example: Display the contents of a file with pagination using "less".

        less largefile.txt
      
  11. grep: Search for patterns in files.

    • Example: Search for the word "error" in a file named "logfile.txt".

        grep "error" logfile.txt
      

      🔴"File Permissions and Ownership"

      1. chmod: Change file permissions.

        • Example: Set read, write, and execute permissions for the owner, read and execute permissions for the group, and read-only permission for others on a file named "Sumit.txt".

            chmod 755 Sumit.txt
          
      2. chown: Change file ownership.

        • Example: Change the owner of a file named "myfile.txt" to the user "ubuntu" and the group "users".

            chown ubuntu:users myfile.txt
          
      3. chgrp: Change group ownership.

        • Example: Change the group of a file named "example.txt" to the group "developers".

            chgrp developers example.txt
          

          🔴"Text Processing and Manipulation"

          1. grep: Search for patterns in text.

            • Example: Search for the word "error" in a text file named "logfile.txt".

                grep "error" logfile.txt
              
          2. sed: Stream editor for text manipulation.

            • Example: Replace all occurrences of "old" with "new" in a file named "example.txt".

                sed -i 's/old/new/g' example.txt
              
          3. awk: Text processing tool for pattern scanning and processing.

            • Example: Print the second column of a file named "data.csv" where fields are separated by commas.

                awk -F ',' '{print $2}' data.csv
              
          4. cut: Extract sections from lines of files.

            • Example: Extract the first 10 characters from each line of a file named "sumit.txt".

                cut -c 1-10 sumit.txt
              

              🔴"System Information and Monitoring"

              1. uname: Display system information.

                • Example: Display the operating system name and version.

                    uname -a
                  
              2. top: Display system resource usage.

                • Example: Display a dynamic, real-time overview of system processes and resource usage.

                    top
                  
              3. free: Display memory usage.

                • Example: Display information about total, used, and free memory.

                    free -m
                  
              4. df: Display disk usage.

                • Example: Display information about disk space usage on mounted filesystems.

                    df -h
                  
              5. ps: Display information about processes.

                • Example: Display a snapshot of currently running processes.

                    ps aux
                  

                  🔴"Networking and Connectivity"

                  1. ifconfig (or ip): Display and configure network interfaces.

                    • Example: Display information about all network interfaces.

                        ifconfig
                      
                    • Example: Display information about a specific network interface (e.g., eth0).

                        ifconfig eth0
                      
                  2. ping: Test network connectivity.

                    • Example: Send ICMP echo requests to a remote host (e.g., google.com).

                        ping google.com
                      
                  3. netstat: Display network connections, routing tables, and interface statistics.

                    • Example: Display all active network connections.

                        netstat -a
                      
                  4. ssh: Secure shell for remote access.

                    • Example: Connect to a remote host (e.g., 192.168.1.100) using SSH.

                        ssh username@192.168.1.100
                      
                  5. scp: Secure copy for transferring files over SSH.

                    • Example: Copy a file (e.g., file.txt) from the local system to a remote host.

                        scp file.txt username@remotehost:/path/to/destination/