Showing posts with label Sysadmin. Show all posts
Showing posts with label Sysadmin. Show all posts

Sunday, February 15, 2026

Comprehensive Guide for Swap Management, VM Stability, and Cleanup

Kali Linux Memory Optimization: Complete Log

Kali Linux Optimization Master Log

Comprehensive Guide for Swap Management, VM Stability, and Cleanup

1. The Core Memory Upgrade (Swap File)

To support 6+ VMs and Local AI on 16GB RAM, we replaced a 1GB physical partition with a 16GB Swap File.

# Create and Activate 16GB Swap
sudo swapoff -a
sudo fallocate -l 16G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
free -h
Verification: free -h should show ~15Gi to 17Gi under the Swap column.

2. Automation & Stability Scripts

A. The Panic Button (Pause All VMs)

Filename: ~/bin/vm_panic.sh

#!/bin/bash
RUNNING_VMS=$(vboxmanage list runningvms | cut -d '"' -f 2)
echo "$RUNNING_VMS" | while read -r VM; do
    vboxmanage controlvm "$VM" pause
done
notify-send -u critical "PANIC" "All VirtualBox VMs PAUSED."

B. The Resume Button (Unpause All)

Filename: ~/bin/vm_resume.sh

#!/bin/bash
PAUSED_VMS=$(vboxmanage list vms | cut -d '"' -f 2 | while read -r VM; do 
    vboxmanage showvminfo "$VM" --machinereadable | grep -q 'VMState="paused"' && echo "$VM"
done)
echo "$PAUSED_VMS" | while read -r VM; do
    vboxmanage controlvm "$VM" resume
done
notify-send "System Restored" "All paused VMs RESUMED."

3. GUI Cleanup: Deleting Ghost Partitions

We deleted the physical 1GB partition (sdb3) and cleaned /etc/fstab to prevent boot delays.

# Cleanup Command
sudo cp /etc/fstab /etc/fstab.bak && sudo sed -i '/sdb3/d' /etc/fstab
sudo systemctl daemon-reload
Warning: Always check cat /etc/fstab after running sed commands to ensure no critical lines were removed.

4. System Health Check

Use this script to verify your setup is still optimal after a reboot.

  1. Create the health check script: nano ~/check_health.sh
  2. Paste the code below and save the file.
  3. Execute with: bash ~/check_health.sh (Do not use python).
#!/bin/bash
# check_health.sh
echo "--- Swap Health Check ---"
SWAP_SIZE=$(free -h | grep Swap | awk '{print $2}')
echo "Active Swap Size: $SWAP_SIZE"
GHOSTS=$(grep "sdb3" /etc/fstab | grep -v "#")
if [ -z "$GHOSTS" ]; then echo "Result: /etc/fstab is CLEAN"; fi
PERMS=$(stat -c "%a" /swapfile)
if [ "$PERMS" == "600" ]; then echo "Result: Permissions are CORRECT"; fi

✔ SYSTEM OPTIMIZED

16GB RAM + 16GB Swap File | 0.03% Disk overhead | VM Stability: High

Generated on Feb 13, 2026 | User: Kali Linux Power User
#KaliLinux #SystemOptimization #CyberSecurity #Automation

Wednesday, February 11, 2026

How to Run 6+ VMs on Kali Linux Without Crashing: The Ultimate Swap & Stability Guide

Kali Linux Power User Guide - Kali AI Scripts

Kali Linux Power User Guide

Optimizing Swap, RAM, and Virtual Machine Stability for AI and Multi-VM Workloads.

Context: This guide is designed for users with 16GB RAM running heavy workloads (e.g., 6 VirtualBox VMs + Local AI + VS Code). When your total required RAM exceeds your physical RAM, these steps prevent system crashes.

The Power User Option: > How to Run 6+ VMs on Kali Linux Without Crashing: The Ultimate Swap & Stability Guide

The Technical Option: > Kali Linux Memory Management: Resizing Swap and Automating VM Stability for Local AI

The "Hacker" Aesthetic: > Hardening Kali: Optimizing RAM & Swap for High-Performance Virtualization

1. Resizing Swap (Switching to a 16GB Swap File)

Instead of resizing risky disk partitions, we use a Swap File. It is faster to set up and provides identical performance on modern SSDs.

Step A: Create the Swap File

Run these commands in order. We are creating a 16GB file to act as a safety buffer for your 16GB RAM.

# Turn off existing swap
sudo swapoff -a

# Create a 16GB file (This may take a moment)
sudo fallocate -l 16G /swapfile

# Secure the file so only root can read it
sudo chmod 600 /swapfile

# Format the file as Swap
sudo mkswap /swapfile

# Activate the new swap
sudo swapon /swapfile

# Verify the size (Look for 16G under the Swap row)
free -h
Kali Linux Swap configuration

Figure 1: Verified 16GiB Swap configuration in the Kali terminal. (Click to enlarge)

Step B: Make it Permanent

Without this step, the swap will disappear when you reboot. We must tell Linux to load it at every boot.

  1. Open the config file: sudo nano /etc/fstab
  2. Look for any old lines containing the word swap and put a # at the start of that line to disable it.
  3. Add this exact line to the very bottom of the file:
    /swapfile none swap sw 0 0

2. Optimizing "Swappiness"

By default, Linux swaps data quite early. For AI tasks, we want to keep data in the physical RAM as long as possible.

# Apply immediately
sudo sysctl vm.swappiness=10

# Make it permanent
echo 'vm.swappiness=10' | sudo tee -a /etc/sysctl.conf

3. Real-Time Monitoring Scripts

A. Create a Per-Process Swap Checker

This shows exactly which VM or AI process is using your swap space.

#!/bin/bash
printf "%-10s %-15s %-10s\n" "PID" "Process" "Swap Used"
echo "------------------------------------------"
for file in /proc/*/status ; do
  awk '/^Name:/ {name=$2} /^Pid:/ {pid=$2} /^VmSwap:/ {swap=$2 " " $3} END {if (swap) printf "%-10s %-15s %-10s\n", pid, name, swap}' "$file" 2>/dev/null
done | sort -k 3 -nr | head -n 15

4. The "Panic Button" for VMs

If the system begins to lag, use this to instantly pause all 6 VMs and freeze their RAM usage.

#!/bin/bash
RUNNING_VMS=$(vboxmanage list runningvms | cut -d '"' -f 2)
echo "$RUNNING_VMS" | while read -r VM; do
    vboxmanage controlvm "$VM" pause
done
notify-send -u critical "PANIC ACTION" "All VirtualBox VMs have been PAUSED."
Final Note: Running 24GB worth of VMs on 16GB of RAM is "Over-provisioning." Your 16GB Swap file makes this possible, but you will still experience disk-speed latency when switching between VMs.

Generated for Kali Linux Power Users - 2026
#Linux #CyberSecurity #VirtualBox #SysAdmin