How do I create a swap partition in Red Hat Enterprise Linux?
Environment
- Red Hat Enterprise Linux 5
- Red Hat Enterprise Linux 6
- Red Hat Enterprise Linux 7
- Red Hat Enterprise Linux 8
- Red Hat Enterprise Linux 9
Issue
- How do I create a swap partition on a hard drive in Red Hat Enterprise Linux?
- I do not have any swap configured for this system. If I attach a new storage device to the system, how can I create a swap partition on that storage?
Resolution
For this example, /dev/sdc is the drive referenced with no partitions. Since we are going to make a single partition filling the disk, note that any data currently on that disk will be lost.
1. As root user perform the following tasks:
# fdisk /dev/sdc
Create the partition:
- A new prompt will appear, type 'p' to display the current partitions.
- Type 'n' to create a new partition.
- Since this will be the first partition make it primary by selecting 'p'.
- Select '1' for the partition number.
- To accept the default settings for the beginning sector, just press 'enter'.
- To accept the default settings for the ending sector, just press 'enter'.
- Press 't' to change the partition type.
- Enter the number '1', to change the partition that was just created
- Type '82' and press enter
- Verify the results by checking the partition table by typing 'p'.
- Type 'w' to write the changes made.
2. Re-read the partition table to allow the kernel to see the changes:
# partprobe
If this does not detect the new partitions, you will need to reboot the system.
3. Create the swap association on the new device:
# mkswap /dev/sdc1
4. Add the entry to "/etc/fstab" to make the swap persistent across reboots:
RHEL 5
/dev/sdc1 swap swap defaults 0 0
RHEL 6 - you can use UUID from the output of mkswap when creating the swap
UUID=9a2fdd61-1a91-48a2-826e-f4995f545f15 swap swap defaults 0 0
5. Activate the swap partition:
# swapon -a
6. Verify it is present by executing:
# swapon -s
This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.