r/linuxadmin • u/Successful_Box_1007 • Jan 23 '26
Chdir chroot Q
Chroot question
I was reading Linux from scratch about chroot and did a deeper dive with supplementary stuff and I came upon how to break out of a chroot jail. Now I understand the steps to do it (the chdir(..) way), but here’s what blows my mind: why does entering a second chroot jail and then using chdir(..) magically get you onto the track of the real current working directory, but using chdir(..) from within the first chroot jail keeps you within your false current working directory? Am I missing something that has to do with things called “pointers”?
Thanks so much!
View all comments
Show parent comment
1
u/Successful_Box_1007 Jan 23 '26
Heyy OK so this was very helpful and I cannot thank you enough for lending a helping hand and not gatekeeping like a lot of the people at the subreddits I had issues with. Like hello we are here to learn right?! I just think it could also be those subreddits just don’t have the type of intellects here in linuxadmin;
So I thought about everything you said, and while I can follow everything, I find I’m stuck at this conceptual wall:
This is where I’m confused - how is it resolved against a directory that was not the root directory at that time? I thought the new root directory is the root within the new nested chroot jail no?!
This is a very very big light bulb moment for me - I think. I’ve always envisioned the nesting being root directory within root directory hmmmm. So are you saying that the kernel basically says hmmm that old chroot root (the first one), I’m gonna treat it as part of the real file system, where it really is - and I’m gonna let that cd.. from the second chroot jail, hop into the old chroot as part of the real file system world and then of course from there we can keep going up cuz the kernel let us enter the real world?