Detailed Notes on Linux

 ## Detailed Notes on Linux


### Introduction: History of Linux


Linux is a Unix-like operating system kernel created by Linus Torvalds in 1991. It started as a personal project by Torvalds while he was a student at the University of Helsinki. He announced his work on the MINIX newsgroup and released the initial version under the GNU General Public License (GPL). This allowed others to freely use, modify, and distribute the software. The collaborative development model quickly attracted developers worldwide, leading to a robust, open-source operating system that powers a significant portion of modern technology infrastructure.


### How to Install Linux on a Local Machine


1. **Download a Linux Distribution**: Choose a distribution (e.g., Ubuntu, Fedora, Debian) and download the ISO image from the official website.

2. **Create Bootable Media**: Use tools like Rufus (Windows) or dd (Linux) to create a bootable USB drive.

   - **Rufus**:

     1. Insert USB drive.

     2. Open Rufus, select the downloaded ISO.

     3. Click "Start" to create the bootable drive.

   - **dd** (Linux):

     ```bash

     sudo dd if=path/to/linux.iso of=/dev/sdX bs=4M

     ```

     Replace `sdX` with your USB device.

3. **Boot from USB**: Restart your computer and boot from the USB drive (change boot order in BIOS/UEFI if necessary).

4. **Install Linux**:

   - Select "Install Linux" from the boot menu.

   - Follow the installation wizard: partition your drive, set up user account, configure settings.

   - Complete the installation and reboot.


### Shutdown and Reboot Commands


- **Shutdown**:

  ```bash

  sudo shutdown -h now       # Shutdown immediately

  sudo shutdown -h +5        # Shutdown in 5 minutes

  sudo poweroff              # Equivalent to shutdown

  sudo halt                  # Stop all processes and halt the machine

  ```

- **Reboot**:

  ```bash

  sudo reboot                # Reboot immediately

  sudo shutdown -r now       # Shutdown and restart immediately

  sudo shutdown -r +5        # Reboot in 5 minutes

  ```


### Basic Commands


- **ls**: List directory contents

  ```bash

  ls -l                      # Long listing format

  ls -a                      # Show all files, including hidden

  ```

- **cd**: Change directory

  ```bash

  cd /path/to/directory      # Change to specified directory

  cd ~                       # Change to home directory

  cd ..                      # Move one directory up

  ```

- **cat**: Concatenate and display files

  ```bash

  cat file.txt               # Display file contents

  ```

- **touch**: Create empty files or update file timestamps

  ```bash

  touch newfile.txt          # Create a new empty file

  ```

- **mkdir**: Create directories

  ```bash

  mkdir newdir               # Create new directory

  ```

- **pwd**: Print working directory

  ```bash

  pwd                        # Show current directory path

  ```

- **nl**: Number lines of files

  ```bash

  nl file.txt                # Display file with line numbers

  ```

- **wc**: Word count

  ```bash

  wc file.txt                # Display line, word, and byte counts

  wc -l file.txt             # Display line count

  wc -w file.txt             # Display word count

  wc -c file.txt             # Display byte count

  ```

- **|**: Pipe, used to pass the output of one command as input to another

  ```bash

  ls -l | grep 'pattern'     # Pipe ls output to grep

  ```


### Advanced Commands


- **find**: Search for files in a directory hierarchy

  ```bash

  find /path -name 'file.txt' # Find files named 'file.txt' starting at /path

  ```

- **grep**: Search text using patterns

  ```bash

  grep 'pattern' file.txt    # Search for 'pattern' in file.txt

  grep -r 'pattern' /path    # Recursively search in /path

  ```

- **sed**: Stream editor for filtering and transforming text

  ```bash

  sed 's/old/new/' file.txt  # Replace 'old' with 'new' in file.txt

  ```

- **cut**: Remove sections from each line of files

  ```bash

  cut -d ':' -f 1 /etc/passwd # Display first field of /etc/passwd using ':' as delimiter

  ```

- **awk**: Pattern scanning and processing language

  ```bash

  awk '{print $1}' file.txt  # Print first column of file.txt

  ```


### Head and Tail Commands


- **head**: Output the first part of files

  ```bash

  head -n 10 file.txt        # Show first 10 lines of file.txt

  ```

- **tail**: Output the last part of files

  ```bash

  tail -n 10 file.txt        # Show last 10 lines of file.txt

  tail -f file.txt           # Continuously monitor file for new lines

  ```


### Vim Editor


- **Open a file**:

  ```bash

  vim file.txt

  ```

- **Basic Commands**:

  - `i`: Enter insert mode

  - `Esc`: Exit insert mode

  - `:w`: Save changes

  - `:q`: Quit vim

  - `:wq`: Save and quit

  - `:q!`: Quit without saving


### Linux User Management


- **Add a user manually**:

  ```bash

  sudo useradd -m username   # Create user with home directory

  sudo passwd username       # Set user password

  ```

- **Modify a user**:

  ```bash

  sudo usermod -aG groupname username # Add user to a group

  ```

- **Delete a user**:

  ```bash

  sudo userdel -r username   # Remove user and their home directory

  ```


### Linux Permission Management


- **View file permissions**:

  ```bash

  ls -l filename

  ```

- **Change permissions**:

  ```bash

  chmod 755 filename         # Change permissions to rwxr-xr-x

  chmod u+x filename         # Add execute permission for the owner

  ```

- **Change ownership**:

  ```bash

  sudo chown user:group filename # Change owner and group

  ```


### Linux Partition Management: AWS EBS


- **Attach an EBS volume**:

  1. Open the EC2 console.

  2. Attach the volume to an instance.

- **Mount the EBS volume**:

  ```bash

  sudo mkdir /mnt/ebs

  sudo mount /dev/xvdf /mnt/ebs   # Mount the volume

  ```

- **Format the EBS volume** (if needed):

  ```bash

  sudo mkfs -t ext4 /dev/xvdf

  ```


### How to Break Root Password (for RHEL Certification)


1. **Reboot the system and enter GRUB menu**.

2. **Edit the GRUB entry**:

   - Press `e` to edit the boot parameters.

   - Find the line starting with `linux` or `linux16`.

   - Append `rd.break` at the end of the line.

3. **Boot with the modified parameters**:

   - Press `Ctrl + X` to boot.

4. **Remount the root filesystem**:

   ```bash

   mount -o remount,rw /sysroot

   ```

5. **Change root into the sysroot directory**:

   ```bash

   chroot /sysroot

   ```

6. **Reset the root password**:

   ```bash

   passwd root

   ```

7. **Re-label the filesystem for SELinux**:

   ```bash

   touch /.autorelabel

   ```

8. **Exit and reboot**:

   ```bash

   exit

   exit

   ```


### Linux SSH and SSH Keygen


- **Generate SSH key pair**:

  ```bash

  ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

  ```

- **Copy public key to remote server**:

  ```bash

  ssh-copy-id user@remote_host

  ```

- **Connect to remote server**:

  ```bash

  ssh user@remote_host

  ```


This comprehensive guide covers the fundamental and advanced aspects of Linux needed for efficient system management and preparation for certification.

Comments