dpway

The middle way!

[Devops][001] Linux

Tags = [ devops ]

devops

Why Learn Linux?

  • Linux skills are in high demand for careers in IT, cybersecurity, DevOps, and system administration.
  • It provides a deep understanding of how operating systems work.
  • It's a cost-effective and powerful tool for personal and professional projects.

Linux Folder Structure

Linux uses a hierarchical tree structure for its filesystem, with the root directory / at the top. Each folder has a specific purpose, ensuring system organization and functionality.

Key Directories and Their Roles

DirectoryPurpose
/Root directory, base of all files and folders.
/binEssential user binaries (e.g., ls, cp).
/bootBoot loader files and kernel.
/devDevice files representing hardware (e.g., /dev/sda for disks).
/etcConfiguration files for system and applications.
/homeUser home directories (e.g., /home/user).
/libShared libraries for essential binaries.
/mediaMount points for external devices (USB, CD-ROM).
/mntTemporary mount points.
/optOptional software installed by users.
/procVirtual filesystem for process and kernel info (e.g., /proc/cpuinfo).
/rootHome directory for the root user.
/sbinSystem binaries for administrative tasks (e.g., reboot, ifconfig).
/tmpTemporary files, cleared on reboot.
/usrUser-installed software and shared resources.
/varFiles that change often, like logs (/var/log) or web files (/var/www).

Linux Commands

Switches to the root user

CommandBehavior
sudo -iOpens an interactive shell as the root user, with root's environment loaded, such as PATH, and loads settings from /root/.bashrc or /root/.profile
sudo -sOpens a shell as the root user but retains the current user's environment variables.
sudo suSwitches to the root user. Uses the current user's password (via sudo). Inherits the current user's environment variables unless modified.
suSwitches to root. Does not require sudo; works if you know the root password. Does not inherit the user's environment by default (clean slate).

Basic Linux Commands

  • ls: Lists files and directories.
  • ls -lta: Used to list directory contents in a detailed (long) format, showing hidden files and sorting the output based on the modification time.
  • ls -ltr: To reverse the order, use -t with -r.
  • cd: Changes directories.
  • pwd: Displays the current directory.
  • mkdir: Creates a new directory.
  • mkdir -p: Used to create directories along with any parent directories that do not already exist.
  • cp: Copies files or directories.
  • cp -r: Used to copy directories and their contents recursively.
  • mv: Moves or renames files.
  • rm: Deletes files or directories.
  • rm -r: Used to remove directories and their contents recursively.
  • rm -rf: A powerful and potentially dangerous command used to forcefully and recursively remove files and directories without any prompts or confirmation.
  • chmod: Changes file permissions.
  • man: Displays manuals for commands (e.g., man ls).
  • shutdown now: Used to immediately shut down the system.
  • clear: Clears all visible text from the terminal.
  • whoami: Prints the current user’s username to the terminal.
  • echo: Used to display a line of text or a variable's value to the terminal.
  • echo "This is a test." > myfile.txt: This will write This is a test. to myfile.txt.
  • echo "This is a test." >> myfile.txt: Used to append the text This is a test. to a file called myfile.txt.
  • tail: used to display the last part (typically the last 10 lines) of a file or output from a command.
  • tail -n 20 filename.txt: Display the last [number] lines of the file. For example, -n 20 shows the last 20 lines.
  • tail -f /var/log/syslog: Follow the file, showing new lines as they are added. This is useful for watching log files in real-time.

Set Up a Static IP

  1. Identify the Interface:
    • Use ip addr or ifconfig to find your interface name (e.g., eth0, enp0s3).
  2. Edit Network Configuration:
    • For Netplan:
      sudo nano /etc/netplan/00-installer-config.yaml
      
      Example Static IP Configuration:
      network:
        version: 2
        ethernets:
          enp0s3:
            dhcp4: no
            addresses: [192.168.1.100/24]
            gateway4: 192.168.1.1
            nameservers:
              addresses: [8.8.8.8, 8.8.4.4]
      
    • Apply changes:
      sudo netplan apply
      

Vim

Vim is a powerful text editor that is widely used in Linux environments. It has a steep learning curve, but once you get the hang of it, it can be extremely efficient for text manipulation. Vim operates in different modes: Normal mode, Insert mode, and Command mode. Here’s a basic guide to get started with Vim.

Vim Modes:

  1. Normal Mode (default mode): Used for navigating and editing text. Press Esc to return to Normal mode.
  2. Insert Mode: Used for typing text. Press i to enter Insert mode from Normal mode.
  3. Command Mode: Used for saving, quitting, searching, and more. Press : (colon) from Normal mode to enter Command mode.

