Showing posts with label Linux. Show all posts
Showing posts with label Linux. Show all posts

Wednesday, 2 April 2025

Understanding IPv4 and IPv6

Have you ever wondered how your computer knows where to send that email or how Netflix finds your device to stream your favorite show? 
The answer lies in something called Internet Protocol addresses - the postal system of the digital world.

What Are IP Addresses?
Think of IP addresses like street addresses for the internet. Just as your home needs a unique address for mail delivery, every device connected to the internet needs a unique identifier so data can find its way to the right destination.

IPv4: The Current Standard
IPv4 (Internet Protocol version 4) has been the backbone of internet communication since the 1980s. Here's what makes it tick:

The Format
IPv4 addresses look like this: 192.168.1.1 or 8.8.8.8. These are four numbers (each between 0-255) separated by dots. Behind the scenes, your computer sees this as a 32-bit binary number.

The Problem
Here's where things get interesting - and problematic. IPv4 can only create about 4.3 billion unique addresses. That sounds like a lot, but with billions of smartphones, computers, smart TVs, and IoT devices, we've essentially run out of new IPv4 addresses.


Types You Might Recognize
Public IP: Your internet-facing address (like 74.125.224.72)
Private IP: Your device's address on your home network (usually starts with 192.168)
Localhost: 127.0.0.1 - your computer talking to itself

IPv6: The Future is Here
IPv6 (Internet Protocol version 6) is the solution to IPv4's limitations, and it's already being rolled out worldwide.

The Format
IPv6 addresses are much longer: 2001:0db8:85a3:0000:0000:8a2e:0370:7334. They use hexadecimal (0-9, a-f) and are separated by colons instead of dots.

The Solution
IPv6 provides approximately 340 undecillion addresses. To put that in perspective, that's enough addresses to give every grain of sand on Earth its own IP address - with plenty left over!

What's Better About IPv6?
Unlimited addresses - no more address exhaustion
Better security - built-in encryption capabilities
Faster routing - simplified packet headers
Auto-configuration - devices can set up their own addresses

The Transition Challenge
So why aren't we all using IPv6 yet? 
The transition is like trying to upgrade every road in the world simultaneously while traffic keeps flowing. Most networks now run "dual-stack" - supporting both IPv4 and IPv6 - allowing for a gradual transition.

What This Means for You
As an everyday internet user, this transition is mostly invisible. Your devices likely already support both protocols, and your internet service provider is handling the complexity behind the scenes.
However, understanding these concepts helps you appreciate the incredible engineering that makes our connected world possible. Every time you send a message, stream a video, or browse the web, you're participating in one of humanity's greatest collaborative achievements - the internet.

Looking Forward
IPv6 isn't just about solving today's address shortage; it's about enabling tomorrow's innovations. From smart cities with millions of connected sensors to personal devices we haven't even imagined yet, IPv6 provides the foundation for our digital future.
The next time you effortlessly connect to the internet, remember the elegant addressing system working behind the scenes - and marvel at how 32 bits evolved into 128 bits to keep our world connected.

Friday, 27 December 2024

Exploring Sysbench: Installation and CPU Benchmarking

What is Sysbench?
Sysbench is designed for evaluating the performance of systems by executing CPU, memory, file I/O, and database benchmarks. It is highly customizable, and the results can be used for performance tuning, comparing different configurations, or validating hardware upgrades.
Installing Sysbench on CentOS and Ubuntu.
Step 1: Install Dependencies
Run the following command to install the required dependencies:
sudo yum install mysql-devel libtool automake autoconf
Step 2: Download Sysbench
For example, to download Sysbench from GitHub, you can use curl:
curl -LO https://github.com/akopytov/sysbench/releases/download/1.0.20/sysbench-1.0.20.tar.gz
If the .tar.gz file is downloaded, extract it:
tar -xzvf sysbench-1.0.20.tar.gz
cd sysbench-1.0.20/

Step 3: Build Sysbench from Source
./autogen.sh ; ./configure ; make -j ; make install
Step 4: Verify the Installation
sysbench --version
sysbench 1.0.20

Running the CPU Benchmark Test:
Sysbench provides a number of tests, but here, we will focus on testing the CPU performance. Sysbench’s CPU test calculates prime numbers up to a specified limit and reports the number of events processed during the benchmark run.
Here’s how you can run the benchmark on the CPU:
Running Sysbench CPU Test with 1 Thread:
sysbench --test=cpu --threads=1 --time=60 run
Sample Output:
WARNING: the --test option is deprecated. You can pass a script name or path on the command line without any options.
sysbench 1.0.20 (using bundled LuaJIT 2.1.0-beta2)
Running the test with following options:
Number of threads: 1
Initializing random number generator from current time
Prime numbers limit: 10000
Initializing worker threads...
Threads started!
CPU speed:
    events per second:  2955.46
General statistics:
    total time:                          60.0002s
    total number of events:              177333
Latency (ms):
        min:                                    0.33
        avg:                                    0.34
        max:                                    0.39
        95th percentile:                        0.35
        sum:                                59978.41
Threads fairness:
    events (avg/stddev):           177333.0000/0.00
    execution time (avg/stddev):   59.9784/0.00

Running Sysbench CPU Test with 2 Threads:
sysbench --test=cpu --threads=2 --time=60 run
Sample Output:
CPU speed:
    events per second:  5903.08
General statistics:
    total time:                          60.0002s
    total number of events:              354195
Latency (ms):
        min:                                    0.33
        avg:                                    0.34
        max:                                    1.29
        95th percentile:                        0.35
        sum:                               119915.68
Threads fairness:
    events (avg/stddev):           177097.5000/89.50
    execution time (avg/stddev):   59.9578/0.00

As you can see, with more threads, the events per second increase as well, which indicates better CPU utilization.
Running Sysbench CPU Test with 4 Threads:
Increasing the number of threads even more tests the system's scalability:
sysbench --test=cpu --threads=4 --time=60 run
Sample Output:
CPU speed:
    events per second: 11819.27
