Sometimes it makes me wonder whether I am inside a chroot environment or not, so let’s check it out.

Non-chroot environment

All you need to do is look for / directory entry inside /proc/mounts file.

You can assume that a positive match means that you are outside of the chroot environment.

$ awk 'BEGIN{exit_code=1} $2 == "/" {exit_code=0} END{exit exit_code}' /proc/mounts
$ echo $?

The regular operating system needs to mount / root file-system.

$ cat /proc/mounts
/dev/mapper/vg00-root / ext4 rw,relatime,errors=remount-ro,data=ordered 0 0
[...]

Chroot environment

Chroot environment does not need to mount / root filesystem.

$ sudo chroot debian-stretch-amd64
#
# awk 'BEGIN{exit_code=1} $2 == "/" {exit_code=0} END{exit exit_code}' /proc/mounts
# echo $?
1

Bind mounted proc file-system does not show mounted / root file-system.

# cat /proc/mounts
udev /dev devtmpfs rw,nosuid,relatime,size=8160212k,nr_inodes=2040053,mode=755 0 0
devpts /dev/pts devpts rw,nosuid,noexec,relatime,gid=5,mode=620,ptmxmode=000 0 0
proc /proc proc rw,nosuid,nodev,noexec,relatime 0 0
sysfs /sys sysfs rw,nosuid,nodev,noexec,relatime 0 0

Chroot environment without mounted proc file-system will return different error code.

# awk 'BEGIN{exit_code=1} $2 == "/" {exit_code=0} END{exit exit_code}' /proc/mounts
awk: cannot open /proc/mounts (No such file or directory)
# echo $?
2

Check specific process

Use a similar method to determine if a particular process is running inside the chroot environment.

Update package index inside the chroot environment.

$ sudo chroot debian-stretch-amd64 bash -c "apt-get update"
Ign:1 http://cdn-fastly.deb.debian.org/debian stretch InRelease
Get:2 http://cdn-fastly.deb.debian.org/debian stretch Release [118 kB]
Get:3 http://cdn-fastly.deb.debian.org/debian stretch Release.gpg [2434 B]
Get:4 http://cdn-fastly.deb.debian.org/debian stretch/main amd64 Packages [7123 kB]
Get:5 http://cdn-fastly.deb.debian.org/debian stretch/main Translation-en [5393 kB]
Get:6 http://cdn-fastly.deb.debian.org/debian stretch/contrib amd64 Packages [50.9 kB]
Get:7 http://cdn-fastly.deb.debian.org/debian stretch/contrib Translation-en [45.9 kB]
Get:8 http://cdn-fastly.deb.debian.org/debian stretch/non-free amd64 Packages [78.0 kB]
Get:9 http://cdn-fastly.deb.debian.org/debian stretch/non-free Translation-en [79.2 kB]
Fetched 12.9 MB in 3s (3501 kB/s)
Reading package lists... Done

Install Nginx inside the chroot environment.

$  sudo chroot debian-stretch-amd64 bash -c "apt-get -y install nginx"
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
  fontconfig-config fonts-dejavu-core geoip-database libbsd0 libfontconfig1 libfreetype6 libgd3 libgeoip1 libicu57 libjbig0 libjpeg62-turbo libnginx-mod-http-auth-pam libnginx-mod-http-dav-ext libnginx-mod-http-echo
  libnginx-mod-http-geoip libnginx-mod-http-image-filter libnginx-mod-http-subs-filter libnginx-mod-http-upstream-fair libnginx-mod-http-xslt-filter libnginx-mod-mail libnginx-mod-stream libpng16-16 libssl1.1 libtiff5 libwebp6
  libx11-6 libx11-data libxau6 libxcb1 libxdmcp6 libxml2 libxpm4 libxslt1.1 nginx-common nginx-full sgml-base ucf xml-core
Suggested packages:
  libgd-tools geoip-bin fcgiwrap nginx-doc ssl-cert sgml-base-doc debhelper
The following NEW packages will be installed:
  fontconfig-config fonts-dejavu-core geoip-database libbsd0 libfontconfig1 libfreetype6 libgd3 libgeoip1 libicu57 libjbig0 libjpeg62-turbo libnginx-mod-http-auth-pam libnginx-mod-http-dav-ext libnginx-mod-http-echo
  libnginx-mod-http-geoip libnginx-mod-http-image-filter libnginx-mod-http-subs-filter libnginx-mod-http-upstream-fair libnginx-mod-http-xslt-filter libnginx-mod-mail libnginx-mod-stream libpng16-16 libssl1.1 libtiff5 libwebp6
  libx11-6 libx11-data libxau6 libxcb1 libxdmcp6 libxml2 libxpm4 libxslt1.1 nginx nginx-common nginx-full sgml-base ucf xml-core
0 upgraded, 39 newly installed, 0 to remove and 0 not upgraded.
Need to get 18.6 MB of archives.
After this operation, 58.9 MB of additional disk space will be used.
[...]

Ensure that Nginx is started inside the chroot environment.

$ sudo chroot debian-stretch-amd64 bash -c "/etc/init.d/nginx start"
[ ok ] Starting nginx: nginx.

Get the process id.

$ sudo chroot debian-stretch-amd64 bash -c "cat /var/run/nginx.pid"
20807

Check the root directory for this particular process from the outside of the chroot environment.

$ sudo stat --format="%N"  /proc/20807/root
'/proc/20807/root' -> '/home/milosz/debian-base-image/debian-stretch-amd64'

Do not perform this check from the inside of the chroot environment as you will get misrepresented results from the point of view of the chroot environment.

$ sudo chroot debian-stretch-amd64 bash -c "stat --format=\"%N\" /proc/20807/root"
'/proc/20807/root' -> '/'
ko-fi