Creating a 2 GB swap file on Debian 13
user: give me the commands to implement a 2 GB swap file on on my Debian 13 laptop workstation
assistant: Got it 🚀 — here’s how you can add a **2 GB swap file** on your Debian 13 laptop workstation. Since you prefer using `root` (not `sudo`), I’ll give you the direct root commands.
---
### Commands
```bash
# 1. Create an empty 2 GB file at /swapfile
fallocate -l 2G /swapfile
# 2. Secure the file so only root can read/write
chmod 600 /swapfile
# 3. Set it up as swap space
mkswap /swapfile
# 4. Enable the swap file immediately
swapon /swapfile
# 5. Verify it’s active
swapon --show
free -h
# 6. Make it persistent across reboots by adding to /etc/fstab
echo '/swapfile none swap sw 0 0' >> /etc/fstab
```
---
### Notes
- If `fallocate` doesn’t work on your filesystem (rare but possible), use:
```bash
dd if=/dev/zero of=/swapfile bs=1M count=2048 status=progress
```
- `chmod 600` is **mandatory** for security (otherwise `mkswap` will complain).
- You can later resize or remove the swap file if needed.
---
Do you want me to also include **tuning `swappiness` and cache pressure** (to control how aggressively Debian uses swap), or just leave it at the default?
user: yes include tuning
assistant: Awesome — here’s a clean setup with **tuning** (optimized for an SSD laptop). I’ll give you both the plain commands and, since you like it, a ready-to-run script in your style.
---
# Quick commands (run as `root`)
```bash
# Create a 2 GB swap file
fallocate -l 2G /swapfile || dd if=/dev/zero of=/swapfile bs=1M count=2048 status=progress
# Secure it
chmod 600 /swapfile
# Make it swap and enable now
mkswap /swapfile
swapon /swapfile
# Persist it across reboots (add pri for higher priority than any future zram, etc.)
grep -q '^/swapfile ' /etc/fstab || echo '/swapfile none swap sw,pri=100 0 0' >> /etc/fstab
# Tune swap behavior (lower = use RAM more, swap less)
cat >/etc/sysctl.d/99-swap-tuning.conf <<'EOF'
vm.swappiness=15
vm.vfs_cache_pressure=50
EOF
# Apply tuning immediately
sysctl --system
# Verify
swapon --show
free -h
sysctl vm.swappiness vm.vfs_cache_pressure
```
**What these tunables do (chosen defaults):**
- `vm.swappiness=15` → prefers RAM, only swaps under pressure (nice for SSDs and interactive desktops).
- `vm.vfs_cache_pressure=50` → keeps inode/dentry caches around longer so your filesystem stays snappy.
---
# One-shot script (your preferred flow)
**1) Create the file with nano**
```bash
nano /home/adam/swap-2g-setup.sh
```
**2) Paste this script content**
```bash
#!/bin/bash
set -euo pipefail
# === 2 GB swap file with SSD-friendly tuning ===
# Create swap file (fallocate preferred, dd fallback)
if ! fallocate -l 2G /swapfile 2>/dev/null; then
dd if=/dev/zero of=/swapfile bs=1M count=2048 status=progress
fi
chmod 600 /swapfile
mkswap /swapfile
# Enable now
swapon /swapfile || true
# Persist in /etc/fstab with a priority
if ! grep -qE '^/swapfile\s+' /etc/fstab; then
echo '/swapfile none swap sw,pri=100 0 0' >> /etc/fstab
fi
# Swap tuning
cat >/etc/sysctl.d/99-swap-tuning.conf <<'EOF'
vm.swappiness=15
vm.vfs_cache_pressure=50
EOF
# Apply immediately
sysctl --system >/dev/null
echo "✅ Swap enabled and tuned:"
swapon --show
free -h…
user: dont i need ot be root to do this?
assistant: Yes 👍 — you **do need to be root** for almost every step here:
- Creating `/swapfile` at the root of the filesystem
- Running `mkswap` / `swapon`
- Editing `/etc/fstab`
- Writing `/etc/sysctl.d/*.conf`
Since you normally work as `root` (old-school Debian style, no `sudo`), just log in as `root` first:
```bash
su -
```
Then run the commands or the script.
⚡ If you want to use the script I wrote:
- Save it under `/home/adam/` while logged in as `adam`.
- Then switch to `root` and run it using the **full path**:
```bash
su -
/home/adam/swap-2g-setup.sh
```
That way you stay consistent with your workflow (scripts stored in `/home/adam/`, executed as `root`).
---
👉 Do you want me to adjust the script so it **auto-detects if it’s not root** and exits with a warning, just to be safe?
user: yes, I think all your scripts should check that
assistant: Perfect — I’ll bake the **root check** into the script and keep everything idempotent and in your preferred style (stored in `/home/adam`, run as **root**, full paths). Emojis included for quick visual scan.
---
# Create → make executable → run (as **root**)
```bash
# Log in as root first:
su -
# 1) Create the script
nano /home/adam/swap-2g-setup.sh
```
**Paste this content:**
```bash
#!/bin/bash
set -euo pipefail
# === 2 GB swap file + SSD-friendly tuning (Debian 13) ===
# Requirements: run as root. Script is idempotent.
# --- Safety: require root ---
if [[ "${EUID:-$(id -u)}" -ne 0 ]]; then
echo "⛔ This script must be run as root. Try: su -"
exit 77
fi
echo "🧰 Starting swap setup..."
# --- Create /swapfile if missing ---
if [[ ! -f /swapfile ]]; then
echo "📦 Creating /swapfile (2G)..."
if ! fallocate -l 2G /swapfile 2>/dev/null; then
echo "⚠️ fallocate unavailable/unsupported; using dd..."
dd if=/dev/zero of=/swapfile bs=1M count=2048 status=progress
fi
else
echo "ℹ️ /swapfile already exists, reusing it."
fi
# --- Secure permissions ---
chmod 600 /swapfile
# --- Initialize swap header (safe to re-run) ---
echo "🪄 Initializing swap area..."
mkswap /swapfile >/dev/null
# --- Enable swap if not already on ---
if ! awk '{print $1}' /proc/swaps | grep -qx '/swapfile'; then
echo "🚀 Enabling swap..."
swapon /swapfile
else
echo "✅ Swap already active on /swapfile."
fi
# --- Persist across reboots in /etc/fstab ---
if ! grep -qE '^/swapfile\s+none\s+swap\s+' /etc/fstab; then
echo "📝 Adding /swapfile to /etc/fstab..."
echo '/swapfile none swap sw,pri=100 0 0' >> /etc/fstab
else
echo "ℹ️ /etc/fstab already contains /swapfile."
fi
# --- Tuning: swappiness & VFS cache pressure ---
echo "🎛️ Applying SSD-friendly VM tuning..."
cat >/etc/sysctl.d/99-swap-tuning.conf <<'EOF'
vm.swappiness=15
vm.vfs_cache_pressure=50
EOF
sysctl --system >/dev/null
# --- Report ---
echo "📊 Current swap:"
swapon --show || true
free -h || true
echo …
user: yes
assistant: Locked in. From now on, I’ll include a **root check + emoji status lines** in every script I write for you.
Here’s a tiny **standard header** you can paste into any script to keep things consistent:
```bash
#!/bin/bash
set -euo pipefail
# === <SCRIPT NAME> (Debian 13) ===
# Requirements: run as root. Idempotent where possible.
# --- Safety: require root ---
if [[ "${EUID:-$(id -u)}" -ne 0 ]]; then
echo "⛔ This script must be run as root. Try: su -"
exit 77
fi
echo "🧰 Starting <SCRIPT NAME>..."
```
Your preferred **one-liner flow** (create → chmod → run) will look like this for any script:
```bash
nano /home/adam/my-script.sh && chmod +x /home/adam/my-script.sh && /home/adam/my-script.sh
```
If you want, I can also give you a tiny **skeleton generator** you run once (as root) that drops a new file pre-filled with the header + placeholders. Just say the word and the desired script name, and I’ll spit out the exact command.
Showing first 10 of 10 messages. View the full conversation at https://mcwai.org/ai-conversation/creating-a-2-gb-swap-file-on-debian-13