General statistics:
    total time:                          60.0003s
    total number of events:              709178
Latency (ms):
        min:                                    0.33
        avg:                                    0.34
        max:                                    1.28
        95th percentile:                        0.35
        sum:                               239860.45
Threads fairness:
    events (avg/stddev):           177294.5000/122.97
    execution time (avg/stddev):   59.9651/0.00


Running Sysbench CPU Test with 8 Threads:
sysbench --test=cpu --threads=8 --time=60 run
Sample Output:
CPU speed:
    events per second: 23637.61
General statistics:
    total time:                          60.0004s
    total number of events:              1418301
Latency (ms):
        min:                                    0.33
        avg:                                    0.34
        max:                                    1.28
        95th percentile:                        0.35
        sum:                               479730.48
Threads fairness:
    events (avg/stddev):           177287.6250/42.17
    execution time (avg/stddev):   59.9663/0.00


Running Sysbench CPU Test with 16 Threads:
sysbench --test=cpu --threads=16 --time=60 run
Sample Output:
    CPU speed:
        events per second: 47267.52
    General statistics:
        total time:                          60.0004s
        total number of events:              2836140
    Latency (ms):
            min:                                    0.33
            avg:                                    0.34
            max:                                    1.42
            95th percentile:                        0.35
            sum:                               959459.02
    Threads fairness:
        events (avg/stddev):           177287.6250/42.17
        execution time (avg/stddev):   59.9662/0.00

Observations:
    Threads and Performance: As the number of threads increases, the events per second increase proportionally. This shows how well your CPU can handle multi-threaded workloads.
    Latency: The latency remains fairly constant across different thread counts, which suggests that the CPU's ability to process each event is relatively consistent.
    Thread Fairness: The fairness remains stable, indicating that Sysbench is distributing tasks evenly across the threads.

Wednesday, 19 July 2023

CIDR: The Modern Way to Handle Subnets

What is CIDR?
CIDR (Classless Inter-Domain Routing) is a method for allocating IP addresses and routing that replaced the old "classful" system. It uses a slash notation to specify how many bits are used for the network portion.
Format: IP Address/Network Bits
Example: 192.168.1.0/24

How CIDR Notation Works
The Number After the Slash
The /24 tells us how many bits (from left) represent the network:
192.168.1.0/24
├── Network bits: 24 (first 24 bits)
└── Host bits: 8 (remaining 8 bits)

Binary view:
11000000.10101000.00000001.00000000
|--------Network (24 bits)-------|Host|

Common CIDR Examples
CIDR    Subnet Mask               Network Bits    Host Bits    Available IPs
/8     255.0.0.0          8       24     16,777,214
/16    255.255.0.0        16      16     65,534
/24    255.255.255.0      24      8      254
/25    255.255.255.128    25      7      126
/26    255.255.255.192    26      6      62
/27    255.255.255.224    27      5      30
/28    255.255.255.240    28      4      14
/30    255.255.255.252    30      2       2

How CIDR Helps with Subnetting
1. Flexible Subnet Sizing
Instead of fixed Class A, B, C sizes, you can create any size you need:
Old way (Classful):
- Class C: 192.168.1.0 = exactly 254 hosts
- No flexibility

New way (CIDR):
- /25 = 126 hosts
- /26 = 62 hosts  
- /27 = 30 hosts
- /28 = 14 hosts

2. Efficient Address Usage
Example: You need 50 IP addresses
Without CIDR: Forced to use Class C (254 IPs) - waste 204 addresses
With CIDR: Use /26 (62 IPs) - waste only 12 addresses

3. Easy Subnet Calculation
Subnetting 192.168.1.0/24 into smaller subnets:
Original: 192.168.1.0/24 (254 hosts)

Split into /25:
├── 192.168.1.0/25   (126 hosts: .1-.126)
└── 192.168.1.128/25 (126 hosts: .129-.254)

Split into /26:
├── 192.168.1.0/26   (62 hosts: .1-.62)
├── 192.168.1.64/26  (62 hosts: .65-.126)
├── 192.168.1.128/26 (62 hosts: .129-.190)
└── 192.168.1.192/26 (62 hosts: .193-.254)

4. Quick Binary Math
To find subnet info:
Network bits: First X bits (from CIDR)
Host bits: Remaining 32-X bits
Subnet size: 2^(host bits) - 2
Subnets possible: 2^(borrowed bits)

Practical CIDR Examples
Example 1: Office Network Planning

Company gets: 10.0.0.0/16

Departments needed:
- Sales (100 users): 10.0.1.0/25 (126 IPs)
- Engineering (200 users): 10.0.2.0/24 (254 IPs)  
- HR (20 users): 10.0.3.0/27 (30 IPs)
- Printers (10 devices): 10.0.4.0/28 (14 IPs)

Example 2: ISP Address Allocation
ISP has: 203.0.113.0/24

Customer allocations:
- Large business: 203.0.113.0/26 (62 IPs)
- Medium business: 203.0.113.64/27 (30 IPs)
- Small business: 203.0.113.96/28 (14 IPs)
- Home users: 203.0.113.112/28 to 203.0.113.240/28

CIDR vs Old Classful System
Old Way (Classful)
Class A: /8  - 16M addresses (usually too big)
Class B: /16 - 65K addresses (often too big) 
Class C: /24 - 254 addresses (often too small)

New Way (CIDR)
Any size from /1 to /32
Perfect fit for actual needs
No address waste

Quick CIDR Calculation Tricks
Finding Subnet Size
Formula: 2^(32-CIDR) - 2

/24: 2^(32-24) - 2 = 2^8 - 2 = 254 usable IPs
/26: 2^(32-26) - 2 = 2^6 - 2 = 62 usable IPs
/30: 2^(32-30) - 2 = 2^2 - 2 = 2 usable IPs

