1. Overview
/dev/zero is a character pseudo-device in Linux that returns an infinite stream of null characters when read. A null character is a byte consisting of 8 bits equal to zero, so /dev/zero is a stream of zeros.
In this tutorial, we’ll explore some of the common uses of /dev/zero in Linux.
2. /dev/zero as Input Source
One of the main uses of /dev/zero is to provide a data source for various programs and scripts that require a stream of zeros. It serves a variety of purposes, such as evaluating disk or network performance, padding or erasing data, creating zero-filled files, cleaning up free disk space, or preallocating space for virtual devices.
2.1. Creating Dummy Files
We can use dd to create a dummy file:
$ dd if=/dev/zero of=dummy_file.bin bs=1024 count=1
[...]
1024 bytes (1,0 kB, 1,0 KiB) copied, 0,000219015 s, 4,7 MB/s
In this case, dd has read one block (count=1) of 1024 bytes (bs=1024) from /dev/zero and written it to dummy_file.bin. hexdump assures us that it’s filled only with zeros:
$ hd -C dummy_file.bin
00000000 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
00000000 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
[...]
As we’ll see in the following examples, there are many situations where we might need a file created in such a way.
2.2. Creating Swap Files
Swap files act as virtual memory extensions to the system’s physical RAM.
In general, the dd and /dev/zero approach seen earlier is more portable than alternatives such as truncate or fallocate. As evidence, the notes in the mkswap manual state that “using cp to create the [swap] file is not acceptable. Neither is use of fallocate on file systems that support preallocated files, such as XFS or ext4, or on copy-on-write filesystems like btrfs. It is recommended to use dd and /dev/zero in these cases.”
That said, let’s create a swap file of 512MiB:
$ sudo dd if=/dev/zero of=/swapfile512M count=512 bs=1MiB
$ sudo chmod 600 /swapfile512M
$ sudo mkswap /swapfile512M
$ sudo swapon /swapfile512M
$ echo "/swapfile512M swap swap defaults 0 0" | sudo tee -a /etc/fstab
Let’s ask swapon for the new swap configuration:
$ swapon --show
NAME TYPE SIZE USED PRIO
/dev/dm-2 partition 976M 1,3M -2
/swapfile512M file 512M 0B -3
It’s as expected. We can also check the total swap through free and reboot to ensure the persistence of the new swap configuration.
2.3. Preallocating Disk Space for Virtual Devices
All hypervisors support virtual disks. However, not all provide easy-to-use GUI tools to create them. Let’s take the USB disk support offered by QEMU as an example.
In the case of physical USB devices, we can use pass-through access. However, if we want to use a virtual USB stick that simulates a physical stick in every way, including the ability to boot from it, we need to start QEMU with the following parameters, as explained in the official EHCI controller support documentation:
qemu-system-x86_64 -hda [...] -m [...] -enable-kvm \
[...] \
-drive if=none,id=usbstick,format=raw,file=./rawUSBdisk.img \
-usb \
-device usb-ehci,id=ehci \
-device usb-tablet,bus=usb-bus.0 \
-device usb-storage,bus=ehci.0,drive=usbstick \
-boot menu=on
We listed only the options relevant to support booting from a virtual USB device in the guest, which corresponds to the rawUSBdisk.img file in the host. To create this raw disk, we can again use dd with /dev/zero:
$ sudo dd if=/dev/zero of=./rawUSBdisk.img count=8192 bs=1MiB
In this case, we created an 8GiB disk. Inside the guest, we can partition and format it using tools like GParted.
2.4. Wiping Techniques
There are still many myths about how to effectively destroy data from our storage media. We can try different approaches, which are also the subject of scientific research. In any case, a single overwriting with zeros on an entire physical disk is enough:
# umount /dev/sdx1
[...] # we must unmount any file system on sdx
# cat /dev/zero > /dev/sdx
cat: write error: No space left on device
# sync
The inability to recover data after such an overwrite has been documented, validating this approach.
3. /dev/zero as Output Destination
In addition to being an input source, /dev/zero is also an output destination. Both /dev/zero and /dev/null behave the same when written to, accepting and discarding all input with no effect. Usually, this helps when a script needs a destination to write data to, but we don’t need the actual contents of that data. This way, we can suppress unwanted output, such as debugging information or status messages.
Every Linux application has three different channels to communicate with other programs or devices: Standard Input (stdin), Standard Output (stdout), and Standard Error (stderr). Let’s see how to redirect stdout and stderr to /dev/zero.
3.1. Redirect stdout to /dev/zero
We can use the > operator followed by /dev/null to redirect stdout to /dev/null:
$ echo "Hello Baeldung" > /dev/null
This suppresses any unwanted standard output.
3.2. Redirect stderr to /dev/zero
First, let’s see that the previous approach won’t work in the following case:
$ ls xxx > /dev/zero
ls: cannot access 'xxx': No such file or directory
This is because the output of ls goes to stderr, not stdout, when we try to list information about a non-existent file. If we want to redirect only stderr to /dev/zero, we can use the 2> operator:
$ ls xxx 2> /dev/zero
$ ls test.sh 2> /dev/zero
test.sh
In this case, the first output goes to stderr, and the second output goes to stdout. So, the 2> /dev/zero redirection is only effective in the first case.
3.3. Redirect Both stdout and stderr to /dev/zero
Let’s redirect both stdout and stderr to /dev/zero:
$ ls xxx > /dev/zero 2>&1
$ ls test.sh > /dev/zero 2>&1
The 2>&1 part redirects stderr to the same destination as stdout, which is /dev/zero because of the first > operator.
4. /dev/zero as Performance Testing Tool
/dev/zero is often used to test disk and network performance, although it’s not an appropriate tool. As an input source, /dev/zero can simulate a large amount of data that we can send to a disk or network. On the other hand, when /dev/zero is an output destination, it can discard the data read by the speed tests.
We won’t delve into this topic, except to note that benchmarking Linux systems and measuring network performance requires specific tools other than /dev/zero.
The problem is that /dev/zero doesn’t reflect real-world data patterns and characteristics. For example, writing /dev/zero to a disk can result in inaccurate throughput and latency measurements because it doesn’t account for factors such as disk caching, compression, encryption, fragmentation, or wear leveling.
Similarly, sending /dev/zero over a network can result in inaccurate bandwidth and latency measurements because it doesn’t take into account factors such as network congestion, packet loss, retransmission, encryption, compression, or protocol overhead.
5. /dev/zero for Anonymous Mapping
Apart from the tasks we looked at, we can also take advantage of /dev/zero for more advanced ones, especially when programming in low-level languages such as C.
One such task is the use of mmap and /dev/zero to perform anonymous mapping. It’s a technique that allows a process to allocate a large amount of memory without using standard memory allocation functions such as malloc or calloc. This can be useful for creating buffers, arrays, or data structures that need to be initialized to zero and may have dynamic sizes.
6. Conclusion
In this article, we looked at the most common uses of /dev/zero in Linux.
In general, /dev/zero is used to create dummy files and overwrite disks for security wipes. Other tasks include preallocating space for virtual disks and suppressing command output, in a similar way to /dev/null. Finally, programming languages such as C can benefit from /dev/zero for advanced tasks.