Monday, 30 November 2026

How to set environment variable using powershell ?

To set an environment variable in PowerShell, use $env:VAR_NAME = "value" for the current session or [System.Environment]::SetEnvironmentVariable() to save it permanently. 
1. Temporary (Current Session Only)
Variables set this way apply only to your current terminal window and any programs launched from it. They disappear when you close PowerShell.
powershell
# Set a new variable or overwrite an existing one
$env:MY_VAR = "my_value"

# Append to an existing variable (like PATH)
$env:PATH += ";C:\new\folder"

2. Permanent (Across Restarts)
On Windows, use the .NET [System.Environment] class to save variables to the registry so they persist across sessions and reboots.
  • For your user account only:
powershell
[System.Environment]::SetEnvironmentVariable("MY_VAR", "my_value", "User")
  • System-wide for all users (requires running PowerShell as Administrator:
powershell
[System.Environment]::SetEnvironmentVariable("MY_VAR", "my_value", "Machine")

What are the basic commands of Terraform ?

 The core Terraform workflow relies on four primary commands (init, plan, apply, and destroy)

Core Workflow Commands
Run these in sequence when provisioning or updating infrastructure:

  • terraform init Prepares your working directory by downloading required provider plugins, installing modules, and configuring the backend. Run this first in any new or cloned project.
  • terraform plan. Previews the exact changes Terraform will make to your infrastructure without modifying anything. It marks additions with +, modifications with ~, and deletions with -.
  • terraform apply. Executes the planned changes to create or update real infrastructure. By default, it prompts you to type yes before proceeding (use -auto-approve in CI/CD pipelines to skip the prompt).
  • terraform destroy. Tears down and deletes all resources managed by your current Terraform configuration.
Code Formatting & Validation
Use these commands to catch errors and keep configuration files clean before running a plan:
  • terraform fmt. Automatically rewrites .tf files to match Terraform's canonical formatting and indentation style.
  • terraform validate. Checks configuration files for syntax errors, invalid arguments, and internal consistency without needing to connect to remote cloud APIs.
State & Output Inspection
Once infrastructure is deployed, use these commands to inspect what Terraform is tracking:
  • terraform show. Displays a human-readable summary of the current state file or a saved plan file.
  • terraform output. Prints the values of any output variables defined in your configuration (such as generated IP addresses or resource IDs).
  • terraform state list. Lists every resource currently tracked in your state file.

Wednesday, 25 November 2026

Difference between Private NAT Gateway and Public NAT Gateway ?

A NAT Gateway (Network Address Translation Gateway) is a managed AWS service that allows resources in a private subnet to connect out to the internet (for OS updates, patches, or external APIs) while blocking anyone on the internet from initiating connections back to them.

How It Works (Step-by-Step)
Because servers in a private subnet only have private IPs (like 10.0.11.25), the public internet cannot route traffic back to them directly. The NAT Gateway acts as a middleman:
  1. Outbound Request: Your private server (10.0.11.25) sends a request (e.g., apt-get update) to 0.0.0.0/0, which its route table points to the NAT Gateway.
  2. Translation: The NAT Gateway sits inside a Public Subnet and holds an Elastic IP (e.g., 54.12.34.56). It swaps your server's private IP for its own Elastic IP.
  3. Internet Access: It sends the request through the Internet Gateway to the internet.
  4. Return Traffic: When the response comes back to 54.12.34.56, the NAT Gateway remembers who asked for it and forwards the data back to 10.0.11.25.
  5. Inbound Block: If an external hacker tries to connect to 54.12.34.56 out of the blue, the NAT Gateway drops it because no private server started that conversation.

NAT Gateway Placement

A working NAT Gateway setup requires two separate route tables:
  • NAT Gateway Placement: Must be created inside a Public Subnet and attached to an Elastic IP.
  • Private Subnet Route Table:
    • 10.0.0.0/16 -> local
    • 0.0.0.0/0 -> nat-xxxx (NAT Gateway)
  • Public Subnet Route Table (where the NAT Gateway lives):
    • 10.0.0.0/16 -> local
    • 0.0.0.0/0 -> igw-xxxx (Internet Gateway)
Key Things to Know
  • One-Way Street: Outbound connections are allowed; unsolicited inbound connections are blocked.
  • Availability Zone Specific: A NAT Gateway lives in a single Availability Zone (AZ). For high availability in production, deploy one NAT Gateway per AZ so an outage in one zone doesn't cut internet access for private servers in another.
  • Cost Consideration: AWS charges an hourly rate (around $0.045/hr depending on region) plus per-GB data processing fees—making it one of the most common surprise charges in small or test AWS accounts if left running.


How Elastic IP address (EIP) is different from a public IPv4 address ?

An Elastic IP (EIP) is a static, permanent public IPv4 address that you allocate to your AWS account and keep until you explicitly release it.

An Elastic IP (EIP) address in Amazon Web Services (AWS) is a static, public IPv4 address designed for dynamic cloud computing. Unlike a default public IP address which changes every time an instance is stopped and started, an Elastic IP remains constant and is associated with your AWS account, not a specific instance. 

When you launch a server (like an EC2 instance) in a public subnet, AWS normally gives it a temporary public IP. That temporary IP changes if you stop and start the server. An Elastic IP solves this problem:
FeatureStandard Public IPElastic IP (EIP)
LifetimeLost when the instance is stopped or terminated.Stays in your AWS account until you release it.
OwnershipBorrowed dynamically from AWS's pool.Reserved specifically for your AWS account.
ReassignmentLocked to one instance for its run.Can be moved instantly from one instance to another.
Cost (AWS)Free while attached.Small hourly fee (~$0.005/hr) per public IPv4 address.

Common Use Cases
  • Failover & High Availability: If Server A crashes, you can instantly remap your Elastic IP to healthy Server B so users keep reaching your app without waiting for DNS changes.
  • IP Whitelisting: External partners or firewalls often require a fixed IP address from your side before allowing API or database connections.
  • NAT Gateways: A NAT Gateway in a public subnet requires an Elastic IP so private servers behind it exit to the internet using one predictable public IP.

Friday, 10 July 2026

What is a UFW in ubuntu ?

UFW (Uncomplicated Firewall) is a user-friendly frontend for managing iptables/nftables firewall rules in Linux systems like Ubuntu and Debian.
Below is a structured reference to the most common UFW commands. Most configuration changes require sudo privileges.

1. General Management & Status

These commands let you control the firewall state and check current active rulesets.
  • Check status:
    sudo ufw status
    
  • Check detailed status (shows logging levels and default policies):
    sudo ufw status verbose
    
  • Enable the firewall (starts on system boot):
    sudo ufw enable
    
  • Disable the firewall:
    sudo ufw disable
    
  • Reload the firewall (applies modifications to configuration files):
    sudo ufw reload
    
  • Reset the firewall (deletes all rules and restores defaults):
    sudo ufw reset
    

2. Default Policies

Setting global policies determines how UFW treats traffic that doesn't match any specific rule. The standard recommended baseline is to block incoming and allow outgoing:
  • Block all incoming traffic by default:
    sudo ufw default deny incoming
    
  • Allow all outgoing traffic by default:
    sudo ufw default allow outgoing
    

3. Managing Port & Protocol Rules

You can open or close ports using service names (defined in /etc/services) or explicit port numbers. 
  • Allow a service by name (e.g., SSH):
    sudo ufw allow ssh
    
  • Allow a specific port (both TCP and UDP):
    sudo ufw allow 80
    
  • Allow a specific port and protocol (recommended):
    sudo ufw allow 443/tcp
    
  • Allow a specific port range (protocol required):
    sudo ufw allow 40000:50000/tcp
    
  • Deny traffic on a port:
    sudo ufw deny 25
    
  • Rate limit connections (useful for preventing brute force on SSH; caps at 6 connections per 30 seconds from a single IP):
    sudo ufw limit 22/tcp

4. IP-Based Filtering

To handle connections originating from specific network locations, use these commands:
  • Allow all traffic from a specific IP address:
    sudo ufw allow from 203.0.113.10
    
  • Block all traffic from a specific IP address:
    sudo ufw deny from 203.0.113.10
    
  • Allow an IP address to access a specific port:
    sudo ufw allow from 203.0.113.10 to any port 22
    
  • Allow an entire subnet (CIDR notation) to access a specific port:
    sudo ufw allow from 10.0.0.0/24 to any port 3306
    

5. Deleting Rules

There are two primary methods to remove rules you no longer need:
  • Delete by original specification: Duplicate the exact rule you added but prepend the word delete.
    sudo ufw delete allow 80/tcp
    
  • Delete by line number (Easiest for clean-up): First, list rules with assigned numbers:
    sudo ufw status numbered
    
    Then, delete using the corresponding index:
    sudo ufw delete 3
    

6. Application Profiles

Many packages (like Nginx, Apache, or OpenSSH) install profiles inside UFW to simplify rule management.
  • List available application profiles:
    sudo ufw app list
    
  • View details about a specific profile:
    sudo ufw app info "Nginx Full"
    
  • Allow an application profile:
    sudo ufw allow "OpenSSH"
    

7. Logging Options

  • Turn logging on/off:
    sudo ufw logging on
    sudo ufw logging off
    
  • Change log level granularity (low, medium, high, full):
    sudo ufw logging medium
    


Important Safety Warning: If you are configuring a remote server over SSH, always execute sudo ufw allow ssh or sudo ufw allow 22/tcp before running sudo ufw enable. Failing to do so will immediately lock you out of your server.