chroot<\/b> command is the following one:<\/p>\n\n\n\nchroot option newroot [command [args]\u2026]<\/pre>\n\n\n\nChroot changes the root directory to the newroot<\/b> directory on the server, which must exist. It then changes the working directory to \/<\/b> and finally runs the command with optional arguments. We go over this in more detail in the following paragraphs:<\/p>\n\n\n\n<\/span>Chroot Linux Options<\/span><\/h2>\n\n\n\nThe chroot command accepts a couple of options:<\/p>\n\n\n\n
\n–groups=groups<\/b>: The group option overrides the supplementary group used by the new process. If we want to disable the supplementary group, we need to use –groups=” and separate them by commas.<\/li>\n\n\n\n–userspec=user[:group]<\/b>: This option is to run the command with a different user and\/or with a different group. If the user is specified, then the groups are set according to the system-defined list for that user.<\/li>\n\n\n\n–skip-chdir<\/b>: Using this option will not change the working directory to \/<\/b> after changing the root directory to newroot<\/b> inside the chroot. Remember that this option is permitted only when the newroot<\/b> is some old directory on the server.<\/li>\n<\/ul>\n\n\n\n<\/span>Chroot Linux Examples<\/span><\/h2>\n\n\n\nWe already mentioned in the previous paragraphs that chroot<\/b> changes the root directory of the running process with its children. It is good to know, that this modifies the environment of the process and the process can not access the files outside the root directory. This modified directory is called chroot jailed<\/b> directory.<\/p>\n\n\n\n
Let’s proceed with a real example and create a new jailed root directory on our server located in the home<\/b> directory.<\/p>\n\n\n\nmkdir -p \/home\/jail\/<\/pre>\n\n\n\nOnce the directory is created, we have to create a couple of directories from the \/<\/b> directory. Let’s first list the content of \/<\/b> directory:<\/p>\n\n\n\nls -al \/<\/pre>\n\n\n\nYou should receive the following output:<\/p>\n\n\n\n
root@host:\/# ls -al \/\ntotal 76\ndrwxr-xr-x 20 root root 4096 Mar 2 04:13 .\ndrwxr-xr-x 20 root root 4096 Mar 2 04:13 ..\nlrwxrwxrwx 1 root root 7 Aug 9 2023 bin -> usr\/bin<\/b>\ndrwxr-xr-x 3 root root 4096 Mar 2 04:12 boot\ndr-xr-xr-x 2 root root 4096 Aug 10 2023 cdrom\ndrwxr-xr-x 19 root root 3960 Mar 2 08:29 dev\ndrwxr-xr-x 107 root root 4096 Mar 2 04:14 etc\ndrwxr-xr-x 2 root root 4096 Jan 16 04:29 home\nlrwxrwxrwx 1 root root 7 Aug 9 2023 lib -> usr\/lib\nlrwxrwxrwx 1 root root 9 Aug 9 2023 lib32 -> usr\/lib32\nlrwxrwxrwx 1 root root 9 Aug 9 2023 lib64 -> usr\/lib64<\/b>\nlrwxrwxrwx 1 root root 10 Aug 9 2023 libx32 -> usr\/libx32\ndrwx------ 2 root root 16384 Jan 16 04:22 lost+found\ndrwxr-xr-x 2 root root 4096 Aug 9 2023 media\ndrwxr-xr-x 2 root root 4096 Aug 9 2023 mnt\ndrwxr-xr-x 3 root root 4096 Mar 4 08:15 opt\ndr-xr-xr-x 172 root root 0 Mar 2 08:29 proc\ndrwx------ 6 root root 4096 Mar 4 07:55 root\ndrwxr-xr-x 31 root root 920 Mar 4 08:16 run\nlrwxrwxrwx 1 root root 8 Aug 9 2023 sbin -> usr\/sbin\ndrwxr-xr-x 6 root root 4096 Aug 9 2023 snap\ndrwxr-xr-x 2 root root 4096 Aug 9 2023 srv\ndr-xr-xr-x 13 root root 0 Mar 2 08:29 sys\ndrwxrwxrwt 13 root root 4096 Mar 4 08:09 tmp<\/b>\ndrwxr-xr-x 14 root root 4096 Aug 9 2023 usr\ndrwxr-xr-x 14 root root 4096 Mar 2 04:13 var\n<\/pre>\n\n\n\n Now, let’s choose one directory from this list, for example, bin<\/b>, and create it into the new jailed root:<\/p>\n\n\n\nmkdir -p \/home\/jail\/bin\/<\/pre>\n\n\n\nAfter creation, copy bash<\/b> from \/bin<\/b> to the new jail root \/home\/jail\/bin\/<\/b><\/p>\n\n\n\nrsync -Waq \/bin\/bash \/home\/jail\/bin<\/pre>\n\n\n\nNext is to copy the required libraries into the \/home\/jail\/. To check those libraries execute the following command:<\/pre>\n\n\n\nldd \/bin\/bash<\/pre>\n\n\n\nCopy them into the \/home\/jail\/lib64\/<\/p>\n\n\n\n
rsync -Waq \/lib\/x86_64-linux-gnu\/libtinfo.so.6 \/lib\/x86_64-linux-gnu\/libc.so.6 \/lib64\/ld-linux-x86-64.so.2 \/home\/jail\/lib64<\/pre>\n\n\n\nNext is to chroot to the new jailed directory:<\/p>\n\n\n\n
chroot \/home\/jail\/ \/bin\/bash<\/pre>\n\n\n\nThat’s it. Now, the user sees the \/home\/jail directory as its root directory. Of course, you do not have to create jailed roots alone. Sign up for one of our NVMe VPS plans<\/a> and submit a support ticket. Our admins will help you with any aspect of this. Feel free to contact us. We are available 24\/7.<\/p>\n\n\n\nPS. If you liked this post about the chroot Linux commands with examples, please share it with your friends on social networks or comment in the comments section. Thank you.<\/p>\n","protected":false},"excerpt":{"rendered":"
This tutorial will explain the chroot Linux command using real examples. The chroot Linux command is a shorthand for “change … <\/p>\n
Read More<\/a><\/p>\n","protected":false},"author":4,"featured_media":48391,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2071],"tags":[168,105],"yoast_head":"\nChroot Linux command with examples - RoseHosting<\/title>\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n\t \n\t \n\t \n \n \n \n \n \n\t \n\t \n\t \n