Skip to content

Infrastructure as Code Concepts

Category: DevOps and System Tools
Type: Linux Commands
Generated on: 2025-07-10 03:22:28
For: System Administration, Development & Technical Interviews


Infrastructure as Code Concepts Cheatsheet (Linux Commands - DevOps & System Tools)

Section titled “Infrastructure as Code Concepts Cheatsheet (Linux Commands - DevOps & System Tools)”

This cheatsheet covers essential Linux commands frequently used in Infrastructure as Code (IaC) and DevOps workflows. It aims to be a quick reference for sysadmins and developers alike.

1. Command Overview:

  • ssh: Secure Shell; remotely access and manage servers. Essential for IaC execution on remote machines.
  • scp: Secure Copy; securely transfer files between systems. Used for deploying configurations and artifacts.
  • rsync: Remote Sync; efficient file synchronization. Ideal for replicating configurations and backups.
  • sed: Stream EDitor; perform text transformations. Used extensively for configuration templating and automation.
  • awk: Pattern scanning and processing language; powerful text processing. Used for data extraction and reporting.
  • grep: Global Regular Expression Print; search for patterns in files. Used for log analysis and configuration validation.
  • find: Locate files based on criteria. Used for managing files across a system.
  • xargs: Build and execute command lines from standard input. Used to chain commands and process large lists of files.
  • curl: Transfer data with URLs; used for API interactions and downloading resources.
  • jq: JSON processor; parse, filter, and transform JSON data. Essential for working with APIs.
  • systemctl: Manage systemd services; control services on modern Linux systems.
  • docker: Containerization platform; build, run, and manage containers.
  • kubectl: Kubernetes command-line tool; manage Kubernetes clusters.
  • ansible: Automation engine; orchestrate configuration management and application deployment.
  • terraform: Infrastructure as Code tool; define and manage infrastructure across multiple cloud providers.

2. Basic Syntax:

  • ssh: ssh [user@]host [command]
  • scp: scp [options] source destination
  • rsync: rsync [options] source destination
  • sed: sed [options] 'command' file
  • awk: awk '{action}' file
  • grep: grep [options] pattern file
  • find: find path [expression]
  • xargs: command | xargs [options] command
  • curl: curl [options] URL
  • jq: jq [options] 'filter' file
  • systemctl: systemctl [command] service
  • docker: docker [command] [options] image
  • kubectl: kubectl [command] [options] resource
  • ansible: ansible [host-pattern] -m module -a 'arguments'
  • terraform: terraform [command]

3. Practical Examples:

  • ssh:

    Terminal window
    # Connect to server 'webserver' as user 'deploy'
    ssh deploy@webserver
    # Execute a command on the remote server and exit
    ssh deploy@webserver "uptime"

    Output:

    14:32:01 up 10 days, 1:22, 1 user, load average: 0.01, 0.02, 0.00
  • scp:

    Terminal window
    # Copy a file to a remote server
    scp local_file.txt deploy@webserver:/tmp/
    # Copy a directory recursively
    scp -r local_directory deploy@webserver:/tmp/
  • rsync:

    Terminal window
    # Sync a local directory to a remote server, recursively and with compression
    rsync -avz local_directory deploy@webserver:/tmp/
    # Sync a remote directory to a local directory
    rsync -avz deploy@webserver:/tmp/remote_directory ./local_directory
  • sed:

    Terminal window
    # Replace all occurrences of 'old_string' with 'new_string' in a file
    sed 's/old_string/new_string/g' input.txt > output.txt
    # Replace in-place (be careful!)
    sed -i 's/old_string/new_string/g' input.txt
    # Replace based on a regular expression
    sed 's/pattern/replacement/g' file.txt
  • awk:

    Terminal window
    # Print the first column of each line
    awk '{print $1}' file.txt
    # Print lines where the second column is greater than 10
    awk '$2 > 10 {print}' file.txt
    # Sum the values in the third column
    awk '{sum += $3} END {print sum}' file.txt
  • grep:

    Terminal window
    # Search for a pattern in a file
    grep "error" logfile.txt
    # Search recursively in a directory
    grep -r "error" /var/log/
    # Case-insensitive search
    grep -i "error" logfile.txt
  • find:

    Terminal window
    # Find all files in the current directory
    find . -type f
    # Find all files modified in the last 24 hours
    find . -type f -mtime -1
    # Find all files with a specific name
    find . -name "important.txt"
    # Find all files and execute a command on them
    find . -type f -exec chmod 644 {} \; #WARNING: Be careful with this.
  • xargs:

    Terminal window
    # Find all files and delete them (WARNING: VERY DANGEROUS!)
    find . -name "*.tmp" -print0 | xargs -0 rm -f
    # Find all files and compress them
    find . -name "*.txt" -print0 | xargs -0 gzip
  • curl:

    Terminal window
    # Download a file
    curl -o output.txt https://example.com/file.txt
    # Send a POST request with data
    curl -d "param1=value1&param2=value2" https://example.com/api
    # Get data from an API with authentication
    curl -u user:password https://example.com/api
  • jq:

    Terminal window
    # Pretty print JSON data
    curl -s https://api.example.com/data | jq .
    # Extract a specific field
    curl -s https://api.example.com/data | jq '.name'
    # Filter data based on a condition
    curl -s https://api.example.com/data | jq '.[] | select(.status == "active")'
  • systemctl:

    Terminal window
    # Start a service
    sudo systemctl start nginx
    # Stop a service
    sudo systemctl stop nginx
    # Restart a service
    sudo systemctl restart nginx
    # Check the status of a service
    systemctl status nginx
    # Enable a service to start on boot
    sudo systemctl enable nginx
    # Disable a service from starting on boot
    sudo systemctl disable nginx
  • docker:

    Terminal window
    # Pull an image
    docker pull ubuntu:latest
    # Run a container
    docker run -d -p 80:80 nginx
    # List running containers
    docker ps
    # Stop a container
    docker stop <container_id>
    # Remove a container
    docker rm <container_id>
    # Build an image from a Dockerfile
    docker build -t my-app .
  • kubectl:

    Terminal window
    # Get all pods
    kubectl get pods
    # Get details about a specific pod
    kubectl describe pod <pod_name>
    # Apply a Kubernetes configuration file
    kubectl apply -f deployment.yaml
    # Delete a resource
    kubectl delete deployment <deployment_name>
    # Get logs from a pod
    kubectl logs <pod_name>
  • ansible:

    Terminal window
    # Ping all hosts defined in the inventory file
    ansible all -m ping
    # Execute a shell command on all hosts
    ansible all -m shell -a "uptime"
    # Copy a file to all hosts
    ansible all -m copy -a "src=local_file.txt dest=/tmp/"
    # Run a playbook
    ansible-playbook my_playbook.yml
  • terraform:

    Terminal window
    # Initialize Terraform
    terraform init
    # Plan the changes
    terraform plan
    # Apply the changes
    terraform apply
    # Destroy the infrastructure
    terraform destroy

