All transactions in chroot Linux

Source: Internet
Author: User
Tags postgresql function syslog perl script
What is chroot? Chroot basically defines the running environment of a program. More specifically, it redefines the "root" directory or "/" of a program (or login session).
That is to say, for a chroot program or shell, directories outside the chroot environment do not exist.

So what's the purpose? If intruders intrude into your computer, they will not be able to see all the files in your system.
In this way, intruders may be restricted from executing commands, thus prohibiting them from overwriting insecure files. But the only drawback is that,
I think this cannot prevent them from viewing network connections and other information. Therefore, you should do something that is not deeply involved in this article:

Protect Network Ports.

Check whether all services run with non-root permissions. In addition, are all services chroot?

Transfers System logs to other computers.

Analyze log files.

Analyze people trying to detect random ports on your computer.

Restrict the CPU and memory resources occupied by the Service.

Activate the user quota.

In my opinion, the reason why chroot can be used as a security defense line is,
If the intruders get a non-root account but do not get the root permission for the files, they can only cause damage to the intruded areas.
Moreover, if the root account is the owner of most files in the intrusion region, there are not many attacks for intruders. Apparently, if your account is compromised,
There must be something wrong, but it is best to reduce the damage that intruders can cause.

Remember that what I did is not 100% correct. This is my first attempt to do so, even if it is only partially valid,
It should also be easy to complete basic configurations. I want to be a chroot howto. Now I am talking about some basic things.

How can we make all services chroot?

Okay, let's create a directory "/chroot" first, and put all our services under it in the following format:

Syslogd runs together with each service in the hroot environment.

Apache runs under/chroot/httpd.

SSH runs under/chroot/sshd.

PostgreSQL runs under/chroot/postmaster.

Sendmail runs in the chroot environment, but unfortunately it must run as root.

NTPD runs under/chroot/ntpd.

Named runs under/chroot/named.
Every service is completely isolated from the outside world.

The Perl script used to create the chroot environment.

Download href = ""> and rename it This Perl script lets you list all installed services, view configuration files, configure services, and start and stop services. Generally, this is what you should do.

Create a chroot directory

Download href = "">
And renamed it/chroot/

If your home directory is not/chroot, change the $ home variable in the Perl script accordingly.

Download my href = ""> configuration file.

Now, it is important that I only
7.2 and RedHat 6.2 have been tested ..

Make changes in the Perl script to adapt to your release.

I wrote a long article about chroot, but with my script, it became much shorter. After many chroot services, I noticed that the files and configurations for chroot services are similar. For a specific service, the easiest way to determine which files need to be copied is to view man. If the program needs to use library files, enter "LDD/usr/bin/File". You can also run the chroot command on the service you are installing and start it manually to check the error or check its log file.

You can install a Service as follows:

./ config Service
./ install Service
./ start service

Chroot ntpd

Ntpd is a time service that Synchronizes time between your computer and other computers. It is very easy to chroot it.

# If you do not use my configuration file, remove the comments in the next line.
#./ config ntpd
./ install ntpd
./ start ntpd

Chroot DNS and named

The howto file already exists. Please refer
Href = "">

Href = "">

If you want to use my script

# If you do not use my configuration file, remove the comments in the next line.
#./ config named
./ install named
./ start named

Perform chroot with Syslog and other services, as well as the difficulties I encountered.

I want to chroot syslogd. The difficulty I encountered is that syslogd uses the/dev/log directory by default, and the chroot service cannot see this directory. Therefore, it is not convenient to use syslogd for logging. The following are possible solutions.

Perform the chroot operation on syslogd and each service respectively. I actually tested it in this way and recorded some logs. I don't like this because I have a service that runs with root permissions.

Check whether we can connect to an external Logging Device.

Directly record logs to files instead of syslogd. This may be the best security option, although intruders can modify logs if they are intruded.

Configure syslogd to view a few places to get all services. You can use the-A option of syslogd to do this.

My only solution is to ensure that syslogd performs the chroot operation on each service separately. I like this solution. It records logs in its own chroot environment (such as network ports) with non-root permissions. This may be feasible, but I am stopping what I do and then seeking for a better solution.

If you do not want to assign an independent syslogd to each service, run the following command at the beginning of syslogd when your system runs syslogd:


If SSH and DNS are running, it looks like this:

For Syslogd, I want to say that I want it to run under a non-root account. I tried a few simple things,
But they failed, so they gave up. If syslogd can run with each service under a non-root account,
I will be satisfied with my security measures. If possible, it is best to record the log to an external device.

Chroot Apache

Very simple. Once I run it, I can execute the Perl script. Now, my configuration file is very long,
Because I have to include Perl and PostgreSQL function libraries in the chroot environment. One thing to note: If you want to connect to the database,
Make sure that your database service runs on the loopback device, and specify the host as In the Perl script about DBI.
The following example shows how to connect Apache to a database permanently:

$ DBH | = DBI-> connect (DBI: PG: dbname = database, "", "", {printerror => 0 });

If ($ DBH) {$ DBH-> {printerror} = 1 ;}
{$ DBH | = DBI-> connect (DBI: PG: dbname = database; host = ,"","",
{Printerror => 1 });}

Source Address:

Compile and install Apache in the/usr/local/Apache directory of your system, and then run the Perl script.

# If you do not use my configuration file, remove the comments in the next line.
#./ config httpd
./ install httpd
./ start httpd

The httpd. conf file contains the following lines:
Extendedstatus on

Sethandler server-status
Order deny, allow
Deny from all
Allow from

