Post

Linux Basics Cheat Sheet for Sysadmins

Linux Basics Cheat Sheet for Sysadmins

Linux Basics Cheat Sheet for Sysadmins

A handy reference for day-to-day Linux system administration.


🔹 File Management

  • List Files:
    1
    
    ls -l  # Long listing format
    
  • List Hidden Files:
    1
    
    ls -la  # Includes hidden files (those starting with .)
    
  • Copy Files:
    1
    
    cp source destination  # Copies a file or directory
    
  • Move Files / Rename:
    1
    
    mv source destination  # Moves or renames a file or directory
    
  • Remove Files:
    1
    
    rm file  # Deletes a file
    
  • Search for Files:
    1
    
    find /path -name "filename"  # Searches for a file by name
    
  • Search Inside Files:
    1
    
    grep "pattern" file  # Searches for a pattern inside a file
    

🔹 User & Group Management

  • Add a User:
    1
    
    sudo useradd username  # Adds a new user
    
  • Delete a User:
    1
    
    sudo userdel username  # Deletes a user
    
  • Add a User with Home Directory:
    1
    
    sudo useradd -m username  # Creates a home directory for the user
    
  • Change Password:
    1
    
    passwd username  # Changes the password for a user
    
  • Add User to Group:
    1
    
    sudo usermod -aG groupname username  # Adds a user to a group
    
  • List Groups:
    1
    
    groups username  # Lists groups a user belongs to
    

🔹 Process Management

  • List Processes:
    1
    
    ps aux  # Displays all running processes
    
  • Tree View of Processes:
    1
    
    pstree -p  # Shows processes in a tree format
    
  • Kill a Process:
    1
    2
    
    kill <pid>  # Sends a termination signal to a process
    kill -9 <pid>  # Forcefully kills a process
    
  • Monitor System Processes:
    1
    2
    
    top  # Displays real-time system processes
    htop  # Interactive process viewer (if installed)
    
  • Check Resource Usage by Process:
    1
    
    ps -eo pid,ppid,cmd,%mem,%cpu --sort=-%mem | head  # Sorts processes by memory usage
    

🔹 Service & Systemd Management

  • Start a Service:
    1
    
    sudo systemctl start service  # Starts a service
    
  • Stop a Service:
    1
    
    sudo systemctl stop service  # Stops a service
    
  • Restart a Service:
    1
    
    sudo systemctl restart service  # Restarts a service
    
  • Enable Service at Boot:
    1
    
    sudo systemctl enable service  # Enables a service to start at boot
    
  • Check Service Status:
    1
    
    sudo systemctl status service  # Displays the status of a service
    
  • List Failed Services:
    1
    
    systemctl --failed  # Lists all failed services
    

🔹 Disk & Storage Management

  • Check Disk Usage:
    1
    
    df -h  # Displays disk usage in human-readable format
    
  • Check Directory Size:
    1
    
    du -sh /path/to/directory  # Displays the size of a directory
    
  • List Block Devices:
    1
    
    lsblk  # Lists information about block devices
    
  • Check Disk Partitions:
    1
    
    fdisk -l  # Lists disk partitions
    
  • Mount a Filesystem:
    1
    
    sudo mount /dev/sdX1 /mnt  # Mounts a filesystem to /mnt
    
  • Unmount a Filesystem:
    1
    
    sudo umount /mnt  # Unmounts a filesystem
    
  • Check Inodes:
    1
    
    df -i  # Displays inode usage
    

🔹 Networking

  • Check IP Address:
    1
    
    ip addr show  # Displays network interfaces and IP addresses
    
  • Check Default Route:
    1
    
    ip route  # Displays the default gateway
    
  • Ping a Host:
    1
    
    ping -c 4 example.com  # Sends 4 ICMP echo requests to a host
    
  • Check Open Ports:
    1
    
    ss -tuln  # Displays listening ports
    
  • DNS Lookup:
    1
    2
    
    dig example.com  # Performs a DNS lookup
    nslookup example.com  # Alternative DNS lookup tool
    
  • Check Connectivity:
    1
    
    curl -I http://example.com  # Fetches HTTP headers to check connectivity
    
  • Test Port Connectivity:
    1
    
    nc -zv host port  # Tests if a port is open on a host
    

