Sometime you will need to increase space fo swap space in Linux – and for that isn’t needed to add new disk or to restart server – if you have enought free space on current environment you can use some amount of that server for extendinx swap. In this tutorial we will use variables for easier (copy-paste) usage and here we will extend swap for 2GB

# Variable - Where you want to be placed new swap file
SWAP_FILE="/mnt/SWAP/swapfile01.img"

# Making a directory for new swap file
mkdir -p `dirname ${SWAP_FILE}`

# Check - how much space we have in that location
df -h `dirname ${SWAP_FILE}`

# Check - is there already created file with same name
# if - exists it will be destroyed. Please use some other name 
# or location in first variable and start again
[ -f ${SWAP_FILE} ] && echo "File exists, CHANGE NAME in first step" || echo "Everything is OK, you can continue.."

# Variable: For how many GB you want to expand current swap (in this case for 8GB)
SWAP_IN_GB=8
dd if=/dev/zero of=${SWAP_FILE} bs=1024 count=${SWAP_IN_GB}M
chmod 0600 ${SWAP_FILE}

# Making od swap system inside created file
mkswap ${SWAP_FILE}

# Adding new swap space in /etc/fsttab (to be mounted with boot process)
cp /etc/fstab ~/`date +%Y%m%d`_backup_fstab #backup
echo "${SWAP_FILE}  swap  swap  sw  0  0" >> /etc/fstab 
mount -a # Check - is there any error while mounting from fstab

# Activating of new swap file
swapon ${SWAP_FILE}

# Check new swap space - is it increased for ${SWAP_IN_GB} GB
grep Swap /proc/meminfo