Linux Q&A

·

12 min read

Different flavors of linux & their package managers

alpine --> apk

centos --> yum, dnf

debian --> dpkg, apt

ubuntu --> apt

kali linux --> apt

redhat --> rpm, yum

amazon linux --> yum

fedora --> rpm


Directories in Linux

/root --> root users home dir

/home --> uses home dir

/etc --> config files

/bin --> user binary files

/sbin --> system binary files

/var --> variable file

/opt --> thirdpart file

/dev --> device file

/usr --> user appliction

/mnt --> mount point

/sys --> virtual file system

/tmp --> temporary file

/lib --> system library
/proc --> process info

/media --> removable devices

/lost-found --> recover broken files

/run --> temporary file system

/boot --> boot loader files

/srv --> service data directory


HTTP vs HTTPS

HTTPS is HTTP with encryption and verification. HTTPS requires an SSL certificate (Secure Socket Layer). TLS (transport layer Security) is an advanced SSL

SSL had some vulnerabilities & TLS is an upgraded version of SSL

HTTPHTTPS
If sent a request using http, anyone between d server & browser can read it relatively easier if one interrupts this exchange of data & due to which it is insecureHTTPS is considered to be secure but at the cost of processing time because Web Server and Web Browser need to exchange encryption keys using Certificates before actual data can be transferred
port: 8080port: 443
HTTP Works at the Application LayerHTTP Works at the Transport Layer

Software application

A computer’s operating system interface to the hardware is referred to as a software application. A number of software applications are run on operating systems to manage hardware resources on a computer.


Command to check the OS

uname
uname -a
cat /etc/os-release

Command to check Kernel version

uname -r

List the processes Running

ps -elf
top
htop

Ping Command

allows a user to test and verify if a particular destination IP address exists and can accept requests


Load Average

The load average is typically displayed as three values representing the average system load over the last 1, 5, and 15 minutes

uptime
cat /proc/loadavg

Telnet - Teletype Network - 1969

It's a Network protocol that provides a command line interface for communication with a remote device or server. It is not encrypted (a clear text will be sent) like ssh, so it is not secure

it is a terminal emulation program

Command line tool

It is used in Local Area Network


SSH

SSH enables secure logins to remote computers


TCP & UDP

TCP (Transmission Control Protocol) and UDP (User Datagram Protocol) are two fundamental protocols of the Internet Protocol (IP) suite. They are transport layer protocols responsible for the transmission of data between applications or devices over a network.

TCP (Transmission Control Protocol)UDP (User Datagram Protocol)
TCP is connection-oriented, meaning it establishes a connection before transferring data and ensures reliable, ordered delivery of data.UDP is connectionless, meaning it does not establish a connection before sending data. Each packet is sent independently.
TCP guarantees reliable delivery of data. It uses mechanisms like acknowledgment and retransmission to ensure that data is received correctly.UDP does not provide reliability mechanisms like acknowledgment or retransmission. It's a "best-effort" protocol, and some packets may be lost or delivered out of order
TCP implements flow control to manage the pace of data transmission, preventing fast senders from overwhelming slower receivers.UDP does not implement flow control, so it does not regulate the rate at which data is sent.
In TCP, data is delivered to the receiver in the same order in which it was sent by the sender.Packets sent via UDP may arrive at the destination out of order.
TCP headers are larger compared to UDP due to the additional control information needed for reliability.UDP headers are smaller compared to TCP, which makes it more efficient in terms of overhead.
Suitable for applications where reliable and ordered delivery of data is crucial, such as file transfer (FTP), email (SMTP), and web browsing (HTTP).Suitable for real-time applications where low latency is more critical than guaranteed delivery, such as online gaming, streaming, and VoIP.
  • TCP provides reliable, ordered, and connection-oriented communication.

  • UDP is faster and more lightweight, but it sacrifices some reliability and ordering in favor of low latency.

  • The choice between TCP and UDP depends on the requirements of the specific application or service being used. Some applications may use a combination of both protocols depending on the type of data being transmitted.


Sticky bit

The sticky bit is a permission bit that protects the files within a directory. If the directory has the sticky bit set, a file can be deleted only by the file owner or by a privileged user

To add sticky bit permission to a directory