Basic Navigation:

  • Arrow keys: Move the cursor up, down, left, and right.
  • gg: Move to the beginning of the file.
  • G: Move to the end of the file.
  • w: Move to the beginning of the next word.
  • b: Move to the beginning of the previous word.
  • 0: Move to the beginning of the current line.
  • $: Move to the end of the current line.

Editing Text:

  • i: Enter Insert mode before the cursor.
  • cw: Change word.
  • ce: Change to the end of word.
  • r [char]: Replace the character under the cursor with [char].
  • x: Delete the character under the cursor.
  • dd: Delete the current line.
  • yy: Copy (yank) the current line.
  • p: Paste the copied or deleted content after the cursor.
  • :s/old/new/: Replace the first occurrence of old with new on the current line.
  • :s/old/new/g: Replace all occurrences of old with new on the current line.
  • :%s/old/new/: Replace all occurrences of old with new in the entire file.

Saving and Quitting:

  • :w: Save the file.
  • :w filename: Save the file with a new name.
  • :q: Quit Vim.
  • :wq or ZZ: Save and quit.
  • :q!: Quit without saving (force quit).

Searching:

  • /search_term: Search for search_term forward in the text.
  • ?search_term: Search for search_term backward in the text.
  • n: Move to the next occurrence of the search term.
  • N: Move to the previous occurrence.

Undo and Redo:

  • u: Undo the last change.
  • Ctrl + r: Redo the undone change.

Monitor

  • free -m: Displays memory usage statistics, including total, used, free, and swap memory in megabytes.
  • df -h: Shows disk space usage in a human-readable format (e.g., GB, MB).
  • top: Displays a dynamic, real-time view of system processes, CPU usage, memory usage, and more.
  • ps aux: Shows all processes running on the system.
  • ss -tuln: This shows active listening ports (t for TCP, u for UDP, l for listening, n for numeric output).
  • uptime: Displays how long the system has been running, including load averages for the last 1, 5, and 15 minutes.
  • watch: Runs a command repeatedly and displays the output. Useful for monitoring changing system metrics.
  • lsof: Lists open files and the processes that opened them.
  • journalctl -xe: Displays logs from the systemd journal, which contains information about system processes, services, and errors.
  • ping: Used to test the network connectivity between your machine and another host.
  • telnet: Used for testing and troubleshooting network services, such as web servers, mail servers, and other services running on a specific port.
  • traceroute: Used for testing and troubleshooting network services, such as web servers, mail servers, and other services running on a specific port.

Management

  • sudo adduser dpway: This will create a new user dpway, along with the default user directory (e.g., /home/dpway) and other necessary configurations.
  • sudo addgroup dev: his creates a new group called dev on the system.
  • sudo usermod -aG dev dpway: This adds the user dpway to the dev group.
  • sudo deluser dpway dev: To remove the user dpway from the dev group.
  • sudo deluser dpway: To delete a user without removing their files.
  • sudo deluser --remove-all-files dpway: To delete a user and their home directory/files.
  • groups dpway: To show the groups for the user dpway.
  • sudo chown -R username:groupname directory: To change ownership for all files and subdirectories within a directory, use the -R (recursive) option.

Change Mode Command

Used to change the permissions (read, write, execute) of files and directories.

File Permissions:

  • r: Read (view file content)
  • w: Write (modify the file)
  • x: Execute (run the file as a program)

Users:

  • u: User (file owner)
  • g: Group (users in the same group)
  • o: Others (everyone else)
  • a: All (user, group, others)

Modes:

  1. Symbolic Mode:

    • + : Add permission
    • - : Remove permission
    • = : Set permission exactly

    Examples:

    • Add execute permission to the user: chmod u+x file.txt
    • Remove write permission from the group: chmod g-w file.txt
    • Set read and write permissions for the user: chmod u=rw file.txt
    • Set read and write permissions for the user, read permission for the group: chmod u=rw, g=r file.txt
  2. Numeric Mode: Permissions are represented by a 3-digit number:

    • r=4, w=2, x=1

    Examples:

    • chmod 755 file.txt: User (owner) gets rwx, Group and Others get r-x
    • chmod 644 file.txt: User gets rw-, Group and Others get r--
  3. Recursive Option (-R): Use the -R option to apply changes to all files and subdirectories within a directory:

    • chmod -R +x /path/to/directory: Adds execute permission to all files in a directory.