brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: mount --bind makes a folder show up in a second place

Linux
~/tips/tip-disk-mount-bind

sudo mount --bind /mnt/data/media /srv/media

Shows the same folder at a second path, which works where a symlink does not.

A program insists on reading from /srv/media, but the files live on a big disk under /mnt/data/media. A symlink often works, until a program refuses to follow it or a chroot can’t see outside itself. A bind mount attaches the same directory at a second path, and to every program it looks like a real folder.

sudo mkdir -p /srv/media
sudo mount --bind /mnt/data/media /srv/media

Now both paths show the same files, and a change in one is a change in the other, because it’s the same filesystem. The mount man page says the kernel doesn’t even remember it was a bind: the two paths are independent, and you can unmount either one. To make it permanent, this is the fstab line:

/mnt/data/media  /srv/media  none  bind  0  0

Where I’d use it: giving a service or a chroot access to one folder and nothing more, or putting data from a second disk where an old config expects it. A read-only bind is handy for sharing a folder without risk. Since util-linux 2.27 you can write mount -o bind,ro /mnt/data/media /srv/media. The man page notes this is done in two steps, so it’s not atomic, and the classic way is a --bind followed by mount -o remount,bind,ro.

Two caveats. A bind only takes that one filesystem, so if another disk is mounted somewhere below the source folder, it won’t show up in the new path. For that you need --rbind. And a read-only bind only protects the new path: the man page is clear that the original folder stays writable. Anyone who can reach /mnt/data/media can still change the files.

Further reading: mount(8) manual page

More Linux tips a2query -m rewrite renice -n 10 -p 4242 cat config.yml | xsel -ib xvfb-run -a your-program --some-flag dpkg --get-selections > packages.txt All Linux tips (261) All topics

Link