Difference between revisions of "Ubuntu/swap"
< Ubuntu
		
		
		
		Jump to navigation
		Jump to search
		| Line 3: | Line 3: | ||
Commands:  | Commands:  | ||
| − | * {{l/cmd|swapon}} --show  | + | * <code>{{l/cmd|swapon}} --show</code>  | 
** Confirms the swap-file status (default is currently 2 gigabytes)  | ** Confirms the swap-file status (default is currently 2 gigabytes)  | ||
| − | * sudo {{l/cmd|fallocate}} -l 4G /swap4G  | + | * <code>sudo {{l/cmd|fallocate}} -l 4G /swap4G</code>  | 
** creates a new swapfile-compatible 4 gigabyte file named <code>/swap4G</code>  | ** creates a new swapfile-compatible 4 gigabyte file named <code>/swap4G</code>  | ||
** Regardless of what's implied by the tldp.org instructions, you can do this ''before'' disabling the old swapfile as long as you give it a different name.  | ** Regardless of what's implied by the tldp.org instructions, you can do this ''before'' disabling the old swapfile as long as you give it a different name.  | ||
| − | * sudo {{l/cmd|chmod}} 0600 /swap4G  | + | * <code>sudo {{l/cmd|chmod}} 0600 /swap4G</code>  | 
** Not required, but reduces warning messages and possible security holes.  | ** Not required, but reduces warning messages and possible security holes.  | ||
| − | * sudo mkswap /swap4G  | + | * <code>sudo mkswap /swap4G</code>  | 
** Mark the new file as a swapfile.  | ** Mark the new file as a swapfile.  | ||
| − | * sudo swapon /swap4G  | + | * <code>sudo swapon /swap4G</code>  | 
** Tell the swap system to use the new file.  | ** Tell the swap system to use the new file.  | ||
| − | * sudo swapoff /swapfile  | + | * <code>sudo swapoff /swapfile</code>  | 
** Turn off usage of the given swapfile (<code>/swapfile</code> is the name of the default) -- but make sure you've got enough RAM free first, or have set up another swapfile!  | ** Turn off usage of the given swapfile (<code>/swapfile</code> is the name of the default) -- but make sure you've got enough RAM free first, or have set up another swapfile!  | ||
** This will take some time, depending on how full this swapfile is.  | ** This will take some time, depending on how full this swapfile is.  | ||
Latest revision as of 14:12, 2 June 2025
About
Newer versions of Ubuntu use a regular(ish) file for swap, rather than a partition -- so resizing it is now possible without rebooting or mucking around with fdisk.
Commands:
swapon --show- Confirms the swap-file status (default is currently 2 gigabytes)
 
sudo fallocate -l 4G /swap4G- creates a new swapfile-compatible 4 gigabyte file named 
/swap4G - Regardless of what's implied by the tldp.org instructions, you can do this before disabling the old swapfile as long as you give it a different name.
 
- creates a new swapfile-compatible 4 gigabyte file named 
 sudo chmod 0600 /swap4G- Not required, but reduces warning messages and possible security holes.
 
sudo mkswap /swap4G- Mark the new file as a swapfile.
 
sudo swapon /swap4G- Tell the swap system to use the new file.
 
sudo swapoff /swapfile- Turn off usage of the given swapfile (
/swapfileis the name of the default) -- but make sure you've got enough RAM free first, or have set up another swapfile! - This will take some time, depending on how full this swapfile is.
 - Yes, you can have two swapfiles in use simultaneously.
 
- Turn off usage of the given swapfile (
 
Then edit /etc/fstab to change the name of the swapfile you want to use. You can delete the old swapfile either before or after that, but safest to do it after.
Notes
- Linux Partition HOWTO: 9. Setting Up Swap Space
- 2020-02-11 How to Increase Swap Size on Ubuntu (using the new swapfile instead of partitioning)
- Note that you have to edit fstab for the change to persist after reboot.
 - Also, it's not necessary to do swapoff except to stop using a given swapfile. You can add a new swapfile at any point, and the system will use more than one swapfile concurrently.
 
 
 - 2020-02-11 How to Increase Swap Size on Ubuntu (using the new swapfile instead of partitioning)
 - upgrade command for 16.04 LTS: 
do-release-upgrade