Dude Engineering

View Original

Essential Commands for System Administrators

Linux powers most enterprise servers and system administrators stand as pivotal figures, ensuring everything runs smoothly. Yet, the fundamentals remain as vital as ever. This article is dedicated to the essential commands every system administrator should master. From managing files and directories to monitoring system performance and securing networks, these commands form the bedrock of effective system administration. Perfect for both newcomers refining their skills and seasoned pros seeking a refresher, this guide aims to fortify your command line prowess, ensuring you can navigate the complexities of modern IT environments with ease and efficiency.

File and Directory Management

  • ls: List directory contents.

    • a: Show hidden files.
    • l: Use a long listing format to display information.
    • h: Display file size in human readable format (KB, MB).

      Example: ls -lah

  • cd: Change the working directory.

    Example: cd /home/user

  • pwd: Print the current working directory.

  • touch: Create a new empty file.

    Example: touch newfile.txt

  • cp: Copy files and directories.

    • r: Copy directories recursively.

      Example: cp -r /path/to/source /path/to/destination

  • mv: Move (or rename) files and directories.

    Example: mv /path/to/source /path/to/destination

  • rm: Remove files and directories.

    • r: Remove directories and their contents recursively.
    • f: Force removal without confirmation.

      Example: rm -rf /path/to/directory

File Searching

  • find: Search for files in a directory hierarchy.

    • name: Search by file name.
    • type: Search by type.

      Example: find / -name "*.txt" -type f

  • grep: Print lines that match a pattern.

    • r or R: Read all files under each directory, recursively.
    • i: Ignore case distinctions in both the pattern and the input files.

      Example: grep -ri "error" /var/log

Network

  • ping: Send ICMP ECHO_REQUEST to network hosts.

    Example: ping google.com

  • netstat: Print network connections, routing tables, interface statistics, masquerade connections, and multicast memberships.

    • t: Display TCP connections.
    • u: Display UDP connections.
    • n: Show numerical addresses instead of resolving hosts.
    • l: Show only listening sockets (i.e., display the servers).

      Example: netstat -tuln

  • ifconfig or ip: Display or configure a network interface.

    Example: ifconfig or ip a

  • ssh: OpenSSH remote login client.

    Example: ssh username@hostname

  • scp: Secure copy (remote file copy program).

    Example: scp /path/to/source username@hostname:/path/to/destination

System Information

  • top: Display Linux processes.
  • df: Report file system disk space usage.

    • h: Print sizes in human readable format (e.g., KB, MB).

      Example: df -h

  • du: Estimate file and directory space usage.

    • h: Print sizes in human readable format (e.g., KB, MB).
    • s: Summarize and just provide the total for each argument.

      Example: du -sh /path/to/directory

  • free: Display amount of free and used memory in the system.

    • h: Display in human readable format.

      Example: free -h

  • uname: Print system information.

    • a: Print all information.

      Example: uname -a

Package Management

  • apt-get (Debian based systems): APT package handling utility.

    • install: Install new packages.
    • remove: Remove a package.
    • update: Update package list.
    • upgrade: Upgrade all upgradable packages.

      Example: sudo apt-get update && sudo apt-get upgrade

  • yum (Red Hat based systems): Command line package management utility.

    • install: Install new packages.
    • remove: Remove a package.
    • update: Update all or specified packages.

      Example: sudo yum update

  • dnf (Fedora): Next-generation dependency resolver and high-level package management tool.

    • install: Install new packages.
    • remove: Remove a package.
    • upgrade: Upgrade all or specified packages.

      Example: sudo dnf upgrade

Text Processing

  • cat: Concatenate files and print on the standard output.

    Example: cat file.txt

  • more or less: View the contents of a text file one screen at a time.

    Example: less file.txt

  • head: Output the first part of files.

    Example: head file.txt

  • tail: Output the last part of files.

    • f: Follow the file and print new lines as they are added.

      Example: tail -f /var/log/syslog

  • wc: Print newline, word, and byte counts for each file.

    • l: Print the newline counts.

      Example: wc -l file.txt

  • sort: Sort lines in text files.

    Example: sort file.txt

  • cut: Remove sections from each line of files.

    Example: cut -d':' -f1 /etc/passwd

  • awk: Pattern scanning and processing language.

    Example: awk '{print $1}' file.txt

  • sed: Stream editor for filtering and transforming text.

    Example: sed 's/foo/bar/g' file.txt

Permissions

  • chmod: Change file mode bits.

    Example: chmod 755 script.sh

  • chown: Change file owner and group.

    Example: chown user:group file.txt

  • chgrp: Change group ownership.

    Example: chgrp group file.txt

Process Management

  • ps: Report a snapshot of the current processes.

    • e: Select all processes.
    • f: Do full-format listing.

      Example: ps -ef

  • top: Display Linux processes.

  • htop: Interactive process viewer (if installed).
  • kill: Send a signal to a process.

    • 9: SIGKILL signal.

      Example: kill -9 PID

  • pkill: Signal processes based on name and other attributes.

    Example: pkill processname

  • bg: Put a process to background.

  • fg: Put a process to foreground.

Disk Usage

  • df: Disk filesystem – report file system disk space usage.

    • h: Human readable.

      Example: df -h

  • du: Estimate file and directory space usage.

    • h: Human readable.
    • s: Display only a total.

      Example: du -sh /path/to/directory

Other

  • man: An interface to the on-line reference manuals.

    Example: man ls

  • history: Command History.

  • alias: Create an alias for a command.

    Example: alias ll='ls -lah'