Sethandler server-Info
Order deny, allow
Deny from all
Allow from

Then, enter http: // or
Http: // and check!

Chroot over SSH

First, it would be ideal to redirect SSH from port 22 to port 2222. Then, when you start SSH,
Let it listen to port 2222 under a non-root account. When initializing an SSH connection, we only want to connect a security account with a password, but do not do anything else.
After they log on, the second SSH program running on port 2222 connects them to the real system-the second SSH program should only listen on the loopback device.
This is what you should do. We don't plan to do it now. The only thing we need to do is use the chroot SSH as an example.
The exercise mentioned above should be completed by the reader: Let the sshd run under a non-root account, and then install the sshd of the second listening loop device to connect people to the real system.

In addition, we only need to chroot SSH and let you have a look at the results (if you only do this, you do not have to observe the entire system ).
Of course, it would be better to record logs on external devices. We should use OpenSSH, but for convenience (this does not seem to be a good excuse), I use a commercial ssh.

Source Address:

Install SSH and run the script under/usr/local/ssh_chroot.

# If you do not use my configuration file, remove the comments in the next line.
#./ config sshd
./ install sshd
./ start sshd

I think the real benefit of placing SSH in the chroot environment is that if you replace the FTP server with it, people only have limited permissions in your region.
Rsync and SCP run very well when people upload files. I am not very fond of setting up an FTP server for people to log on. Many FTP servers run in the chroot environment,
But I don't like the plaintext password they still send.

Chroot postsql

This is almost as simple as Perl, except that it requires some additional function libraries. In general, this is not difficult to do.
One thing I have to do is to put PostgreSQL on the network, but only on the loopback device. Because it is chroot,
Therefore, other services that are already chroot cannot be connected to them, just like Apache on the Web server.
I compiled Perl into PostgreSQL, so I have to add a lot of Perl stuff to my configuration file.

Source code: href = "">

Compile and install Apache in the/usr/local/Postgres directory in your system. Then run the Perl script.

# If you do not use my configuration file, remove the comments in the next line.
#./ config Postgres
./ install ipvs
./ start Postgres

Run Sendmail for chroot

Execute my Perl script.
# If you do not use my configuration file, remove the comments in the next line.
#./ config sendmail
./ install sendmail
./ start sendmail

What have you found? Yes, he still runs with the root account. In addition, when Sendmail is started, the program/etc/rc. d/init. d/sendmail will recreate some files.
My script does not solve this problem. At any time, if you have made any changes under/etc/mail, copy the changed files to the/chroot/sendmail/etc directory.
You must also direct/var/spool/mail to/chroot/sendmail/var/spool/mail for Sendmail programs and users (when they log in) see the same file.

Fortunately, you can send emails at any time. problems may occur only when you receive emails. Therefore, I can install Sendmail and Apache together without any problems.
Some of my Perl scripts will send emails out, so I want to copy the Sendmail program to Apache's chroot environment.

Other things about chroot.

My point is as follows:

Your machine includes Sendmail, ssh, Apache,
All services, including PostgreSQL and syslog, must run in the chroot environment.

Every service must run with a non-Root Account (you may need to redirect protected ports to unprotected ports. This includes Sendmail and syslog.

Logs should be kept away from the site.

Each service is assigned a disk quota to limit the disk occupied by intruders. When the disk is full, you should install the file system for some services on the loopback device.

The owner of all files that do not need to be modified should be the root account.
Now, speaking of Sendmail and syslogd, I still think they should not run under the root account.
This may be possible for Sendmail, but I found it extremely difficult to run it under a non-root account. At least I have not succeeded.
I think Sendmail cannot run under a non-root account. It should be a very serious error. Although I know it is very difficult to make it run under a non-root account,
But I think all the difficulties can be solved. As long as the File Permission problem is solved, I don't think Sendmail needs to run with root permission.
I must have ignored something. I don't believe that these obstacles are insurmountable.

I have not tried syslog, but I think it is feasible to record logs with a non-root account.
At least I can record logs for each service in the chroot environment.

All services must run under non-root accounts, or even NFS. Remember, it is all services.


Run two sshd Daemon Processes and perform Secondary Logon.

Try to make sendmail or other emails run under a non-root account.

Delete unnecessary function libraries under/lib. I copied the library I needed. In fact, you don't need most of them.

Use syslogd for remote logging to check whether syslogd can connect to the network port and obtain logs of all services running on the network port of the loopback device.
Check whether syslogd can run in a non-root account.


I think chroot is so cool for all services. I think it should be a big mistake not to allow all services to run in a non-root account chroot environment.
I hope that the major releases should do this, and of course, other releases should do the same. Since Mandrake started and developed with RedHat compatibility, people can follow Mandrake's example,
Expand chroot Based on others. I think this is feasible, because in GNU/Linux, nothing will prevent you from redo the work of others.
If a company wants all the services of chroot and creates an environment for people to easily manage the services of chroot, it has an ideal release.
Remember, Linux is becoming mainstream and people don't want to see the command line any more. So if everything can be done in the GUI environment, people don't need to understand the internal structure,
And you don't need to know what is running, as long as they can configure and know that this is effective.

I absolutely support running all services in a non-root chroot environment, and I will not consider using it in a production environment.
I am making all the services run in the chroot environment and try my best to make more and more things run like this-in the end, I will achieve my ideal.

I plan to write a howto for chroot. I am sending a request. I hope someone can convert my article to the lyx format so that it can be put on Linux's howto.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.