Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Essential Linux System Administration Commands

Tech 1

System Operations

Help Commands

1. The man Command

The man (manual) command displays comprehensive documentation for system utilities and configuration files.

  • To view the manual for the ls command: man ls.
  • Manual pages are categorized into sections. Use man man to see information about the manual itself.

2. The help Command

Commands are categorized as internal (built into the shell) or external (seperate programs).

  • For internal commands like cd, use: help cd.
  • For external commands like ls, use: ls --help.

3. The info Command

The info system provides detailed, often more extensive, documentation than man. Use info ls for information on the ls command.

File Deletion Commands

  • rm (Remove): Deletes files or directories.
    • rm -f file.tar.gz: Forcefully deletes the specified file.
    • rm -rf nginx-1.7.8: Recursively and forcefully deletes the specified directory and all its contents.
      • -r or -R: Operate recursively.
      • -f: Force removal without confirmation.

Disk Management Commands

1. Viewing Disk Space

  • df -h: Displays disk usage for mounted filesystems in a human-readable format.

2. Assessing File and Directory Size

  • ll -h: Lists files and directories with human-readable sizes.
  • du -h: Summarizes disk usage of files and directories in human-readable format. For example: du -h /data.

3. Analyzing Disk I/O

  • iostat -x: Displays extended I/O statistics.
    • Key CPU metrics:
      • %iowait: CPU idle time waiting for I/O.
      • %idle: CPU idle time.
    • Key disk metrics:
      • %util: Percentage of CPU time spent on I/O requests.
      • await: Average I/O request wait time.
      • svctm: Average I/O request service time.
      • avgqu-sz: Average I/O queue length.

Memory Management Commands

1. System Memory Overview

  • free -h: Shows memory usage.
    • total: Total physical RAM.
    • used: Memory used (includes caches and buffers).
    • free: Unused memory.
    • buff/cache: Memory used for kernel buffers and page cache.
    • The -/+ buffers/cache line shows memory used by applications (used - buff/cache) and memory available to applications (free + buff/cache).

2. Process Memory Details

  • ps with memory flags: ps -o vsz,rss,sz,size,pid -p [PID]
    • VSZ: Virtual memory size.
    • RSS: Resident Set Size, the non-swapped physical memory used.

CPU Information Commands

  • lscpu: Provides detailed CPU architecture information, including:
    • Number of CPU cores and threads.
    • CPU model name and frequency.
    • Cache sizes.
    • Virtualization support.

Package Management

YUM (CentOS/RHEL) and APT (Ubuntu/Debian)

  • Check for installed package manager: which yum or which apt-get.
  • Search for a package: yum search jdk or apt-cache search jdk.
  • Install a package: yum install package_name or apt-get install package_name.
  • Remove a package: yum erase package_name or apt-get purge package_name.

Homebrew (macOS)

  • brew is a package manager for macOS, similar to yum/apt-get.

Installation Methods

  • Using Package Files: rpm -i package.rpm (CentOS) or dpkg -i package.deb (Ubuntu).
  • Using Archive Files: Download a .tar.gz archive with wget [URL], then extract with tar -xzf archive.tar.gz.

Locating Installed Software

  • Use which to find the executable path: which java.

Process Execution

  • Interactive Execution: Run with ./program_name or, if in PATH, just program_name.
  • Background Execution: Use nohup to keep a process running after terminal logout: nohup command > output.log 2>&1 &.
  • Service Management: Use system service managers (systemctl, service) for daemons.

Terminating Processes

Find and terminate a process by keyword:

ps -ef | grep keyword | awk '{print $2}' | xargs kill -9

Or terminate by Process ID (PID): kill -9 [PID].


File Operations

Editing Files with Vim

Vim operates in three modes:

  1. Command Mode: Default mode for navigation and commands (e.g., x to delete, i to insert).
  2. Insert Mode: For text entry (entered by pressing i).
  3. Command-Line Mode: For save/quit commands (entered by pressing :).

Basic Vim Workflow

  1. Open a file: vim filename.txt.
  2. Press i to enter Insert mode and edit.
  3. Press Esc to return to Command mode.
  4. Type :wq and press Enter to save and quit.

Transferring Files

  • scp (Secure Copy): scp -r local_directory user@remote_host:/remote/path/.

Viewing File Contents

  • cat: Outputs entire file.
  • head -n 5 file.log: Shows first 5 lines.
  • tail: Shows last lines of a file.

Listing Files and Permissions

  • ls -l: Detailed list with permissions.
  • The first character indicates file type (- for regular file, d for directory).
  • ls -la: Lists all files, including hidden ones.

Permission Escalation

  • su: Switch user (e.g., su root).
  • sudo -i: Start a shell with root privileges.

Searching for Installed Files

  • Use pipes (|) and grep: rpm -qa | grep jdk or dpkg -l | grep jdk.

Basic File Operations

  • Create a directory: mkdir new_folder.
  • Remove a directory: rm -r folder_name.

Network and Process Management

Network Commands

  • netstat: Displays network connections, routing tables, interface statistics.
  • Example: netstat -na | grep 3306 to check for MySQL connections.

Process Management

  • Viewing Processes: ps -ef | grep java.
  • Terminating Processes:
    • kill [PID]: Sends SIGTERM (graceful termination).
    • kill -9 [PID]: Sends SIGKILL (forceful termination).

System Resource Monitoring

  • top: Dynamic, real-time view of system processes and resource usage (CPU, memory).
  • Key columns include PID, USER, %CPU, %MEM, COMMAND.
  • To monitor a specific process: top -p [PID].
Tags: Linux

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.