Skip to content

~/avetyan.com/cheatsheets

Cheat sheets

/dev/null, /dev/zero and /dev/urandom cheat sheet

What Linux's special files do and how to use them: discard output, make test and swap files, wipe disks, generate random data and secrets, and test disk speed.

4 min read


These aren’t real files on disk. The kernel answers reads and writes on them itself.

The files

FileRead givesWrite does

/dev/null

bash
command > /dev/null                     # hide output
command 2> /dev/null                    # hide errors
command > /dev/null 2>&1                # hide both
command &> /dev/null                    # same, bash
grep -q pattern file                    # often better than > /dev/null: stops at the first match
: > app.log                             # truncate a file (same as cat /dev/null > app.log)
cp /dev/null app.log                    # also truncates
ssh [email protected] 'cmd' < /dev/null   # stop ssh reading stdin in loops

Truncating with : > file keeps the inode, so a process writing to the file keeps working and the space is freed. Deleting an open file doesn’t free space.

/dev/zero

bash
# a 1 GiB file of zeros
dd if=/dev/zero of=test.img bs=1M count=1024 status=progress
fallocate -l 1G test.img                # instant, same size, no writes

# swap file
dd if=/dev/zero of=/swapfile bs=1M count=2048
chmod 600 /swapfile && mkswap /swapfile && swapon /swapfile

# sparse file: 10 GiB apparent, almost no disk used
truncate -s 10G sparse.img
du -h --apparent-size sparse.img; du -h sparse.img

fallocate swap files work on ext4 and on XFS with current kernels; dd is the safe choice when swapon rejects one. Btrfs needs its own command: btrfs filesystem mkswapfile.

/dev/urandom

bash
head -c 32 /dev/urandom | base64        # 32 random bytes as text
tr -dc 'A-Za-z0-9' < /dev/urandom | head -c 24; echo   # alphanumeric password
od -An -N4 -tu4 /dev/urandom            # a random 32-bit number
dd if=/dev/urandom of=random.bin bs=1M count=100       # incompressible test data
shuf -n1 --random-source=/dev/urandom words.txt

/dev/urandom is the right choice for keys and passwords. The old advice to wait on /dev/random no longer applies on modern kernels.

Wipe a disk

bash
lsblk                                   # be sure which device it is
dd if=/dev/zero of=/dev/sdX bs=4M status=progress oflag=direct
dd if=/dev/urandom of=/dev/sdX bs=4M status=progress   # slower, random
wipefs -a /dev/sdX                      # only remove filesystem signatures
blkdiscard /dev/nvme0n1                 # SSD/NVMe: discard every block

On SSDs, overwriting doesn’t reach every cell. Use the drive’s secure erase (nvme format -s1, hdparm --security-erase) or full-disk encryption from the start.

Test with them

bash
# rough sequential write speed, bypassing the page cache
dd if=/dev/zero of=/srv/test.img bs=1M count=2048 oflag=direct status=progress

# rough read speed
dd if=/srv/test.img of=/dev/null bs=1M iflag=direct status=progress

# network throughput without disks in the way
dd if=/dev/zero bs=1M count=1024 | ssh [email protected] 'cat > /dev/null'

# how a program handles a full disk
echo test > /dev/full

# CPU load on one core
cat /dev/zero > /dev/null &

Zeros compress perfectly, so compressed or deduplicating storage reports fake speeds; use /dev/urandom data or fio for real benchmarks.

/dev/tcp

bash
# bash only: open a TCP connection without nc or telnet
timeout 3 bash -c '</dev/tcp/203.0.113.10/22' && echo open || echo closed

exec 3<>/dev/tcp/example.com/80
printf 'HEAD / HTTP/1.1\r\nHost: example.com\r\nConnection: close\r\n\r\n' >&3
cat <&3

These paths don’t exist on disk; bash handles them itself.

Keep in mind

  • A mistyped of= in dd overwrites whatever it points to, with no prompt. Read the command twice.
  • If /dev/null turns into a regular file (a script ran rm /dev/null as root), recreate it: rm /dev/null; mknod -m 666 /dev/null c 1 3.

Comments

Sign in with Google, GitHub or Facebook to comment.

Loading comments…