How to exec command in chroot filesystem


In my system, i create a new filesystem contains all file, then i try exec command like

chroot /new_root_path /bin/bash -c "echo a > a"

I found file a write to new filesystem root path, but i try to exec command like this, and i think different

chroot /new_root_path /bin/bash -c "hostname > a"

the a file content is not new filesystem‘s hostname different from new filesystem /etc/hostname, file content is old filesystem‘s hostname

how can i exec command such as hostname or other command in new filesystem‘s /bin or /sbin

I found similar questions in stackoverflow, but did’t found conclusion


You can execute hostname everywhere. But since it’s tagged C, you could also call gethostname(). That tells you why you see what you see: you’re asking the kernel. It’s the same kernel, so you get the same answer.

/etc/hostname is used to call sethostname() on startup, and it uses the file content at that moment. It’s not necessarily the same at any moment in time, even without a chroot.

Answered By – MSalters

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published