Finding Number of Subnets
To subnet /24 into /26:
Borrowed bits = 26 - 24 = 2
Number of subnets = 2^2 = 4 subnets

Real-World Benefits
Reduced Routing Tables - ISPs can aggregate routes
Flexible Allocation - Match network size to actual needs
Address Conservation - Use only what you need
Simplified Management - Consistent notation across all networks



Tuesday, 1 November 2022

Manage Linux Services with systemctl

What is systemctl?
`systemctl` is the command-line interface to **systemd**, the modern init system used by most major Linux distributions (like Ubuntu, CentOS, RHEL, Debian, Arch, Fedora).

You use `systemctl` to:
- Start and stop services
- Enable services to run at boot
- Check service status
- View logs
- Troubleshoot failures
- Manage dependencies

Real-World Example: Creating a Custom Service
Let’s say you have a Python web application that you want to run as a systemd service.
Create Your Application Directory
sudo mkdir -p /opt/mywebapp
sudo chown $USER:$USER /opt/mywebapp
Add your app file (for example, app.py) inside /opt/mywebapp/.
Create the systemd Service File
sudo nano /etc/systemd/system/mywebapp.service

Paste this configuration:
[Unit]
Description=My Python Web Application
After=network.target

[Service]
ExecStart=/usr/bin/python3 /opt/mywebapp/app.py
WorkingDirectory=/opt/mywebapp
Restart=always
RestartSec=20
User=webuser
Group=webgroup
Environment="ENV=production"

[Install]
WantedBy=multi-user.target

Explanation:
    After=network.target ensures the network is up before starting.
    Restart=always and RestartSec=20 ensures the app auto-recovers on failure.
    User and Group ensure minimal privileges.
    WantedBy=multi-user.target ensures the service starts at system boot.

Reload systemd
sudo systemctl daemon-reload

Complete systemctl Command Reference
Action    Command
Start    sudo systemctl start <service>
Stop    sudo systemctl stop <service>
Restart    sudo systemctl restart <service>
Reload    sudo systemctl reload <service>
Status    sudo systemctl status <service>
Enable/Disable Services at Boot
Action    Command
Enable    sudo systemctl enable <service>
Disable    sudo systemctl disable <service>
Check Enabled    systemctl is-enabled <service>
Masking Services
Action    Command
Mask    sudo systemctl mask <service>
Unmask    sudo systemctl unmask <service>
Editing Services Safely
Action    Command
Edit    sudo systemctl edit <service>
Revert    sudo systemctl revert <service>
List Services and Units
Action    Command
Running Services    systemctl list-units --type=service
All Unit Files    systemctl list-unit-files
Enabled Units    systemctl list-unit-files --state=enabled
View Logs (journalctl)
Action    Command
View Logs    journalctl -u <service>
Follow Logs    journalctl -fu <service>
Today’s Logs    journalctl -u <service> --since today

Saturday, 15 October 2022

Default ports for major products and services

Web Services
    HTTP: 80
    HTTPS: 443
    HTTP/3 (QUIC): 443

Database Systems
    MySQL: 3306
    PostgreSQL: 5432
    MongoDB: 27017
    Redis: 6379
    Microsoft SQL Server: 1433
    Oracle Database: 1521
    Cassandra: 9042
    CouchDB: 5984
    InfluxDB: 8086
    Elasticsearch: 9200

Message Brokers & Queues
    Apache Kafka: 9092
    RabbitMQ: 5672 (AMQP), 15672 (Management UI)
    Apache ActiveMQ: 61616
    Amazon SQS: 443 (HTTPS)

Application Servers & Frameworks
    Apache Tomcat: 8080
    Jetty: 8080
    Node.js (Express): 3000 (common default)
    Django: 8000
    Flask: 5000
    Spring Boot: 8080
    Ruby on Rails: 3000

Email Services
    SMTP: 25 (unencrypted), 587 (TLS), 465 (SSL)
    POP3: 110 (unencrypted), 995 (SSL)
    IMAP: 143 (unencrypted), 993 (SSL)
    
File Transfer & Remote Access
    FTP: 21
    SFTP/SSH: 22
    Telnet: 23
    RDP (Remote Desktop): 3389
    VNC: 5900

DNS & Network Services
    DNS: 53
    DHCP: 67/68
    SNMP: 161
    NTP: 123
    LDAP: 389 (unencrypted), 636 (SSL)

Cloud & Container Services
    Docker: 2376 (secure), 2375 (insecure)
    Kubernetes API: 6443
    etcd: 2379 (client), 2380 (peer)
    Consul: 8500
    Vault: 8200

Monitoring & Observability
    Prometheus: 9090
    Grafana: 3000
    Jaeger: 16686 (UI), 14268 (collector)
    Zipkin: 9411
    New Relic: 443

Development Tools
    Jenkins: 8080
    GitLab: 80/443
    SonarQube: 9000
    Nexus Repository: 8081
    JFrog Artifactory: 8081

Gaming & Entertainment
    Minecraft: 25565
    Steam: 27015
    Discord: 443

IoT & Smart Home
    MQTT: 1883 (unencrypted), 8883 (SSL)
    CoAP: 5683




Monday, 3 October 2022

Linux basic commands

