Command documentation sourced from the linux-command project This comprehensive command reference is part of the linux-command documentation project.
mkswap - Setup Linux swap area
The mkswap command sets up a Linux swap area on a device or file, preparing it for use as virtual memory by the system. It initializes the specified device or file with a swap signature, sets up the swap space format, and can optionally specify a label for identification. Swap space is crucial for system performance when physical RAM becomes scarce, allowing the kernel to move less frequently used memory pages to disk.
Basic Syntax
mkswap [OPTIONS] DEVICE [SIZE]
Common Options
Size Options
-f, --force- Force creation, even if the device appears to be in use-p, --pagesize SIZE- Set page size (in bytes)-L, --label LABEL- Set a label for the swap area-v, --version- Display version information-h, --help- Display help information
Check Options
-c, --check- Check for bad blocks before creating swap-U, --uuid UUID- Set the UUID for the swap area--verbose- Provide verbose output
Data Options
--lock- Use exclusive device lock (default)--lockfd NUM- Keep file descriptor NUM locked
Usage Examples
Basic Swap Area Creation
Creating Swap on Devices
# Create swap on entire disk partition
mkswap /dev/sda2
# Create swap with specific label
mkswap -L "swap_volume" /dev/sda3
# Force creation (use with caution)
mkswap -f /dev/sdb1
# Create swap with custom UUID
mkswap -U a1b2c3d4-e5f6-7890-abcd-ef1234567890 /dev/sda2
Creating Swap Files
# Create a swap file
dd if=/dev/zero of=/swapfile bs=1G count=8
chmod 600 /swapfile
mkswap /swapfile
# Create labeled swap file
mkswap -L "file_swap" /swapfile
# Create swap file with UUID
mkswap -f /swapfile
Advanced Swap Setup
Multiple Swap Areas
# Create multiple swap areas with different priorities
mkswap -L "swap_primary" /dev/sda2
mkswap -L "swap_secondary" /dev/sdb1
# Create swap areas for specific purposes
mkswap -L "emergency_swap" /dev/sdc1
mkswap -L "hibernation_swap" /dev/sdd1
System-Specific Configurations
# Create swap with specific page size
mkswap -p 4096 /dev/sda2
# Create swap for 32-bit system
mkswap -p 4096 /dev/sda2
# Create swap with verbose output
mkswap --verbose /dev/sda2
Practical Examples
System Administration
Emergency Swap Creation
# Create emergency swap when system is low on memory
dd if=/dev/zero of=/tmp/emergency_swap bs=1M count=1024
chmod 600 /tmp/emergency_swap
mkswap -L "emergency_swap" /tmp/emergency_swap
swapon /tmp/emergency_swap
# Monitor swap usage
free -h
swapon --show
Swap Space Management
# Check available space before creating swap
lsblk
fdisk -l
# Create swap on new partition
fdisk /dev/sdb # Create partition, type 82 (Linux swap)
mkswap -L "new_swap" /dev/sdb1
swapon /dev/sdb1
# Update /etc/fstab for persistent swap
echo "LABEL=new_swap none swap sw 0 0" >> /etc/fstab
Swap Space Optimization
# Create optimized swap for SSD
mkswap -L "ssd_swap" /dev/nvme0n1p2
# Create swap with specific priorities
mkswap -L "fast_swap" /dev/nvme0n1p2 # NVMe SSD
mkswap -L "slow_swap" /dev/sdb1 # HDD
Virtual Machine Setup
KVM/QEMU Swap Setup
# Create swap file for VM
qemu-img create -f raw swap.img 8G
mkswap -L "vm_swap" swap.img
# Add to VM configuration
echo "UUID=$(blkid -s UUID -o value swap.img) none swap sw 0 0" >> /etc/fstab
Container Swap Configuration
# Create swap for containers
mkdir -p /var/lib/containers/swap
dd if=/dev/zero of=/var/lib/containers/swap/swapfile bs=1G count=4
chmod 600 /var/lib/containers/swap/swapfile
mkswap -L "container_swap" /var/lib/containers/swap/swapfile
High Availability Systems
Redundant Swap Setup
# Create redundant swap areas
for disk in /dev/sd{b,c,d}; do
mkswap -L "redundant_swap_${disk##*/}" "${disk}1"
done
# Configure priority-based swap
echo "LABEL=redundant_swap_sdb1 none swap sw,pri=1 0 0" >> /etc/fstab
echo "LABEL=redundant_swap_sdc1 none swap sw,pri=2 0 0" >> /etc/fstab
echo "LABEL=redundant_swap_sdd1 none swap sw,pri=3 0 0" >> /etc/fstab
Swap for Database Systems
# Create dedicated swap for database
mkswap -L "db_swap" /dev/sdb1
# Create swap file with specific permissions
dd if=/dev/zero of=/var/lib/postgresql/swap bs=1G count=16
chown postgres:postgres /var/lib/postgresql/swap
chmod 600 /var/lib/postgresql/swap
mkswap -L "postgres_swap" /var/lib/postgresql/swap
Performance Tuning
Swap Area Configuration
# Create swap with specific alignment for SSD
mkswap -L "aligned_swap" /dev/sdb1
# Configure swappiness after creating swap
echo 10 > /proc/sys/vm/swappiness
echo "vm.swappiness=10" >> /etc/sysctl.conf
Swap Priority Management
# Create multiple swap areas with different priorities
mkswap -L "high_priority_swap" /dev/nvme0n1p2
mkswap -L "low_priority_swap" /dev/sdb1
# Configure with priority values
echo "LABEL=high_priority_swap none swap sw,pri=100 0 0" >> /etc/fstab
echo "LABEL=low_priority_swap none swap sw,pri=1 0 0" >> /etc/fstab
Integration and Automation
Shell Scripts
Automated Swap Setup Script
#!/bin/bash
# Automated swap setup script
DEVICE=$1
LABEL=${2:-"system_swap"}
SIZE=${3:-""}
if [ -z "$DEVICE" ]; then
echo "Usage: $0 <device> [label] [size]"
exit 1
fi
# Check if device exists
if [ ! -b "$DEVICE" ]; then
echo "Error: Device $DEVICE does not exist"
exit 1
fi
# Create swap area
echo "Creating swap area on $DEVICE..."
mkswap -L "$LABEL" $DEVICE
# Add to fstab if not already present
UUID=$(blkid -s UUID -o value $DEVICE)
if ! grep -q "$UUID" /etc/fstab; then
echo "UUID=$UUID none swap sw 0 0" >> /etc/fstab
echo "Added to /etc/fstab"
fi
# Enable swap
swapon $DEVICE
echo "Swap area created and enabled successfully"
# Display status
free -h
swapon --show
Swap Space Monitoring Script
#!/bin/bash
# Monitor and manage swap space
# Check swap usage
SWAP_USAGE=$(free | grep Swap | awk '{printf "%.1f", $3/$2 * 100.0}')
if (( $(echo "$SWAP_USAGE > 80" | bc -l) )); then
echo "Warning: Swap usage is at ${SWAP_USAGE}%"
# Create emergency swap if needed
if [ -f /tmp/emergency_swap ]; then
echo "Emergency swap already exists"
else
echo "Creating emergency swap..."
dd if=/dev/zero of=/tmp/emergency_swap bs=1M count=1024
chmod 600 /tmp/emergency_swap
mkswap -L "emergency_swap" /tmp/emergency_swap
swapon /tmp/emergency_swap
fi
fi
# Display swap information
echo "Swap areas:"
swapon --show
Systemd Integration
Swap Service Configuration
# Create systemd service for swap management
cat > /etc/systemd/system/custom-swap.service << 'EOF'
[Unit]
Description=Custom Swap Setup
After=local-fs.target
[Service]
Type=oneshot
ExecStart=/usr/local/bin/setup_swap.sh
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target
EOF
# Enable and start the service
systemctl enable custom-swap
systemctl start custom-swap
Troubleshooting
Common Issues
Device Already in Use
# Error: Device or resource busy
# Solution: Check and stop any processes using the device
lsof /dev/sda2
swapoff /dev/sda2
mkswap -f /dev/sda2
Permission Issues
# Error: Permission denied
# Solution: Use sudo or check file permissions
sudo mkswap /dev/sda2
sudo chmod 600 /swapfile
sudo mkswap /swapfile
Invalid Device
# Error: Invalid argument
# Solution: Check if device exists and is block device
ls -la /dev/sda2
file /dev/sda2
fdisk -l
Swap Space Problems
Swap Not Activated
# Check why swap isn't active
swapon --show
cat /proc/swaps
free -h
# Activate swap manually
swapon /dev/sda2
swapon /swapfile
# Check fstab configuration
cat /etc/fstab
systemctl daemon-reload
Performance Issues
# Monitor swap activity
vmstat 1
iostat -x 1
sar -W 1
# Adjust swappiness
cat /proc/sys/vm/swappiness
echo 10 > /proc/sys/vm/swappiness
# Check swap priority
cat /proc/swaps
swapon --priority=100 /dev/sda2
Advanced Usage
Swap File Management
Creating Multiple Swap Files
# Create multiple swap files for better management
for i in {1..4}; do
dd if=/dev/zero of="/swapfile_$i" bs=1G count=2
chmod 600 "/swapfile_$i"
mkswap -L "swap_file_$i" "/swapfile_$i"
swapon "/swapfile_$i"
done
# Configure in fstab
for i in {1..4}; do
echo "/swapfile_$i none swap sw 0 0" >> /etc/fstab
done
Swap File Resizing
# Resize swap file
swapoff /swapfile
dd if=/dev/zero of=/swapfile bs=1G count=16 seek=8
mkswap /swapfile
swapon /swapfile
Encryption and Security
Encrypted Swap Setup
# Create encrypted swap device
cryptsetup luksFormat /dev/sda2
cryptsetup open /dev/sda2 crypt_swap
mkswap -L "encrypted_swap" /dev/mapper/crypt_swap
swapon /dev/mapper/crypt_swap
Secure Swap File Creation
# Create secure swap file with random data
dd if=/dev/urandom of=/secure_swap bs=1M count=1024
chmod 600 /secure_swap
mkswap -L "secure_swap" /secure_swap
Related Commands
swapon- Enable devices and files for paging and swappingswapoff- Disable devices and files for paging and swappingfree- Display amount of free and used memory in the systemvmstat- Report virtual memory statisticstop- Display Linux processesfdisk- Manipulate disk partition tablelsblk- List block devicesblkid- Locate/print block device attributesdd- Convert and copy a file
Best Practices
- Use appropriate swap size: 1-2x RAM for desktop systems, 0.5-1x RAM for servers
- Label swap areas: Always use
-Lfor easy identification - Check devices: Use
-coption to check for bad blocks on new devices - Secure permissions: Set 600 permissions on swap files
- Update fstab: Add swap areas to
/etc/fstabfor automatic activation - Monitor usage: Regularly check swap usage and performance impact
- Use priority: Set appropriate priorities when using multiple swap areas
- Consider SSD: For SSD systems, consider enabling swap with careful monitoring
- Test setup: Always test swap activation after creation
- Backup configuration: Keep records of swap configurations
Performance Tips
- Use fast storage for swap areas (SSD or NVMe preferred)
- Dedicated partition is generally faster than swap files
- Multiple swap areas with proper priorities can improve performance
- Monitor swappiness setting and adjust based on workload
- Consider hibernation needs when sizing swap space
- Use RAID arrays for high-availability swap configurations
- Enable swap compression if supported by your kernel
- Regular monitoring of swap usage patterns for optimization
The mkswap command is essential for system memory management, providing the foundation for virtual memory operations. Proper configuration and monitoring of swap areas are crucial for maintaining optimal system performance, especially on systems with limited physical RAM or heavy workloads.