Serverspace Black Friday
JT
June 27, 2023
Updated June 27, 2023

How to use the Chroot in Linux? Part 2

Debian Networks

The Chroot command in Unix allows users to make a secure temporary milieu for programs by changing the root of the file systems. This isolation prevents any harm or damage to the primary system. In the previous episode, we consider two other functions of this utility.

How to use it?

Chroot creates a new shell environment, meaning that any modifications made within this environment will only affect files and directories within the Chroot environment, not the central system.

For instance, if you need to test a program on a different operating system version that requires specific libraries unavailable on your primary system, you can build a Chroot environment with the necessary libraries and run the program in this isolated environment. This ensures that the program works correctly without causing any issues on the main system.

However, it is crucial to note that Chroot should only be used by experienced users because improper usage may cause system instability. Moreover, Chroot can help recover systems following emergency failures or malicious attacks.

Installation and exploit

In that article, we consider one of the main functions of utility — system recovery. Chroot can be used for system recovery after a crash or malicious attack. They mean not a last role in that mechanism, the program isolates the file system from the impact that can appear after maintenance. Let’s have a look!

If you have VDS on Serverspace then you need to open the control panel and go to the recovery tab:

Preparation
Screenshot №1 — Preparation
Tab
Screenshot №2 — Tab

Then reboot your server and go ahead. Before usage, main utility update and upgrade your system :

sudo apt update -y && sudo apt upgrade -y
Update
Screenshot №2 — Update

In the first step, we need to discover the label of the recovery disk with the command below:

fdisk -l
Label of device
Screenshot №3 — Label of device

Mount device for point, therefore we will work with this. The command below creates a folder and that point:

mkdir mnt && mount dev/vda2 /mnt
Mount disk
Screenshot №4 — Mount disk

Then create a folder and copy the configuration file:

mkdir /mnt/chroot/etc && cp -p /etc/(passwd,group,shadow,gshadow} /mnt/chroot/etc
Configuration files
Screenshot №5 — Configuration files

Creating folders and mounting directories into the system:

mkdir /mnt/chroot/dev
mount --bind /dev /mnt/chroot/dev
mkdir /mnt/chroot/proc
mkdir /mnt/chroot/sys
mount --bind /proc /mnt/chroot/proc
mount --bind /sys /mmt/chroot/sys
Mounting folder
Screenshot №6 — Mounting folder

Start the virtual environment and perform the necessary operations to restore the system, for example, updating packages or restoring the boot sector:

chroot /mnt
Start chroot
Screenshot №7 — Start chroot

After the operations are completed, exit the chroot environment and mount the file systems:

umount /mnt/chroot/sys && umount /mnt/chroot/proc && umount /mnt/chroot/dev
Unmount directories
Screenshot №8 — Unmount directories

And at the end of the process reboot your preparation system!

Conclusion

The Chroot command in Unix provides a secure temporary environment for running programs by isolating them from the main system. This isolation prevents any harm or damage to the primary system and can be used for testing programs on different operating system versions with specific libraries unavailable on the primary system. However, it is important to note that it should only be used by experienced users because improper usage may cause system instability. Additionally, Chroot can also be used for system recovery following emergency failures or malicious attacks.

In this article, we have seen how to use Chroot for system recovery after a crash or malicious attack on a VDS server on Serverspace. By following the steps outlined, users can update and upgrade their system, discover the label of the recovery disk, mount the device, create folders and copy configuration files, start the virtual environment and perform necessary operations like updating packages or restoring the boot sector. Finally, they can exit the chroot environment and mount file systems before rebooting their preparation system.

Vote:
5 out of 5
Аverage rating : 5
Rated by: 1
33145 North Miami, FL 2520 Coral Way apt 2-135
+1 302 425-97-76
700 300
ITGLOBAL.COM CORP
700 300

You might also like...

We use cookies to make your experience on the Serverspace better. By continuing to browse our website, you agree to our
Use of Cookies and Privacy Policy.