Here are some basic Linux commands along with their full forms and brief descriptions:

  1. ls (List): Lists files and directories in the current working directory. Example: ls -l (long format listing), ls -a (include hidden files), ls -h (human-readable sizes).
  2. cd (Change Directory): Changes the current working directory. Example: cd /path/to/directory.
  3. pwd (Print Working Directory): Displays the current working directory. Example: pwd.
  4. cp (Copy): Copies files and directories. Example: cp file.txt destination/.
    copy file with same  timestamp
    cp -p oldfile newfile
  5. mv (Move): Moves or renames files and directories. Example: mv file.txt new_location/, mv old_name.txt new_name.txt.
  6. rm (Remove): Deletes files and directories. Example: rm file.txt, rm -r directory/ (recursively delete a directory).
  7. mkdir (Make Directory): Creates a new directory. Example: mkdir new_directory.
  8. rmdir (Remove Directory): Deletes an empty directory. Example: rmdir empty_directory.
  9. cat (Concatenate): Displays the contents of a file. Example: cat file.txt.
  10. more or less: Pager commands to view text files page by page. Example: more file.txt, less file.txt.
  11. head and tail: Display the first or last part of a file. Example: head file.txt (displays the first few lines), tail file.txt (displays the last few lines).
  12. grep (Global Regular Expression Print): Searches for a pattern in a file or input. Example: grep "keyword" file.txt.
  13. find: Searches for files and directories based on different criteria. Example: find /path/to/search -name "filename".
  14. chmod (Change Mode): Modifies file permissions. Example: chmod +x script.sh (adds execute permission), chmod 644 file.txt (sets read-write permissions for owner, read-only for group and others).
  15. chown (Change Ownership): Changes the owner of a file or directory. Example: chown user:group file.txt.
  16. ps (Process Status): Displays information about currently running processes. 

Example: ps aux (lists all processes), ps -ef | grep process_name (search for a specific process).

  1. kill: Sends a signal to terminate a process. Example: kill PID (PID is the process ID).
  2. df (Disk Free): Shows disk space usage on file systems. Example: df -h ,df -g(displays sizes in human-readable format).
  3. free: Shows system memory usage. Example: free -h,free-g (displays sizes in human-readable format).
  4. top and htop: Displays real-time system resource usage and processes. Example: top, htop (htop is an improved version of top).
  5. ifconfig : Interface Configuration: Displays and configures network interfaces (deprecated on modern systems).
  6. ip - IP: A modern alternative to ifconfig for network configuration.
  7. ping : Sends an ICMP echo request to a host to check network connectivity.
  8. ssh - Secure Shell: Allows secure remote login to another machine over a network.
  9. scp - Secure Copy: Securely copies files between hosts over a network.
  10. tar - Tape Archive: Used to archive multiple files into a single tarball file.
  11. gzip - GNU Zip: Compresses files to reduce their size.
  12. unzip - Unzip: Extracts files from a zip archive.
  13. who - Who: Displays a list of currently logged-in users.
  14. history - History: Shows a list of previously executed commands.
     

 

Thursday, 29 September 2022

Layers of the OSI Model

OSI (Open Systems Interconnection) model is a conceptual framework that standardizes how different network protocols communicate across a computer network. It was developed by the International Organization for Standardization (ISO) in 1984 to provide a universal set of rules for network communication.

7 Layers of the OSI Model
The OSI model is organized into 7 distinct layers, each with specific responsibilities. Data flows from the top layer (Application) down to the bottom layer (Physical) when sending, and from bottom to top when receiving.
🔝 Layer 7: Application Layer
What it does: Interface between user applications and the network
    Examples: HTTP/HTTPS, FTP, SMTP, DNS, DHCP
    Function: Provides network services directly to applications
    Real-world example: When you browse a website, your browser uses HTTP/HTTPS protocols

📋 Layer 6: Presentation Layer
What it does: Data translation, encryption, and compression
    Examples: SSL/TLS, JPEG, GIF, ASCII, EBCDIC
    Function: Ensures data is readable by the receiving system
    Real-world example: Encrypting your password when logging into a website

🔗 Layer 5: Session Layer
What it does: Manages communication sessions between applications
    Examples: NetBIOS, RPC, SQL sessions
    Function: Establishes, maintains, and terminates connections
    Real-world example: Maintaining your login session on a website

🚚 Layer 4: Transport Layer
What it does: Reliable data transfer and error correction
    Examples: TCP, UDP
    Function: Ensures complete data transfer with error checking
    Real-world example: TCP ensures all parts of a file download arrive correctly

🗺️ Layer 3: Network Layer
What it does: Routing and logical addressing
    Examples: IP (IPv4/IPv6), ICMP, OSPF, BGP
    Function: Determines the best path for data across multiple networks
    Real-world example: Routers use IP addresses to forward data packets

🔌 Layer 2: Data Link Layer
What it does: Node-to-node delivery and error detection
    Examples: Ethernet, Wi-Fi (802.11), PPP
    Function: Handles communication between directly connected devices
    Real-world example: Your computer communicating with your router via Ethernet or Wi-Fi

⚡ Layer 1: Physical Layer
What it does: Raw data transmission over physical medium
    Examples: Cables, fiber optics, radio waves, electrical signals
    Function: Transmits raw bits over the physical medium
    Real-world example: Electrical signals traveling through an Ethernet cable

🔄 How Data Flows Through the OSI Model
Sending Data (Top to Bottom)
    Application → User creates data (email, web request)
    Presentation → Data is encrypted/compressed
    Session → Session is established
    Transport → Data is segmented and prepared for reliable delivery
    Network → Routing information is added
    Data Link → Frame headers are added for local delivery
    Physical → Data becomes electrical/optical signals





Sunday, 4 September 2022

zsh: no matches found

"zsh: no matches found" typically occurs in the Zsh shell when you use wildcard characters (such as *, ?, or []) in a command, and there are no files or directories that match the specified pattern in the current directory.

Here are a few common scenarios where you might encounter this error:
Using a command with a wildcard pattern that doesn't match any files:
$ ls *.xyz
zsh: no matches found: *.xyz

In this example, Zsh couldn't find any files in the current directory with the ".xyz" extension.
Using a command with a wildcard pattern, but the pattern itself doesn't match anything:

$ echo [a-z]
zsh: no matches found: [a-z]

If there are no lowercase letters in the current directory, the [a-z] pattern won't match anything.

To resolve this issue, you can do the following:

1.Double-check your command or wildcard pattern to ensure it matches existing files or directories in the current directory.
2.Use proper escaping or quoting if necessary. For example, if you want to list files with a literal asterisk in their names, you can use single or double quotes to prevent globbing:

$ ls '*.*'

