I'd like to use another machine's unused RAM as swapspace for my primary Linux installation. I was just curious about performance of network ramdisks compared to local (slow) mechanical hard disks.

The swapfile is on a tmpfs mountpoint and is shared through samba. However, every time I try to issue:

swapon /mnt/ramswap/swapfile

I get:

swapon: /mnt/ramswap/swapfile: swapon failed: Invalid argument

and in dmesg I read:

[ 9569.806483] swapon: swapfile has holes

I've tried to allocate the swapfile with dd if=/dev/zero of=swapfile bs=1024 (but also =4096 and =1048576) and with truncate -s 2G (both followed by mkswap swapfile) but the result is always the same.

In this post (dated back to 2002) someone says that using a swapfile over NFS/SMB is not possible in Linux. Is this statement still valid? And if yes, what is the reason of this choice and is there any workaround to have this working?

  • 1
    I can see this making your system extremely, extremely unstable. The slightest blip in the network could cause system-critical processes to crash if parts of them get paged out. Not to mention anyone that can hack into your Wifi can potentially read any memory that happens to get paged out. Oct 31, 2012 at 21:28
  • 3
    I don't use WiFi, but a short network cable that connects two Gbps Ethernet cards. The OSI pile has several checksum fields layered on top of each other that should be enough to prevent most faults and corruptions. For the moment I'd like just to know if and how this is possible. I will think about stability and security implications later on.
    – Avio
    Oct 31, 2012 at 21:43
  • 1

4 Answers 4


I know this is a bit of an old thread now but I've just run across this problem and found this it is possible to use an NFS swap partition by making use of the kernel loopback device:

Make an empty file (fill with zeros from /dev/zero):

root@machine:/# dd if=/dev/zero of=/swap bs=1024 count=1048576

Make a loop device:

root@machine:/# losetup /dev/loop0 /swap

Make it suitable for swapping:

root@machine:/# mkswap /dev/loop0
Setting up swapspace version 1, size = 1048572 KiB
no label, UUID=931d6e04-78ec-41fd-ab2c-22522ac2711d

Now use it:

root@machine:/# swapon /dev/loop0

Make the change permanent

Write a script to make this swap permanent. /etc/init.d/swap may be like this:


set -e

case "$1" in
losetup /dev/loop0 /swap
swapon /dev/loop0
swapoff /dev/loop0
losetup -d /dev/loop0
swapoff /dev/loop0
swapon /dev/loop0
echo "Usage: swap { start | stop | restart }" >&2
exit 1

exit 0

Make the file executable:

root@machine:/# chmod +x /etc/init.d/swap

Enable it:

root@machine:/# update-rc.d swap defaults

Source: http://www.emanuelis.eu/2010/06/21/how-to-swap-to-nfs-mount/

  • 1
    How is this networked? There's no mention of any networking involved.
    – Ken Sharp
    Jan 30, 2017 at 11:23
  • For instance, if you're using root on NFS. Sep 12, 2019 at 14:43
  • 1
    dd is not needed. You can use truncate -s 10T swapfile.
    – Ole Tange
    Jun 1, 2020 at 20:53
  • There's also fallocate.
    – Ken Sharp
    Jun 19, 2020 at 23:39

To quote the man page...

This is due to the swap file implementation in the kernel expecting to be able to write to the file directly, without the assistance of the file system.

It's not possible to directly write to an NFS mount without going through the filesystem, so you get this slightly confusing error message.

As @MattH noted, you might be able to use iSCSI to do this, but the real question is whether it's worth it at all. RAM is quite cheap, after all.

  • Thank you for the explanation. Put in this way it makes sense, even if I continue to see it as a limitation. I don't know if its worth or not. But if I can't even try it out, I will never know.
    – Avio
    Nov 1, 2012 at 20:05

If you could do it as you describe I should point out that you're not using another machines unused RAM. Swap files are a file based backing for local faster RAM. You'd be using up the other machines hard disk space.

You can't do it because Linux has a special partition type for swap, it must be on a local drive, not a network share. Windows doesn't do it this way, it uses just a file on an existing filesystem. If I recall correctly, that file also must be on a local drive. So it can't be on NFS or SMB.

However, I think you may be able to do it with iSCSI. If you mount an iSCSI target locally and provide a swap within it may work. Is it a good idea? no. I think it's a terrible idea for reasons of performance and reliability. Most likely Linux will freeze if the network goes down and it needs to access the swap partition. You should get better performance with AoE. But again, one issue with the network or the other machine and you're in for a bumpy ride.

Just buy more RAM, or if you can't fit anymore, get an SSD and create a swap partition on it.

EDIT: Ok you could do it. Still not recommended but this is one way to achieve it in theory.

  1. Create a tmpfs or ramfs on the remote host.
  2. Format the tmpfs or ramfs as ext4.
  3. Create a raw disk image file on it. (dd or truncate will do it)
  4. Export the disk image with iscsi or AoE.
  5. On your local linux PC, connect the target. Then create the swap on the target with mkswap.
  6. Then swapon.

Good luck!

  • 1
    Probably I forgot to mention it, but this experiment is aimed at using the existing hardware in a more rational way. As far as I can understand about iSCSI, it is simply overly expensive for what I'm trying to do. And I wouldn't be using another machine's hdd because the swapfile is created inside a tmpfs mountpoint, so it's basically inside a ramdrive.
    – Avio
    Oct 31, 2012 at 21:57
  • 1
    @Avio There is nothing "rational" about this :) Oct 31, 2012 at 22:00
  • 1
    Linux gives you the freedom to use every kind of media for every kind of purpose: it's up to you to decide if the game’s not worth the candle. Now tell me why I can use a floppy as swapspace but I can't use a network ramdrive. :P
    – Avio
    Oct 31, 2012 at 22:12
  • iscsi is not expensive, there is target software that's as open as linux itself.
    – hookenz
    Oct 31, 2012 at 22:17
  • 1
    Well it's an interesting idea. Worth trying out. I look forward to the day when RDMA is standard and we are able to use other machines unused ram in a reliable way. In theory... why not?
    – hookenz
    Oct 31, 2012 at 23:19

I'm sharing the RAM of another machine via a swap over NBD


Setup ramfs (if needed, if not just skip to nbd)

sudo mkdir /mnt/ram
sudo mount -t tmpfs -o size=16G tmpfs /mnt/ram

Config file /etc/nbd-server/config:

  user = nbd
  group = nbd
  exportname = /mnt/ram/swap
  copyonwrite = false
  prerun = truncate -s 16G /mnt/ram/swap && mkswap /mnt/ram/swap

Start NBD

sudo systemctl start nbd


sudo nbd-client -name swap /dev/nbd0
sudo swapon /dev/nbd0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.