4. Common Options:

  • ssh:

    • -i: Specify identity (private key) file.
    • -p: Specify port number.
    • -v: Verbose mode for debugging.
    • -N: Do not execute a remote command (port forwarding).
    • -L: Local port forwarding.
    • -R: Remote port forwarding.
  • scp:

    • -r: Recursive copy (for directories).
    • -p: Preserve modification times, access times, and modes.
    • -C: Enable compression.
    • -q: Quiet mode.
  • rsync:

    • -a: Archive mode (recursive, preserves permissions, etc.).
    • -v: Verbose mode.
    • -z: Enable compression.
    • -P: Show progress and keep partially transferred files if interrupted.
    • --delete: Delete extraneous files on the destination.
    • --exclude: Exclude files or directories.
  • sed:

    • -i: Edit file in place. USE WITH CAUTION!
    • -n: Suppress automatic printing of pattern space.
    • -e: Execute multiple commands.
    • -r: Use extended regular expressions.
  • awk:

    • -F: Specify field separator.
    • -v: Assign a variable.
  • grep:

    • -i: Case-insensitive search.
    • -r: Recursive search.
    • -n: Show line numbers.
    • -v: Invert match (show lines not matching).
    • -c: Count the number of matching lines.
  • find:

    • -type: Specify file type (e.g., f for file, d for directory).
    • -name: Specify file name.
    • -mtime: Specify modification time (in days).
    • -exec: Execute a command on found files.
    • -delete: Delete found files. USE WITH EXTREME CAUTION!
  • xargs:

    • -n: Maximum number of arguments per command line.
    • -I: Replace occurrences of a placeholder in the command.
    • -0: Expect input items to be terminated by a null character (for handling filenames with spaces).
  • curl:

    • -o: Specify output file.
    • -d: Send POST data.
    • -H: Add a custom header.
    • -u: Specify username and password.
    • -X: Specify request method (e.g., POST, PUT, DELETE).
    • -s: Silent mode.
  • jq:

    • .: Represents the entire input.
    • []: Array/object value iterator.
    • |: Pipe output from one filter to another.
    • .key: Access object properties.
    • [index]: Access array elements.
  • systemctl:

    • start: Start a service.
    • stop: Stop a service.
    • restart: Restart a service.
    • status: Check the status of a service.
    • enable: Enable a service to start on boot.
    • disable: Disable a service from starting on boot.
  • docker:

    • run: Run a container.
    • ps: List containers.
    • stop: Stop a container.
    • rm: Remove a container.
    • build: Build an image from a Dockerfile.
    • pull: Pull an image from a registry.
  • kubectl:

    • get: Get resources.
    • describe: Get details about a resource.
    • apply: Apply a configuration file.
    • delete: Delete a resource.
    • logs: Get logs from a pod.
  • ansible:

    • -m: Specify the module to use.
    • -a: Specify arguments for the module.
    • -k: Ask for SSH password.
    • -i: Specify inventory file.
  • terraform:

    • init: Initialize Terraform.
    • plan: Plan the changes.
    • apply: Apply the changes.
    • destroy: Destroy the infrastructure.

