Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7757199
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T13:01:25+00:00 2026-06-01T13:01:25+00:00

I chrooted directory using following commands: os.chroot(/mydir) How to return to directory to previous

  • 0

I chrooted directory using following commands:

os.chroot("/mydir")

How to return to directory to previous – before chrooting?
Maybe it is possible to unchroot directory?

SOLUTION:

Thanks to Phihag. I found a solution. Simple example:

import os
os.mkdir('/tmp/new_dir')
dir1 = os.open('.', os.O_RDONLY)
dir2 = os.open('/tmp/new_dir', os.O_RDONLY)

os.getcwd()                # we are in 'tmp'
os.chroot('/tmp/new_dir')  # chrooting 'new_dir' directory
os.fchdir(dir2)
os.getcwd()                # we are in chrooted directory, but path is '/'. It's OK.
os.fchdir(dir1)
os.getcwd()                # we came back to not chrooted 'tmp' directory

os.close(dir1)
os.close(dir2)

More info

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T13:01:27+00:00Added an answer on June 1, 2026 at 1:01 pm

    If you haven’t changed your current working directory, you can simply call

    os.chroot('../..') # Add '../' as needed
    

    Of course, this requires the CAP_SYS_CHROOT capability (usually only given to root).

    If you have changed your working directory, you can still escape, but it’s harder:

    os.mkdir('tmp')
    os.chroot('tmp')
    os.chdir('../../') # Add '../' as needed
    os.chroot('.')
    

    If chroot changes the current working directory, you can get around that by opening the directory, and using fchdir to go back.

    Of course, if you intend to go out of a chroot in the course of a normal program (i.e. not a demonstration or security exploit), you should rethink your program. First of all, do you really need to escape the chroot? Why can’t you just copy the required info into it beforehand?

    Also, consider using a second process that stays outside of the chroot and answers to the requests of the chrooted one.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to do something like this: chroot /mount-point /path/to/script $var1 $var 2
I use a chroot development environment for developing software for devices. The chroot dev
I want to check if a file path is in current directory tree. Suppose
Has anyone been able to get xinc to run correctly under OpenBSD's chrooted default
php generates GIFs on the web server using a databases on a second server.
I've got a problem with ProFTPD on Debian. I use users from MySQL database
I'm maintaining and publishing an open source package which needs to be built for
I have written a small custom web server application in C running on Linux.
Here's the scenario I'm having: I've created a debootstrap ubuntu maverick (64-bit) environment. I
I'm repackaging some software (which has its own installer) for private use under a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.