1

I wanted to find whether I am inside a chroot as part of a script where I'm not allowed to mount /proc if it is not (unlike the case in How do I tell I'm running in a chroot?). How to find whether I'm in a chroot even if /proc is not mounted?

I do have root access. This is on Fedora. The solution should not depend on the filesystems used.

nithinj
  • 180

3 Answers3

0

Debian chroot put a specific file to /etc, but this need cooperation.

But if it is like normal root, it is like normal root. So if you don't find differences, assume that you are not in chroot (in any case it is equivalent).

The hard way is to check if / is the same as what you will get mounting the root device, but this requires you to be root. Other methods requires /proc (or root).

0

Use systemd-detect-virt -r. (must be root)

0

Unlike other answers, this one doesn't need you to be root. Run unshare -U true. If it succeeds, then you're not in a chroot. If it fails with unshare: unshare failed: Operation not permitted, then you are. This works because of commit 3151527ee007b73a0ebd296010f1c0454a919c7d: userns: Don't allow creation if the user is chrooted.

Caveat: unshare can fail for a few other reasons too. If it succeeds, then you're definitely not in a chroot, but if it fails, then you only probably are.