all Python 3Linux · macOSosfunction
os.chroot
os — directories
Change the process's root directory (needs privilege). Availability: Unix.
Syntax
os.chroot(...)
It needs superuser privileges and is not a security mechanism: a process running as root can escape a chroot, and the current directory is not changed along with the root — change it separately. Applications are isolated with containers and namespaces. chroot rarely appears in teaching tasks.
Examples
import os
os.chroot('/srv/jail') # → ? только Unix: корнем процесса становится /srv/jail, функция ничего не возвращает
os.chdir('/') # → ? только Unix: обязательный шаг после chroot — иначе рабочий каталог остаётся вне «клетки»
print(os.listdir('/')) # → ? только Unix: видно уже содержимое /srv/jail, например ['bin', 'etc', 'lib']
os.chroot('/srv/нет-такого') # → FileNotFoundError: каталог нового корня должен существовать
os.chroot('/srv/jail') # → PermissionError, если процесс запущен не от root
See also
Common errors
Open in the full glossarydocs.python.orgReport an inaccuracy