ServerlessBase Blog
  • Essential Linux Commands Every Server Admin Should Know

    Master the core Linux commands every server administrator needs for daily operations, troubleshooting, and system management.

    Essential Linux Commands Every Server Admin Should Know

    You've just SSH'd into a fresh server, and the first thing you need to do is check what's actually running. You try to run a command, but it's not found. You need to see what processes are consuming CPU, but you don't know where to look. You need to restart a service, but you're not sure which command to use. This is where the real work of server administration begins.

    Linux commands are your primary interface with the system. Every task you perform—deploying applications, monitoring performance, troubleshooting issues, managing users—relies on these commands. This guide covers the essential commands you'll use daily, with practical examples you can adapt to your own environment.

    Understanding the Command Line Basics

    Before diving into specific commands, it helps to understand how the command line works. The terminal accepts commands in this format:

    command [options] [arguments]
    • command: The actual program you want to run (like ls, grep, ps)
    • options: Flags that modify the command's behavior (like -l, -a, -r)
    • arguments: The targets the command operates on (like filenames, directories, processes)

    Options typically start with a hyphen (-) or double hyphen (--). Some commands accept both single and multiple options combined, like ls -la (list all files, including hidden ones, in long format).

    File and Directory Management

    Listing Files and Directories

    The ls command lists directory contents. The most commonly used options are:

    ls -la

    This shows all files (including hidden ones starting with .) in long format, including permissions, owner, size, and modification time.

    ls -lh

    Shows files in human-readable format (KB, MB, GB instead of bytes).

    ls -lt

    Sorts files by modification time, with the most recently modified files at the top.

    Change to a directory:

    cd /var/www/html

    Navigate to the parent directory:

    cd ..

    Go to the home directory:

    cd ~

    Go to the previous directory:

    cd -

    Creating and Removing Directories

    Create a new directory:

    mkdir project-alpha

    Create multiple nested directories at once:

    mkdir -p /var/www/project-alpha/logs

    The -p flag creates parent directories as needed.

    Remove a directory:

    rmdir empty_directory

    This only works for empty directories. To remove a directory and its contents:

    rm -rf non_empty_directory

    Warning: The -r (recursive) and -f (force) flags remove everything without confirmation. Use with caution.

    Viewing File Contents

    Display the entire file:

    cat /var/log/syslog

    Display file contents with line numbers:

    cat -n /etc/hosts

    View a file in a paginated format (useful for large files):

    less /var/log/nginx/access.log

    Press q to exit.

    Display the first 10 lines:

    head /etc/nginx/nginx.conf

    Display the last 10 lines:

    tail /var/log/nginx/error.log

    Follow a growing log file in real-time:

    tail -f /var/log/nginx/access.log

    Press Ctrl+C to stop.

    Searching for Text in Files

    Search for a pattern in a file:

    grep "error" /var/log/nginx/error.log

    Search recursively in all files in a directory:

    grep -r "database_connection" /var/www/html

    Count matching lines:

    grep -c "error" /var/log/nginx/error.log

    Case-insensitive search:

    grep -i "warning" /var/log/syslog

    Process Management

    Viewing Running Processes

    List all running processes:

    ps aux

    This shows all processes with user, PID, CPU usage, memory usage, and command details.

    Find a specific process:

    ps aux | grep nginx

    Use pgrep for a cleaner output:

    pgrep nginx

    Monitoring Processes in Real-Time

    View real-time process statistics:

    top

    Press q to exit. Use h for help.

    Monitor specific processes:

    htop

    htop provides a more interactive interface with color-coded processes and the ability to sort by different metrics.

    Killing Processes

    Find the PID of a process:

    pgrep nginx

    Kill a process by PID:

    kill 1234

    Force kill a process:

    kill -9 1234

    The -9 signal is the most aggressive and should be used as a last resort.

    Kill all processes matching a pattern:

    pkill -f "python manage.py"

    Managing Services

    List all services:

    systemctl list-units --type=service

    Check the status of a service:

    systemctl status nginx

    Start a service:

    systemctl start nginx

    Stop a service:

    systemctl stop nginx

    Restart a service:

    systemctl restart nginx

    Reload a service without dropping connections:

    systemctl reload nginx

    Enable a service to start on boot:

    systemctl enable nginx

    Disable a service from starting on boot:

    systemctl disable nginx

    System Information and Monitoring

    Getting System Information

    Display system information:

    uname -a

    Show kernel version:

    uname -r

    Display CPU information:

    lscpu

    Show memory usage:

    free -h

    Show disk usage:

    df -h

    Show mounted file systems:

    mount

    Monitoring System Resources

    Display real-time resource usage:

    htop

    Monitor CPU and memory usage:

    vmstat 1

    The 1 argument updates every second.

    Check disk I/O statistics:

    iostat -x 1

    Monitor network connections:

    netstat -tulpn

    Or use the newer ss command:

    ss -tulpn

    User and Permission Management

    Managing Users

    Create a new user:

    useradd -m -s /bin/bash newuser

    The -m flag creates a home directory, and -s sets the default shell.

    Set a password for the user:

    passwd newuser

    Delete a user:

    userdel newuser

    Delete a user and their home directory:

    userdel -r newuser

    Managing User Groups

    Create a new group:

    groupadd developers

    Add a user to a group:

    usermod -aG developers newuser

    List all groups:

    groups newuser

    File Permissions

    View file permissions:

    ls -l /var/www/html/index.html

    Change file ownership:

    chown www-data:www-data /var/www/html/index.html

    Change file permissions:

    chmod 644 /var/www/html/index.html

    The numeric format represents permissions as follows:

    • 4 (read)
    • 2 (write)
    • 1 (execute)

    For directories, add execute permission to allow traversal:

    chmod 755 /var/www/html

    Network Configuration and Troubleshooting

    Checking Network Connections

    Show all network connections:

    netstat -tulpn

    Or use ss:

    ss -tulpn

    Check if a port is listening:

    netstat -tulpn | grep :80

    Or:

    ss -tulpn | grep :80

    Testing Network Connectivity

    Ping a host:

    ping google.com

    Check DNS resolution:

    nslookup google.com

    Or use dig:

    dig google.com

    Test connectivity to a specific port:

    nc -zv google.com 443

    Or use telnet:

    telnet google.com 443

    Viewing Network Statistics

    Show network interface statistics:

    ip -s link

    Or use ifconfig:

    ifconfig

    Show routing table:

    ip route

    Or:

    netstat -r

    Package Management

    Debian/Ubuntu Systems

    Update package lists:

    apt update

    Upgrade all packages:

    apt upgrade

    Install a package:

    apt install nginx

    Remove a package:

    apt remove nginx

    Remove a package and its configuration files:

    apt purge nginx

    Search for a package:

    apt search nginx

    Red Hat/CentOS Systems

    Update package lists:

    yum update

    Install a package:

    yum install nginx

    Remove a package:

    yum remove nginx

    Search for a package:

    yum search nginx

    Practical Walkthrough: Setting Up a Production Web Server

    Let's walk through a complete setup of a production web server using these commands. This example uses Ubuntu, but the concepts apply to other Linux distributions.

    Step 1: Update the System

    First, ensure your system is up to date:

    sudo apt update && sudo apt upgrade -y

    The sudo command runs the command with superuser privileges. The && operator runs the second command only if the first succeeds.

    Step 2: Install Nginx

    Install the web server:

    sudo apt install nginx -y

    Start and enable Nginx:

    sudo systemctl start nginx
    sudo systemctl enable nginx

    Verify it's running:

    sudo systemctl status nginx

    Step 3: Configure the Firewall

    Allow HTTP and HTTPS traffic:

    sudo ufw allow 'Nginx Full'

    Enable the firewall:

    sudo ufw enable

    Step 4: Deploy Your Application

    Create a directory for your application:

    sudo mkdir -p /var/www/myapp

    Set ownership:

    sudo chown -R $USER:$USER /var/www/myapp

    Create a simple HTML file:

    sudo nano /var/www/myapp/index.html

    Add your HTML content and save with Ctrl+O, Enter, then Ctrl+X.

    Step 5: Configure Nginx

    Create a new server block:

    sudo nano /etc/nginx/sites-available/myapp

    Add the following configuration:

    server {
        listen 80;
        server_name yourdomain.com;
     
        root /var/www/myapp;
        index index.html;
     
        location / {
            try_files $uri $uri/ =404;
        }
    }

    Enable the site:

    sudo ln -s /etc/nginx/sites-available/myapp /etc/nginx/sites-enabled/

    Test the configuration:

    sudo nginx -t

    Reload Nginx:

    sudo systemctl reload nginx

    Step 6: Set Up SSL with Let's Encrypt

    Install Certbot:

    sudo apt install certbot python3-certbot-nginx -y

    Obtain and install SSL certificate:

    sudo certbot --nginx -d yourdomain.com

    Follow the prompts to accept the terms and enter your email address.

    Step 7: Verify Everything is Working

    Check if Nginx is listening:

    sudo netstat -tulpn | grep :80

    Or:

    sudo ss -tulpn | grep :80

    Test your website in a browser at http://yourdomain.com.

    Common Troubleshooting Commands

    Finding Missing Commands

    If you try to run a command and get "command not found":

    which python3

    This shows where the command is located. If it returns nothing, the command isn't installed.

    Checking Disk Space

    If you're running out of disk space:

    df -h

    Find large files:

    du -sh * | sort -rh | head -n 10

    This shows the top 10 largest directories in the current location.

    Checking for Running Processes

    If a service isn't working:

    systemctl status nginx

    Check if a process is running:

    pgrep nginx

    If nothing is returned, the process isn't running.

    Checking Logs

    View system logs:

    journalctl -xe

    View application logs:

    tail -f /var/log/nginx/error.log

    Essential Command Shortcuts

    Master these keyboard shortcuts to work more efficiently:

    • Ctrl+C: Cancel the current command
    • Ctrl+D: Exit the terminal
    • Ctrl+L: Clear the screen
    • Ctrl+R: Search command history
    • Tab: Auto-complete commands and filenames
    • Ctrl+A: Move cursor to the beginning of the line
    • Ctrl+E: Move cursor to the end of the line
    • Ctrl+U: Clear the current line
    • Ctrl+K: Cut everything from cursor to end of line
    • Ctrl+Y: Paste the last cut text

    Conclusion

    These commands form the foundation of Linux server administration. You'll use them daily, whether you're deploying applications, monitoring performance, or troubleshooting issues. The key is to understand not just what each command does, but when and why you'd use it.

    Start by practicing these commands in a safe environment. Create a virtual machine or use a cloud server with a fresh installation. Build a simple web application and deploy it using these commands. As you gain confidence, you'll naturally learn more advanced commands and techniques.

    Platforms like ServerlessBase can simplify many of these tasks by providing managed services and automated deployments, but understanding the underlying commands remains essential for troubleshooting and optimization. When something goes wrong, you'll need to know how to inspect logs, check processes, and manage services manually.

    The command line is a powerful tool that, once mastered, gives you complete control over your server. Take time to practice each command, understand its options, and build a mental model of how Linux systems work. This knowledge will serve you throughout your career as a server administrator or DevOps engineer.

    Next Steps

    Now that you know the essential commands, consider learning:

    • Shell scripting to automate repetitive tasks
    • Advanced file management with find and xargs
    • Log analysis with grep, awk, and sed
    • Network troubleshooting with tcpdump and wireshark
    • Container management with Docker and Kubernetes

    Each of these areas builds on the fundamentals covered here and will expand your ability to manage and troubleshoot Linux systems effectively.

    Leave comment