You may have many reasons to isolate an application, user, or environment from your Linux system. Different operating systems have different implementation methods. In Linux, a typical method is the chroot environment. In this tutorial, I will step by step guide you how to use the chroot command to configure an independent environment separated from the real system. This function is mainly used for testing projects. The following steps are performed on Ubuntu14.04 virtual private server (VPS. Learning
You may have many reasons to isolate an application, user, or environment from your Linux system. Different operating systems have different implementation methods. In Linux, a typical method is the chroot environment.
In this tutorial, I will step by step guide you how to use the chroot command to configure an independent environment separated from the real system. This function can be used to test projects.Ubuntu 14.04Run the command on the virtual private server (VPS.
Learning to quickly build a simple chroot environment is a very practical skill, and most system administrators can benefit from it.
Chroot environment
A chroot environment is called by the system to temporarily change a local directory to the root directory. Generally, the system root directory is the mount point "/". However, after using the chroot command, you can use other directories as the root directory.
In principle, any application running in the chroot environment cannot access other information in the system, applications running in the directory can only access files in the local directory, but cannot access files outside the directory. However, applications running in the chroot environment can access information outside the environment through the sysfs file system. Therefore, there is a "principle" modifier here ).
Use of the Chroot environment
Testing an unstable application service does not affect the entire host system.
Even if the root permission is used for some improper operations, the chroot environment will not be affected.
You can run another operating system in your system.
For example, you can compile, install, and test software in the chroot environment without moving the real system. You can alsoCreate a 32-bit environment using chroot in a 64-bit environment, and then run a 32-bit program(LCTT ze Note: If your real environment is 32-bit, you cannot chroot a 64-bit environment ).
However, for the sake of security, the chroot environment imposes very strict restrictions on non-privileged users, rather than providing complete security policies. If you need a comprehensive security policy isolation solution, consider LXC, Docker, vservers, and so on.
Debootstrap and Schroot
Installation is required to use the chroot environmentDebootstrapAndSchrootBoth software are in the Ubuntu image source. Schroot is used to create a chroot environment.
DebootstrapYou can install any Debian (or Debian-based) system through the image source. The installed system contains the most basic commands.
SchrootCommand allows you to use the same mechanism to create a chroot environment, but some permission checks are performed when you access the chroot environment, and some additional automatic settings are allowed, such as mounting some file systems.
On Ubuntu 14.04 LTS, we can implement this function in two steps:
1. install the software package
Step 1: Install debootstrap and schroot on the Ubuntu 14.04 LTS host system:
- $ Sudo apt-get install debootstrap
- $ Sudo apt-get install schroot
2. Configure Schroot
Now that we have a tool in hand, we need to specify a directory as our chroot environment. Create a directory first:
- Sudo mkdir/linoxide
Edit the configuration file of schroot:
- Sudo nano/etc/schroot. conf
Again, we are now on the Ubuntu 14.04 LTS system. If you want to test whether a software package can run on Ubuntu 13.10 (codenamed "Saucy Salamander"), you can add the following content to the configuration file:
- [Saucy]
- Description = polictusaucy
- Location =/linoxide
- Priority = 3
- Users = arun
- Root-groups = root
Adjust the preceding configuration according to your system requirements.
3. Use debootstrap to install a 32-bit Ubuntu System
The Debootstrap command willChroot environmentDownload and install a minimum system. As long as you can access the image source, you can install any Debian-based system version.
We have created/LinoxideThe directory is used to place the chroot environment. Now we can run debootstrap in this directory:
- Cd/linoxide
- Sudo debootstrap -- variant = buildd -- arch amd64 saucy/linoxide/http://archive.ubuntu.com/ubuntu/
- Sudo chroot/linoxide/debootstrap -- second-stage
You can change the -- arch parameter to i386 or another architecture, as long as there is an image source for this architecture. You can also replace the image source http://archive.ubuntu.com/ubuntu/ with the image source closest to you. For details, refer to the official Ubuntu image home page.
Note: If you create a 32-bit system in a 64-bit system, you need to add the -- foreign option to the above 3rd-line command, just like the following command:
- Sudo debootstrap -- variant = buildd -- foreign -- arch i386 saucy/linoxide/http://archive.ubuntu.com/ubuntu/
It may take some time to download your network bandwidth. The minimum system size is approximately 500 mb.
4. Complete the chroot environment
After installing the system, we need to finish some work to ensure that the system runs normally. First, ensure that the fstab program of the host is aware of the existence of the chroot environment:
- Sudo nano/etc/fstab
Add the following configuration at the end of the file:
- Proc/linoxide/proc defaults 00
- Sysfs/linoxide/sys sysfs ults 00
Save and close the file.
Mount some file systems to the chroot environment:
- $ Sudo mount proc/linoxide/proc-t proc
- $ Sudo mount sysfs/linoxide/sys-t sysfs
Copy the/etc/hosts file to the chroot environment so that the network can be used in the chroot environment:
- $ Sudo cp/etc/hosts/linoxide/etc/hosts
Finally, run the schroot-l command to list all the chroot environments on the system:
- $ Schroot-l
Run the following command to enter the chroot environment:
- $ Sudo chroot/linoxide // bin/bash
Version of the test and installation:
- # Lsb_release-
- # Uname-
To use the graphical interface in the chroot environment, you need to set the DISPLAY environment variable:
- $ Show =: 0.0./apps
So far, I have successfully installed Ubuntu 14.04 on Ubuntu 13.10 LTS.
Exit the chroot environment:
- # Exit
Clean up and uninstall the file system:
- $ Sudo umount/test/proc
- $ Sudo umount/test/sys
For more information about Ubuntu, see Ubuntu special page http://www.linuxidc.com/topicnews.aspx? Tid = 2
This article permanently updates the link address: Http://www.linuxidc.com/Linux/2015-02/113975.htm