chmod 1000 <dir_name>
chmod +t <dir_name>

To remove sticky bit permission from a directory

chmod -t <dir_name>

Boot Process in Linux

BIOS (Basic Input Output system)

  • 1st prog that executes which is stored in RAM (on mother of computer)

  • perform POST (power-on self-test) , verifies the hardware components to ensure if computer is in working condition

  • check for bootable device like pendrive, hardisk etc

  • handover control to 1st section of storage device i.e. MBR

  • [apart from BIOS , UEFI (Unified Extensible Firmware Interface) is used ]

MBR (Master boot record)

  • It is of the size 512 bytes

  • It contains machine code instructions to boot a machine

  • It will load the boot loader into memory and handover to it

GRUB

  • Its main job is to load the kernel into memory and handover to it

  • GRUB allows users to choose between different operating systems or configurations at boot time.

Kernel

  • The kernel is the core component of the operating system. It manages system resources, communicates with hardware devices, and provides essential services to higher-level software.

  • (In Linux, the kernel is loaded into memory during the boot process and is responsible for initializing the system.)

  • Ones main file system is loaded, kernel initializes the 1st process i.e. init/systemd

SystemD

  • systemd is a system and service manager for Linux. It is responsible for initializing and managing system services and daemons.

  • It plays a crucial role in the boot process, as it is responsible for starting and managing system processes, including user sessions.

  • it starts all the required process


Linux Architecture

Hardware -

The term "hardware" refers to the physical components of a computer system, including the central processing unit (CPU), memory (RAM), storage devices (hard drives or SSDs), input/output devices (keyboard, mouse, display), and other peripherals. The Linux operating system interacts with hardware through its kernel, which serves as an intermediary layer between software and hardware.

Kernel -

  • The kernel is the core component of the Linux operating system.

  • It interacts directly with the computer hardware, managing resources like CPU, memory, devices, and file systems.

  • It provides essential services to higher-level software and ensures they can run smoothly.

Shell -

  • The shell is a command-line interface that allows users to interact with the operating system.

  • It interprets user commands and communicates them to the kernel.

  • Users can type commands in the shell to perform tasks, navigate the file system, and manage programs.

Application -

  • User applications are the programs and software that users run on the Linux system.

  • These applications include text editors, web browsers, email clients, and various other software.

  • They rely on the kernel for essential services and communicate with the user through the shell.


XARGS

xargs is a Unix command which can be used to build and execute commands from standard input.


Zombie Process

A zombie process is a process that has finished its execution, but its parent process has not yet collected the exit status, so it will have an entry in the process table

When a process finishes, it sends a termination signal to its parent process. The parent process is responsible for collecting the exit status of its child process through system calls like wait() or waitpid(). If the parent does not collect this information, the child process entry remains in the process table, marked as a "zombie" process.
Zombie processes consume system resources, such as a process ID (PID) and some memory, but they do not actively use CPU time. The presence of a few zombie processes is usually not a problem. However, if a large number of zombie processes accumulate, it may indicate a problem with how the parent process is managing its child processes.

A process can create new processes using the fork() system call

Zombie processes are usually cleaned up automatically by the parent process or, if the parent process fails to do so, by the init process

For example, if the PID of the zombie process is 12345, you can use the following command in the terminal:

kill -s SIGCHLD 12345

If all else fails and you are unable to identify or fix the issue, a system reboot will clear all processes, including zombie processes.


LILO

Here are some key points about LILO:

  1. Boot Loader:

    • LILO is a boot loader that resides in the Master Boot Record (MBR) of the hard drive. It is responsible for loading the Linux kernel into memory and passing control to it during the boot process.
  2. Configuration:

    • The configuration file for LILO is usually /etc/lilo.conf. This file contains information about the location of the kernel image, boot parameters, and other configuration options.
  3. Installation:

    • To install or update LILO with a new configuration, the lilo command is typically used. Running lilo writes the boot loader code to the MBR and installs the configuration.
    sudo lilo
  1. Limitations:

    • One limitation of LILO is that it doesn't support dynamic loading of additional operating systems, and it requires updating the configuration and reinstalling the bootloader whenever changes are made.
  2. Legacy Status:

    • LILO was widely used in the past, but due to its limitations and the emergence of more feature-rich boot loaders like GRUB, it is less commonly used in modern Linux distributions.