5. Advanced Usage:

  • ssh: Port forwarding for secure tunneling:

    Terminal window
    # Local port forwarding: Access a service on the remote server via localhost
    ssh -L 8080:localhost:80 deploy@webserver
    # Remote port forwarding: Allow others to access a service on your local machine via the remote server
    ssh -R 8080:localhost:80 deploy@webserver
  • sed: Complex substitutions with backreferences:

    Terminal window
    # Swap the first and second words on each line
    sed 's/\(\w\+\) \(\w\+\)/\2 \1/' file.txt
  • awk: Using custom functions and arrays:

    # Calculate the average of numbers in a file
    awk '{sum += $1; count++} END {print sum/count}' numbers.txt
  • find + xargs: Parallel execution for faster processing:

    Terminal window
    # Find all files and compress them in parallel using multiple cores
    find . -name "*.txt" -print0 | xargs -0 -P 4 gzip # -P 4 uses 4 parallel processes
  • curl + jq: Chaining to extract specific data from APIs:

    Terminal window
    # Get the public IP address of the current machine using an external API
    curl -s https://api.ipify.org?format=json | jq -r .ip
  • ansible: Using with_items to iterate over a list of items:

    - name: Create users
    user:
    name: "{{ item }}"
    state: present
    loop:
    - user1
    - user2
    - user3
  • terraform: Using variables and modules for reusable infrastructure:

    variable "region" {
    type = string
    default = "us-east-1"
    }
    module "ec2_instance" {
    source = "./modules/ec2"
    ami = "ami-0c55b4cdcec567a0a" # Example AMI ID
    region = var.region
    }

6. Tips & Tricks:

  • Shell Aliases: Define aliases for frequently used commands to save time and reduce typing errors. Example: alias k=kubectl
  • Bash History: Use Ctrl+R to search your command history.
  • Tab Completion: Use Tab to auto-complete commands, file names, and options.
  • Pipe to less: Pipe long output to less for easier viewing (command | less). Use Spacebar to go down one page, q to quit.
  • tee: Use tee to both display output on the screen and save it to a file (command | tee output.txt).
  • history | grep <command>: Search your command history for a specific command.

7. Troubleshooting:

  • ssh Connection Refused: Ensure the SSH service is running on the remote server and that the firewall is not blocking connections on port 22 (or the custom SSH port).
  • scp Permission Denied: Verify you have the necessary permissions to write to the destination directory on the remote server.
  • sed Incorrect Substitution: Double-check your regular expressions and escape characters. Use a testing tool like regex101.com to validate your regex.
  • find Unexpected Results: Carefully review your search criteria and ensure the path is correct.
  • curl Connection Errors: Check your network connection and ensure the URL is correct.
  • jq Invalid JSON: Verify the JSON data is valid using a validator like jsonlint.com.
  • systemctl Service Fails to Start: Check the service logs for errors using journalctl -u <service_name>.
  • docker Container Fails to Start: Check the container logs for errors using docker logs <container_id>.
  • kubectl Resource Not Found: Ensure the resource exists in the specified namespace and that you have the correct permissions.
  • ansible Host Unreachable: Verify the host is reachable via SSH and that the inventory file is correctly configured. Check SSH key permissions.
  • terraform State Corruption: Use terraform state pull and terraform state push to back up and restore your Terraform state. Consider using remote state storage like S3 or Azure Blob Storage.

8. Related Commands:

  • Package Management: apt, yum, dnf, pacman (for installing and managing software packages).
  • Networking: ifconfig, ip, netstat, ss, ping, traceroute (for network configuration and troubleshooting).
  • File Management: ls, cd, mkdir, rm, cp, mv, chmod, chown (for basic file operations).
  • Process Management: ps, top, htop, kill, pkill (for monitoring and managing processes).
  • User Management: useradd, userdel, usermod, passwd (for managing user accounts).
  • Disk Management: df, du, fdisk, mkfs (for managing disk space and partitions).
  • Cloud-Specific CLIs: aws, gcloud, az (for interacting with cloud services).

This cheatsheet provides a solid foundation for using Linux commands in Infrastructure as Code workflows. Remember to always test your commands in a safe environment before applying them to production systems. Always double-check destructive commands like rm -rf and find -delete.