If you want to use a wildcard pattern to match files in subdirectories as well, consider enabling the globstar option in Zsh:

$ setopt globstar
$ ls **/*.txt

The ** pattern will match files in subdirectories.

Saturday, 27 August 2022

How to Increase the max_map_count Kernel Parameter in Linux to Optimize Vector Server Performance

If you're running a Vector Server or any application that relies heavily on memory-mapped areas, you might encounter issues related to running out of map areas. This problem often stems from a default kernel parameter setting that limits the number of map areas available. Fortunately, you can address this issue by increasing the max_map_count kernel parameter. 

How to Increase max_map_count:
1. Edit the sysctl.conf File

vi /etc/sysctl.conf
Add the following line to the file:
vm.max_map_count=map_count

Replace map_count with an appropriate value based on your system’s memory. As a general guideline, set map_count to approximately 1 per 128 KB of system memory. For instance, on a system with 256 GB of RAM, you might set:

vm.max_map_count=2097152

2. Reload the Configuration

After updating the sysctl.conf file, apply the new settings by reloading the configuration:
sudo sysctl -p

3. Verify the New Setting
cat /proc/sys/vm/max_map_count

This command should display the updated value you set in the sysctl.conf file.
4. Restart Vector

sudo systemctl restart vector


or whatever service management command is appropriate for your setup.

Sunday, 8 May 2022

Fixing "mount: wrong fs type, bad option, bad superblock" Error in Linux

If you're trying to mount a disk in Linux and encounter the error:
mount: /mnt/volumeaidata: wrong fs type, bad option, bad superblock on /dev/sdb1, missing codepage or helper program, or other error.
don't worry—this is a common issue with several possible causes and solutions.

Understanding the Error
This error usually indicates one of the following problems:
    The filesystem type is incorrect or unsupported.
    The disk is corrupted or has an unreadable superblock.
    The necessary filesystem utilities are missing.
    There are bad sectors on the disk.
    
Step 1: Check the Filesystem Type
First, determine the filesystem type of your partition with:
sudo blkid /dev/sdb1
This will return something like:
/dev/sdb1: UUID="1234-5678" TYPE="ext4"
If you see ext4, xfs, ntfs, or another filesystem type, ensure you are using the correct mount command. For example:
sudo mount -t ext4 /dev/sdb1 /mnt/volumeaidata
Unsure of the filesystem type, try auto-detect:
sudo mount -a

Step 2: Check for Missing Filesystem Utilities
Different filesystems require different utilities. Install the required packages:
    ext4 (ext2/ext3/ext4):
sudo apt install e2fsprogs  # Debian/Ubuntu
sudo yum install e2fsprogs  # RHEL/CentOS
XFS:
sudo apt install xfsprogs
sudo yum install xfsprogs
NTFS:
    sudo apt install ntfs-3g
    sudo yum install ntfs-3g
After installing, retry the mount command.

Step 3: Check and Repair the Disk
If the disk is corrupted, you may need to repair it.
    For ext4 (or ext2/ext3) Filesystems:
sudo fsck -y /dev/sdb1
For XFS Filesystems:
sudo xfs_repair /dev/sdb1
For NTFS Filesystems:
    sudo ntfsfix /dev/sdb1
After repairing, try mounting again.

Step 4: Check Superblock Issues
If the filesystem is still not mounting, the superblock may be damaged. Try finding backup superblocks:
sudo mke2fs -n /dev/sdb1
This will display backup superblock locations. Restore one using:
sudo e2fsck -b <backup-superblock> /dev/sdb1
sudo mount /dev/sdb1 /mnt/volumeaidata

Step 5: Verify Kernel and Logs
Check system logs for more details:
dmesg | tail -20
If  filesystem-related errors,need to update your kernel or install additional modules.

Monday, 6 December 2021

Understanding chmod Permissions in Linux

What Are Linux Permissions?
Before diving into chmod, let’s understand what file permissions in Linux mean. Linux uses a system of file permissions to control who can read, write, or execute a file or directory. These permissions are divided into three main categories:
  •     Owner (User): The person who owns the file or directory.
  •     Group: A group of users that can share the file.
  •     Others: All other users who are not the owner or part of the group.
Each category can have three types of permissions:
  •     Read (r): Permission to view the content of a file.
  •     Write (w): Permission to modify or delete the file.
  •     Execute (x): Permission to run the file as a program or script.
The basic syntax for the chmod command is:
chmod [options] mode file
  • mode specifies the permissions you want to set.
  • file refers to the name of the file or directory you want to modify.
Symbolic Mode vs Numeric Mode
Symbolic Mode
In symbolic mode, we use characters to represent the different types of users and permissions:
    u: Owner (user)
    g: Group
    o: Others
    a: All (owner, group, and others)
Permissions are represented by:
    r: Read
    w: Write
    x: Execute
You can modify the permissions using + (add), - (remove), and = (set exactly).
Examples:
    chmod u+x file.txt: Add execute permission for the owner.
    chmod g-w file.txt: Remove write permission for the group.
    chmod o=r file.txt: Set read-only permission for others.

Numeric Mode
In numeric mode, permissions are represented by numbers. Each permission is given a numeric value:
    r (read) = 4
    w (write) = 2
    x (execute) = 1
    No permission = 0
Each category (Owner, Group, and Others) gets a number, and these values are combined to form a three-digit number. Here’s how:
  •     Owner permissions are set by the first digit.
  •     Group permissions are set by the second digit.
  •     Others permissions are set by the third digit.
The value is calculated by adding the permissions for each category. For example, if you want the owner to have read, write, and execute permissions (7), the group to have only read permissions (4), and others to have no permissions (0),
use:
chmod 740 file.txt

Common chmod Examples:
Let’s look at some common chmod scenarios and how you can apply them:
1. Setting Full Permissions for the Owner (Read, Write, Execute)
To allow the file owner full control (read, write, execute), but restrict permissions for the group and others:
chmod 700 file.txt
This means:
    Owner: Read, Write, Execute (7)
    Group: No permissions (0)
    Others: No permissions (0)
2. Making a File Read-Only for Everyone
To make a file readable by everyone but not writable or executable, use:
chmod 444 file.txt
This means:
    Owner: Read-only (4)
    Group: Read-only (4)
    Others: Read-only (4)
3. Allowing Everyone to Read, Write, and Execute a File
To give read, write, and execute permissions to everyone (Owner, Group, and Others), use:
chmod 777 file.txt
This means:
    Owner: Read, Write, Execute (7)
    Group: Read, Write, Execute (7)
    Others: Read, Write, Execute (7)
Warning: Granting full permissions (777) to files, especially sensitive ones, can create security vulnerabilities. It’s best to use this sparingly and only on files that require full access.
4. Granting Execute Permission to a Script for the Owner
If you want to make a script executable by the owner, use:
chmod u+x script.sh
This adds the execute permission to the owner of the file.
5. Revoking Write Permission from Group
To remove write permission for the group but leave the read and execute permissions intact:
chmod g-w file.txt
Special Permissions:
In addition to the regular read, write, and execute permissions, there are a few special permissions in Linux that can be set using chmod:
  • Setuid: When set on an executable file, the process runs with the permissions of the file owner. (Numeric value: 4)
  • Setgid: When set on a directory, new files created within inherit the group of the directory. (Numeric value: 2)
  • Sticky Bit: Often used on directories, it allows only the owner of a file within a directory to delete or rename that file. (Numeric value: 1)
To set these special permissions, you can add a leading digit to the numeric mode. For example:
chmod 4755 script.sh

This would set the setuid permission along with standard read, write, and execute permissions.

Thursday, 11 November 2021

ps -ef Command Cheat Sheet

## Basic Syntax and Options
1. ps -ef                    # Basic full format listing
2. ps -ef -ww               # Full format with unlimited width
3. ps -efw                  # Wide format
4. ps -ef --cols 1000       # Set output width to 1000
5. ps -ef --no-headers      # Remove header line

## Output Formatting
6. ps -ef -o pid,ppid,user,cmd    # Custom columns
7. ps -ef -o pid,ppid,user,%cpu,%mem,cmd  # With CPU and memory
8. ps -ef -o pid,ppid,user,cmd --sort=-pid  # Sort by PID
9. ps -ef -o pid,ppid,user,cmd --sort=-%cpu # Sort by CPU usage
10. ps -ef -o pid,ppid,user,cmd --sort=-%mem # Sort by memory usage

## Process Filtering
11. ps -ef | grep <pattern>        # Basic grep
12. ps -ef | grep -i <pattern>     # Case-insensitive
13. ps -ef | grep -v <pattern>     # Exclude pattern
14. ps -ef | grep -A 2 -B 2 <pattern>  # Show context
15. ps -ef | grep -E "pattern1|pattern2"  # Multiple patterns

## Process Tree
16. ps -ef --forest               # Show process tree
17. ps -ef --forest | grep <pattern>  # Filter process tree
18. ps -ef --forest -o pid,ppid,user,cmd  # Custom tree format
19. ps -ef --forest --sort=-%cpu  # Tree sorted by CPU
20. ps -ef --forest --sort=-%mem  # Tree sorted by memory

## User-Specific Commands
21. ps -ef | grep ^root          # Root processes
22. ps -ef | grep ^$USER         # Current user processes
23. ps -ef -u username           # Specific user processes
24. ps -ef -u root,username      # Multiple users
25. ps -ef | grep -E "^root|^$USER"  # Root and current user

## CPU and Memory Monitoring
26. ps -ef --sort=-%cpu | head -n 10  # Top 10 CPU processes
27. ps -ef --sort=-%mem | head -n 10  # Top 10 memory processes
28. ps -ef -o pid,ppid,user,%cpu,%mem,cmd --sort=-%cpu  # Detailed CPU
29. ps -ef -o pid,ppid,user,%cpu,%mem,cmd --sort=-%mem  # Detailed memory
30. ps -ef | awk '$3 > 50 {print}'  # High CPU usage

## Process Timing
31. ps -ef | grep "$(date +%H:%M)"  # Current hour:minute
32. ps -ef | grep "$(date +%H)"     # Current hour
33. ps -ef -o pid,ppid,user,lstart,cmd  # With start time
34. ps -ef -o pid,ppid,user,etime,cmd   # With elapsed time
35. ps -ef | grep -E "$(date +%H):[0-5][0-9]"  # Current hour range

## Process State
36. ps -ef | grep -i "zombie"      # Zombie processes
37. ps -ef | grep -i "defunct"     # Defunct processes
38. ps -ef | grep -i "sleep"       # Sleeping processes
39. ps -ef | grep -i "running"     # Running processes
40. ps -ef | grep -i "stopped"     # Stopped processes

## Port and Network
41. ps -ef | grep <port_number>    # Processes by port
42. ps -ef | grep -i "listen"      # Listening processes
43. ps -ef | grep -i "established" # Established connections
44. ps -ef | grep -i "netstat"     # Network statistics
45. ps -ef | grep -i "tcp"         # TCP processes

## Application Specific

46. ps -ef | grep java            # Java processes
47. ps -ef | grep python          # Python processes
48. ps -ef | grep nginx           # Nginx processes
49. ps -ef | grep apache          # Apache processes
50. ps -ef | grep mysql           # MySQL processes

## Process Relationships
51. ps -ef | grep -E "^[0-9]+ [0-9]+ 1 "  # Direct children of init
52. ps -ef | grep -E "^[0-9]+ [0-9]+ $PPID "  # Children of current process
53. ps -ef | grep -E "^[0-9]+ $PPID "  # Parent of current process
54. ps -ef | grep -E "^[0-9]+ [0-9]+ $PID "  # Children of specific PID
55. ps -ef | grep -E "^[0-9]+ $PID "  # Parent of specific PID

## Resource Monitoring
56. ps -ef | awk '$3 > 50 {print}'  # High CPU usage
57. ps -ef | awk '$4 > 50 {print}'  # High memory usage
58. ps -ef | awk '$3 > 50 || $4 > 50 {print}'  # High CPU or memory
59. ps -ef | awk '$3 > 50 && $4 > 50 {print}'  # High CPU and memory
60. ps -ef | awk '$3 > 50 {print $2}'  # PIDs with high CPU

## Process Cleanup

61. ps -ef | grep -i "defunct" | awk '{print $2}'  # Defunct PIDs
62. ps -ef | grep -i "zombie" | awk '{print $2}'   # Zombie PIDs
63. ps -ef | grep -i "stopped" | awk '{print $2}'  # Stopped PIDs
64. ps -ef | grep -i "sleep" | awk '{print $2}'    # Sleeping PIDs
65. ps -ef | grep -i "running" | awk '{print $2}'  # Running PIDs

## Process Analysis
66. ps -ef | grep -i "cron"        # Cron jobs
67. ps -ef | grep -i "daemon"      # Daemon processes
68. ps -ef | grep -i "systemd"     # Systemd processes
69. ps -ef | grep -i "kernel"      # Kernel processes
70. ps -ef | grep -i "init"        # Init processes

## Process Monitoring

71. watch -n 1 'ps -ef | grep <pattern>'  # Real-time monitoring
72. ps -ef | grep <pattern> | wc -l  # Count matching processes
73. ps -ef | grep <pattern> | awk '{print $2}'  # Get PIDs
74. ps -ef | grep <pattern> | awk '{print $1}'  # Get users
75. ps -ef | grep <pattern> | awk '{print $3}'  # Get PPIDs

## Process Statistics

76. ps -ef | awk '{print $1}' | sort | uniq -c  # Process count by user
77. ps -ef | awk '{print $3}' | sort | uniq -c  # Process count by PPID
78. ps -ef | awk '{print $2}' | sort | uniq -c  # Process count by PID
79. ps -ef | awk '{print $5}' | sort | uniq -c  # Process count by TTY
80. ps -ef | awk '{print $6}' | sort | uniq -c  # Process count by time

## Process Debugging
81. ps -ef | grep -i "error"       # Error processes
82. ps -ef | grep -i "fail"        # Failed processes
83. ps -ef | grep -i "crash"       # Crashed processes
84. ps -ef | grep -i "hang"        # Hung processes
85. ps -ef | grep -i "dead"        # Dead processes

## Process Management
86. ps -ef | grep -i "kill"        # Kill processes
87. ps -ef | grep -i "stop"        # Stop processes
88. ps -ef | grep -i "start"       # Start processes
89. ps -ef | grep -i "restart"     # Restart processes
90. ps -ef | grep -i "reload"      # Reload processes

## Process Security

91. ps -ef | grep -i "root"        # Root processes
92. ps -ef | grep -i "sudo"        # Sudo processes
93. ps -ef | grep -i "su"          # Su processes
94. ps -ef | grep -i "ssh"         # SSH processes
95. ps -ef | grep -i "telnet"      # Telnet processes

## Process Optimization
96. ps -ef | grep -i "high"        # High resource usage
97. ps -ef | grep -i "low"         # Low resource usage
98. ps -ef | grep -i "normal"      # Normal resource usage
99. ps -ef | grep -i "critical"    # Critical processes
100. ps -ef | grep -i "important"  # Important processes

## Best Practices
- Always use -ww or --cols for full command lines
- Use less or more for long outputs
- Use grep --color=auto for better visibility
- Save output to file for analysis
- Use --forest to understand process relationships
- Use --sort to organize output by different criteria
- Use awk for complex filtering
- Use watch for real-time monitoring
- Use custom formats for specific needs
- Use process trees for relationship analysis %    

Thursday, 28 October 2021

How to Fix the "Side Channel and TCP Port Forwarding" Error in OpenSSH

If you've encountered an error while trying to establish an SSH connection that involves "side channel" or "TCP port forwarding," it typically means that your OpenSSH server (SSHD) is rejecting your client's request to open a side channel or initiate TCP port forwarding. This issue can be fixed by ensuring that the correct SSHD settings are enabled on the server. 

Here's how to resolve the issue:
1. Settings You Need to Enable on the SSHD Server
The following settings must be enabled in your SSHD configuration to allow side-channel communication and TCP port forwarding:
    TCPKeepAlive: This setting specifies whether the system should send TCP keepalive messages to the other side to maintain the connection.
    AllowTCPForwarding: This option needs to be enabled on the server to allow port forwarding. By default, SSH servers may have this option disabled for security purposes.
    PermitOpen: This option specifies which destinations are allowed for TCP port forwarding. You need to set this correctly to allow forwarding.

2. Find Current Settings
Before making any changes, it's a good idea to check the current SSHD settings to see if any of the required options are disabled.
sudo sshd -T | grep -Ei 'TCPKeepAlive|AllowTCPForwarding|PermitOpen'
/etc/ssh/sshd_config line 32: Deprecated option KeyRegenerationInterval
/etc/ssh/sshd_config line 33: Deprecated option ServerKeyBits
/etc/ssh/sshd_config line 39: Deprecated option RSAAuthentication
/etc/ssh/sshd_config line 41: Deprecated option RhostsRSAAuthentication
tcpkeepalive no
allowtcpforwarding yes
permitopen any

tcpkeepalive no: This means that TCP keepalive messages are disabled, which could lead to connection timeouts.
 
3. Set Correct Values
    Open the SSH configuration file (/etc/ssh/sshd_config) in a text editor:
sudo nano /etc/ssh/sshd_config

Look for the following directives and adjust them as needed:
TCPKeepAlive yes
AllowTCPForwarding yes
PermitOpen any

Verify the changes by running the command again to check that the settings have been applied correctly:
sudo sshd -T | grep -Ei 'TCPKeepAlive|AllowTCPForwarding|PermitOpen'
    tcpkeepalive yes
    allowtcpforwarding yes
    permitopen any

4. Reload SSHD to Apply Changes
After updating the SSHD configuration file, you'll need to reload the SSHD service to apply the changes.
sudo systemctl reload sshd.service

Monday, 20 September 2021

How Computers Understand IPv4 Addresses

IPv4 in Different Formats
What Humans See vs What Computers See
Human Format (Dotted Decimal): 192.168.1.1
Computer Format (32-bit Binary): 11000000.10101000.00000001.00000001
Computer Format (Single Decimal): 3232235777

Step-by-Step Conversion Example
Let's take the IP address 192.168.1.1 and see how a computer processes it:
Step 1: Convert Each Octet to Binary
192 = 11000000
168 = 10101000
1   = 00000001
1   = 00000001

Step 2: How to Convert Decimal to Binary
Let's break down 192:
192 ÷ 2 = 96 remainder 0
96  ÷ 2 = 48 remainder 0
48  ÷ 2 = 24 remainder 0
24  ÷ 2 = 12 remainder 0
12  ÷ 2 = 6  remainder 0
6   ÷ 2 = 3  remainder 0
3   ÷ 2 = 1  remainder 1
1   ÷ 2 = 0  remainder 1
Reading upward: 11000000

Step 3: Computer's Internal Representation
The computer sees the entire IP as one 32-bit number:
11000000 10101000 00000001 00000001

Step 4: Converting to Single Decimal Number
To get the decimal equivalent:
Binary: 11000000101010000000000100000001
Decimal: 3,232,235,777

More Examples
Example 1: Google's DNS 8.8.8.8
Human: 8.8.8.8
Binary: 00001000.00001000.00001000.00001000
Single Decimal: 134,744,072
Conversion of 8 to binary:
8 ÷ 2 = 4 remainder 0
4 ÷ 2 = 2 remainder 0  
2 ÷ 2 = 1 remainder 0
1 ÷ 2 = 0 remainder 1
Result: 00001000

Example 2: Localhost 127.0.0.1
Human: 127.0.0.1
Binary: 01111111.00000000.00000000.00000001
Single Decimal: 2,130,706,433

###  Behind the Scenes: How Your Computer Uses This
Here's what happens when you type `ping 192.168.1.1`:
1. Parse: Computer splits "192.168.1.1" into [192, 168, 1, 1]
2. Convert: Each number becomes 8-bit binary
3. Combine: Creates the 32-bit address `11000000101010000000000100000001`
4. Process: Uses binary operations for routing decisions

### Network Calculations Made Simple
Your computer uses these binary representations for lightning-fast network calculations:
Your IP:       192.168.1.100 → 11000000.10101000.00000001.01100100
Subnet Mask:   255.255.255.0 → 11111111.11111111.11111111.00000000
Network:       192.168.1.0   → 11000000.10101000.00000001.00000000

This binary math helps your router instantly determine whether to send data locally or to the next hop on the internet.


Delete the last 10 files in a directory in Linux

To delete the last 10 files in a directory using a command in Linux, you can use a combination of ls, tail, xargs, and rm

ls -ltr | tail -n 10 | awk '{print $9}' | xargs rm

Breakdown of the Command:
 ls -ltr: Lists files in long format (-l), sorted by modification time, with the newest files last (-t), and in reverse order (-r).
 tail -n 10: Selects the last 10 lines from the ls output. Note that tail -n 9 would give you only 9 files; -n 10 ensures you are selecting the last 10 files.
 awk '{print $9}': Extracts the filenames from the ls output. This assumes filenames are in the 9th column. This might need adjustments based on the actual output format, especially if filenames have spaces.
 xargs rm: Passes the extracted filenames to rm for deletion.

 
 
 

 

Friday, 2 October 2020

You (oracle) are not allowed to use this program (crontab)

error while checking crontab details as oracle user

[oracle@dbracnode02 ~]$ crontab -l
You (oracle) are not allowed to use this program (crontab)
See crontab(1) for more information

Check entries in /etc/cron.allow

[root@dbracnode02 ~]# cat /etc/cron.allow


To solve this error you should add oracle or any other users that will use the Crontab into this file as follows.

Add oracle user and save /etc/cron.allow
[root@dbracnode02 ~]# vi /etc/cron.allow
root
oracle
grid

[root@dbracnode02 ~]# cat /etc/cron.allow
root
oracle
grid

Check now:
[oracle@dbracnode02 ~]$ crontab -l
no crontab for oracle


Sunday, 6 September 2020

Check CPU Clock Speed on Linux

we can find cpu processor details using below commands 


1. lscpu: 

It display information about the CPU architecture

# lscpu

Architecture:          x86_64

CPU op-mode(s):        32-bit, 64-bit

Byte Order:            Little Endian

CPU(s):                96

On-line CPU(s) list:   0-95

Thread(s) per core:    2

Core(s) per socket:    24

Socket(s):             2

NUMA node(s):          1

Vendor ID:             GenuineIntel

CPU family:            6

Model:                 85

Model name:            Intel(R) Xeon(R) Platinum 8168 CPU @ 2.70GHz

Stepping:              4

CPU MHz:               3374.652

CPU max MHz:           3700.0000

CPU min MHz:           1200.0000

BogoMIPS:              5400.00

Virtualization:        VT-x

L1d cache:             32K

L1i cache:             32K

L2 cache:              1024K

L3 cache:              33792K

NUMA node0 CPU(s):     0-95

2. dmidecode: print cpu speed

# dmidecode -t processor | grep "Speed"

        Max Speed: 4000 MHz

        Current Speed: 2700 MHz

        Max Speed: 4000 MHz

        Current Speed: 2700 MHz


3. Each CPU processor details from  /proc/cpuinfo file 

cat  /proc/cpuinfo

cat  /proc/cpuinfo | grep