Jak używać chroot na Debianie 11

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:

Utwórz nowy katalog

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:

Utwórz katalogi systemowe

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:

Użyj którego polecenia

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:

Znajdź ścieżki programu za pomocą którego polecenia
  • 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:

Podkatalogi Chroot

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:

Skopiuj pliki binarne

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:

polecenie ldd

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:

Dowiedz się, jakich bibliotek wymaga plik binarny za pomocą ldd

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:

Kopiuj zależności

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

Zależności plików

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

zależności lib64

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

polecenie skopiowane

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:

dd razem

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

Zależności powłoki bash

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:

Kopiowanie zależności plików bash

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

skopiowane pliki

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

lib64 dep

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:

skopiowane pliki

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:

Użyj chroota

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:

użyj polecenia ip w więzieniu

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:

Create a new directory

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:

Create system directories

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:

Use which command

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:

Find program paths using which command
  • 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:

Chroot subdirectories

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:

Copy binary files

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:

ldd command

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:

Find out which libraries a binary file requires with ldd

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:

Copy dependencies

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

File dependencies

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

lib64 dependencies

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

command copied

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:

ldd bash

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

bash shell dependencies

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:

Copying bash file dependencies

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

copied files

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

lib64 dep

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:

copied files

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:

Use chroot

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:

use ip command in jail

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