Home > other >  How to exec command in chroot filesystem
How to exec command in chroot filesystem

Time:07-13

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

CodePudding user response:

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.

  • Related