Change root with os.chroot()

EasyOS Module Programs · 41 of 49

Write a Python program that uses os.chroot() where available.

Advertisement
Output
os.chroot is not available on this platform

Approach

  1. chroot is Unix-only and needs root privileges, so guard it.

Solution

Python
import os if not hasattr(os, "chroot"):    print("os.chroot is not available on this platform")elif os.geteuid() != 0:    print("os.chroot needs root privileges")else:    os.chroot("/tmp")    print("root changed")
Output
os.chroot is not available on this platform
Note

chroot limits which part of the file system a process can see. It is used by system tools, rarely in everyday scripts.

Learn the concept

New to this topic? Read the lesson, then come back.

More os module programs

← All 49 OS Module Programs

Leave a Comment