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.

Thursday, 9 July 2026

What is getent command in linux ?

getent stands for "get entries".

It is a Linux command that retrieves entries from system databases configured in NSS (Name Service Switch). Instead of reading only local files like /etc/passwd, it queries the system's configured user/group sources (local files, LDAP, NIS, Active Directory integration, etc.).

Syntax

getent <database>

Common examples


Get all users

getent passwd

Output:

root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
dinesh:x:1000:1000:Dinesh:/home/dinesh:/bin/bash

Get a specific user

getent passwd dinesh

Output:

dinesh:x:1000:1000:Dinesh:/home/dinesh:/bin/bash

Get all groups

getent group

Get a specific group

getent group sudo

Output:

sudo:x:27:dinesh,admin

Get host information

getent hosts localhost

Output:

127.0.0.1 localhost


Why use getent passwd instead of cat /etc/passwd?

cat /etc/passwd

Only reads the local file.

getent passwd

Queries the system's user database, which may include:

  • Local users (/etc/passwd)
  • LDAP users
  • Active Directory users
  • NIS users

Therefore, getent is generally the recommended command for user lookups.

To list only usernames

getent passwd | cut -d: -f1

Example output:

root
daemon
ubuntu
dinesh

This is one of the most common commands to see all users known to an Ubuntu system.

How to display the users in a Ubunu linux ?

 On Ubuntu (or any Linux system), you can view all user accounts in several ways.

1. List all users from /etc/passwd

cat /etc/passwd

Or just display the usernames:

cut -d: -f1 /etc/passwd

Example output:

root
daemon
bin
sys
ubuntu
dinesh

2. Get all users from the system database

This method also works if users come from LDAP or other authentication services:

getent passwd

To show only usernames:

getent passwd | cut -d: -f1

3. List regular (human) users only

Most system accounts have UIDs below 1000. To show regular users:

awk -F: '$3 >= 1000 {print $1}' /etc/passwd

Example:

dinesh
john
alice

4. See currently logged-in users

who

or

w

Example:

dinesh pts/0 2026-07-09 09:00

5. Check users with sudo privileges

Members of the sudo group can run administrative commands:

getent group sudo

Or:

grep '^sudo:' /etc/group

6. Count total users

getent passwd | wc -l

7. Display users and their UID/GID

getent passwd | awk -F: '{print "User=" $1 ", UID=" $3 ", GID=" $4}'

This can help distinguish system users from normal users.


Most commonly used command:

getent passwd | cut -d: -f1

It lists all users known to the Ubuntu system.