In modern Linux systems, GRUB has become the default choice for a boot loader. GRUB offers more flexibility, support for a wider range of file systems, dynamic module loading, and a menu system that allows users to choose from multiple operating systems during boot. If you're working with a recent Linux distribution, chances are it uses GRUB instead of LILO.


Print all the files that do not have matching string

grep -L "your_string" $(find /path/to/search -type f)

grep -L "your_string": This part of the command searches for lines in each file that do not contain the specified string (your_string in this example). The -L option is used to print only the names of files with no matches.

$(...): This syntax is used to substitute the output of the find command into the grep command.


How to increase inode numbers

The number of inodes in a filesystem is typically set at the time of filesystem creation and is not easily changed afterward. However, there are a few strategies you can use to effectively increase the number of available inodes:

  1. Create a New Filesystem: If you need a significant increase in the number of inodes, one approach is to create a new filesystem with a higher inode density. You can use a filesystem type that allows for more inodes per unit of space, such as XFS or JFS, compared to ext4.

  2. Adjust Inode Density: Some filesystems allow you to adjust the inode density when creating the filesystem. For example, with the mkfs.ext4 command on Linux, you can use the -i option to set the bytes per inode ratio. A lower ratio means more inodes will be created, effectively increasing the number of available inodes.

     mkfs.ext4 -i <bytes-per-inode> /dev/sdX
    
  3. Resize the Filesystem: If your filesystem supports online resizing (e.g., ext4 with the resize2fs command), you can resize the filesystem to a larger size, which indirectly increases the number of available inodes. However, keep in mind that this won't increase the inode density; it will only provide more space for additional inodes based on the existing density.

  4. Use Multiple Filesystems: If you have multiple storage devices or partitions available, you can spread your data across multiple filesystems. Each filesystem will have its own pool of inodes, effectively increasing the total number of available inodes.

  5. Consider Alternatives: In some cases, it might be more practical to consider alternative storage solutions that inherently offer higher inode densities or more flexible inode management.

Before making any changes to your filesystem or storage configuration, it's crucial to back up your data and carefully plan the adjustments to avoid data loss or disruption to your system.


How to create swap mem

To create a swap memory (swap space) on a Linux system, you can follow these steps:

  1. Check Current Swap Usage (Optional): Before creating a new swap space, you can check the current swap usage on your system using the free or swapon command:

     free -h
    

    or

     swapon -s
    

    This will show you if there is already existing swap space and how much is being used.

  2. Create a Swap File or Swap Partition:

    • Swap File: If you want to create a swap file, you can use the fallocate or dd command to create a file of the desired size. For example, to create a 2 GB swap file:

        sudo fallocate -l 2G /swapfile
      

      Next, set the correct permissions on the swap file:

        sudo chmod 600 /swapfile
      
    • Swap Partition: If you prefer to create a swap partition, you can use a partitioning tool such as fdisk, parted, or gparted to create a new partition and set its type to Linux swap.

  3. Set Up Swap: For a swap file, you need to initialize it as swap space using the mkswap command:

     sudo mkswap /swapfile
    

    For a swap partition, you can directly use the partition as swap without additional initialization.

  4. Enable the Swap: To enable the swap space (swap file or swap partition), use the swapon command:

    • For a swap file:

        sudo swapon /swapfile
      
    • For a swap partition (e.g., /dev/sdXn where n is the partition number):

        sudo swapon /dev/sdXn
      
  5. Make Swap Permanent (Optional): To make the swap space persistent across reboots, add an entry for it in the /etc/fstab file:

    • For a swap file, add a line like:

        /swapfile   swap    swap    defaults    0   0
      
    • For a swap partition, add a line similar to:

        UUID=<swap-partition-UUID>   swap    swap    defaults    0   0
      

Replace <swap-partition-UUID> with the UUID of your swap partition, which you can find using the blkid command.

  1. Verify Swap: After enabling and configuring the swap space, you can verify its usage with the free or swapon command again to see the total swap space available and its usage.

     free -h
    

    or

     swapon -s
    

That's it! Your swap space should now be created and available for use on your Linux system.