Czasami, gdy chcemy poeksperymentować z czymś nowym, zawsze wolimy uruchomić to na maszynie wirtualnej lub w środowisku piaskownicy, aby nie mogło to spowodować uszkodzenia naszej oryginalnej maszyny lub jej ważnych plików. Polecenie chroot pozwala nam uruchamiać polecenia i programy w bezpiecznym środowisku, tworząc dla nich alternatywny katalog główny. Dlatego będziemy mówić o metodzie używania tego polecenia w Debianie 11.
Używanie chroot w Debianie
Aby używać chroot w Debianie 11, musisz wykonać następujące kroki:
Uruchom terminal w Debianie, a następnie wpisz w nim następujące polecenie, a następnie naciśnij klawisz Enter, aby utworzyć nowy katalog główny:
sudo mkdir ~/new_root
To polecenie jest również pokazane na poniższym obrazku:

Po pomyślnym utworzeniu nowego katalogu głównego dodamy wszystkie odpowiednie podkatalogi do nowego katalogu głównego. Aby to zrobić, wpisz następujące polecenie w terminalu, a następnie naciśnij klawisz Enter:
sudo mkdir –p ~/new_root/{bin,lib,lib64}
Uruchomienie tego polecenia spowoduje utworzenie podkatalogów bin, lib i lib64 w nowym katalogu głównym. To polecenie jest pokazane na poniższym obrazku:

Po pomyślnym utworzeniu tych podkatalogów następną rzeczą, którą musisz zrobić, jest skopiowanie plików binarnych poleceń i programów, które chcesz przetestować za pomocą chroot w nowym katalogu głównym. Aby to zrobić, musisz najpierw poszukać tych plików binarnych. W tym przykładzie zamierzamy znaleźć pliki binarne następujących poleceń: ip, ls i pwd. Ponadto będziemy również szukać pliku binarnego programu bash. Aby to zrobić, użyjemy polecenia who, jak pokazano poniżej:
który adres IP ls pwd bash
Wpisz to polecenie w terminalu, a następnie naciśnij klawisz Enter. Jest to również pokazane na poniższym obrazku:

Gdy tylko to polecenie zostanie pomyślnie wykonane, będziesz mógł zobaczyć ścieżki do plików binarnych odpowiednich poleceń i programu w swoim terminalu, jak pokazano na poniższym obrazku:

- Po znalezieniu wszystkich odpowiednich plików binarnych musisz skopiować je do nowego katalogu głównego. Aby to zrobić, wpisz następujące polecenie w terminalu, a następnie naciśnij klawisz Enter:
sudo cp –v /bin/{ip,ls,pwd,bash} ~/new_root/bin
To polecenie jest pokazane na poniższym obrazku:

Pomyślne wykonanie tego polecenia pokaże na twoim terminalu coś takiego, co w rzeczywistości jest potwierdzeniem, że pliki binarne zostały skopiowane do nowego katalogu głównego:

Następnym krokiem jest znalezienie i skopiowanie zależności tych poleceń i programów w nowym katalogu głównym. Aby znaleźć zależności polecenia ip, wpisz następujące polecenie w terminalu, a następnie naciśnij klawisz Enter:
ldd /bin/ip
To polecenie jest również pokazane na poniższym obrazku:

Uruchomienie tego polecenia pokaże wszystkie zależności polecenia ip. Możesz znaleźć zależności dla poleceń ls i pwd w ten sam sposób. Zależności polecenia ip pokazano na poniższym obrazku:

Teraz musisz skopiować wszystkie zależności polecenia ip do nowego katalogu głównego. Aby to zrobić, wpisz polecenie, jak pokazano na poniższym obrazku:

Gdy tylko to polecenie zostanie pomyślnie wykonane, twój terminal będzie wyglądał mniej więcej tak:

Jednak w celu skopiowania zależności /lib64 należy wpisać polecenie w terminalu, jak pokazano na poniższym obrazku:

Twój terminal wyświetli monit z komunikatem informującym, że pliki /lib64 zostały pomyślnie skopiowane, jak pokazano na poniższym obrazku:

Teraz musisz znaleźć zależności dla programu bash. Aby to zrobić, wpisz następujące polecenie w terminalu, a następnie naciśnij klawisz Enter:
ldd /bin/bash
To polecenie jest również pokazane na poniższym obrazku:

Pomyślne uruchomienie tego polecenia pokaże wszystkie wymagane zależności dla programu bash, jak pokazano na poniższym obrazku:

Teraz skopiuj te zależności do nowego katalogu głównego, wpisując w terminalu polecenie pokazane na poniższym obrazku, a następnie naciskając klawisz Enter:

Jeśli to polecenie zostanie wykonane pomyślnie, twój terminal będzie wyglądał mniej więcej tak:

Jednak w celu skopiowania zależności /lib64 wpisz w terminalu polecenie pokazane na poniższym obrazku, a następnie naciśnij klawisz Enter:

Twój terminal wyświetli komunikat potwierdzający, że wyżej wymieniona zależność została pomyślnie skopiowana do nowego katalogu głównego, jak pokazano na poniższym obrazku:

Po pomyślnym wykonaniu tych kroków musisz przejść do nowego katalogu głównego. Aby to zrobić, wpisz następujące polecenie w terminalu, a następnie naciśnij klawisz Enter:
sudo chroot ~/new_root/bin/bash
To polecenie przeniesie Cię do środowiska programu bash. Jest to również pokazane na poniższym obrazku:

Po uruchomieniu programu bash w nowo utworzonym katalogu głównym spróbuj uruchomić polecenia, których konfiguracje wykonałeś powyżej. W tym przykładzie spróbuję uruchomić polecenie ip i dowiedzieć się, czy działa ono w nowym katalogu głównym, czy nie. Aby to zrobić, wpisz następujące polecenie w terminalu, a następnie naciśnij klawisz Enter:
łącze ip
Uruchomienie tego polecenia spowoduje wyświetlenie wyniku pokazanego na poniższym obrazku, który jest potwierdzeniem, że polecenie ip działa pomyślnie w nowym katalogu głównym:

Wniosek
Postępując zgodnie z metodą opisaną w tym artykule, możesz łatwo użyć polecenia chroot w Debianie 11. Chociaż cały proces wydaje się dość długi, jeśli uda ci się poprawnie wykonać wszystkie kroki, nigdy nie wpadniesz w żadne kłopoty.
At times, when we want to experiment with something new, we always prefer to run it on a virtual machine or in a sandbox environment so that it cannot cause any damage to our original machine or its important files. The chroot command allows us to run commands and programs in a safe environment by creating an alternate root directory for them. Therefore, we will be talking about the method of using this command on Debian 11.
Using chroot on Debian
For using chroot on Debian 11, you will need to perform the following steps:
Launch the terminal in Debian and then type the following command in it followed by pressing the Enter key to create a new root directory:
sudo mkdir ~/new_root
This command is also shown in the image below:

After successfully creating a new root directory, we will add all the relevant sub-directories in the new root directory. For doing that, type the following command in your terminal and then press the Enter key:
sudo mkdir –p ~/new_root/{bin,lib,lib64}
Running this command will create the bin, lib, and lib64 sub-directories within the new root directory. This command is shown in the following image:

Once you have successfully created these sub-directories, the next thing that you need to do is to copy the binaries of the commands and programs that you want to test using chroot in the new root directory. For doing that, you first need to look for these binaries. In this example, we are going to find out the binaries of the following commands: ip, ls, and pwd. Moreover, we are also going to look for the binary of the bash program. For doing that, we will make use of the which command as shown below:
which ip ls pwd bash
Type this command in your terminal and then press the Enter key. It is also shown in the image below:

As soon as this command will execute successfully, you will be able to see the paths to the binaries of the respective commands and program in your terminal as shown in the following image:

- Once you have found out all the respective binaries, you need to copy them into the new root directory. For doing that, type the following command in your terminal and then press the Enter key:
sudo cp –v /bin/{ip,ls,pwd,bash} ~/new_root/bin
This command is shown in the image below:

The successful execution of this command will show you something like this on your terminal which is, in fact, a confirmation that the binaries have been copied into the new root directory:

The next step is to find out and copy the dependencies of these commands and programs in the new root directory. For finding out the dependencies of the ip command, type the following command in your terminal and then press the Enter key:
ldd /bin/ip
This command is also shown in the following image:

Running this command will show you all the dependencies of the ip command. You can find out the dependencies for the ls and pwd command in the very same manner. The dependencies of the ip command are shown in the image below:

Now you need to copy all the dependencies of the ip command in the new root directory. For doing that, type the command as shown in the following image:

As soon as this command will execute successfully, your terminal will look something like this:

However, for copying the /lib64 dependency, you will need to type the command in your terminal as shown in the image below:

Your terminal will prompt you with a message showing that the /lib64 files have been copied successfully as shown in the following image:

Now you need to find out the dependencies for the bash program. For doing that, type the following command in your terminal and then press the Enter key:
ldd /bin/bash
This command is also shown in the image below:

Running this command successfully will show you all the required dependencies for the bash program as shown in the following image:

Now copy these dependencies into the new root directory by typing the command shown in the image below in your terminal and then pressing the Enter key:

If this command executes successfully, your terminal will look something like this:

However, for copying the /lib64 dependency, type the command shown in the following image in your terminal and then press the Enter key:

Your terminal will display a message confirming that the above-mentioned dependency has been copied to the new root directory successfully as shown in the image below:

Once these steps are executed successfully, you need to navigate to the new root directory. For doing that, type the following command in your terminal and then press the Enter key:
sudo chroot ~/new_root/bin/bash
This command will take you to the bash program environment. It is also shown in the following image:

Once you are running the bash program in your newly created root directory, try running the commands whose configurations you have done above. In this example, I will try to run the ip command and find out if it works in the new root directory or not. For doing that, type the following command in your terminal and then press the Enter key:
ip link
Running this command will display the output shown in the image below which is a confirmation that the ip command is running successfully in the new root directory:

Conclusion
By following the method described in this article, you can easily use the chroot command on Debian 11. Although the whole process seems quite long if you manage to follow all the steps correctly, you will never get yourself in any trouble.
Źródło: link