🔹 Permissions & Ownership

  • Change Ownership:
    1
    
    sudo chown user:group file  # Changes ownership of a file
    
  • Change Permissions:
    1
    
    chmod 755 file  # Sets read, write, execute permissions for user, and read/execute for others
    
  • Symbolic Modes:
    1
    2
    3
    
    chmod u+x file   # Adds execute permission for the user
    chmod g-w file   # Removes write permission for the group
    chmod o-r file   # Removes read permission for others
    

🔹 Logs & Monitoring

  • View System Logs:
    1
    
    journalctl -xe  # Displays recent system logs with details
    
  • Logs for a Service:
    1
    
    journalctl -u servicename  # Displays logs for a specific service
    
  • Follow Logs in Real Time:
    1
    2
    
    tail -f /var/log/syslog  # Follows the system log in real-time
    tail -f /var/log/messages  # Follows the messages log in real-time
    
  • Check Last Boot Messages:
    1
    
    dmesg | less  # Displays kernel messages from the last boot
    

🔹 Package Management

Debian/Ubuntu

  • Update Packages:
    1
    
    sudo apt update && sudo apt upgrade  # Updates package lists and upgrades installed packages
    
  • Install a Package:
    1
    
    sudo apt install package  # Installs a package
    
  • Remove a Package:
    1
    
    sudo apt remove package  # Removes a package
    

RHEL/CentOS/Fedora

  • Update Packages:
    1
    2
    
    sudo yum update  # Updates all packages
    sudo dnf upgrade  # Updates all packages (dnf for newer systems)
    
  • Install a Package:
    1
    2
    
    sudo yum install package  # Installs a package
    sudo dnf install package  # Installs a package (dnf for newer systems)
    
  • Remove a Package:
    1
    
    sudo yum remove package  # Removes a package
    

🔹 Security & Hardening

  • Check for Active Users:
    1
    2
    
    who  # Displays logged-in users
    w  # Displays logged-in users with additional details
    
  • Check Last Logins:
    1
    
    last  # Displays login history
    
  • Check Sudo Access:
    1
    
    sudo -l  # Lists sudo privileges for the current user
    
  • Lock a User Account:
    1
    
    sudo usermod -L username  # Locks a user account
    
  • Check Listening Ports:
    1
    
    ss -tulnp  # Displays listening ports with process details
    
  • Enable Firewall (UFW example):
    1
    2
    3
    
    sudo ufw enable  # Enables the firewall
    sudo ufw allow ssh  # Allows SSH traffic
    sudo ufw status  # Displays firewall status
    
  • Generate SSH Key Pair:
    1
    
    ssh-keygen -t ed25519  # Generates an SSH key pair using the ed25519 algorithm
    

🔹 Quick Recovery & Troubleshooting

  • Check System Load:
    1
    
    uptime  # Displays system uptime and load averages
    
  • Check Memory Usage:
    1
    
    free -h  # Displays memory usage in human-readable format
    
  • Check Disk IO:
    1
    
    iostat  # Displays disk I/O statistics
    
  • Check Open Files:
    1
    
    lsof | less  # Lists open files
    
  • Check Failed SSH Logins:
    1
    
    grep "Failed password" /var/log/auth.log  # Searches for failed SSH login attempts
    

🔹 Legend (Linux Terms)

  • PID – Process ID.
  • UID – User ID.
  • GID – Group ID.
  • TTY – Terminal session.
  • Daemon – Background process/service.
  • Inode – Index node storing file metadata.
  • Mount Point – Directory where a filesystem is attached.
  • Systemd – Init system and service manager in modern Linux.
  • SELinux/AppArmor – Linux security frameworks.

🔹 Best Practices

  • Keep your system updated regularly.
  • Use man <command> or --help to learn command options.
  • Use tmux or screen for persistent sessions over SSH.
  • Always set up SSH key-based authentication instead of passwords.
  • Monitor disk usage with alerts (df -h, du -sh).
  • Never run daily tasks as root — use sudo.
  • Automate repetitive tasks with shell scripts.
  • Regularly review /var/log for errors and security issues.
This post is licensed under CC BY 4.0 by the author.