Linux Command Line Basics

    A beginner-friendly guide to the essential commands you'll use every day on your VPS

    If you've just set up your first VPS, the Linux command line can feel overwhelming. This guide covers the essential commands you'll use on a daily basis — from navigating files to managing services and troubleshooting. Every command includes a brief explanation so you understand what it does, not just how to run it.

    What You'll Learn

    Navigate the filesystem and manage files
    View and edit configuration files
    Understand permissions and ownership
    Install packages and manage services
    Monitor processes and system resources
    Use pipes, redirection, and search tools

    💡 Convention: Throughout this guide, lines starting with # inside code blocks are comments explaining the command. The $ symbol represents your shell prompt — don't type it.

    1. Connecting via SSH

    SSH (Secure Shell) is how you access your VPS remotely. You'll need an SSH client — macOS and Linux have one built in; on Windows, use PowerShell, Windows Terminal, or PuTTY.

    Connect to your VPS
    # Basic SSH connection (replace with your server's IP)
    ssh root@YOUR_SERVER_IP
    
    # Connect with a specific SSH key
    ssh -i ~/.ssh/my_key root@YOUR_SERVER_IP
    
    # Connect on a non-standard port
    ssh -p 2222 root@YOUR_SERVER_IP

    Related Documentation

    • SSH Keys — Set up key-based authentication (more secure than passwords)
    • VNC Console Access — Access your server via a web-based console if SSH is unavailable

    3. Working with Files & Directories

    Creating files and directories
    # Create an empty file
    touch myfile.txt
    
    # Create a directory
    mkdir mydir
    
    # Create nested directories (parents included)
    mkdir -p /var/www/mysite/html
    Copying, moving, and renaming
    # Copy a file
    cp file.txt /tmp/file.txt
    
    # Copy a directory recursively
    cp -r /var/www/mysite /var/www/mysite-backup
    
    # Move or rename a file
    mv oldname.txt newname.txt
    
    # Move a file to another directory
    mv file.txt /tmp/
    Deleting files and directories
    # Remove a file
    rm file.txt
    
    # Remove a directory and all its contents
    rm -r mydir
    
    # Remove without confirmation prompts (use with caution!)
    rm -rf mydir

    ⚠️ Caution: rm -rf is permanent and does not go to a trash folder. Always double-check the path before running it, especially as root.

    4. Viewing & Editing Files

    Viewing file contents
    # Display entire file contents
    cat /etc/hostname
    
    # View a long file page by page (press q to quit)
    less /var/log/syslog
    
    # Show the first 20 lines
    head -n 20 /var/log/syslog
    
    # Show the last 20 lines
    tail -n 20 /var/log/syslog
    
    # Follow a log file in real time (Ctrl+C to stop)
    tail -f /var/log/syslog

    Editing Files with nano

    nano is the most beginner-friendly text editor available on most Linux systems:

    Using nano
    # Open a file for editing
    nano /etc/ssh/sshd_config
    
    # Keyboard shortcuts inside nano:
    # Ctrl+O    Save the file
    # Ctrl+X    Exit nano
    # Ctrl+W    Search for text
    # Ctrl+K    Cut the current line
    # Ctrl+U    Paste the cut line

    💡 Tip: If you see vim or vi open by accident, press Esc then type :q! and press Enter to quit without saving.

    5. Permissions & Ownership

    Every file and directory has an owner, a group, and permission settings that control who can read, write, or execute it. When you run ls -la, the output looks like this:

    Example output
    -rw-r--r-- 1 www-data www-data  1234 Jun 15 10:30 index.html
    drwxr-xr-x 2 root     root      4096 Jun 15 10:30 config/
    SymbolMeaning
    rRead permission
    wWrite permission
    xExecute permission (or enter for directories)
    -Permission not set

    Permissions are displayed in three groups of three: owner, group, others. So rw-r--r-- means: owner can read+write, group can read, others can read.

    Changing permissions and ownership
    # Change file permissions using numeric mode
    chmod 644 file.txt        # Owner: rw, Group: r, Others: r
    chmod 755 script.sh       # Owner: rwx, Group: rx, Others: rx
    chmod 600 secrets.key     # Owner: rw, no access for others
    
    # Change file owner
    chown deploy:deploy file.txt
    
    # Change owner recursively for a directory
    chown -R www-data:www-data /var/www/mysite
    
    # Change only the group
    chgrp www-data file.txt

    💡 Common permission numbers: 644 for regular files, 755 for directories and scripts, 600 for private keys, 700 for the .ssh directory.

    6. Users & Sudo

    On a Linux server, root is the superuser with unrestricted access. Best practice is to use a regular user and elevate privileges with sudo only when needed.

    User management
    # See who you're logged in as
    whoami
    
    # Switch to another user
    su - deploy
    
    # Run a single command as root
    sudo systemctl restart nginx
    
    # Open a root shell (use sparingly)
    sudo -i
    
    # Create a new user
    sudo adduser newuser
    
    # Add a user to the sudo group (Ubuntu/Debian)
    sudo usermod -aG sudo newuser
    
    # Add a user to the wheel group (AlmaLinux/Rocky)
    sudo usermod -aG wheel newuser

    For full details on creating a sudo user and securing SSH access, see our initial setup guides for Ubuntu 24.04, Debian 13, or AlmaLinux 9.

    7. Package Management

    Package managers let you install, update, and remove software. The commands differ between distributions:

    Ubuntu / Debian (apt)

    APT commands
    # Update the package index
    sudo apt update
    
    # Upgrade all installed packages
    sudo apt upgrade -y
    
    # Install a package
    sudo apt install nginx -y
    
    # Remove a package
    sudo apt remove nginx
    
    # Remove a package and its config files
    sudo apt purge nginx
    
    # Search for a package
    apt search redis
    
    # Show package details
    apt show nginx

    AlmaLinux / Rocky Linux (dnf)

    DNF commands
    # Update all packages
    sudo dnf update -y
    
    # Install a package
    sudo dnf install nginx -y
    
    # Remove a package
    sudo dnf remove nginx
    
    # Search for a package
    dnf search redis
    
    # Show package details
    dnf info nginx
    
    # List installed packages
    dnf list installed

    Related Documentation

    8. Managing Services (systemctl)

    Services (daemons) like web servers, databases, and SSH run in the background. Use systemctl to manage them:

    Service management
    # Check if a service is running
    sudo systemctl status nginx
    
    # Start a service
    sudo systemctl start nginx
    
    # Stop a service
    sudo systemctl stop nginx
    
    # Restart a service (stop + start)
    sudo systemctl restart nginx
    
    # Reload config without stopping (if supported)
    sudo systemctl reload nginx
    
    # Enable a service to start on boot
    sudo systemctl enable nginx
    
    # Disable a service from starting on boot
    sudo systemctl disable nginx
    
    # List all running services
    systemctl list-units --type=service --state=running

    9. Processes & System Resources

    Monitoring commands
    # Show running processes (snapshot)
    ps aux
    
    # Interactive process monitor (press q to quit)
    top
    
    # Better alternative to top (install with: apt install htop)
    htop
    
    # Show memory usage
    free -h
    
    # Show disk usage for all mounted filesystems
    df -h
    
    # Show disk usage of the current directory
    du -sh *
    
    # Show system uptime and load average
    uptime
    Managing processes
    # Find a process by name
    ps aux | grep nginx
    
    # Kill a process by PID
    kill 1234
    
    # Force-kill an unresponsive process
    kill -9 1234
    
    # Kill all processes matching a name
    killall nginx

    Related Documentation

    10. Networking Basics

    Network commands
    # Show your server's IP addresses
    ip addr show
    
    # Test connectivity to a host
    ping -c 4 google.com
    
    # Check which ports are listening
    ss -tulnp
    
    # Download a file from the web
    wget https://example.com/file.tar.gz
    
    # Download with curl
    curl -O https://example.com/file.tar.gz
    
    # Make an HTTP request and show headers
    curl -I https://example.com
    
    # DNS lookup
    dig example.com
    
    # Trace network route
    traceroute example.com

    Related Documentation

    11. Pipes & Redirection

    One of Linux's most powerful features is chaining commands together. Pipes (|) send the output of one command as input to another. Redirection (>, >>) sends output to a file.

    Pipes and redirection
    # Pipe: search for "error" in a log file
    cat /var/log/syslog | grep "error"
    
    # Simpler way to do the same thing
    grep "error" /var/log/syslog
    
    # Count the number of matching lines
    grep -c "error" /var/log/syslog
    
    # Chain multiple commands
    ps aux | grep nginx | grep -v grep
    
    # Redirect output to a file (overwrites)
    echo "Hello" > output.txt
    
    # Append output to a file
    echo "World" >> output.txt
    
    # Redirect errors to a file
    command 2> errors.log
    
    # Redirect both output and errors
    command > output.log 2>&1

    12. Finding Things

    Search commands
    # Find files by name
    find /var/www -name "*.php"
    
    # Find files modified in the last 24 hours
    find /var/log -mtime -1
    
    # Find files larger than 100MB
    find / -size +100M
    
    # Search inside files for a pattern
    grep -r "database" /etc/nginx/
    
    # Search inside files (case-insensitive)
    grep -ri "error" /var/log/
    
    # Find which package provides a command
    # Ubuntu/Debian:
    dpkg -S /usr/bin/curl
    # AlmaLinux/Rocky:
    rpm -qf /usr/bin/curl
    
    # Find where a command is located
    which nginx
    whereis nginx

    13. Compression & Archives

    tar and zip
    # Create a compressed archive (.tar.gz)
    tar -czf backup.tar.gz /var/www/mysite
    
    # Extract a .tar.gz archive
    tar -xzf backup.tar.gz
    
    # Extract to a specific directory
    tar -xzf backup.tar.gz -C /tmp/
    
    # List contents of an archive without extracting
    tar -tzf backup.tar.gz
    
    # Create a zip file
    zip -r backup.zip /var/www/mysite
    
    # Extract a zip file
    unzip backup.zip

    14. Helpful Tips

    Tab completion

    Press Tab to auto-complete file names, directories, and commands. Press it twice to see all options.

    Command history

    Press / to cycle through previous commands. Type history to see them all, or Ctrl+R to search your history.

    Stop a running command

    Press Ctrl+C to interrupt a running command. Press Ctrl+Z to suspend it (resume with fg).

    Clear the screen

    Type clear or press Ctrl+L to clear your terminal.

    Get help on any command

    Add --help after a command (e.g., ls --help) or use man ls to read the full manual page (press q to quit).

    Run multiple commands

    Use && to run the next command only if the previous one succeeded: apt update && apt upgrade -y. Use ; to run commands regardless of success.