<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.openvz.org/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=DmitryKoterov</id>
	<title>OpenVZ Virtuozzo Containers Wiki - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.openvz.org/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=DmitryKoterov"/>
	<link rel="alternate" type="text/html" href="https://wiki.openvz.org/Special:Contributions/DmitryKoterov"/>
	<updated>2026-06-10T00:08:44Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.31.1</generator>
	<entry>
		<id>https://wiki.openvz.org/index.php?title=Setting_up_an_iptables_firewall&amp;diff=9347</id>
		<title>Setting up an iptables firewall</title>
		<link rel="alternate" type="text/html" href="https://wiki.openvz.org/index.php?title=Setting_up_an_iptables_firewall&amp;diff=9347"/>
		<updated>2010-11-11T23:37:11Z</updated>

		<summary type="html">&lt;p&gt;DmitryKoterov: /* Simple firewall configuration independent to IP addresses: vzfirewall */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This document consists of two parts. The first is setting up a firewall (using iptables) on the HN, which will restrict traffic to the containers. The effect would emulate, as far as the containers and their customers are concerned, an external hardware firewall controlled by the sysadmin. The second is setting up a firewall that protects the [[HN]] itself but still allows traffic to the containers, thus allowing individual containers to define their own iptables.&lt;br /&gt;
&lt;br /&gt;
While the firewalls shown here can be accomplished using iptables manually (or using Fedora core's iptables service), the methods presented here are especially modular and easy to modify. This is important when you have 20+ containers and a lot of other things to be doing...&lt;br /&gt;
&lt;br /&gt;
The scripts and pathnames given here are for Fedora Core 6, though they can probably be applied to most similar SysV-like systems with little modification.&lt;br /&gt;
&lt;br /&gt;
== A little background ==&lt;br /&gt;
&lt;br /&gt;
On our systems, we use the HN to provide privileged services which are not appropriate for access by the containers. For example, the HN acts as a backup server, runs Nagios for health monitoring, has a webserver for managing the 3ware RAID controller, etc. The containers are leased to customers, who can't entirely be trusted, especially if they get hacked. As such, our scenario is one in which the HN must be protected from all access (even from the containers) except for a few trusted hosts (e.g. my home-office).&lt;br /&gt;
&lt;br /&gt;
The exception to this is the nameserver, which we want open to the world. We use it as a caching nameserver for our containers and also to host DNS for a few customer domain.&lt;br /&gt;
&lt;br /&gt;
== Simple firewall configuration independent to IP addresses: vzfirewall ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;Vzfirewall&amp;lt;/code&amp;gt; tool allows you to open/close ports for incoming connections with no dependencies to foreign IP addresses. E.g. you may allow a hostname &amp;lt;code&amp;gt;release.prod.example.com&amp;lt;/code&amp;gt; to connect to port 5432 of VE 1234 and leave all other ports closed by modifying &amp;lt;code&amp;gt;1234.conf&amp;lt;/code&amp;gt; file adding multiline &amp;lt;code&amp;gt;FIREWALL&amp;lt;/code&amp;gt; directive into it:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
...&lt;br /&gt;
PRIVVMPAGES=&amp;quot;300000:300000&amp;quot;&lt;br /&gt;
HOSTNAME=&amp;quot;example.com&amp;quot;&lt;br /&gt;
...&lt;br /&gt;
FIREWALL=&amp;quot;&lt;br /&gt;
    ...&lt;br /&gt;
    # Allow access to PostgreSQL port only from release.prod &lt;br /&gt;
    # and release.test machines. You may use domain names here.&lt;br /&gt;
    [5432]&lt;br /&gt;
    release.prod.example.com&lt;br /&gt;
    release.test.example.com&lt;br /&gt;
    ...&lt;br /&gt;
&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You must then run &amp;lt;code&amp;gt;vzfirewall -a&amp;lt;/code&amp;gt; on your hardware node to apply changes made in &amp;lt;code&amp;gt;*.conf&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Note that it is recommended to use hostnames instead of IP addresses here, so the configuration is persistent for VE movements to different IP-address: you just need to run &amp;lt;code&amp;gt;vzfirewall -a&amp;lt;/code&amp;gt; again after movement. It is also reboot-safe, because applied to &amp;lt;code&amp;gt;/etc/sysconfig/iptables&amp;lt;/code&amp;gt; (at RHEL systems).&lt;br /&gt;
&lt;br /&gt;
Vzfirewall and its documentation are available at [http://en.dklab.ru/lib/dklab_vzfirewall/ http://en.dklab.ru/lib/dklab_vzfirewall/].&lt;br /&gt;
&lt;br /&gt;
== An alternative from the author of Shorewall ==&lt;br /&gt;
&lt;br /&gt;
For those who might find the solution provided in this wiki article unsatisfactory (for whatever reason), the creator of Shorewall (Tom Eastep) has written a nice article explaining how to use Shorewall on an OpenVZ host node to manage the host node, containers, and more... with quite a complex setup as an example.  The article IS NOT an introduction to Shorewall for beginners, so some pre-existing knowledge and understanding of Shorewall may be required.&lt;br /&gt;
&lt;br /&gt;
Shorewall and OpenVZ by Tom Eastep - http://www.shorewall.net/OpenVZ.html&lt;br /&gt;
&lt;br /&gt;
See also this OpenVZ Forum posting - http://forum.openvz.org/index.php?t=msg&amp;amp;goto=16406&amp;amp;&lt;br /&gt;
&lt;br /&gt;
== Setting up a HN-based firewall ==&lt;br /&gt;
&lt;br /&gt;
This setup emulates (to the containers anyway) an external hardware firewall. It protects the HN from any access and then defines what services and ports are allowed/banned for individual containers. This leaves the firewall controlled by the site administrator, not be individual containers and the hackers who've gotten into them. ;)&lt;br /&gt;
&lt;br /&gt;
First off, let's disable Fedora's existing &amp;lt;code&amp;gt;iptables&amp;lt;/code&amp;gt; service:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
service iptables stop&lt;br /&gt;
chkconfig iptables off&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now create the new &amp;lt;code&amp;gt;firewall&amp;lt;/code&amp;gt; service. This code should be &amp;lt;code&amp;gt;/etc/init.d/firewall&amp;lt;/code&amp;gt; and then should be chmod'd 755.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/bin/sh&lt;br /&gt;
# firewall      Start iptables firewall&lt;br /&gt;
# chkconfig: 2345 08 92&lt;br /&gt;
# description:  Starts, stops and saves iptables firewall&lt;br /&gt;
# This script sets up the firewall for the INPUT chain (which is for&lt;br /&gt;
# the HN itself) and then processes the config files under&lt;br /&gt;
# /etc/firewall.d to set up additional rules in the FORWARD chain&lt;br /&gt;
# to allow access to containers' services.&lt;br /&gt;
&lt;br /&gt;
. /etc/init.d/functions&lt;br /&gt;
&lt;br /&gt;
# the IP block allocated to this server&lt;br /&gt;
SEGMENT=&amp;quot;192.168.0.0/24&amp;quot;&lt;br /&gt;
# the IP used by the hosting server itself&lt;br /&gt;
THISHOST=&amp;quot;192.168.0.1&amp;quot;&lt;br /&gt;
# services that should be allowed to the HN;&lt;br /&gt;
# services for containers are configured in /etc/firewall.d/*&lt;br /&gt;
OKPORTS=&amp;quot;53&amp;quot;&lt;br /&gt;
# hosts allowed full access through the firewall,&lt;br /&gt;
# to all containers and to this server&lt;br /&gt;
DMZS=&amp;quot;12.34.56.78 90.123.45.67&amp;quot;&lt;br /&gt;
&lt;br /&gt;
purge() {&lt;br /&gt;
  echo -n &amp;quot;Firewall: Purging and allowing all traffic&amp;quot;&lt;br /&gt;
  iptables -P OUTPUT ACCEPT&lt;br /&gt;
  iptables -P FORWARD ACCEPT&lt;br /&gt;
  iptables -P INPUT ACCEPT&lt;br /&gt;
  iptables -F&lt;br /&gt;
  success ; echo&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
setup() {&lt;br /&gt;
  echo -n &amp;quot;Firewall: Setting default policies to DROP&amp;quot;&lt;br /&gt;
  iptables -P INPUT DROP&lt;br /&gt;
  iptables -P FORWARD DROP&lt;br /&gt;
  iptables -I INPUT   -j ACCEPT -m state --state ESTABLISHED,RELATED&lt;br /&gt;
  iptables -I FORWARD -j ACCEPT -m state --state ESTABLISHED,RELATED&lt;br /&gt;
  iptables -I INPUT -j ACCEPT -i lo&lt;br /&gt;
  iptables -I FORWARD -j ACCEPT --source $SEGMENT&lt;br /&gt;
  success ; echo&lt;br /&gt;
&lt;br /&gt;
  echo &amp;quot;Firewall: Allowing access to HN&amp;quot;&lt;br /&gt;
  for port in $OKPORTS ; do&lt;br /&gt;
    echo -n &amp;quot;          port $port&amp;quot;&lt;br /&gt;
    iptables -I INPUT -j ACCEPT -s $SEGMENT -d $THISHOST --protocol tcp --destination-port $port&lt;br /&gt;
    iptables -I INPUT -j ACCEPT -s $SEGMENT -d $THISHOST --protocol udp --destination-port $port&lt;br /&gt;
    success ; echo&lt;br /&gt;
  done&lt;br /&gt;
  for ip in $DMZS ; do&lt;br /&gt;
    echo -n &amp;quot;          DMZ $ip&amp;quot;&lt;br /&gt;
    iptables -I INPUT   -i eth0 -j ACCEPT -s $ip&lt;br /&gt;
    iptables -I FORWARD -i eth0 -j ACCEPT -s $ip&lt;br /&gt;
    success ; echo&lt;br /&gt;
  done&lt;br /&gt;
&lt;br /&gt;
  CTSETUPS=`echo /etc/firewall.d/*`&lt;br /&gt;
  if [ &amp;quot;$CTSETUPS&amp;quot; != &amp;quot;/etc/firewall.d/*&amp;quot; ] ; then&lt;br /&gt;
  echo &amp;quot;Firewall: Setting up container firewalls&amp;quot;&lt;br /&gt;
  for i in $CTSETUPS ; do&lt;br /&gt;
    . $i&lt;br /&gt;
    echo -n &amp;quot;          $CTNAME CT$CTID&amp;quot;&lt;br /&gt;
    if [ -n &amp;quot;$BANNED&amp;quot; ]; then&lt;br /&gt;
      for source in $BANNED ;  do iptables -I FORWARD -j DROP --destination $CTIP --source $source ; done&lt;br /&gt;
    fi&lt;br /&gt;
    if [ -n &amp;quot;$OPENPORTS&amp;quot; ]; then&lt;br /&gt;
      for port in $OPENPORTS ; do iptables -I FORWARD -j ACCEPT --protocol tcp --destination $CTIP --destination-port $port ; done&lt;br /&gt;
      for port in $OPENPORTS ; do iptables -I FORWARD -j ACCEPT --protocol udp --destination $CTIP --destination-port $port ; done&lt;br /&gt;
    fi&lt;br /&gt;
    if [ -n &amp;quot;$DMZS&amp;quot; ]; then&lt;br /&gt;
      for source in $DMZS ; do iptables -I FORWARD -j ACCEPT --protocol tcp --destination $CTIP --source $source ; done&lt;br /&gt;
      for source in $DMZS ; do iptables -I FORWARD -j ACCEPT --protocol udp --destination $CTIP --source $source ; done&lt;br /&gt;
    fi&lt;br /&gt;
    [ $? -eq 0 ] &amp;amp;&amp;amp; success || failure&lt;br /&gt;
    echo&lt;br /&gt;
  done&lt;br /&gt;
  fi&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
case &amp;quot;$1&amp;quot; in&lt;br /&gt;
  start)&lt;br /&gt;
    echo &amp;quot;Starting firewall...&amp;quot;&lt;br /&gt;
    purge&lt;br /&gt;
    setup&lt;br /&gt;
    ;;&lt;br /&gt;
  stop)&lt;br /&gt;
    echo &amp;quot;Stopping firewall...&amp;quot;&lt;br /&gt;
    purge&lt;br /&gt;
    ;;&lt;br /&gt;
  restart)&lt;br /&gt;
    $0 stop&lt;br /&gt;
    $0 start&lt;br /&gt;
    ;;&lt;br /&gt;
  status)&lt;br /&gt;
    iptables -n -L&lt;br /&gt;
    ;;&lt;br /&gt;
  *)&lt;br /&gt;
    echo &amp;quot;Usage: $0 &amp;lt;start|stop|restart|status&amp;gt;&amp;quot;&lt;br /&gt;
    ;;&lt;br /&gt;
esac&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note: This will only allow access to the HN from the hosts/networks defined in SEGMENT. If you'd like to open up the OKPORTS on the HN to everybody, you can remove the ''-s $SEGMENT'' parameters from the iptables commands under the &amp;quot;Firewall: Allowing access to HN&amp;quot; section. The modified lines would look like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
iptables -I INPUT -j ACCEPT -d $THISHOST --protocol tcp --destination-port $port&lt;br /&gt;
iptables -I INPUT -j ACCEPT -d $THISHOST --protocol udp --destination-port $port&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above script can be called like this:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
service firewall start&lt;br /&gt;
service firewall stop&lt;br /&gt;
service firewall restart&lt;br /&gt;
service firewall status&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It will set up the firewall for the HN according to the parameters you specified for OKPORTS, DMZs, etc. and then it will call each file under /etc/firewall.d and process  its configuration.&lt;br /&gt;
&lt;br /&gt;
So create a file under /etc/firewall.d The exact filename isn't important, as long as it's meaningful to you, e.g. &amp;lt;code&amp;gt;ExampleCompany&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;ve12&amp;lt;/code&amp;gt; and give it content like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# This file is processed by /etc/init.d/firewall&lt;br /&gt;
CTID=&amp;quot;1&amp;quot;			# the container's ID#&lt;br /&gt;
CTNAME=&amp;quot;Customer1&amp;quot;		# A human-friendly label for the container&lt;br /&gt;
CTIP=&amp;quot;192.168.1.34&amp;quot;		# the IP address for this container &lt;br /&gt;
OPENPORTS=&amp;quot;80 443&amp;quot;		# ports that should be universally opened&lt;br /&gt;
				# to the entire Internet&lt;br /&gt;
DMZS=&amp;quot;1.2.3.0/24 5.6.7.8/32&amp;quot;	# IPs and blocks that should have full access&lt;br /&gt;
				# to the container's services&lt;br /&gt;
BANNED=&amp;quot;&amp;quot;			# IPs and blocks that should be entirely&lt;br /&gt;
				# blocked from the container's services&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And there you go. Go ahead and start the firewall and check its status:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
service firewall restart&lt;br /&gt;
service firewall status&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As you can see, you can now add and edit the configurations for individual containers very easily. This method proves a lot easier to manage than Fedora's iptables-config mechamism!&lt;br /&gt;
&lt;br /&gt;
To make the firewall service automatically start when the HN boots, run&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
chkconfig --add firewall&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Debian Notes ===&lt;br /&gt;
&lt;br /&gt;
The setup above works fine for Debian as well, however /etc/init.d/functions is missing. Here is a very simple version that you can use:&lt;br /&gt;
&lt;br /&gt;
  # /etc/init.d/functions&lt;br /&gt;
  &lt;br /&gt;
  success() {&lt;br /&gt;
    echo -n &amp;quot;...success&amp;quot;&lt;br /&gt;
  } &lt;br /&gt;
  failure() {&lt;br /&gt;
    echo -n &amp;quot;...failure&amp;quot;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
== Setting up a firewall that allows per-container configuration ==&lt;br /&gt;
&lt;br /&gt;
This setup configures iptables on the HN to disallow access to all hosts, including the containers. However, it allows all traffic into the containers so they may define their own iptables rules and therefore manage their own firewall.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
iptables -P FORWARD ACCEPT&lt;br /&gt;
iptables -F FORWARD&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This will remove all rules for the FORWARD chain so all packets can pass back and forth between containers and the outside world.&lt;br /&gt;
&lt;br /&gt;
If you want to use a firewall inside a container, please load these modules BEFORE starting the container:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
modprobe xt_tcpudp&lt;br /&gt;
modprobe ip_conntrack&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you do not, you will get an error like this: &amp;quot;iptables: No chain/target/match by that name&amp;quot;&lt;br /&gt;
&lt;br /&gt;
If you want to use stateful firewall rules (and you should!) you will also need to make sure that 'ipt_state' is in the 'IPTABLES' option in your vz.conf file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
IPTABLES=&amp;quot;ipt_REJECT ipt_tos ipt_limit ipt_multiport iptable_filter iptable_mangle ipt_TCPMSS ipt_tcpmss ipt_ttl ipt_length ipt_state&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Also make sure the 'xt_state' module is loaded on the host:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
modprobe xt_state&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
&lt;br /&gt;
* [[Traffic accounting with iptables]]&lt;br /&gt;
&lt;br /&gt;
[[Category: Networking]]&lt;/div&gt;</summary>
		<author><name>DmitryKoterov</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.openvz.org/index.php?title=UBC_primary_parameters&amp;diff=9346</id>
		<title>UBC primary parameters</title>
		<link rel="alternate" type="text/html" href="https://wiki.openvz.org/index.php?title=UBC_primary_parameters&amp;diff=9346"/>
		<updated>2010-11-11T23:33:18Z</updated>

		<summary type="html">&lt;p&gt;DmitryKoterov: /* vmguarpages */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{UBC toc}}&lt;br /&gt;
&lt;br /&gt;
The most important parameters determining the resources available to&lt;br /&gt;
container are explained below. The meaning of the parameters&lt;br /&gt;
is illustrated assuming that the container runs some network&lt;br /&gt;
server applications.&lt;br /&gt;
&lt;br /&gt;
== numproc ==&lt;br /&gt;
Maximum number of processes and kernel-level threads allowed for this container.&lt;br /&gt;
&lt;br /&gt;
Many server applications (like Apache Web server, FTP and mail servers)&lt;br /&gt;
spawn a process to handle each client, so the limit on number of processes&lt;br /&gt;
defines how many clients the application will be able to handle in parallel.&lt;br /&gt;
However, the number of processes doesn't limit how “heavy” the application is&lt;br /&gt;
and whether the server will be able to serve heavy requests from clients.&lt;br /&gt;
&lt;br /&gt;
Configuring resource control system, it is important to estimate both&lt;br /&gt;
the maximum number of processes and the average number of processes&lt;br /&gt;
(referred to as &amp;lt;code&amp;gt;avnumproc&amp;lt;/code&amp;gt; later). Other (dependent) resource&lt;br /&gt;
control parameters may depend both on the limit and the average number (see [[UBC consistency check]]).&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;barrier&amp;lt;/code&amp;gt; of &amp;lt;code&amp;gt;numproc&amp;lt;/code&amp;gt; doesn't provide additional control and should be set equal to the &amp;lt;code&amp;gt;limit&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
There is a restriction on the total number of processes in the system.&lt;br /&gt;
More than about 16000 processes start to cause poor responsiveness of&lt;br /&gt;
the system, worsening when the number grows. Total number of processes&lt;br /&gt;
exceeding 32000 is very likely to cause hang of the system.&lt;br /&gt;
&lt;br /&gt;
Note that in practice the number of processes is usually less. Each&lt;br /&gt;
process consumes some memory, and the available memory and the&lt;br /&gt;
&amp;quot;low memory&amp;quot; (see [[UBC systemwide configuration#“Low memory”|“Low memory”]]) limit the number of processes to lower&lt;br /&gt;
values. With typical processes, it is normal to be able to run only up&lt;br /&gt;
to 8000 processes in a system.&lt;br /&gt;
&lt;br /&gt;
== numtcpsock ==&lt;br /&gt;
Maximum number of TCP sockets.&lt;br /&gt;
&lt;br /&gt;
This parameter limits the&lt;br /&gt;
number of TCP connections and, thus, the number of clients the server&lt;br /&gt;
application can handle in parallel.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;barrier&amp;lt;/code&amp;gt; of this parameter should be set equal to the &amp;lt;code&amp;gt;limit&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
If each container has it's own set of IP addresses (which is&lt;br /&gt;
the only way a OpenVZ system can be configured), there are no direct&lt;br /&gt;
limits on the total number of TCP sockets in the system. The number&lt;br /&gt;
of sockets needs to be controlled because each socket needs certain&lt;br /&gt;
amount of memory for receive and transmit buffers (see descriptions&lt;br /&gt;
of &amp;lt;code&amp;gt;[[tcpsndbuf]]&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;[[tcprcvbuf]]&amp;lt;/code&amp;gt;), and&lt;br /&gt;
the memory is a limited resource.&lt;br /&gt;
&lt;br /&gt;
== numothersock ==&lt;br /&gt;
Maximum number of non-TCP sockets (local sockets, UDP and other types of sockets).&lt;br /&gt;
&lt;br /&gt;
Local (UNIX-domain) sockets are used for communications inside the&lt;br /&gt;
system. Multi-tier applications (for example, a Web application with a&lt;br /&gt;
database server as a back-end) may need one or multiple local sockets&lt;br /&gt;
to serve each client. Straightforward applications (for example, most&lt;br /&gt;
mail servers, with the exception of Postfix) do not use local sockets.&lt;br /&gt;
&lt;br /&gt;
UDP sockets are used for Domain Name Service (DNS) queries, but&lt;br /&gt;
the number of such sockets opened simultaneously is low. UDP and&lt;br /&gt;
other sockets may also be used in some very special applications&lt;br /&gt;
(SNMP agents and others).&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;barrier&amp;lt;/code&amp;gt; of this parameter should be set equal to the &amp;lt;code&amp;gt;limit&amp;lt;/code&amp;gt;.&lt;br /&gt;
The number of local sockets in a system is not limited. The number of&lt;br /&gt;
UDP sockets in a system, similarly to TCP sockets, is not limited in&lt;br /&gt;
OpenVZ systems.&lt;br /&gt;
&lt;br /&gt;
Similarly to &amp;lt;code&amp;gt;numtcpsock&amp;lt;/code&amp;gt; parameter discussed above, the number of&lt;br /&gt;
non-TCP sockets needs to be controlled because each socket needs certain&lt;br /&gt;
amount of memory for its buffers, and the memory is a limited&lt;br /&gt;
resource.&lt;br /&gt;
&lt;br /&gt;
== vmguarpages ==&lt;br /&gt;
Memory allocation guarantee.&lt;br /&gt;
&lt;br /&gt;
This parameter controls how much memory is available to the Virtual&lt;br /&gt;
Environment (i.e. how much memory its applications can allocate by&lt;br /&gt;
&amp;lt;code&amp;gt;malloc(3)&amp;lt;/code&amp;gt; or other standard Linux memory allocation mechanisms).&lt;br /&gt;
The more clients are served or the more “heavy” the application is, the&lt;br /&gt;
more memory it needs.&lt;br /&gt;
&lt;br /&gt;
The amount of memory that container's applications are&lt;br /&gt;
guaranteed to be able to allocate is specified as the &amp;lt;code&amp;gt;barrier&amp;lt;/code&amp;gt; of&lt;br /&gt;
&amp;lt;code&amp;gt;vmguarpages&amp;lt;/code&amp;gt; parameter. The current amount of allocated memory space&lt;br /&gt;
is accounted into &amp;lt;code&amp;gt;privvmpages&amp;lt;/code&amp;gt; parameter, and &amp;lt;code&amp;gt;vmguarpages&amp;lt;/code&amp;gt;&lt;br /&gt;
parameter does not have its own accounting. The &amp;lt;code&amp;gt;barrier&amp;lt;/code&amp;gt; and the &amp;lt;code&amp;gt;limit&amp;lt;/code&amp;gt; of&lt;br /&gt;
&amp;lt;code&amp;gt;privvmpages&amp;lt;/code&amp;gt; parameter impose an upper limit on the memory allocations&lt;br /&gt;
(see [[privvmpages]]). The meaning of the &amp;lt;code&amp;gt;limit&amp;lt;/code&amp;gt; for the&lt;br /&gt;
&amp;lt;code&amp;gt;vmguarpages&amp;lt;/code&amp;gt; parameter is unspecified in the current version and should be set&lt;br /&gt;
to the maximal allowed value ([[MAX_ULONG]]).&lt;br /&gt;
&lt;br /&gt;
If the current amount of allocated memory space does not exceed the&lt;br /&gt;
guaranteed amount (the &amp;lt;code&amp;gt;barrier&amp;lt;/code&amp;gt; of &amp;lt;code&amp;gt;vmguarpages&amp;lt;/code&amp;gt;),&lt;br /&gt;
memory allocations of container's applications always succeed.&lt;br /&gt;
If the current amount of allocated memory space exceeds the guarantee but below&lt;br /&gt;
the &amp;lt;code&amp;gt;barrier&amp;lt;/code&amp;gt; of &amp;lt;code&amp;gt;privvmpages&amp;lt;/code&amp;gt;, allocations may or may not succeed,&lt;br /&gt;
depending on the total amount of available memory in the system.&lt;br /&gt;
&lt;br /&gt;
Starting from the &amp;lt;code&amp;gt;barrier&amp;lt;/code&amp;gt; of &amp;lt;code&amp;gt;privvmpages&amp;lt;/code&amp;gt;,&lt;br /&gt;
normal priority allocations and, starting from the &amp;lt;code&amp;gt;limit&amp;lt;/code&amp;gt;&lt;br /&gt;
of &amp;lt;code&amp;gt;privvmpages&amp;lt;/code&amp;gt;, all memory allocations&lt;br /&gt;
made by the applications fail.&lt;br /&gt;
The memory allocation guarantee (&amp;lt;code&amp;gt;vmguarpages&amp;lt;/code&amp;gt;) is a primary tool for&lt;br /&gt;
controlling the memory available to containers, because&lt;br /&gt;
it allows administrators to provide Service Level Agreements — agreements&lt;br /&gt;
guaranteeing certain quality of service, certain amount of resources&lt;br /&gt;
and general availability of the service. The unit of measurement&lt;br /&gt;
of vmguarpages values is memory pages (4KB on x86 and x86_64 processors).&lt;br /&gt;
The total memory allocation guarantees given to containers&lt;br /&gt;
are limited by the physical resources of the computer — the size of RAM&lt;br /&gt;
and the swap space — as discussed in [[UBC systemwide configuration]].&lt;br /&gt;
&lt;br /&gt;
There is a ''pseudo-graphical'' tool - &amp;lt;code&amp;gt;[http://en.dklab.ru/lib/dklab_vzmem/ vzmem]&amp;lt;/code&amp;gt; - which allows you to distribute physical memory among all VEs consistently. It shows all physical memory blocks graphically in &amp;lt;code&amp;gt;/etc/vz/conf/MEM-MAP&amp;lt;/code&amp;gt; text file and lets you to move these blocks from one VE to another to redistribute the memory. Also you may specify &amp;quot;additional&amp;quot; memory personally for each VE: such memory will be obtained from system's free memory or swap (it is reflected as modifying of &amp;lt;code&amp;gt;privvmpages&amp;lt;/code&amp;gt; parameter).&lt;/div&gt;</summary>
		<author><name>DmitryKoterov</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.openvz.org/index.php?title=UBC_primary_parameters&amp;diff=9345</id>
		<title>UBC primary parameters</title>
		<link rel="alternate" type="text/html" href="https://wiki.openvz.org/index.php?title=UBC_primary_parameters&amp;diff=9345"/>
		<updated>2010-11-11T23:32:36Z</updated>

		<summary type="html">&lt;p&gt;DmitryKoterov: /* vmguarpages */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{UBC toc}}&lt;br /&gt;
&lt;br /&gt;
The most important parameters determining the resources available to&lt;br /&gt;
container are explained below. The meaning of the parameters&lt;br /&gt;
is illustrated assuming that the container runs some network&lt;br /&gt;
server applications.&lt;br /&gt;
&lt;br /&gt;
== numproc ==&lt;br /&gt;
Maximum number of processes and kernel-level threads allowed for this container.&lt;br /&gt;
&lt;br /&gt;
Many server applications (like Apache Web server, FTP and mail servers)&lt;br /&gt;
spawn a process to handle each client, so the limit on number of processes&lt;br /&gt;
defines how many clients the application will be able to handle in parallel.&lt;br /&gt;
However, the number of processes doesn't limit how “heavy” the application is&lt;br /&gt;
and whether the server will be able to serve heavy requests from clients.&lt;br /&gt;
&lt;br /&gt;
Configuring resource control system, it is important to estimate both&lt;br /&gt;
the maximum number of processes and the average number of processes&lt;br /&gt;
(referred to as &amp;lt;code&amp;gt;avnumproc&amp;lt;/code&amp;gt; later). Other (dependent) resource&lt;br /&gt;
control parameters may depend both on the limit and the average number (see [[UBC consistency check]]).&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;barrier&amp;lt;/code&amp;gt; of &amp;lt;code&amp;gt;numproc&amp;lt;/code&amp;gt; doesn't provide additional control and should be set equal to the &amp;lt;code&amp;gt;limit&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
There is a restriction on the total number of processes in the system.&lt;br /&gt;
More than about 16000 processes start to cause poor responsiveness of&lt;br /&gt;
the system, worsening when the number grows. Total number of processes&lt;br /&gt;
exceeding 32000 is very likely to cause hang of the system.&lt;br /&gt;
&lt;br /&gt;
Note that in practice the number of processes is usually less. Each&lt;br /&gt;
process consumes some memory, and the available memory and the&lt;br /&gt;
&amp;quot;low memory&amp;quot; (see [[UBC systemwide configuration#“Low memory”|“Low memory”]]) limit the number of processes to lower&lt;br /&gt;
values. With typical processes, it is normal to be able to run only up&lt;br /&gt;
to 8000 processes in a system.&lt;br /&gt;
&lt;br /&gt;
== numtcpsock ==&lt;br /&gt;
Maximum number of TCP sockets.&lt;br /&gt;
&lt;br /&gt;
This parameter limits the&lt;br /&gt;
number of TCP connections and, thus, the number of clients the server&lt;br /&gt;
application can handle in parallel.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;barrier&amp;lt;/code&amp;gt; of this parameter should be set equal to the &amp;lt;code&amp;gt;limit&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
If each container has it's own set of IP addresses (which is&lt;br /&gt;
the only way a OpenVZ system can be configured), there are no direct&lt;br /&gt;
limits on the total number of TCP sockets in the system. The number&lt;br /&gt;
of sockets needs to be controlled because each socket needs certain&lt;br /&gt;
amount of memory for receive and transmit buffers (see descriptions&lt;br /&gt;
of &amp;lt;code&amp;gt;[[tcpsndbuf]]&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;[[tcprcvbuf]]&amp;lt;/code&amp;gt;), and&lt;br /&gt;
the memory is a limited resource.&lt;br /&gt;
&lt;br /&gt;
== numothersock ==&lt;br /&gt;
Maximum number of non-TCP sockets (local sockets, UDP and other types of sockets).&lt;br /&gt;
&lt;br /&gt;
Local (UNIX-domain) sockets are used for communications inside the&lt;br /&gt;
system. Multi-tier applications (for example, a Web application with a&lt;br /&gt;
database server as a back-end) may need one or multiple local sockets&lt;br /&gt;
to serve each client. Straightforward applications (for example, most&lt;br /&gt;
mail servers, with the exception of Postfix) do not use local sockets.&lt;br /&gt;
&lt;br /&gt;
UDP sockets are used for Domain Name Service (DNS) queries, but&lt;br /&gt;
the number of such sockets opened simultaneously is low. UDP and&lt;br /&gt;
other sockets may also be used in some very special applications&lt;br /&gt;
(SNMP agents and others).&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;barrier&amp;lt;/code&amp;gt; of this parameter should be set equal to the &amp;lt;code&amp;gt;limit&amp;lt;/code&amp;gt;.&lt;br /&gt;
The number of local sockets in a system is not limited. The number of&lt;br /&gt;
UDP sockets in a system, similarly to TCP sockets, is not limited in&lt;br /&gt;
OpenVZ systems.&lt;br /&gt;
&lt;br /&gt;
Similarly to &amp;lt;code&amp;gt;numtcpsock&amp;lt;/code&amp;gt; parameter discussed above, the number of&lt;br /&gt;
non-TCP sockets needs to be controlled because each socket needs certain&lt;br /&gt;
amount of memory for its buffers, and the memory is a limited&lt;br /&gt;
resource.&lt;br /&gt;
&lt;br /&gt;
== vmguarpages ==&lt;br /&gt;
Memory allocation guarantee.&lt;br /&gt;
&lt;br /&gt;
This parameter controls how much memory is available to the Virtual&lt;br /&gt;
Environment (i.e. how much memory its applications can allocate by&lt;br /&gt;
&amp;lt;code&amp;gt;malloc(3)&amp;lt;/code&amp;gt; or other standard Linux memory allocation mechanisms).&lt;br /&gt;
The more clients are served or the more “heavy” the application is, the&lt;br /&gt;
more memory it needs.&lt;br /&gt;
&lt;br /&gt;
The amount of memory that container's applications are&lt;br /&gt;
guaranteed to be able to allocate is specified as the &amp;lt;code&amp;gt;barrier&amp;lt;/code&amp;gt; of&lt;br /&gt;
&amp;lt;code&amp;gt;vmguarpages&amp;lt;/code&amp;gt; parameter. The current amount of allocated memory space&lt;br /&gt;
is accounted into &amp;lt;code&amp;gt;privvmpages&amp;lt;/code&amp;gt; parameter, and &amp;lt;code&amp;gt;vmguarpages&amp;lt;/code&amp;gt;&lt;br /&gt;
parameter does not have its own accounting. The &amp;lt;code&amp;gt;barrier&amp;lt;/code&amp;gt; and the &amp;lt;code&amp;gt;limit&amp;lt;/code&amp;gt; of&lt;br /&gt;
&amp;lt;code&amp;gt;privvmpages&amp;lt;/code&amp;gt; parameter impose an upper limit on the memory allocations&lt;br /&gt;
(see [[privvmpages]]). The meaning of the &amp;lt;code&amp;gt;limit&amp;lt;/code&amp;gt; for the&lt;br /&gt;
&amp;lt;code&amp;gt;vmguarpages&amp;lt;/code&amp;gt; parameter is unspecified in the current version and should be set&lt;br /&gt;
to the maximal allowed value ([[MAX_ULONG]]).&lt;br /&gt;
&lt;br /&gt;
If the current amount of allocated memory space does not exceed the&lt;br /&gt;
guaranteed amount (the &amp;lt;code&amp;gt;barrier&amp;lt;/code&amp;gt; of &amp;lt;code&amp;gt;vmguarpages&amp;lt;/code&amp;gt;),&lt;br /&gt;
memory allocations of container's applications always succeed.&lt;br /&gt;
If the current amount of allocated memory space exceeds the guarantee but below&lt;br /&gt;
the &amp;lt;code&amp;gt;barrier&amp;lt;/code&amp;gt; of &amp;lt;code&amp;gt;privvmpages&amp;lt;/code&amp;gt;, allocations may or may not succeed,&lt;br /&gt;
depending on the total amount of available memory in the system.&lt;br /&gt;
&lt;br /&gt;
Starting from the &amp;lt;code&amp;gt;barrier&amp;lt;/code&amp;gt; of &amp;lt;code&amp;gt;privvmpages&amp;lt;/code&amp;gt;,&lt;br /&gt;
normal priority allocations and, starting from the &amp;lt;code&amp;gt;limit&amp;lt;/code&amp;gt;&lt;br /&gt;
of &amp;lt;code&amp;gt;privvmpages&amp;lt;/code&amp;gt;, all memory allocations&lt;br /&gt;
made by the applications fail.&lt;br /&gt;
The memory allocation guarantee (&amp;lt;code&amp;gt;vmguarpages&amp;lt;/code&amp;gt;) is a primary tool for&lt;br /&gt;
controlling the memory available to containers, because&lt;br /&gt;
it allows administrators to provide Service Level Agreements — agreements&lt;br /&gt;
guaranteeing certain quality of service, certain amount of resources&lt;br /&gt;
and general availability of the service. The unit of measurement&lt;br /&gt;
of vmguarpages values is memory pages (4KB on x86 and x86_64 processors).&lt;br /&gt;
The total memory allocation guarantees given to containers&lt;br /&gt;
are limited by the physical resources of the computer — the size of RAM&lt;br /&gt;
and the swap space — as discussed in [[UBC systemwide configuration]].&lt;br /&gt;
&lt;br /&gt;
There is a tool - &amp;lt;code&amp;gt;[http://en.dklab.ru/lib/dklab_vzmem/ vzmem]&amp;lt;/code&amp;gt; - which allows you to distribute physical memory among all VEs consistently. It shows all physical memory blocks graphically in &amp;lt;code&amp;gt;/etc/vz/conf/MEM-MAP&amp;lt;/code&amp;gt; text file and lets you to move these blocks from one VE to another to redistribute the memory. Also you may specify &amp;quot;additional&amp;quot; memory personally for each VE: such memory will be obtained from system's free memory or swap (it is reflected as modifying of &amp;lt;code&amp;gt;privvmpages&amp;lt;/code&amp;gt; parameter).&lt;/div&gt;</summary>
		<author><name>DmitryKoterov</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.openvz.org/index.php?title=UBC_primary_parameters&amp;diff=9344</id>
		<title>UBC primary parameters</title>
		<link rel="alternate" type="text/html" href="https://wiki.openvz.org/index.php?title=UBC_primary_parameters&amp;diff=9344"/>
		<updated>2010-11-11T23:31:38Z</updated>

		<summary type="html">&lt;p&gt;DmitryKoterov: /* vmguarpages */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{UBC toc}}&lt;br /&gt;
&lt;br /&gt;
The most important parameters determining the resources available to&lt;br /&gt;
container are explained below. The meaning of the parameters&lt;br /&gt;
is illustrated assuming that the container runs some network&lt;br /&gt;
server applications.&lt;br /&gt;
&lt;br /&gt;
== numproc ==&lt;br /&gt;
Maximum number of processes and kernel-level threads allowed for this container.&lt;br /&gt;
&lt;br /&gt;
Many server applications (like Apache Web server, FTP and mail servers)&lt;br /&gt;
spawn a process to handle each client, so the limit on number of processes&lt;br /&gt;
defines how many clients the application will be able to handle in parallel.&lt;br /&gt;
However, the number of processes doesn't limit how “heavy” the application is&lt;br /&gt;
and whether the server will be able to serve heavy requests from clients.&lt;br /&gt;
&lt;br /&gt;
Configuring resource control system, it is important to estimate both&lt;br /&gt;
the maximum number of processes and the average number of processes&lt;br /&gt;
(referred to as &amp;lt;code&amp;gt;avnumproc&amp;lt;/code&amp;gt; later). Other (dependent) resource&lt;br /&gt;
control parameters may depend both on the limit and the average number (see [[UBC consistency check]]).&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;barrier&amp;lt;/code&amp;gt; of &amp;lt;code&amp;gt;numproc&amp;lt;/code&amp;gt; doesn't provide additional control and should be set equal to the &amp;lt;code&amp;gt;limit&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
There is a restriction on the total number of processes in the system.&lt;br /&gt;
More than about 16000 processes start to cause poor responsiveness of&lt;br /&gt;
the system, worsening when the number grows. Total number of processes&lt;br /&gt;
exceeding 32000 is very likely to cause hang of the system.&lt;br /&gt;
&lt;br /&gt;
Note that in practice the number of processes is usually less. Each&lt;br /&gt;
process consumes some memory, and the available memory and the&lt;br /&gt;
&amp;quot;low memory&amp;quot; (see [[UBC systemwide configuration#“Low memory”|“Low memory”]]) limit the number of processes to lower&lt;br /&gt;
values. With typical processes, it is normal to be able to run only up&lt;br /&gt;
to 8000 processes in a system.&lt;br /&gt;
&lt;br /&gt;
== numtcpsock ==&lt;br /&gt;
Maximum number of TCP sockets.&lt;br /&gt;
&lt;br /&gt;
This parameter limits the&lt;br /&gt;
number of TCP connections and, thus, the number of clients the server&lt;br /&gt;
application can handle in parallel.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;barrier&amp;lt;/code&amp;gt; of this parameter should be set equal to the &amp;lt;code&amp;gt;limit&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
If each container has it's own set of IP addresses (which is&lt;br /&gt;
the only way a OpenVZ system can be configured), there are no direct&lt;br /&gt;
limits on the total number of TCP sockets in the system. The number&lt;br /&gt;
of sockets needs to be controlled because each socket needs certain&lt;br /&gt;
amount of memory for receive and transmit buffers (see descriptions&lt;br /&gt;
of &amp;lt;code&amp;gt;[[tcpsndbuf]]&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;[[tcprcvbuf]]&amp;lt;/code&amp;gt;), and&lt;br /&gt;
the memory is a limited resource.&lt;br /&gt;
&lt;br /&gt;
== numothersock ==&lt;br /&gt;
Maximum number of non-TCP sockets (local sockets, UDP and other types of sockets).&lt;br /&gt;
&lt;br /&gt;
Local (UNIX-domain) sockets are used for communications inside the&lt;br /&gt;
system. Multi-tier applications (for example, a Web application with a&lt;br /&gt;
database server as a back-end) may need one or multiple local sockets&lt;br /&gt;
to serve each client. Straightforward applications (for example, most&lt;br /&gt;
mail servers, with the exception of Postfix) do not use local sockets.&lt;br /&gt;
&lt;br /&gt;
UDP sockets are used for Domain Name Service (DNS) queries, but&lt;br /&gt;
the number of such sockets opened simultaneously is low. UDP and&lt;br /&gt;
other sockets may also be used in some very special applications&lt;br /&gt;
(SNMP agents and others).&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;barrier&amp;lt;/code&amp;gt; of this parameter should be set equal to the &amp;lt;code&amp;gt;limit&amp;lt;/code&amp;gt;.&lt;br /&gt;
The number of local sockets in a system is not limited. The number of&lt;br /&gt;
UDP sockets in a system, similarly to TCP sockets, is not limited in&lt;br /&gt;
OpenVZ systems.&lt;br /&gt;
&lt;br /&gt;
Similarly to &amp;lt;code&amp;gt;numtcpsock&amp;lt;/code&amp;gt; parameter discussed above, the number of&lt;br /&gt;
non-TCP sockets needs to be controlled because each socket needs certain&lt;br /&gt;
amount of memory for its buffers, and the memory is a limited&lt;br /&gt;
resource.&lt;br /&gt;
&lt;br /&gt;
== vmguarpages ==&lt;br /&gt;
Memory allocation guarantee.&lt;br /&gt;
&lt;br /&gt;
This parameter controls how much memory is available to the Virtual&lt;br /&gt;
Environment (i.e. how much memory its applications can allocate by&lt;br /&gt;
&amp;lt;code&amp;gt;malloc(3)&amp;lt;/code&amp;gt; or other standard Linux memory allocation mechanisms).&lt;br /&gt;
The more clients are served or the more “heavy” the application is, the&lt;br /&gt;
more memory it needs.&lt;br /&gt;
&lt;br /&gt;
The amount of memory that container's applications are&lt;br /&gt;
guaranteed to be able to allocate is specified as the &amp;lt;code&amp;gt;barrier&amp;lt;/code&amp;gt; of&lt;br /&gt;
&amp;lt;code&amp;gt;vmguarpages&amp;lt;/code&amp;gt; parameter. The current amount of allocated memory space&lt;br /&gt;
is accounted into &amp;lt;code&amp;gt;privvmpages&amp;lt;/code&amp;gt; parameter, and &amp;lt;code&amp;gt;vmguarpages&amp;lt;/code&amp;gt;&lt;br /&gt;
parameter does not have its own accounting. The &amp;lt;code&amp;gt;barrier&amp;lt;/code&amp;gt; and the &amp;lt;code&amp;gt;limit&amp;lt;/code&amp;gt; of&lt;br /&gt;
&amp;lt;code&amp;gt;privvmpages&amp;lt;/code&amp;gt; parameter impose an upper limit on the memory allocations&lt;br /&gt;
(see [[privvmpages]]). The meaning of the &amp;lt;code&amp;gt;limit&amp;lt;/code&amp;gt; for the&lt;br /&gt;
&amp;lt;code&amp;gt;vmguarpages&amp;lt;/code&amp;gt; parameter is unspecified in the current version and should be set&lt;br /&gt;
to the maximal allowed value ([[MAX_ULONG]]).&lt;br /&gt;
&lt;br /&gt;
If the current amount of allocated memory space does not exceed the&lt;br /&gt;
guaranteed amount (the &amp;lt;code&amp;gt;barrier&amp;lt;/code&amp;gt; of &amp;lt;code&amp;gt;vmguarpages&amp;lt;/code&amp;gt;),&lt;br /&gt;
memory allocations of container's applications always succeed.&lt;br /&gt;
If the current amount of allocated memory space exceeds the guarantee but below&lt;br /&gt;
the &amp;lt;code&amp;gt;barrier&amp;lt;/code&amp;gt; of &amp;lt;code&amp;gt;privvmpages&amp;lt;/code&amp;gt;, allocations may or may not succeed,&lt;br /&gt;
depending on the total amount of available memory in the system.&lt;br /&gt;
&lt;br /&gt;
Starting from the &amp;lt;code&amp;gt;barrier&amp;lt;/code&amp;gt; of &amp;lt;code&amp;gt;privvmpages&amp;lt;/code&amp;gt;,&lt;br /&gt;
normal priority allocations and, starting from the &amp;lt;code&amp;gt;limit&amp;lt;/code&amp;gt;&lt;br /&gt;
of &amp;lt;code&amp;gt;privvmpages&amp;lt;/code&amp;gt;, all memory allocations&lt;br /&gt;
made by the applications fail.&lt;br /&gt;
The memory allocation guarantee (&amp;lt;code&amp;gt;vmguarpages&amp;lt;/code&amp;gt;) is a primary tool for&lt;br /&gt;
controlling the memory available to containers, because&lt;br /&gt;
it allows administrators to provide Service Level Agreements — agreements&lt;br /&gt;
guaranteeing certain quality of service, certain amount of resources&lt;br /&gt;
and general availability of the service. The unit of measurement&lt;br /&gt;
of vmguarpages values is memory pages (4KB on x86 and x86_64 processors).&lt;br /&gt;
The total memory allocation guarantees given to containers&lt;br /&gt;
are limited by the physical resources of the computer — the size of RAM&lt;br /&gt;
and the swap space — as discussed in [[UBC systemwide configuration]].&lt;br /&gt;
&lt;br /&gt;
There is a tool - &amp;lt;code&amp;gt;[http://en.dklab.ru/lib/dklab_vzmem/ vzmem]&amp;lt;/code&amp;gt; - which allows you to distribute physical memory among all VEs consistently. It shows all the physical memory blocks graphically in /etc/vz/conf/MEM-MAP text file and lets you to move these blocks from one VE to another to redistribute the memory. Also you may specify &amp;quot;additional&amp;quot; memory personally for each VE: such memory will be obtained from system's free memory or swap (it is reflected as modifying of &amp;lt;code&amp;gt;privvmpages&amp;lt;/code&amp;gt; parameter).&lt;/div&gt;</summary>
		<author><name>DmitryKoterov</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.openvz.org/index.php?title=Setting_up_an_iptables_firewall&amp;diff=9343</id>
		<title>Setting up an iptables firewall</title>
		<link rel="alternate" type="text/html" href="https://wiki.openvz.org/index.php?title=Setting_up_an_iptables_firewall&amp;diff=9343"/>
		<updated>2010-11-11T23:08:21Z</updated>

		<summary type="html">&lt;p&gt;DmitryKoterov: /* Simple firewall configuration independent to IP addresses: vzfirewall */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This document consists of two parts. The first is setting up a firewall (using iptables) on the HN, which will restrict traffic to the containers. The effect would emulate, as far as the containers and their customers are concerned, an external hardware firewall controlled by the sysadmin. The second is setting up a firewall that protects the [[HN]] itself but still allows traffic to the containers, thus allowing individual containers to define their own iptables.&lt;br /&gt;
&lt;br /&gt;
While the firewalls shown here can be accomplished using iptables manually (or using Fedora core's iptables service), the methods presented here are especially modular and easy to modify. This is important when you have 20+ containers and a lot of other things to be doing...&lt;br /&gt;
&lt;br /&gt;
The scripts and pathnames given here are for Fedora Core 6, though they can probably be applied to most similar SysV-like systems with little modification.&lt;br /&gt;
&lt;br /&gt;
== A little background ==&lt;br /&gt;
&lt;br /&gt;
On our systems, we use the HN to provide privileged services which are not appropriate for access by the containers. For example, the HN acts as a backup server, runs Nagios for health monitoring, has a webserver for managing the 3ware RAID controller, etc. The containers are leased to customers, who can't entirely be trusted, especially if they get hacked. As such, our scenario is one in which the HN must be protected from all access (even from the containers) except for a few trusted hosts (e.g. my home-office).&lt;br /&gt;
&lt;br /&gt;
The exception to this is the nameserver, which we want open to the world. We use it as a caching nameserver for our containers and also to host DNS for a few customer domain.&lt;br /&gt;
&lt;br /&gt;
== Simple firewall configuration independent to IP addresses: vzfirewall ==&lt;br /&gt;
&lt;br /&gt;
''Vzfirewall'' tool allows you to open/close ports for incoming connections with no dependencies to foreign IP addresses. E.g. you may allow a hostname ''release.prod.example.com'' to connect to port 5432 of VE 1234 and leave all other ports closed by modifying 1234.conf file adding multiline ''FIREWALL'' directive into it:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
...&lt;br /&gt;
PRIVVMPAGES=&amp;quot;300000:300000&amp;quot;&lt;br /&gt;
HOSTNAME=&amp;quot;example.com&amp;quot;&lt;br /&gt;
...&lt;br /&gt;
FIREWALL=&amp;quot;&lt;br /&gt;
    ...&lt;br /&gt;
    # Allow access to PostgreSQL port only from release.prod &lt;br /&gt;
    # and release.test machines. You may use domain names here.&lt;br /&gt;
    [5432]&lt;br /&gt;
    release.prod.example.com&lt;br /&gt;
    release.test.example.com&lt;br /&gt;
    ...&lt;br /&gt;
&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You must then run ''vzfirewall -a'' on your hardware node to apply changes made in *.conf.&lt;br /&gt;
&lt;br /&gt;
Note that it is recommended to use hostnames instead of IP addresses here, so the configuration is persistent for VE movements to different IP-address: you just need to run ''vzfirewall -a'' again after movement. It is also reboot-safe, because applied to /etc/sysconfig/iptables (at RHEL systems).&lt;br /&gt;
&lt;br /&gt;
Vzfirewall and its documentation are available at [http://en.dklab.ru/lib/dklab_vzfirewall/ http://en.dklab.ru/lib/dklab_vzfirewall/].&lt;br /&gt;
&lt;br /&gt;
== An alternative from the author of Shorewall ==&lt;br /&gt;
&lt;br /&gt;
For those who might find the solution provided in this wiki article unsatisfactory (for whatever reason), the creator of Shorewall (Tom Eastep) has written a nice article explaining how to use Shorewall on an OpenVZ host node to manage the host node, containers, and more... with quite a complex setup as an example.  The article IS NOT an introduction to Shorewall for beginners, so some pre-existing knowledge and understanding of Shorewall may be required.&lt;br /&gt;
&lt;br /&gt;
Shorewall and OpenVZ by Tom Eastep - http://www.shorewall.net/OpenVZ.html&lt;br /&gt;
&lt;br /&gt;
See also this OpenVZ Forum posting - http://forum.openvz.org/index.php?t=msg&amp;amp;goto=16406&amp;amp;&lt;br /&gt;
&lt;br /&gt;
== Setting up a HN-based firewall ==&lt;br /&gt;
&lt;br /&gt;
This setup emulates (to the containers anyway) an external hardware firewall. It protects the HN from any access and then defines what services and ports are allowed/banned for individual containers. This leaves the firewall controlled by the site administrator, not be individual containers and the hackers who've gotten into them. ;)&lt;br /&gt;
&lt;br /&gt;
First off, let's disable Fedora's existing &amp;lt;code&amp;gt;iptables&amp;lt;/code&amp;gt; service:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
service iptables stop&lt;br /&gt;
chkconfig iptables off&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now create the new &amp;lt;code&amp;gt;firewall&amp;lt;/code&amp;gt; service. This code should be &amp;lt;code&amp;gt;/etc/init.d/firewall&amp;lt;/code&amp;gt; and then should be chmod'd 755.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/bin/sh&lt;br /&gt;
# firewall      Start iptables firewall&lt;br /&gt;
# chkconfig: 2345 08 92&lt;br /&gt;
# description:  Starts, stops and saves iptables firewall&lt;br /&gt;
# This script sets up the firewall for the INPUT chain (which is for&lt;br /&gt;
# the HN itself) and then processes the config files under&lt;br /&gt;
# /etc/firewall.d to set up additional rules in the FORWARD chain&lt;br /&gt;
# to allow access to containers' services.&lt;br /&gt;
&lt;br /&gt;
. /etc/init.d/functions&lt;br /&gt;
&lt;br /&gt;
# the IP block allocated to this server&lt;br /&gt;
SEGMENT=&amp;quot;192.168.0.0/24&amp;quot;&lt;br /&gt;
# the IP used by the hosting server itself&lt;br /&gt;
THISHOST=&amp;quot;192.168.0.1&amp;quot;&lt;br /&gt;
# services that should be allowed to the HN;&lt;br /&gt;
# services for containers are configured in /etc/firewall.d/*&lt;br /&gt;
OKPORTS=&amp;quot;53&amp;quot;&lt;br /&gt;
# hosts allowed full access through the firewall,&lt;br /&gt;
# to all containers and to this server&lt;br /&gt;
DMZS=&amp;quot;12.34.56.78 90.123.45.67&amp;quot;&lt;br /&gt;
&lt;br /&gt;
purge() {&lt;br /&gt;
  echo -n &amp;quot;Firewall: Purging and allowing all traffic&amp;quot;&lt;br /&gt;
  iptables -P OUTPUT ACCEPT&lt;br /&gt;
  iptables -P FORWARD ACCEPT&lt;br /&gt;
  iptables -P INPUT ACCEPT&lt;br /&gt;
  iptables -F&lt;br /&gt;
  success ; echo&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
setup() {&lt;br /&gt;
  echo -n &amp;quot;Firewall: Setting default policies to DROP&amp;quot;&lt;br /&gt;
  iptables -P INPUT DROP&lt;br /&gt;
  iptables -P FORWARD DROP&lt;br /&gt;
  iptables -I INPUT   -j ACCEPT -m state --state ESTABLISHED,RELATED&lt;br /&gt;
  iptables -I FORWARD -j ACCEPT -m state --state ESTABLISHED,RELATED&lt;br /&gt;
  iptables -I INPUT -j ACCEPT -i lo&lt;br /&gt;
  iptables -I FORWARD -j ACCEPT --source $SEGMENT&lt;br /&gt;
  success ; echo&lt;br /&gt;
&lt;br /&gt;
  echo &amp;quot;Firewall: Allowing access to HN&amp;quot;&lt;br /&gt;
  for port in $OKPORTS ; do&lt;br /&gt;
    echo -n &amp;quot;          port $port&amp;quot;&lt;br /&gt;
    iptables -I INPUT -j ACCEPT -s $SEGMENT -d $THISHOST --protocol tcp --destination-port $port&lt;br /&gt;
    iptables -I INPUT -j ACCEPT -s $SEGMENT -d $THISHOST --protocol udp --destination-port $port&lt;br /&gt;
    success ; echo&lt;br /&gt;
  done&lt;br /&gt;
  for ip in $DMZS ; do&lt;br /&gt;
    echo -n &amp;quot;          DMZ $ip&amp;quot;&lt;br /&gt;
    iptables -I INPUT   -i eth0 -j ACCEPT -s $ip&lt;br /&gt;
    iptables -I FORWARD -i eth0 -j ACCEPT -s $ip&lt;br /&gt;
    success ; echo&lt;br /&gt;
  done&lt;br /&gt;
&lt;br /&gt;
  CTSETUPS=`echo /etc/firewall.d/*`&lt;br /&gt;
  if [ &amp;quot;$CTSETUPS&amp;quot; != &amp;quot;/etc/firewall.d/*&amp;quot; ] ; then&lt;br /&gt;
  echo &amp;quot;Firewall: Setting up container firewalls&amp;quot;&lt;br /&gt;
  for i in $CTSETUPS ; do&lt;br /&gt;
    . $i&lt;br /&gt;
    echo -n &amp;quot;          $CTNAME CT$CTID&amp;quot;&lt;br /&gt;
    if [ -n &amp;quot;$BANNED&amp;quot; ]; then&lt;br /&gt;
      for source in $BANNED ;  do iptables -I FORWARD -j DROP --destination $CTIP --source $source ; done&lt;br /&gt;
    fi&lt;br /&gt;
    if [ -n &amp;quot;$OPENPORTS&amp;quot; ]; then&lt;br /&gt;
      for port in $OPENPORTS ; do iptables -I FORWARD -j ACCEPT --protocol tcp --destination $CTIP --destination-port $port ; done&lt;br /&gt;
      for port in $OPENPORTS ; do iptables -I FORWARD -j ACCEPT --protocol udp --destination $CTIP --destination-port $port ; done&lt;br /&gt;
    fi&lt;br /&gt;
    if [ -n &amp;quot;$DMZS&amp;quot; ]; then&lt;br /&gt;
      for source in $DMZS ; do iptables -I FORWARD -j ACCEPT --protocol tcp --destination $CTIP --source $source ; done&lt;br /&gt;
      for source in $DMZS ; do iptables -I FORWARD -j ACCEPT --protocol udp --destination $CTIP --source $source ; done&lt;br /&gt;
    fi&lt;br /&gt;
    [ $? -eq 0 ] &amp;amp;&amp;amp; success || failure&lt;br /&gt;
    echo&lt;br /&gt;
  done&lt;br /&gt;
  fi&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
case &amp;quot;$1&amp;quot; in&lt;br /&gt;
  start)&lt;br /&gt;
    echo &amp;quot;Starting firewall...&amp;quot;&lt;br /&gt;
    purge&lt;br /&gt;
    setup&lt;br /&gt;
    ;;&lt;br /&gt;
  stop)&lt;br /&gt;
    echo &amp;quot;Stopping firewall...&amp;quot;&lt;br /&gt;
    purge&lt;br /&gt;
    ;;&lt;br /&gt;
  restart)&lt;br /&gt;
    $0 stop&lt;br /&gt;
    $0 start&lt;br /&gt;
    ;;&lt;br /&gt;
  status)&lt;br /&gt;
    iptables -n -L&lt;br /&gt;
    ;;&lt;br /&gt;
  *)&lt;br /&gt;
    echo &amp;quot;Usage: $0 &amp;lt;start|stop|restart|status&amp;gt;&amp;quot;&lt;br /&gt;
    ;;&lt;br /&gt;
esac&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note: This will only allow access to the HN from the hosts/networks defined in SEGMENT. If you'd like to open up the OKPORTS on the HN to everybody, you can remove the ''-s $SEGMENT'' parameters from the iptables commands under the &amp;quot;Firewall: Allowing access to HN&amp;quot; section. The modified lines would look like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
iptables -I INPUT -j ACCEPT -d $THISHOST --protocol tcp --destination-port $port&lt;br /&gt;
iptables -I INPUT -j ACCEPT -d $THISHOST --protocol udp --destination-port $port&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above script can be called like this:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
service firewall start&lt;br /&gt;
service firewall stop&lt;br /&gt;
service firewall restart&lt;br /&gt;
service firewall status&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It will set up the firewall for the HN according to the parameters you specified for OKPORTS, DMZs, etc. and then it will call each file under /etc/firewall.d and process  its configuration.&lt;br /&gt;
&lt;br /&gt;
So create a file under /etc/firewall.d The exact filename isn't important, as long as it's meaningful to you, e.g. &amp;lt;code&amp;gt;ExampleCompany&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;ve12&amp;lt;/code&amp;gt; and give it content like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# This file is processed by /etc/init.d/firewall&lt;br /&gt;
CTID=&amp;quot;1&amp;quot;			# the container's ID#&lt;br /&gt;
CTNAME=&amp;quot;Customer1&amp;quot;		# A human-friendly label for the container&lt;br /&gt;
CTIP=&amp;quot;192.168.1.34&amp;quot;		# the IP address for this container &lt;br /&gt;
OPENPORTS=&amp;quot;80 443&amp;quot;		# ports that should be universally opened&lt;br /&gt;
				# to the entire Internet&lt;br /&gt;
DMZS=&amp;quot;1.2.3.0/24 5.6.7.8/32&amp;quot;	# IPs and blocks that should have full access&lt;br /&gt;
				# to the container's services&lt;br /&gt;
BANNED=&amp;quot;&amp;quot;			# IPs and blocks that should be entirely&lt;br /&gt;
				# blocked from the container's services&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And there you go. Go ahead and start the firewall and check its status:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
service firewall restart&lt;br /&gt;
service firewall status&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As you can see, you can now add and edit the configurations for individual containers very easily. This method proves a lot easier to manage than Fedora's iptables-config mechamism!&lt;br /&gt;
&lt;br /&gt;
To make the firewall service automatically start when the HN boots, run&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
chkconfig --add firewall&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Debian Notes ===&lt;br /&gt;
&lt;br /&gt;
The setup above works fine for Debian as well, however /etc/init.d/functions is missing. Here is a very simple version that you can use:&lt;br /&gt;
&lt;br /&gt;
  # /etc/init.d/functions&lt;br /&gt;
  &lt;br /&gt;
  success() {&lt;br /&gt;
    echo -n &amp;quot;...success&amp;quot;&lt;br /&gt;
  } &lt;br /&gt;
  failure() {&lt;br /&gt;
    echo -n &amp;quot;...failure&amp;quot;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
== Setting up a firewall that allows per-container configuration ==&lt;br /&gt;
&lt;br /&gt;
This setup configures iptables on the HN to disallow access to all hosts, including the containers. However, it allows all traffic into the containers so they may define their own iptables rules and therefore manage their own firewall.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
iptables -P FORWARD ACCEPT&lt;br /&gt;
iptables -F FORWARD&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This will remove all rules for the FORWARD chain so all packets can pass back and forth between containers and the outside world.&lt;br /&gt;
&lt;br /&gt;
If you want to use a firewall inside a container, please load these modules BEFORE starting the container:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
modprobe xt_tcpudp&lt;br /&gt;
modprobe ip_conntrack&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you do not, you will get an error like this: &amp;quot;iptables: No chain/target/match by that name&amp;quot;&lt;br /&gt;
&lt;br /&gt;
If you want to use stateful firewall rules (and you should!) you will also need to make sure that 'ipt_state' is in the 'IPTABLES' option in your vz.conf file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
IPTABLES=&amp;quot;ipt_REJECT ipt_tos ipt_limit ipt_multiport iptable_filter iptable_mangle ipt_TCPMSS ipt_tcpmss ipt_ttl ipt_length ipt_state&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Also make sure the 'xt_state' module is loaded on the host:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
modprobe xt_state&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
&lt;br /&gt;
* [[Traffic accounting with iptables]]&lt;br /&gt;
&lt;br /&gt;
[[Category: Networking]]&lt;/div&gt;</summary>
		<author><name>DmitryKoterov</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.openvz.org/index.php?title=Setting_up_an_iptables_firewall&amp;diff=9342</id>
		<title>Setting up an iptables firewall</title>
		<link rel="alternate" type="text/html" href="https://wiki.openvz.org/index.php?title=Setting_up_an_iptables_firewall&amp;diff=9342"/>
		<updated>2010-11-11T23:06:56Z</updated>

		<summary type="html">&lt;p&gt;DmitryKoterov: /* Simple firewall configuration independent to IP addresses: vzfirewall */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This document consists of two parts. The first is setting up a firewall (using iptables) on the HN, which will restrict traffic to the containers. The effect would emulate, as far as the containers and their customers are concerned, an external hardware firewall controlled by the sysadmin. The second is setting up a firewall that protects the [[HN]] itself but still allows traffic to the containers, thus allowing individual containers to define their own iptables.&lt;br /&gt;
&lt;br /&gt;
While the firewalls shown here can be accomplished using iptables manually (or using Fedora core's iptables service), the methods presented here are especially modular and easy to modify. This is important when you have 20+ containers and a lot of other things to be doing...&lt;br /&gt;
&lt;br /&gt;
The scripts and pathnames given here are for Fedora Core 6, though they can probably be applied to most similar SysV-like systems with little modification.&lt;br /&gt;
&lt;br /&gt;
== A little background ==&lt;br /&gt;
&lt;br /&gt;
On our systems, we use the HN to provide privileged services which are not appropriate for access by the containers. For example, the HN acts as a backup server, runs Nagios for health monitoring, has a webserver for managing the 3ware RAID controller, etc. The containers are leased to customers, who can't entirely be trusted, especially if they get hacked. As such, our scenario is one in which the HN must be protected from all access (even from the containers) except for a few trusted hosts (e.g. my home-office).&lt;br /&gt;
&lt;br /&gt;
The exception to this is the nameserver, which we want open to the world. We use it as a caching nameserver for our containers and also to host DNS for a few customer domain.&lt;br /&gt;
&lt;br /&gt;
== Simple firewall configuration independent to IP addresses: vzfirewall ==&lt;br /&gt;
&lt;br /&gt;
''Vzfirewall'' tool allows you to open/close ports for incoming connections with no dependencies to foreign IP addresses. E.g. you may allow a hostname ''release.prod.example.com'' to connect to port 5432 of VE 1234 and leave all other ports closed by modifying 1234.conf file adding multiline ''FIREWALL'' directive into it:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
...&lt;br /&gt;
PRIVVMPAGES=&amp;quot;300000:300000&amp;quot;&lt;br /&gt;
HOSTNAME=&amp;quot;example.com&amp;quot;&lt;br /&gt;
...&lt;br /&gt;
FIREWALL=&amp;quot;&lt;br /&gt;
    ...&lt;br /&gt;
    # Allow access to PostgreSQL port only from release.prod &lt;br /&gt;
    # and release.test machines. You may use domain names here.&lt;br /&gt;
    [5432]&lt;br /&gt;
    release.prod.example.com&lt;br /&gt;
    release.test.example.com&lt;br /&gt;
    ...&lt;br /&gt;
&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You must then run ''vzfirewall -a'' on your hardware node to apply changes made in *.conf.&lt;br /&gt;
&lt;br /&gt;
Note that it is recommended to use hostnames instead of IP addresses here, so the configuration is persistent fore VE movements to different IP-address.&lt;br /&gt;
&lt;br /&gt;
Vzfirewall and its documentation are available at [http://en.dklab.ru/lib/dklab_vzfirewall/ http://en.dklab.ru/lib/dklab_vzfirewall/].&lt;br /&gt;
&lt;br /&gt;
== An alternative from the author of Shorewall ==&lt;br /&gt;
&lt;br /&gt;
For those who might find the solution provided in this wiki article unsatisfactory (for whatever reason), the creator of Shorewall (Tom Eastep) has written a nice article explaining how to use Shorewall on an OpenVZ host node to manage the host node, containers, and more... with quite a complex setup as an example.  The article IS NOT an introduction to Shorewall for beginners, so some pre-existing knowledge and understanding of Shorewall may be required.&lt;br /&gt;
&lt;br /&gt;
Shorewall and OpenVZ by Tom Eastep - http://www.shorewall.net/OpenVZ.html&lt;br /&gt;
&lt;br /&gt;
See also this OpenVZ Forum posting - http://forum.openvz.org/index.php?t=msg&amp;amp;goto=16406&amp;amp;&lt;br /&gt;
&lt;br /&gt;
== Setting up a HN-based firewall ==&lt;br /&gt;
&lt;br /&gt;
This setup emulates (to the containers anyway) an external hardware firewall. It protects the HN from any access and then defines what services and ports are allowed/banned for individual containers. This leaves the firewall controlled by the site administrator, not be individual containers and the hackers who've gotten into them. ;)&lt;br /&gt;
&lt;br /&gt;
First off, let's disable Fedora's existing &amp;lt;code&amp;gt;iptables&amp;lt;/code&amp;gt; service:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
service iptables stop&lt;br /&gt;
chkconfig iptables off&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now create the new &amp;lt;code&amp;gt;firewall&amp;lt;/code&amp;gt; service. This code should be &amp;lt;code&amp;gt;/etc/init.d/firewall&amp;lt;/code&amp;gt; and then should be chmod'd 755.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/bin/sh&lt;br /&gt;
# firewall      Start iptables firewall&lt;br /&gt;
# chkconfig: 2345 08 92&lt;br /&gt;
# description:  Starts, stops and saves iptables firewall&lt;br /&gt;
# This script sets up the firewall for the INPUT chain (which is for&lt;br /&gt;
# the HN itself) and then processes the config files under&lt;br /&gt;
# /etc/firewall.d to set up additional rules in the FORWARD chain&lt;br /&gt;
# to allow access to containers' services.&lt;br /&gt;
&lt;br /&gt;
. /etc/init.d/functions&lt;br /&gt;
&lt;br /&gt;
# the IP block allocated to this server&lt;br /&gt;
SEGMENT=&amp;quot;192.168.0.0/24&amp;quot;&lt;br /&gt;
# the IP used by the hosting server itself&lt;br /&gt;
THISHOST=&amp;quot;192.168.0.1&amp;quot;&lt;br /&gt;
# services that should be allowed to the HN;&lt;br /&gt;
# services for containers are configured in /etc/firewall.d/*&lt;br /&gt;
OKPORTS=&amp;quot;53&amp;quot;&lt;br /&gt;
# hosts allowed full access through the firewall,&lt;br /&gt;
# to all containers and to this server&lt;br /&gt;
DMZS=&amp;quot;12.34.56.78 90.123.45.67&amp;quot;&lt;br /&gt;
&lt;br /&gt;
purge() {&lt;br /&gt;
  echo -n &amp;quot;Firewall: Purging and allowing all traffic&amp;quot;&lt;br /&gt;
  iptables -P OUTPUT ACCEPT&lt;br /&gt;
  iptables -P FORWARD ACCEPT&lt;br /&gt;
  iptables -P INPUT ACCEPT&lt;br /&gt;
  iptables -F&lt;br /&gt;
  success ; echo&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
setup() {&lt;br /&gt;
  echo -n &amp;quot;Firewall: Setting default policies to DROP&amp;quot;&lt;br /&gt;
  iptables -P INPUT DROP&lt;br /&gt;
  iptables -P FORWARD DROP&lt;br /&gt;
  iptables -I INPUT   -j ACCEPT -m state --state ESTABLISHED,RELATED&lt;br /&gt;
  iptables -I FORWARD -j ACCEPT -m state --state ESTABLISHED,RELATED&lt;br /&gt;
  iptables -I INPUT -j ACCEPT -i lo&lt;br /&gt;
  iptables -I FORWARD -j ACCEPT --source $SEGMENT&lt;br /&gt;
  success ; echo&lt;br /&gt;
&lt;br /&gt;
  echo &amp;quot;Firewall: Allowing access to HN&amp;quot;&lt;br /&gt;
  for port in $OKPORTS ; do&lt;br /&gt;
    echo -n &amp;quot;          port $port&amp;quot;&lt;br /&gt;
    iptables -I INPUT -j ACCEPT -s $SEGMENT -d $THISHOST --protocol tcp --destination-port $port&lt;br /&gt;
    iptables -I INPUT -j ACCEPT -s $SEGMENT -d $THISHOST --protocol udp --destination-port $port&lt;br /&gt;
    success ; echo&lt;br /&gt;
  done&lt;br /&gt;
  for ip in $DMZS ; do&lt;br /&gt;
    echo -n &amp;quot;          DMZ $ip&amp;quot;&lt;br /&gt;
    iptables -I INPUT   -i eth0 -j ACCEPT -s $ip&lt;br /&gt;
    iptables -I FORWARD -i eth0 -j ACCEPT -s $ip&lt;br /&gt;
    success ; echo&lt;br /&gt;
  done&lt;br /&gt;
&lt;br /&gt;
  CTSETUPS=`echo /etc/firewall.d/*`&lt;br /&gt;
  if [ &amp;quot;$CTSETUPS&amp;quot; != &amp;quot;/etc/firewall.d/*&amp;quot; ] ; then&lt;br /&gt;
  echo &amp;quot;Firewall: Setting up container firewalls&amp;quot;&lt;br /&gt;
  for i in $CTSETUPS ; do&lt;br /&gt;
    . $i&lt;br /&gt;
    echo -n &amp;quot;          $CTNAME CT$CTID&amp;quot;&lt;br /&gt;
    if [ -n &amp;quot;$BANNED&amp;quot; ]; then&lt;br /&gt;
      for source in $BANNED ;  do iptables -I FORWARD -j DROP --destination $CTIP --source $source ; done&lt;br /&gt;
    fi&lt;br /&gt;
    if [ -n &amp;quot;$OPENPORTS&amp;quot; ]; then&lt;br /&gt;
      for port in $OPENPORTS ; do iptables -I FORWARD -j ACCEPT --protocol tcp --destination $CTIP --destination-port $port ; done&lt;br /&gt;
      for port in $OPENPORTS ; do iptables -I FORWARD -j ACCEPT --protocol udp --destination $CTIP --destination-port $port ; done&lt;br /&gt;
    fi&lt;br /&gt;
    if [ -n &amp;quot;$DMZS&amp;quot; ]; then&lt;br /&gt;
      for source in $DMZS ; do iptables -I FORWARD -j ACCEPT --protocol tcp --destination $CTIP --source $source ; done&lt;br /&gt;
      for source in $DMZS ; do iptables -I FORWARD -j ACCEPT --protocol udp --destination $CTIP --source $source ; done&lt;br /&gt;
    fi&lt;br /&gt;
    [ $? -eq 0 ] &amp;amp;&amp;amp; success || failure&lt;br /&gt;
    echo&lt;br /&gt;
  done&lt;br /&gt;
  fi&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
case &amp;quot;$1&amp;quot; in&lt;br /&gt;
  start)&lt;br /&gt;
    echo &amp;quot;Starting firewall...&amp;quot;&lt;br /&gt;
    purge&lt;br /&gt;
    setup&lt;br /&gt;
    ;;&lt;br /&gt;
  stop)&lt;br /&gt;
    echo &amp;quot;Stopping firewall...&amp;quot;&lt;br /&gt;
    purge&lt;br /&gt;
    ;;&lt;br /&gt;
  restart)&lt;br /&gt;
    $0 stop&lt;br /&gt;
    $0 start&lt;br /&gt;
    ;;&lt;br /&gt;
  status)&lt;br /&gt;
    iptables -n -L&lt;br /&gt;
    ;;&lt;br /&gt;
  *)&lt;br /&gt;
    echo &amp;quot;Usage: $0 &amp;lt;start|stop|restart|status&amp;gt;&amp;quot;&lt;br /&gt;
    ;;&lt;br /&gt;
esac&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note: This will only allow access to the HN from the hosts/networks defined in SEGMENT. If you'd like to open up the OKPORTS on the HN to everybody, you can remove the ''-s $SEGMENT'' parameters from the iptables commands under the &amp;quot;Firewall: Allowing access to HN&amp;quot; section. The modified lines would look like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
iptables -I INPUT -j ACCEPT -d $THISHOST --protocol tcp --destination-port $port&lt;br /&gt;
iptables -I INPUT -j ACCEPT -d $THISHOST --protocol udp --destination-port $port&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above script can be called like this:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
service firewall start&lt;br /&gt;
service firewall stop&lt;br /&gt;
service firewall restart&lt;br /&gt;
service firewall status&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It will set up the firewall for the HN according to the parameters you specified for OKPORTS, DMZs, etc. and then it will call each file under /etc/firewall.d and process  its configuration.&lt;br /&gt;
&lt;br /&gt;
So create a file under /etc/firewall.d The exact filename isn't important, as long as it's meaningful to you, e.g. &amp;lt;code&amp;gt;ExampleCompany&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;ve12&amp;lt;/code&amp;gt; and give it content like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# This file is processed by /etc/init.d/firewall&lt;br /&gt;
CTID=&amp;quot;1&amp;quot;			# the container's ID#&lt;br /&gt;
CTNAME=&amp;quot;Customer1&amp;quot;		# A human-friendly label for the container&lt;br /&gt;
CTIP=&amp;quot;192.168.1.34&amp;quot;		# the IP address for this container &lt;br /&gt;
OPENPORTS=&amp;quot;80 443&amp;quot;		# ports that should be universally opened&lt;br /&gt;
				# to the entire Internet&lt;br /&gt;
DMZS=&amp;quot;1.2.3.0/24 5.6.7.8/32&amp;quot;	# IPs and blocks that should have full access&lt;br /&gt;
				# to the container's services&lt;br /&gt;
BANNED=&amp;quot;&amp;quot;			# IPs and blocks that should be entirely&lt;br /&gt;
				# blocked from the container's services&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And there you go. Go ahead and start the firewall and check its status:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
service firewall restart&lt;br /&gt;
service firewall status&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As you can see, you can now add and edit the configurations for individual containers very easily. This method proves a lot easier to manage than Fedora's iptables-config mechamism!&lt;br /&gt;
&lt;br /&gt;
To make the firewall service automatically start when the HN boots, run&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
chkconfig --add firewall&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Debian Notes ===&lt;br /&gt;
&lt;br /&gt;
The setup above works fine for Debian as well, however /etc/init.d/functions is missing. Here is a very simple version that you can use:&lt;br /&gt;
&lt;br /&gt;
  # /etc/init.d/functions&lt;br /&gt;
  &lt;br /&gt;
  success() {&lt;br /&gt;
    echo -n &amp;quot;...success&amp;quot;&lt;br /&gt;
  } &lt;br /&gt;
  failure() {&lt;br /&gt;
    echo -n &amp;quot;...failure&amp;quot;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
== Setting up a firewall that allows per-container configuration ==&lt;br /&gt;
&lt;br /&gt;
This setup configures iptables on the HN to disallow access to all hosts, including the containers. However, it allows all traffic into the containers so they may define their own iptables rules and therefore manage their own firewall.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
iptables -P FORWARD ACCEPT&lt;br /&gt;
iptables -F FORWARD&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This will remove all rules for the FORWARD chain so all packets can pass back and forth between containers and the outside world.&lt;br /&gt;
&lt;br /&gt;
If you want to use a firewall inside a container, please load these modules BEFORE starting the container:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
modprobe xt_tcpudp&lt;br /&gt;
modprobe ip_conntrack&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you do not, you will get an error like this: &amp;quot;iptables: No chain/target/match by that name&amp;quot;&lt;br /&gt;
&lt;br /&gt;
If you want to use stateful firewall rules (and you should!) you will also need to make sure that 'ipt_state' is in the 'IPTABLES' option in your vz.conf file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
IPTABLES=&amp;quot;ipt_REJECT ipt_tos ipt_limit ipt_multiport iptable_filter iptable_mangle ipt_TCPMSS ipt_tcpmss ipt_ttl ipt_length ipt_state&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Also make sure the 'xt_state' module is loaded on the host:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
modprobe xt_state&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
&lt;br /&gt;
* [[Traffic accounting with iptables]]&lt;br /&gt;
&lt;br /&gt;
[[Category: Networking]]&lt;/div&gt;</summary>
		<author><name>DmitryKoterov</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.openvz.org/index.php?title=Setting_up_an_iptables_firewall&amp;diff=9341</id>
		<title>Setting up an iptables firewall</title>
		<link rel="alternate" type="text/html" href="https://wiki.openvz.org/index.php?title=Setting_up_an_iptables_firewall&amp;diff=9341"/>
		<updated>2010-11-11T23:05:38Z</updated>

		<summary type="html">&lt;p&gt;DmitryKoterov: /* Simple firewall configuration independent to IP addresses: vzfirewall */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This document consists of two parts. The first is setting up a firewall (using iptables) on the HN, which will restrict traffic to the containers. The effect would emulate, as far as the containers and their customers are concerned, an external hardware firewall controlled by the sysadmin. The second is setting up a firewall that protects the [[HN]] itself but still allows traffic to the containers, thus allowing individual containers to define their own iptables.&lt;br /&gt;
&lt;br /&gt;
While the firewalls shown here can be accomplished using iptables manually (or using Fedora core's iptables service), the methods presented here are especially modular and easy to modify. This is important when you have 20+ containers and a lot of other things to be doing...&lt;br /&gt;
&lt;br /&gt;
The scripts and pathnames given here are for Fedora Core 6, though they can probably be applied to most similar SysV-like systems with little modification.&lt;br /&gt;
&lt;br /&gt;
== A little background ==&lt;br /&gt;
&lt;br /&gt;
On our systems, we use the HN to provide privileged services which are not appropriate for access by the containers. For example, the HN acts as a backup server, runs Nagios for health monitoring, has a webserver for managing the 3ware RAID controller, etc. The containers are leased to customers, who can't entirely be trusted, especially if they get hacked. As such, our scenario is one in which the HN must be protected from all access (even from the containers) except for a few trusted hosts (e.g. my home-office).&lt;br /&gt;
&lt;br /&gt;
The exception to this is the nameserver, which we want open to the world. We use it as a caching nameserver for our containers and also to host DNS for a few customer domain.&lt;br /&gt;
&lt;br /&gt;
== Simple firewall configuration independent to IP addresses: vzfirewall ==&lt;br /&gt;
&lt;br /&gt;
''Vzfirewall'' tool allows you to open/close ports for incoming connections with no dependencies to foreign IP addresses. E.g. you may allow a hostname ''release.prod.example.com'' to connect to port 5432 of VE 1234 and leave all other ports closed by modifying 1234.conf file adding multiline ''FIREWALL'' directive into it:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
...&lt;br /&gt;
PRIVVMPAGES=&amp;quot;300000:300000&amp;quot;&lt;br /&gt;
HOSTNAME=&amp;quot;example.com&amp;quot;&lt;br /&gt;
...&lt;br /&gt;
FIREWALL=&amp;quot;&lt;br /&gt;
    ...&lt;br /&gt;
    # Allow access to PostgreSQL port only from release.prod machine.&lt;br /&gt;
    # Note that you may use domain names here.&lt;br /&gt;
    [5432]&lt;br /&gt;
    release.prod.example.com&lt;br /&gt;
    release.test.example.com&lt;br /&gt;
    ...&lt;br /&gt;
&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You must then run ''vzfirewall -a'' on your hardware node to apply changes made in *.conf.&lt;br /&gt;
&lt;br /&gt;
Note that it is recommended to use hostnames instead of IP addresses here, so the configuration is persistent fore VE movements to different IP-address.&lt;br /&gt;
&lt;br /&gt;
Vzfirewall and its documentation are available at [http://en.dklab.ru/lib/dklab_vzfirewall/ http://en.dklab.ru/lib/dklab_vzfirewall/].&lt;br /&gt;
&lt;br /&gt;
== An alternative from the author of Shorewall ==&lt;br /&gt;
&lt;br /&gt;
For those who might find the solution provided in this wiki article unsatisfactory (for whatever reason), the creator of Shorewall (Tom Eastep) has written a nice article explaining how to use Shorewall on an OpenVZ host node to manage the host node, containers, and more... with quite a complex setup as an example.  The article IS NOT an introduction to Shorewall for beginners, so some pre-existing knowledge and understanding of Shorewall may be required.&lt;br /&gt;
&lt;br /&gt;
Shorewall and OpenVZ by Tom Eastep - http://www.shorewall.net/OpenVZ.html&lt;br /&gt;
&lt;br /&gt;
See also this OpenVZ Forum posting - http://forum.openvz.org/index.php?t=msg&amp;amp;goto=16406&amp;amp;&lt;br /&gt;
&lt;br /&gt;
== Setting up a HN-based firewall ==&lt;br /&gt;
&lt;br /&gt;
This setup emulates (to the containers anyway) an external hardware firewall. It protects the HN from any access and then defines what services and ports are allowed/banned for individual containers. This leaves the firewall controlled by the site administrator, not be individual containers and the hackers who've gotten into them. ;)&lt;br /&gt;
&lt;br /&gt;
First off, let's disable Fedora's existing &amp;lt;code&amp;gt;iptables&amp;lt;/code&amp;gt; service:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
service iptables stop&lt;br /&gt;
chkconfig iptables off&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now create the new &amp;lt;code&amp;gt;firewall&amp;lt;/code&amp;gt; service. This code should be &amp;lt;code&amp;gt;/etc/init.d/firewall&amp;lt;/code&amp;gt; and then should be chmod'd 755.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/bin/sh&lt;br /&gt;
# firewall      Start iptables firewall&lt;br /&gt;
# chkconfig: 2345 08 92&lt;br /&gt;
# description:  Starts, stops and saves iptables firewall&lt;br /&gt;
# This script sets up the firewall for the INPUT chain (which is for&lt;br /&gt;
# the HN itself) and then processes the config files under&lt;br /&gt;
# /etc/firewall.d to set up additional rules in the FORWARD chain&lt;br /&gt;
# to allow access to containers' services.&lt;br /&gt;
&lt;br /&gt;
. /etc/init.d/functions&lt;br /&gt;
&lt;br /&gt;
# the IP block allocated to this server&lt;br /&gt;
SEGMENT=&amp;quot;192.168.0.0/24&amp;quot;&lt;br /&gt;
# the IP used by the hosting server itself&lt;br /&gt;
THISHOST=&amp;quot;192.168.0.1&amp;quot;&lt;br /&gt;
# services that should be allowed to the HN;&lt;br /&gt;
# services for containers are configured in /etc/firewall.d/*&lt;br /&gt;
OKPORTS=&amp;quot;53&amp;quot;&lt;br /&gt;
# hosts allowed full access through the firewall,&lt;br /&gt;
# to all containers and to this server&lt;br /&gt;
DMZS=&amp;quot;12.34.56.78 90.123.45.67&amp;quot;&lt;br /&gt;
&lt;br /&gt;
purge() {&lt;br /&gt;
  echo -n &amp;quot;Firewall: Purging and allowing all traffic&amp;quot;&lt;br /&gt;
  iptables -P OUTPUT ACCEPT&lt;br /&gt;
  iptables -P FORWARD ACCEPT&lt;br /&gt;
  iptables -P INPUT ACCEPT&lt;br /&gt;
  iptables -F&lt;br /&gt;
  success ; echo&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
setup() {&lt;br /&gt;
  echo -n &amp;quot;Firewall: Setting default policies to DROP&amp;quot;&lt;br /&gt;
  iptables -P INPUT DROP&lt;br /&gt;
  iptables -P FORWARD DROP&lt;br /&gt;
  iptables -I INPUT   -j ACCEPT -m state --state ESTABLISHED,RELATED&lt;br /&gt;
  iptables -I FORWARD -j ACCEPT -m state --state ESTABLISHED,RELATED&lt;br /&gt;
  iptables -I INPUT -j ACCEPT -i lo&lt;br /&gt;
  iptables -I FORWARD -j ACCEPT --source $SEGMENT&lt;br /&gt;
  success ; echo&lt;br /&gt;
&lt;br /&gt;
  echo &amp;quot;Firewall: Allowing access to HN&amp;quot;&lt;br /&gt;
  for port in $OKPORTS ; do&lt;br /&gt;
    echo -n &amp;quot;          port $port&amp;quot;&lt;br /&gt;
    iptables -I INPUT -j ACCEPT -s $SEGMENT -d $THISHOST --protocol tcp --destination-port $port&lt;br /&gt;
    iptables -I INPUT -j ACCEPT -s $SEGMENT -d $THISHOST --protocol udp --destination-port $port&lt;br /&gt;
    success ; echo&lt;br /&gt;
  done&lt;br /&gt;
  for ip in $DMZS ; do&lt;br /&gt;
    echo -n &amp;quot;          DMZ $ip&amp;quot;&lt;br /&gt;
    iptables -I INPUT   -i eth0 -j ACCEPT -s $ip&lt;br /&gt;
    iptables -I FORWARD -i eth0 -j ACCEPT -s $ip&lt;br /&gt;
    success ; echo&lt;br /&gt;
  done&lt;br /&gt;
&lt;br /&gt;
  CTSETUPS=`echo /etc/firewall.d/*`&lt;br /&gt;
  if [ &amp;quot;$CTSETUPS&amp;quot; != &amp;quot;/etc/firewall.d/*&amp;quot; ] ; then&lt;br /&gt;
  echo &amp;quot;Firewall: Setting up container firewalls&amp;quot;&lt;br /&gt;
  for i in $CTSETUPS ; do&lt;br /&gt;
    . $i&lt;br /&gt;
    echo -n &amp;quot;          $CTNAME CT$CTID&amp;quot;&lt;br /&gt;
    if [ -n &amp;quot;$BANNED&amp;quot; ]; then&lt;br /&gt;
      for source in $BANNED ;  do iptables -I FORWARD -j DROP --destination $CTIP --source $source ; done&lt;br /&gt;
    fi&lt;br /&gt;
    if [ -n &amp;quot;$OPENPORTS&amp;quot; ]; then&lt;br /&gt;
      for port in $OPENPORTS ; do iptables -I FORWARD -j ACCEPT --protocol tcp --destination $CTIP --destination-port $port ; done&lt;br /&gt;
      for port in $OPENPORTS ; do iptables -I FORWARD -j ACCEPT --protocol udp --destination $CTIP --destination-port $port ; done&lt;br /&gt;
    fi&lt;br /&gt;
    if [ -n &amp;quot;$DMZS&amp;quot; ]; then&lt;br /&gt;
      for source in $DMZS ; do iptables -I FORWARD -j ACCEPT --protocol tcp --destination $CTIP --source $source ; done&lt;br /&gt;
      for source in $DMZS ; do iptables -I FORWARD -j ACCEPT --protocol udp --destination $CTIP --source $source ; done&lt;br /&gt;
    fi&lt;br /&gt;
    [ $? -eq 0 ] &amp;amp;&amp;amp; success || failure&lt;br /&gt;
    echo&lt;br /&gt;
  done&lt;br /&gt;
  fi&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
case &amp;quot;$1&amp;quot; in&lt;br /&gt;
  start)&lt;br /&gt;
    echo &amp;quot;Starting firewall...&amp;quot;&lt;br /&gt;
    purge&lt;br /&gt;
    setup&lt;br /&gt;
    ;;&lt;br /&gt;
  stop)&lt;br /&gt;
    echo &amp;quot;Stopping firewall...&amp;quot;&lt;br /&gt;
    purge&lt;br /&gt;
    ;;&lt;br /&gt;
  restart)&lt;br /&gt;
    $0 stop&lt;br /&gt;
    $0 start&lt;br /&gt;
    ;;&lt;br /&gt;
  status)&lt;br /&gt;
    iptables -n -L&lt;br /&gt;
    ;;&lt;br /&gt;
  *)&lt;br /&gt;
    echo &amp;quot;Usage: $0 &amp;lt;start|stop|restart|status&amp;gt;&amp;quot;&lt;br /&gt;
    ;;&lt;br /&gt;
esac&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note: This will only allow access to the HN from the hosts/networks defined in SEGMENT. If you'd like to open up the OKPORTS on the HN to everybody, you can remove the ''-s $SEGMENT'' parameters from the iptables commands under the &amp;quot;Firewall: Allowing access to HN&amp;quot; section. The modified lines would look like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
iptables -I INPUT -j ACCEPT -d $THISHOST --protocol tcp --destination-port $port&lt;br /&gt;
iptables -I INPUT -j ACCEPT -d $THISHOST --protocol udp --destination-port $port&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above script can be called like this:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
service firewall start&lt;br /&gt;
service firewall stop&lt;br /&gt;
service firewall restart&lt;br /&gt;
service firewall status&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It will set up the firewall for the HN according to the parameters you specified for OKPORTS, DMZs, etc. and then it will call each file under /etc/firewall.d and process  its configuration.&lt;br /&gt;
&lt;br /&gt;
So create a file under /etc/firewall.d The exact filename isn't important, as long as it's meaningful to you, e.g. &amp;lt;code&amp;gt;ExampleCompany&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;ve12&amp;lt;/code&amp;gt; and give it content like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# This file is processed by /etc/init.d/firewall&lt;br /&gt;
CTID=&amp;quot;1&amp;quot;			# the container's ID#&lt;br /&gt;
CTNAME=&amp;quot;Customer1&amp;quot;		# A human-friendly label for the container&lt;br /&gt;
CTIP=&amp;quot;192.168.1.34&amp;quot;		# the IP address for this container &lt;br /&gt;
OPENPORTS=&amp;quot;80 443&amp;quot;		# ports that should be universally opened&lt;br /&gt;
				# to the entire Internet&lt;br /&gt;
DMZS=&amp;quot;1.2.3.0/24 5.6.7.8/32&amp;quot;	# IPs and blocks that should have full access&lt;br /&gt;
				# to the container's services&lt;br /&gt;
BANNED=&amp;quot;&amp;quot;			# IPs and blocks that should be entirely&lt;br /&gt;
				# blocked from the container's services&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And there you go. Go ahead and start the firewall and check its status:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
service firewall restart&lt;br /&gt;
service firewall status&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As you can see, you can now add and edit the configurations for individual containers very easily. This method proves a lot easier to manage than Fedora's iptables-config mechamism!&lt;br /&gt;
&lt;br /&gt;
To make the firewall service automatically start when the HN boots, run&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
chkconfig --add firewall&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Debian Notes ===&lt;br /&gt;
&lt;br /&gt;
The setup above works fine for Debian as well, however /etc/init.d/functions is missing. Here is a very simple version that you can use:&lt;br /&gt;
&lt;br /&gt;
  # /etc/init.d/functions&lt;br /&gt;
  &lt;br /&gt;
  success() {&lt;br /&gt;
    echo -n &amp;quot;...success&amp;quot;&lt;br /&gt;
  } &lt;br /&gt;
  failure() {&lt;br /&gt;
    echo -n &amp;quot;...failure&amp;quot;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
== Setting up a firewall that allows per-container configuration ==&lt;br /&gt;
&lt;br /&gt;
This setup configures iptables on the HN to disallow access to all hosts, including the containers. However, it allows all traffic into the containers so they may define their own iptables rules and therefore manage their own firewall.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
iptables -P FORWARD ACCEPT&lt;br /&gt;
iptables -F FORWARD&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This will remove all rules for the FORWARD chain so all packets can pass back and forth between containers and the outside world.&lt;br /&gt;
&lt;br /&gt;
If you want to use a firewall inside a container, please load these modules BEFORE starting the container:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
modprobe xt_tcpudp&lt;br /&gt;
modprobe ip_conntrack&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you do not, you will get an error like this: &amp;quot;iptables: No chain/target/match by that name&amp;quot;&lt;br /&gt;
&lt;br /&gt;
If you want to use stateful firewall rules (and you should!) you will also need to make sure that 'ipt_state' is in the 'IPTABLES' option in your vz.conf file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
IPTABLES=&amp;quot;ipt_REJECT ipt_tos ipt_limit ipt_multiport iptable_filter iptable_mangle ipt_TCPMSS ipt_tcpmss ipt_ttl ipt_length ipt_state&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Also make sure the 'xt_state' module is loaded on the host:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
modprobe xt_state&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
&lt;br /&gt;
* [[Traffic accounting with iptables]]&lt;br /&gt;
&lt;br /&gt;
[[Category: Networking]]&lt;/div&gt;</summary>
		<author><name>DmitryKoterov</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.openvz.org/index.php?title=Setting_up_an_iptables_firewall&amp;diff=9340</id>
		<title>Setting up an iptables firewall</title>
		<link rel="alternate" type="text/html" href="https://wiki.openvz.org/index.php?title=Setting_up_an_iptables_firewall&amp;diff=9340"/>
		<updated>2010-11-11T23:04:47Z</updated>

		<summary type="html">&lt;p&gt;DmitryKoterov: /* Simple firewall configuration independent to IP addresses: vzfirewall */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This document consists of two parts. The first is setting up a firewall (using iptables) on the HN, which will restrict traffic to the containers. The effect would emulate, as far as the containers and their customers are concerned, an external hardware firewall controlled by the sysadmin. The second is setting up a firewall that protects the [[HN]] itself but still allows traffic to the containers, thus allowing individual containers to define their own iptables.&lt;br /&gt;
&lt;br /&gt;
While the firewalls shown here can be accomplished using iptables manually (or using Fedora core's iptables service), the methods presented here are especially modular and easy to modify. This is important when you have 20+ containers and a lot of other things to be doing...&lt;br /&gt;
&lt;br /&gt;
The scripts and pathnames given here are for Fedora Core 6, though they can probably be applied to most similar SysV-like systems with little modification.&lt;br /&gt;
&lt;br /&gt;
== A little background ==&lt;br /&gt;
&lt;br /&gt;
On our systems, we use the HN to provide privileged services which are not appropriate for access by the containers. For example, the HN acts as a backup server, runs Nagios for health monitoring, has a webserver for managing the 3ware RAID controller, etc. The containers are leased to customers, who can't entirely be trusted, especially if they get hacked. As such, our scenario is one in which the HN must be protected from all access (even from the containers) except for a few trusted hosts (e.g. my home-office).&lt;br /&gt;
&lt;br /&gt;
The exception to this is the nameserver, which we want open to the world. We use it as a caching nameserver for our containers and also to host DNS for a few customer domain.&lt;br /&gt;
&lt;br /&gt;
== Simple firewall configuration independent to IP addresses: vzfirewall ==&lt;br /&gt;
&lt;br /&gt;
''Vzfirewall'' tool allows you to open/close ports for incoming connections with no dependencies to foreign IP addresses. E.g. you may allow a hostname ''release.prod.example.com'' to connect to port 5432 of VE 1234 and leave all other ports closed by modifying 1234.conf file adding multiline ''FIREWALL'' directive into it:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
...&lt;br /&gt;
PRIVVMPAGES=&amp;quot;300000:300000&amp;quot;&lt;br /&gt;
HOSTNAME=&amp;quot;example.com&amp;quot;&lt;br /&gt;
...&lt;br /&gt;
FIREWALL=&amp;quot;&lt;br /&gt;
    ...&lt;br /&gt;
    # Allow access to PostgreSQL port only from release.prod machine.&lt;br /&gt;
    # Note that you may use domain names here.&lt;br /&gt;
    [5432]&lt;br /&gt;
    release.prod.example.com&lt;br /&gt;
    release.test.example.com&lt;br /&gt;
    ...&lt;br /&gt;
&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You must then run ''vzfirewall -a'' on your hardware node to apply changes made in *.conf.&lt;br /&gt;
&lt;br /&gt;
Note that it is recommended to use hostnames instead of IP addresses here, so the configuration is persistent fore VE movements to different IP-address.&lt;br /&gt;
&lt;br /&gt;
Vzfirewall and its documentation tool is available at [http://en.dklab.ru/lib/dklab_vzfirewall/ http://en.dklab.ru/lib/dklab_vzfirewall/].&lt;br /&gt;
&lt;br /&gt;
== An alternative from the author of Shorewall ==&lt;br /&gt;
&lt;br /&gt;
For those who might find the solution provided in this wiki article unsatisfactory (for whatever reason), the creator of Shorewall (Tom Eastep) has written a nice article explaining how to use Shorewall on an OpenVZ host node to manage the host node, containers, and more... with quite a complex setup as an example.  The article IS NOT an introduction to Shorewall for beginners, so some pre-existing knowledge and understanding of Shorewall may be required.&lt;br /&gt;
&lt;br /&gt;
Shorewall and OpenVZ by Tom Eastep - http://www.shorewall.net/OpenVZ.html&lt;br /&gt;
&lt;br /&gt;
See also this OpenVZ Forum posting - http://forum.openvz.org/index.php?t=msg&amp;amp;goto=16406&amp;amp;&lt;br /&gt;
&lt;br /&gt;
== Setting up a HN-based firewall ==&lt;br /&gt;
&lt;br /&gt;
This setup emulates (to the containers anyway) an external hardware firewall. It protects the HN from any access and then defines what services and ports are allowed/banned for individual containers. This leaves the firewall controlled by the site administrator, not be individual containers and the hackers who've gotten into them. ;)&lt;br /&gt;
&lt;br /&gt;
First off, let's disable Fedora's existing &amp;lt;code&amp;gt;iptables&amp;lt;/code&amp;gt; service:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
service iptables stop&lt;br /&gt;
chkconfig iptables off&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now create the new &amp;lt;code&amp;gt;firewall&amp;lt;/code&amp;gt; service. This code should be &amp;lt;code&amp;gt;/etc/init.d/firewall&amp;lt;/code&amp;gt; and then should be chmod'd 755.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/bin/sh&lt;br /&gt;
# firewall      Start iptables firewall&lt;br /&gt;
# chkconfig: 2345 08 92&lt;br /&gt;
# description:  Starts, stops and saves iptables firewall&lt;br /&gt;
# This script sets up the firewall for the INPUT chain (which is for&lt;br /&gt;
# the HN itself) and then processes the config files under&lt;br /&gt;
# /etc/firewall.d to set up additional rules in the FORWARD chain&lt;br /&gt;
# to allow access to containers' services.&lt;br /&gt;
&lt;br /&gt;
. /etc/init.d/functions&lt;br /&gt;
&lt;br /&gt;
# the IP block allocated to this server&lt;br /&gt;
SEGMENT=&amp;quot;192.168.0.0/24&amp;quot;&lt;br /&gt;
# the IP used by the hosting server itself&lt;br /&gt;
THISHOST=&amp;quot;192.168.0.1&amp;quot;&lt;br /&gt;
# services that should be allowed to the HN;&lt;br /&gt;
# services for containers are configured in /etc/firewall.d/*&lt;br /&gt;
OKPORTS=&amp;quot;53&amp;quot;&lt;br /&gt;
# hosts allowed full access through the firewall,&lt;br /&gt;
# to all containers and to this server&lt;br /&gt;
DMZS=&amp;quot;12.34.56.78 90.123.45.67&amp;quot;&lt;br /&gt;
&lt;br /&gt;
purge() {&lt;br /&gt;
  echo -n &amp;quot;Firewall: Purging and allowing all traffic&amp;quot;&lt;br /&gt;
  iptables -P OUTPUT ACCEPT&lt;br /&gt;
  iptables -P FORWARD ACCEPT&lt;br /&gt;
  iptables -P INPUT ACCEPT&lt;br /&gt;
  iptables -F&lt;br /&gt;
  success ; echo&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
setup() {&lt;br /&gt;
  echo -n &amp;quot;Firewall: Setting default policies to DROP&amp;quot;&lt;br /&gt;
  iptables -P INPUT DROP&lt;br /&gt;
  iptables -P FORWARD DROP&lt;br /&gt;
  iptables -I INPUT   -j ACCEPT -m state --state ESTABLISHED,RELATED&lt;br /&gt;
  iptables -I FORWARD -j ACCEPT -m state --state ESTABLISHED,RELATED&lt;br /&gt;
  iptables -I INPUT -j ACCEPT -i lo&lt;br /&gt;
  iptables -I FORWARD -j ACCEPT --source $SEGMENT&lt;br /&gt;
  success ; echo&lt;br /&gt;
&lt;br /&gt;
  echo &amp;quot;Firewall: Allowing access to HN&amp;quot;&lt;br /&gt;
  for port in $OKPORTS ; do&lt;br /&gt;
    echo -n &amp;quot;          port $port&amp;quot;&lt;br /&gt;
    iptables -I INPUT -j ACCEPT -s $SEGMENT -d $THISHOST --protocol tcp --destination-port $port&lt;br /&gt;
    iptables -I INPUT -j ACCEPT -s $SEGMENT -d $THISHOST --protocol udp --destination-port $port&lt;br /&gt;
    success ; echo&lt;br /&gt;
  done&lt;br /&gt;
  for ip in $DMZS ; do&lt;br /&gt;
    echo -n &amp;quot;          DMZ $ip&amp;quot;&lt;br /&gt;
    iptables -I INPUT   -i eth0 -j ACCEPT -s $ip&lt;br /&gt;
    iptables -I FORWARD -i eth0 -j ACCEPT -s $ip&lt;br /&gt;
    success ; echo&lt;br /&gt;
  done&lt;br /&gt;
&lt;br /&gt;
  CTSETUPS=`echo /etc/firewall.d/*`&lt;br /&gt;
  if [ &amp;quot;$CTSETUPS&amp;quot; != &amp;quot;/etc/firewall.d/*&amp;quot; ] ; then&lt;br /&gt;
  echo &amp;quot;Firewall: Setting up container firewalls&amp;quot;&lt;br /&gt;
  for i in $CTSETUPS ; do&lt;br /&gt;
    . $i&lt;br /&gt;
    echo -n &amp;quot;          $CTNAME CT$CTID&amp;quot;&lt;br /&gt;
    if [ -n &amp;quot;$BANNED&amp;quot; ]; then&lt;br /&gt;
      for source in $BANNED ;  do iptables -I FORWARD -j DROP --destination $CTIP --source $source ; done&lt;br /&gt;
    fi&lt;br /&gt;
    if [ -n &amp;quot;$OPENPORTS&amp;quot; ]; then&lt;br /&gt;
      for port in $OPENPORTS ; do iptables -I FORWARD -j ACCEPT --protocol tcp --destination $CTIP --destination-port $port ; done&lt;br /&gt;
      for port in $OPENPORTS ; do iptables -I FORWARD -j ACCEPT --protocol udp --destination $CTIP --destination-port $port ; done&lt;br /&gt;
    fi&lt;br /&gt;
    if [ -n &amp;quot;$DMZS&amp;quot; ]; then&lt;br /&gt;
      for source in $DMZS ; do iptables -I FORWARD -j ACCEPT --protocol tcp --destination $CTIP --source $source ; done&lt;br /&gt;
      for source in $DMZS ; do iptables -I FORWARD -j ACCEPT --protocol udp --destination $CTIP --source $source ; done&lt;br /&gt;
    fi&lt;br /&gt;
    [ $? -eq 0 ] &amp;amp;&amp;amp; success || failure&lt;br /&gt;
    echo&lt;br /&gt;
  done&lt;br /&gt;
  fi&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
case &amp;quot;$1&amp;quot; in&lt;br /&gt;
  start)&lt;br /&gt;
    echo &amp;quot;Starting firewall...&amp;quot;&lt;br /&gt;
    purge&lt;br /&gt;
    setup&lt;br /&gt;
    ;;&lt;br /&gt;
  stop)&lt;br /&gt;
    echo &amp;quot;Stopping firewall...&amp;quot;&lt;br /&gt;
    purge&lt;br /&gt;
    ;;&lt;br /&gt;
  restart)&lt;br /&gt;
    $0 stop&lt;br /&gt;
    $0 start&lt;br /&gt;
    ;;&lt;br /&gt;
  status)&lt;br /&gt;
    iptables -n -L&lt;br /&gt;
    ;;&lt;br /&gt;
  *)&lt;br /&gt;
    echo &amp;quot;Usage: $0 &amp;lt;start|stop|restart|status&amp;gt;&amp;quot;&lt;br /&gt;
    ;;&lt;br /&gt;
esac&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note: This will only allow access to the HN from the hosts/networks defined in SEGMENT. If you'd like to open up the OKPORTS on the HN to everybody, you can remove the ''-s $SEGMENT'' parameters from the iptables commands under the &amp;quot;Firewall: Allowing access to HN&amp;quot; section. The modified lines would look like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
iptables -I INPUT -j ACCEPT -d $THISHOST --protocol tcp --destination-port $port&lt;br /&gt;
iptables -I INPUT -j ACCEPT -d $THISHOST --protocol udp --destination-port $port&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above script can be called like this:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
service firewall start&lt;br /&gt;
service firewall stop&lt;br /&gt;
service firewall restart&lt;br /&gt;
service firewall status&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It will set up the firewall for the HN according to the parameters you specified for OKPORTS, DMZs, etc. and then it will call each file under /etc/firewall.d and process  its configuration.&lt;br /&gt;
&lt;br /&gt;
So create a file under /etc/firewall.d The exact filename isn't important, as long as it's meaningful to you, e.g. &amp;lt;code&amp;gt;ExampleCompany&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;ve12&amp;lt;/code&amp;gt; and give it content like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# This file is processed by /etc/init.d/firewall&lt;br /&gt;
CTID=&amp;quot;1&amp;quot;			# the container's ID#&lt;br /&gt;
CTNAME=&amp;quot;Customer1&amp;quot;		# A human-friendly label for the container&lt;br /&gt;
CTIP=&amp;quot;192.168.1.34&amp;quot;		# the IP address for this container &lt;br /&gt;
OPENPORTS=&amp;quot;80 443&amp;quot;		# ports that should be universally opened&lt;br /&gt;
				# to the entire Internet&lt;br /&gt;
DMZS=&amp;quot;1.2.3.0/24 5.6.7.8/32&amp;quot;	# IPs and blocks that should have full access&lt;br /&gt;
				# to the container's services&lt;br /&gt;
BANNED=&amp;quot;&amp;quot;			# IPs and blocks that should be entirely&lt;br /&gt;
				# blocked from the container's services&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And there you go. Go ahead and start the firewall and check its status:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
service firewall restart&lt;br /&gt;
service firewall status&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As you can see, you can now add and edit the configurations for individual containers very easily. This method proves a lot easier to manage than Fedora's iptables-config mechamism!&lt;br /&gt;
&lt;br /&gt;
To make the firewall service automatically start when the HN boots, run&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
chkconfig --add firewall&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Debian Notes ===&lt;br /&gt;
&lt;br /&gt;
The setup above works fine for Debian as well, however /etc/init.d/functions is missing. Here is a very simple version that you can use:&lt;br /&gt;
&lt;br /&gt;
  # /etc/init.d/functions&lt;br /&gt;
  &lt;br /&gt;
  success() {&lt;br /&gt;
    echo -n &amp;quot;...success&amp;quot;&lt;br /&gt;
  } &lt;br /&gt;
  failure() {&lt;br /&gt;
    echo -n &amp;quot;...failure&amp;quot;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
== Setting up a firewall that allows per-container configuration ==&lt;br /&gt;
&lt;br /&gt;
This setup configures iptables on the HN to disallow access to all hosts, including the containers. However, it allows all traffic into the containers so they may define their own iptables rules and therefore manage their own firewall.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
iptables -P FORWARD ACCEPT&lt;br /&gt;
iptables -F FORWARD&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This will remove all rules for the FORWARD chain so all packets can pass back and forth between containers and the outside world.&lt;br /&gt;
&lt;br /&gt;
If you want to use a firewall inside a container, please load these modules BEFORE starting the container:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
modprobe xt_tcpudp&lt;br /&gt;
modprobe ip_conntrack&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you do not, you will get an error like this: &amp;quot;iptables: No chain/target/match by that name&amp;quot;&lt;br /&gt;
&lt;br /&gt;
If you want to use stateful firewall rules (and you should!) you will also need to make sure that 'ipt_state' is in the 'IPTABLES' option in your vz.conf file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
IPTABLES=&amp;quot;ipt_REJECT ipt_tos ipt_limit ipt_multiport iptable_filter iptable_mangle ipt_TCPMSS ipt_tcpmss ipt_ttl ipt_length ipt_state&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Also make sure the 'xt_state' module is loaded on the host:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
modprobe xt_state&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
&lt;br /&gt;
* [[Traffic accounting with iptables]]&lt;br /&gt;
&lt;br /&gt;
[[Category: Networking]]&lt;/div&gt;</summary>
		<author><name>DmitryKoterov</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.openvz.org/index.php?title=Setting_up_an_iptables_firewall&amp;diff=9339</id>
		<title>Setting up an iptables firewall</title>
		<link rel="alternate" type="text/html" href="https://wiki.openvz.org/index.php?title=Setting_up_an_iptables_firewall&amp;diff=9339"/>
		<updated>2010-11-11T23:03:33Z</updated>

		<summary type="html">&lt;p&gt;DmitryKoterov: /* Simple firewall configuration independent to IP addresses */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This document consists of two parts. The first is setting up a firewall (using iptables) on the HN, which will restrict traffic to the containers. The effect would emulate, as far as the containers and their customers are concerned, an external hardware firewall controlled by the sysadmin. The second is setting up a firewall that protects the [[HN]] itself but still allows traffic to the containers, thus allowing individual containers to define their own iptables.&lt;br /&gt;
&lt;br /&gt;
While the firewalls shown here can be accomplished using iptables manually (or using Fedora core's iptables service), the methods presented here are especially modular and easy to modify. This is important when you have 20+ containers and a lot of other things to be doing...&lt;br /&gt;
&lt;br /&gt;
The scripts and pathnames given here are for Fedora Core 6, though they can probably be applied to most similar SysV-like systems with little modification.&lt;br /&gt;
&lt;br /&gt;
== A little background ==&lt;br /&gt;
&lt;br /&gt;
On our systems, we use the HN to provide privileged services which are not appropriate for access by the containers. For example, the HN acts as a backup server, runs Nagios for health monitoring, has a webserver for managing the 3ware RAID controller, etc. The containers are leased to customers, who can't entirely be trusted, especially if they get hacked. As such, our scenario is one in which the HN must be protected from all access (even from the containers) except for a few trusted hosts (e.g. my home-office).&lt;br /&gt;
&lt;br /&gt;
The exception to this is the nameserver, which we want open to the world. We use it as a caching nameserver for our containers and also to host DNS for a few customer domain.&lt;br /&gt;
&lt;br /&gt;
== Simple firewall configuration independent to IP addresses: vzfirewall ==&lt;br /&gt;
&lt;br /&gt;
''Vzfirewall'' tool allows you to open/close ports for incoming connections with no dependencies to foreign IP addresses. E.g. you may allow a hostname ''release.prod.example.com'' to connect to port 5432 of VE 1234 by modifying 1234.conf file adding multiline ''FIREWALL'' directive into it:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
...&lt;br /&gt;
PRIVVMPAGES=&amp;quot;300000:300000&amp;quot;&lt;br /&gt;
HOSTNAME=&amp;quot;example.com&amp;quot;&lt;br /&gt;
...&lt;br /&gt;
FIREWALL=&amp;quot;&lt;br /&gt;
    ...&lt;br /&gt;
    # Allow access to PostgreSQL port only from release.prod machine.&lt;br /&gt;
    # Note that you may use domain names here.&lt;br /&gt;
    [5432]&lt;br /&gt;
    release.prod.example.com&lt;br /&gt;
    release.test.example.com&lt;br /&gt;
    ...&lt;br /&gt;
&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You must then run ''vzfirewall -a'' on your hardware node to apply changes made in *.conf.&lt;br /&gt;
&lt;br /&gt;
Note that it is recommended to use hostnames instead of IP addresses here, so the configuration is persistent fore VE movements to different IP-address.&lt;br /&gt;
&lt;br /&gt;
Vzfirewall and its documentation tool is available at [http://en.dklab.ru/lib/dklab_vzfirewall/].&lt;br /&gt;
&lt;br /&gt;
== An alternative from the author of Shorewall ==&lt;br /&gt;
&lt;br /&gt;
For those who might find the solution provided in this wiki article unsatisfactory (for whatever reason), the creator of Shorewall (Tom Eastep) has written a nice article explaining how to use Shorewall on an OpenVZ host node to manage the host node, containers, and more... with quite a complex setup as an example.  The article IS NOT an introduction to Shorewall for beginners, so some pre-existing knowledge and understanding of Shorewall may be required.&lt;br /&gt;
&lt;br /&gt;
Shorewall and OpenVZ by Tom Eastep - http://www.shorewall.net/OpenVZ.html&lt;br /&gt;
&lt;br /&gt;
See also this OpenVZ Forum posting - http://forum.openvz.org/index.php?t=msg&amp;amp;goto=16406&amp;amp;&lt;br /&gt;
&lt;br /&gt;
== Setting up a HN-based firewall ==&lt;br /&gt;
&lt;br /&gt;
This setup emulates (to the containers anyway) an external hardware firewall. It protects the HN from any access and then defines what services and ports are allowed/banned for individual containers. This leaves the firewall controlled by the site administrator, not be individual containers and the hackers who've gotten into them. ;)&lt;br /&gt;
&lt;br /&gt;
First off, let's disable Fedora's existing &amp;lt;code&amp;gt;iptables&amp;lt;/code&amp;gt; service:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
service iptables stop&lt;br /&gt;
chkconfig iptables off&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now create the new &amp;lt;code&amp;gt;firewall&amp;lt;/code&amp;gt; service. This code should be &amp;lt;code&amp;gt;/etc/init.d/firewall&amp;lt;/code&amp;gt; and then should be chmod'd 755.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/bin/sh&lt;br /&gt;
# firewall      Start iptables firewall&lt;br /&gt;
# chkconfig: 2345 08 92&lt;br /&gt;
# description:  Starts, stops and saves iptables firewall&lt;br /&gt;
# This script sets up the firewall for the INPUT chain (which is for&lt;br /&gt;
# the HN itself) and then processes the config files under&lt;br /&gt;
# /etc/firewall.d to set up additional rules in the FORWARD chain&lt;br /&gt;
# to allow access to containers' services.&lt;br /&gt;
&lt;br /&gt;
. /etc/init.d/functions&lt;br /&gt;
&lt;br /&gt;
# the IP block allocated to this server&lt;br /&gt;
SEGMENT=&amp;quot;192.168.0.0/24&amp;quot;&lt;br /&gt;
# the IP used by the hosting server itself&lt;br /&gt;
THISHOST=&amp;quot;192.168.0.1&amp;quot;&lt;br /&gt;
# services that should be allowed to the HN;&lt;br /&gt;
# services for containers are configured in /etc/firewall.d/*&lt;br /&gt;
OKPORTS=&amp;quot;53&amp;quot;&lt;br /&gt;
# hosts allowed full access through the firewall,&lt;br /&gt;
# to all containers and to this server&lt;br /&gt;
DMZS=&amp;quot;12.34.56.78 90.123.45.67&amp;quot;&lt;br /&gt;
&lt;br /&gt;
purge() {&lt;br /&gt;
  echo -n &amp;quot;Firewall: Purging and allowing all traffic&amp;quot;&lt;br /&gt;
  iptables -P OUTPUT ACCEPT&lt;br /&gt;
  iptables -P FORWARD ACCEPT&lt;br /&gt;
  iptables -P INPUT ACCEPT&lt;br /&gt;
  iptables -F&lt;br /&gt;
  success ; echo&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
setup() {&lt;br /&gt;
  echo -n &amp;quot;Firewall: Setting default policies to DROP&amp;quot;&lt;br /&gt;
  iptables -P INPUT DROP&lt;br /&gt;
  iptables -P FORWARD DROP&lt;br /&gt;
  iptables -I INPUT   -j ACCEPT -m state --state ESTABLISHED,RELATED&lt;br /&gt;
  iptables -I FORWARD -j ACCEPT -m state --state ESTABLISHED,RELATED&lt;br /&gt;
  iptables -I INPUT -j ACCEPT -i lo&lt;br /&gt;
  iptables -I FORWARD -j ACCEPT --source $SEGMENT&lt;br /&gt;
  success ; echo&lt;br /&gt;
&lt;br /&gt;
  echo &amp;quot;Firewall: Allowing access to HN&amp;quot;&lt;br /&gt;
  for port in $OKPORTS ; do&lt;br /&gt;
    echo -n &amp;quot;          port $port&amp;quot;&lt;br /&gt;
    iptables -I INPUT -j ACCEPT -s $SEGMENT -d $THISHOST --protocol tcp --destination-port $port&lt;br /&gt;
    iptables -I INPUT -j ACCEPT -s $SEGMENT -d $THISHOST --protocol udp --destination-port $port&lt;br /&gt;
    success ; echo&lt;br /&gt;
  done&lt;br /&gt;
  for ip in $DMZS ; do&lt;br /&gt;
    echo -n &amp;quot;          DMZ $ip&amp;quot;&lt;br /&gt;
    iptables -I INPUT   -i eth0 -j ACCEPT -s $ip&lt;br /&gt;
    iptables -I FORWARD -i eth0 -j ACCEPT -s $ip&lt;br /&gt;
    success ; echo&lt;br /&gt;
  done&lt;br /&gt;
&lt;br /&gt;
  CTSETUPS=`echo /etc/firewall.d/*`&lt;br /&gt;
  if [ &amp;quot;$CTSETUPS&amp;quot; != &amp;quot;/etc/firewall.d/*&amp;quot; ] ; then&lt;br /&gt;
  echo &amp;quot;Firewall: Setting up container firewalls&amp;quot;&lt;br /&gt;
  for i in $CTSETUPS ; do&lt;br /&gt;
    . $i&lt;br /&gt;
    echo -n &amp;quot;          $CTNAME CT$CTID&amp;quot;&lt;br /&gt;
    if [ -n &amp;quot;$BANNED&amp;quot; ]; then&lt;br /&gt;
      for source in $BANNED ;  do iptables -I FORWARD -j DROP --destination $CTIP --source $source ; done&lt;br /&gt;
    fi&lt;br /&gt;
    if [ -n &amp;quot;$OPENPORTS&amp;quot; ]; then&lt;br /&gt;
      for port in $OPENPORTS ; do iptables -I FORWARD -j ACCEPT --protocol tcp --destination $CTIP --destination-port $port ; done&lt;br /&gt;
      for port in $OPENPORTS ; do iptables -I FORWARD -j ACCEPT --protocol udp --destination $CTIP --destination-port $port ; done&lt;br /&gt;
    fi&lt;br /&gt;
    if [ -n &amp;quot;$DMZS&amp;quot; ]; then&lt;br /&gt;
      for source in $DMZS ; do iptables -I FORWARD -j ACCEPT --protocol tcp --destination $CTIP --source $source ; done&lt;br /&gt;
      for source in $DMZS ; do iptables -I FORWARD -j ACCEPT --protocol udp --destination $CTIP --source $source ; done&lt;br /&gt;
    fi&lt;br /&gt;
    [ $? -eq 0 ] &amp;amp;&amp;amp; success || failure&lt;br /&gt;
    echo&lt;br /&gt;
  done&lt;br /&gt;
  fi&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
case &amp;quot;$1&amp;quot; in&lt;br /&gt;
  start)&lt;br /&gt;
    echo &amp;quot;Starting firewall...&amp;quot;&lt;br /&gt;
    purge&lt;br /&gt;
    setup&lt;br /&gt;
    ;;&lt;br /&gt;
  stop)&lt;br /&gt;
    echo &amp;quot;Stopping firewall...&amp;quot;&lt;br /&gt;
    purge&lt;br /&gt;
    ;;&lt;br /&gt;
  restart)&lt;br /&gt;
    $0 stop&lt;br /&gt;
    $0 start&lt;br /&gt;
    ;;&lt;br /&gt;
  status)&lt;br /&gt;
    iptables -n -L&lt;br /&gt;
    ;;&lt;br /&gt;
  *)&lt;br /&gt;
    echo &amp;quot;Usage: $0 &amp;lt;start|stop|restart|status&amp;gt;&amp;quot;&lt;br /&gt;
    ;;&lt;br /&gt;
esac&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note: This will only allow access to the HN from the hosts/networks defined in SEGMENT. If you'd like to open up the OKPORTS on the HN to everybody, you can remove the ''-s $SEGMENT'' parameters from the iptables commands under the &amp;quot;Firewall: Allowing access to HN&amp;quot; section. The modified lines would look like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
iptables -I INPUT -j ACCEPT -d $THISHOST --protocol tcp --destination-port $port&lt;br /&gt;
iptables -I INPUT -j ACCEPT -d $THISHOST --protocol udp --destination-port $port&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above script can be called like this:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
service firewall start&lt;br /&gt;
service firewall stop&lt;br /&gt;
service firewall restart&lt;br /&gt;
service firewall status&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It will set up the firewall for the HN according to the parameters you specified for OKPORTS, DMZs, etc. and then it will call each file under /etc/firewall.d and process  its configuration.&lt;br /&gt;
&lt;br /&gt;
So create a file under /etc/firewall.d The exact filename isn't important, as long as it's meaningful to you, e.g. &amp;lt;code&amp;gt;ExampleCompany&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;ve12&amp;lt;/code&amp;gt; and give it content like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# This file is processed by /etc/init.d/firewall&lt;br /&gt;
CTID=&amp;quot;1&amp;quot;			# the container's ID#&lt;br /&gt;
CTNAME=&amp;quot;Customer1&amp;quot;		# A human-friendly label for the container&lt;br /&gt;
CTIP=&amp;quot;192.168.1.34&amp;quot;		# the IP address for this container &lt;br /&gt;
OPENPORTS=&amp;quot;80 443&amp;quot;		# ports that should be universally opened&lt;br /&gt;
				# to the entire Internet&lt;br /&gt;
DMZS=&amp;quot;1.2.3.0/24 5.6.7.8/32&amp;quot;	# IPs and blocks that should have full access&lt;br /&gt;
				# to the container's services&lt;br /&gt;
BANNED=&amp;quot;&amp;quot;			# IPs and blocks that should be entirely&lt;br /&gt;
				# blocked from the container's services&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And there you go. Go ahead and start the firewall and check its status:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
service firewall restart&lt;br /&gt;
service firewall status&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As you can see, you can now add and edit the configurations for individual containers very easily. This method proves a lot easier to manage than Fedora's iptables-config mechamism!&lt;br /&gt;
&lt;br /&gt;
To make the firewall service automatically start when the HN boots, run&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
chkconfig --add firewall&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Debian Notes ===&lt;br /&gt;
&lt;br /&gt;
The setup above works fine for Debian as well, however /etc/init.d/functions is missing. Here is a very simple version that you can use:&lt;br /&gt;
&lt;br /&gt;
  # /etc/init.d/functions&lt;br /&gt;
  &lt;br /&gt;
  success() {&lt;br /&gt;
    echo -n &amp;quot;...success&amp;quot;&lt;br /&gt;
  } &lt;br /&gt;
  failure() {&lt;br /&gt;
    echo -n &amp;quot;...failure&amp;quot;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
== Setting up a firewall that allows per-container configuration ==&lt;br /&gt;
&lt;br /&gt;
This setup configures iptables on the HN to disallow access to all hosts, including the containers. However, it allows all traffic into the containers so they may define their own iptables rules and therefore manage their own firewall.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
iptables -P FORWARD ACCEPT&lt;br /&gt;
iptables -F FORWARD&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This will remove all rules for the FORWARD chain so all packets can pass back and forth between containers and the outside world.&lt;br /&gt;
&lt;br /&gt;
If you want to use a firewall inside a container, please load these modules BEFORE starting the container:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
modprobe xt_tcpudp&lt;br /&gt;
modprobe ip_conntrack&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you do not, you will get an error like this: &amp;quot;iptables: No chain/target/match by that name&amp;quot;&lt;br /&gt;
&lt;br /&gt;
If you want to use stateful firewall rules (and you should!) you will also need to make sure that 'ipt_state' is in the 'IPTABLES' option in your vz.conf file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
IPTABLES=&amp;quot;ipt_REJECT ipt_tos ipt_limit ipt_multiport iptable_filter iptable_mangle ipt_TCPMSS ipt_tcpmss ipt_ttl ipt_length ipt_state&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Also make sure the 'xt_state' module is loaded on the host:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
modprobe xt_state&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
&lt;br /&gt;
* [[Traffic accounting with iptables]]&lt;br /&gt;
&lt;br /&gt;
[[Category: Networking]]&lt;/div&gt;</summary>
		<author><name>DmitryKoterov</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.openvz.org/index.php?title=Setting_up_an_iptables_firewall&amp;diff=9338</id>
		<title>Setting up an iptables firewall</title>
		<link rel="alternate" type="text/html" href="https://wiki.openvz.org/index.php?title=Setting_up_an_iptables_firewall&amp;diff=9338"/>
		<updated>2010-11-11T23:01:03Z</updated>

		<summary type="html">&lt;p&gt;DmitryKoterov: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This document consists of two parts. The first is setting up a firewall (using iptables) on the HN, which will restrict traffic to the containers. The effect would emulate, as far as the containers and their customers are concerned, an external hardware firewall controlled by the sysadmin. The second is setting up a firewall that protects the [[HN]] itself but still allows traffic to the containers, thus allowing individual containers to define their own iptables.&lt;br /&gt;
&lt;br /&gt;
While the firewalls shown here can be accomplished using iptables manually (or using Fedora core's iptables service), the methods presented here are especially modular and easy to modify. This is important when you have 20+ containers and a lot of other things to be doing...&lt;br /&gt;
&lt;br /&gt;
The scripts and pathnames given here are for Fedora Core 6, though they can probably be applied to most similar SysV-like systems with little modification.&lt;br /&gt;
&lt;br /&gt;
== A little background ==&lt;br /&gt;
&lt;br /&gt;
On our systems, we use the HN to provide privileged services which are not appropriate for access by the containers. For example, the HN acts as a backup server, runs Nagios for health monitoring, has a webserver for managing the 3ware RAID controller, etc. The containers are leased to customers, who can't entirely be trusted, especially if they get hacked. As such, our scenario is one in which the HN must be protected from all access (even from the containers) except for a few trusted hosts (e.g. my home-office).&lt;br /&gt;
&lt;br /&gt;
The exception to this is the nameserver, which we want open to the world. We use it as a caching nameserver for our containers and also to host DNS for a few customer domain.&lt;br /&gt;
&lt;br /&gt;
== Simple firewall configuration independent to IP addresses ==&lt;br /&gt;
&lt;br /&gt;
''Vzfirewall'' tool allows you to open/close ports for incoming connections with no dependencies to foreign IP addresses. E.g. you may allow a hostname ''release.prod.example.com'' to connect to VE 1234 by modifying 1234.conf file adding multiline ''FIREWALL'' directive into it:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
...&lt;br /&gt;
PRIVVMPAGES=&amp;quot;300000:300000&amp;quot;&lt;br /&gt;
HOSTNAME=&amp;quot;example.com&amp;quot;&lt;br /&gt;
...&lt;br /&gt;
FIREWALL=&amp;quot;&lt;br /&gt;
    ...&lt;br /&gt;
    # Allow access to PostgreSQL port only from release.prod machine.&lt;br /&gt;
    # Note that you may use domain names here.&lt;br /&gt;
    [5432]&lt;br /&gt;
    release.prod.example.com&lt;br /&gt;
    release.test.example.com&lt;br /&gt;
    ...&lt;br /&gt;
&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note that you may use hostnames instead of IP addresses, so the configuration is persistent fore VE movements to different IP-address.&lt;br /&gt;
&lt;br /&gt;
Vzfirewall and its documentation tool is available at [http://en.dklab.ru/lib/dklab_vzfirewall/].&lt;br /&gt;
&lt;br /&gt;
== An alternative from the author of Shorewall ==&lt;br /&gt;
&lt;br /&gt;
For those who might find the solution provided in this wiki article unsatisfactory (for whatever reason), the creator of Shorewall (Tom Eastep) has written a nice article explaining how to use Shorewall on an OpenVZ host node to manage the host node, containers, and more... with quite a complex setup as an example.  The article IS NOT an introduction to Shorewall for beginners, so some pre-existing knowledge and understanding of Shorewall may be required.&lt;br /&gt;
&lt;br /&gt;
Shorewall and OpenVZ by Tom Eastep - http://www.shorewall.net/OpenVZ.html&lt;br /&gt;
&lt;br /&gt;
See also this OpenVZ Forum posting - http://forum.openvz.org/index.php?t=msg&amp;amp;goto=16406&amp;amp;&lt;br /&gt;
&lt;br /&gt;
== Setting up a HN-based firewall ==&lt;br /&gt;
&lt;br /&gt;
This setup emulates (to the containers anyway) an external hardware firewall. It protects the HN from any access and then defines what services and ports are allowed/banned for individual containers. This leaves the firewall controlled by the site administrator, not be individual containers and the hackers who've gotten into them. ;)&lt;br /&gt;
&lt;br /&gt;
First off, let's disable Fedora's existing &amp;lt;code&amp;gt;iptables&amp;lt;/code&amp;gt; service:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
service iptables stop&lt;br /&gt;
chkconfig iptables off&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now create the new &amp;lt;code&amp;gt;firewall&amp;lt;/code&amp;gt; service. This code should be &amp;lt;code&amp;gt;/etc/init.d/firewall&amp;lt;/code&amp;gt; and then should be chmod'd 755.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/bin/sh&lt;br /&gt;
# firewall      Start iptables firewall&lt;br /&gt;
# chkconfig: 2345 08 92&lt;br /&gt;
# description:  Starts, stops and saves iptables firewall&lt;br /&gt;
# This script sets up the firewall for the INPUT chain (which is for&lt;br /&gt;
# the HN itself) and then processes the config files under&lt;br /&gt;
# /etc/firewall.d to set up additional rules in the FORWARD chain&lt;br /&gt;
# to allow access to containers' services.&lt;br /&gt;
&lt;br /&gt;
. /etc/init.d/functions&lt;br /&gt;
&lt;br /&gt;
# the IP block allocated to this server&lt;br /&gt;
SEGMENT=&amp;quot;192.168.0.0/24&amp;quot;&lt;br /&gt;
# the IP used by the hosting server itself&lt;br /&gt;
THISHOST=&amp;quot;192.168.0.1&amp;quot;&lt;br /&gt;
# services that should be allowed to the HN;&lt;br /&gt;
# services for containers are configured in /etc/firewall.d/*&lt;br /&gt;
OKPORTS=&amp;quot;53&amp;quot;&lt;br /&gt;
# hosts allowed full access through the firewall,&lt;br /&gt;
# to all containers and to this server&lt;br /&gt;
DMZS=&amp;quot;12.34.56.78 90.123.45.67&amp;quot;&lt;br /&gt;
&lt;br /&gt;
purge() {&lt;br /&gt;
  echo -n &amp;quot;Firewall: Purging and allowing all traffic&amp;quot;&lt;br /&gt;
  iptables -P OUTPUT ACCEPT&lt;br /&gt;
  iptables -P FORWARD ACCEPT&lt;br /&gt;
  iptables -P INPUT ACCEPT&lt;br /&gt;
  iptables -F&lt;br /&gt;
  success ; echo&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
setup() {&lt;br /&gt;
  echo -n &amp;quot;Firewall: Setting default policies to DROP&amp;quot;&lt;br /&gt;
  iptables -P INPUT DROP&lt;br /&gt;
  iptables -P FORWARD DROP&lt;br /&gt;
  iptables -I INPUT   -j ACCEPT -m state --state ESTABLISHED,RELATED&lt;br /&gt;
  iptables -I FORWARD -j ACCEPT -m state --state ESTABLISHED,RELATED&lt;br /&gt;
  iptables -I INPUT -j ACCEPT -i lo&lt;br /&gt;
  iptables -I FORWARD -j ACCEPT --source $SEGMENT&lt;br /&gt;
  success ; echo&lt;br /&gt;
&lt;br /&gt;
  echo &amp;quot;Firewall: Allowing access to HN&amp;quot;&lt;br /&gt;
  for port in $OKPORTS ; do&lt;br /&gt;
    echo -n &amp;quot;          port $port&amp;quot;&lt;br /&gt;
    iptables -I INPUT -j ACCEPT -s $SEGMENT -d $THISHOST --protocol tcp --destination-port $port&lt;br /&gt;
    iptables -I INPUT -j ACCEPT -s $SEGMENT -d $THISHOST --protocol udp --destination-port $port&lt;br /&gt;
    success ; echo&lt;br /&gt;
  done&lt;br /&gt;
  for ip in $DMZS ; do&lt;br /&gt;
    echo -n &amp;quot;          DMZ $ip&amp;quot;&lt;br /&gt;
    iptables -I INPUT   -i eth0 -j ACCEPT -s $ip&lt;br /&gt;
    iptables -I FORWARD -i eth0 -j ACCEPT -s $ip&lt;br /&gt;
    success ; echo&lt;br /&gt;
  done&lt;br /&gt;
&lt;br /&gt;
  CTSETUPS=`echo /etc/firewall.d/*`&lt;br /&gt;
  if [ &amp;quot;$CTSETUPS&amp;quot; != &amp;quot;/etc/firewall.d/*&amp;quot; ] ; then&lt;br /&gt;
  echo &amp;quot;Firewall: Setting up container firewalls&amp;quot;&lt;br /&gt;
  for i in $CTSETUPS ; do&lt;br /&gt;
    . $i&lt;br /&gt;
    echo -n &amp;quot;          $CTNAME CT$CTID&amp;quot;&lt;br /&gt;
    if [ -n &amp;quot;$BANNED&amp;quot; ]; then&lt;br /&gt;
      for source in $BANNED ;  do iptables -I FORWARD -j DROP --destination $CTIP --source $source ; done&lt;br /&gt;
    fi&lt;br /&gt;
    if [ -n &amp;quot;$OPENPORTS&amp;quot; ]; then&lt;br /&gt;
      for port in $OPENPORTS ; do iptables -I FORWARD -j ACCEPT --protocol tcp --destination $CTIP --destination-port $port ; done&lt;br /&gt;
      for port in $OPENPORTS ; do iptables -I FORWARD -j ACCEPT --protocol udp --destination $CTIP --destination-port $port ; done&lt;br /&gt;
    fi&lt;br /&gt;
    if [ -n &amp;quot;$DMZS&amp;quot; ]; then&lt;br /&gt;
      for source in $DMZS ; do iptables -I FORWARD -j ACCEPT --protocol tcp --destination $CTIP --source $source ; done&lt;br /&gt;
      for source in $DMZS ; do iptables -I FORWARD -j ACCEPT --protocol udp --destination $CTIP --source $source ; done&lt;br /&gt;
    fi&lt;br /&gt;
    [ $? -eq 0 ] &amp;amp;&amp;amp; success || failure&lt;br /&gt;
    echo&lt;br /&gt;
  done&lt;br /&gt;
  fi&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
case &amp;quot;$1&amp;quot; in&lt;br /&gt;
  start)&lt;br /&gt;
    echo &amp;quot;Starting firewall...&amp;quot;&lt;br /&gt;
    purge&lt;br /&gt;
    setup&lt;br /&gt;
    ;;&lt;br /&gt;
  stop)&lt;br /&gt;
    echo &amp;quot;Stopping firewall...&amp;quot;&lt;br /&gt;
    purge&lt;br /&gt;
    ;;&lt;br /&gt;
  restart)&lt;br /&gt;
    $0 stop&lt;br /&gt;
    $0 start&lt;br /&gt;
    ;;&lt;br /&gt;
  status)&lt;br /&gt;
    iptables -n -L&lt;br /&gt;
    ;;&lt;br /&gt;
  *)&lt;br /&gt;
    echo &amp;quot;Usage: $0 &amp;lt;start|stop|restart|status&amp;gt;&amp;quot;&lt;br /&gt;
    ;;&lt;br /&gt;
esac&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note: This will only allow access to the HN from the hosts/networks defined in SEGMENT. If you'd like to open up the OKPORTS on the HN to everybody, you can remove the ''-s $SEGMENT'' parameters from the iptables commands under the &amp;quot;Firewall: Allowing access to HN&amp;quot; section. The modified lines would look like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
iptables -I INPUT -j ACCEPT -d $THISHOST --protocol tcp --destination-port $port&lt;br /&gt;
iptables -I INPUT -j ACCEPT -d $THISHOST --protocol udp --destination-port $port&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above script can be called like this:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
service firewall start&lt;br /&gt;
service firewall stop&lt;br /&gt;
service firewall restart&lt;br /&gt;
service firewall status&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It will set up the firewall for the HN according to the parameters you specified for OKPORTS, DMZs, etc. and then it will call each file under /etc/firewall.d and process  its configuration.&lt;br /&gt;
&lt;br /&gt;
So create a file under /etc/firewall.d The exact filename isn't important, as long as it's meaningful to you, e.g. &amp;lt;code&amp;gt;ExampleCompany&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;ve12&amp;lt;/code&amp;gt; and give it content like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# This file is processed by /etc/init.d/firewall&lt;br /&gt;
CTID=&amp;quot;1&amp;quot;			# the container's ID#&lt;br /&gt;
CTNAME=&amp;quot;Customer1&amp;quot;		# A human-friendly label for the container&lt;br /&gt;
CTIP=&amp;quot;192.168.1.34&amp;quot;		# the IP address for this container &lt;br /&gt;
OPENPORTS=&amp;quot;80 443&amp;quot;		# ports that should be universally opened&lt;br /&gt;
				# to the entire Internet&lt;br /&gt;
DMZS=&amp;quot;1.2.3.0/24 5.6.7.8/32&amp;quot;	# IPs and blocks that should have full access&lt;br /&gt;
				# to the container's services&lt;br /&gt;
BANNED=&amp;quot;&amp;quot;			# IPs and blocks that should be entirely&lt;br /&gt;
				# blocked from the container's services&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And there you go. Go ahead and start the firewall and check its status:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
service firewall restart&lt;br /&gt;
service firewall status&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As you can see, you can now add and edit the configurations for individual containers very easily. This method proves a lot easier to manage than Fedora's iptables-config mechamism!&lt;br /&gt;
&lt;br /&gt;
To make the firewall service automatically start when the HN boots, run&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
chkconfig --add firewall&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Debian Notes ===&lt;br /&gt;
&lt;br /&gt;
The setup above works fine for Debian as well, however /etc/init.d/functions is missing. Here is a very simple version that you can use:&lt;br /&gt;
&lt;br /&gt;
  # /etc/init.d/functions&lt;br /&gt;
  &lt;br /&gt;
  success() {&lt;br /&gt;
    echo -n &amp;quot;...success&amp;quot;&lt;br /&gt;
  } &lt;br /&gt;
  failure() {&lt;br /&gt;
    echo -n &amp;quot;...failure&amp;quot;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
== Setting up a firewall that allows per-container configuration ==&lt;br /&gt;
&lt;br /&gt;
This setup configures iptables on the HN to disallow access to all hosts, including the containers. However, it allows all traffic into the containers so they may define their own iptables rules and therefore manage their own firewall.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
iptables -P FORWARD ACCEPT&lt;br /&gt;
iptables -F FORWARD&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This will remove all rules for the FORWARD chain so all packets can pass back and forth between containers and the outside world.&lt;br /&gt;
&lt;br /&gt;
If you want to use a firewall inside a container, please load these modules BEFORE starting the container:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
modprobe xt_tcpudp&lt;br /&gt;
modprobe ip_conntrack&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you do not, you will get an error like this: &amp;quot;iptables: No chain/target/match by that name&amp;quot;&lt;br /&gt;
&lt;br /&gt;
If you want to use stateful firewall rules (and you should!) you will also need to make sure that 'ipt_state' is in the 'IPTABLES' option in your vz.conf file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
IPTABLES=&amp;quot;ipt_REJECT ipt_tos ipt_limit ipt_multiport iptable_filter iptable_mangle ipt_TCPMSS ipt_tcpmss ipt_ttl ipt_length ipt_state&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Also make sure the 'xt_state' module is loaded on the host:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
modprobe xt_state&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
&lt;br /&gt;
* [[Traffic accounting with iptables]]&lt;br /&gt;
&lt;br /&gt;
[[Category: Networking]]&lt;/div&gt;</summary>
		<author><name>DmitryKoterov</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.openvz.org/index.php?title=Setting_up_an_iptables_firewall&amp;diff=9337</id>
		<title>Setting up an iptables firewall</title>
		<link rel="alternate" type="text/html" href="https://wiki.openvz.org/index.php?title=Setting_up_an_iptables_firewall&amp;diff=9337"/>
		<updated>2010-11-11T23:00:21Z</updated>

		<summary type="html">&lt;p&gt;DmitryKoterov: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This document consists of two parts. The first is setting up a firewall (using iptables) on the HN, which will restrict traffic to the containers. The effect would emulate, as far as the containers and their customers are concerned, an external hardware firewall controlled by the sysadmin. The second is setting up a firewall that protects the [[HN]] itself but still allows traffic to the containers, thus allowing individual containers to define their own iptables.&lt;br /&gt;
&lt;br /&gt;
While the firewalls shown here can be accomplished using iptables manually (or using Fedora core's iptables service), the methods presented here are especially modular and easy to modify. This is important when you have 20+ containers and a lot of other things to be doing...&lt;br /&gt;
&lt;br /&gt;
The scripts and pathnames given here are for Fedora Core 6, though they can probably be applied to most similar SysV-like systems with little modification.&lt;br /&gt;
&lt;br /&gt;
== A little background ==&lt;br /&gt;
&lt;br /&gt;
On our systems, we use the HN to provide privileged services which are not appropriate for access by the containers. For example, the HN acts as a backup server, runs Nagios for health monitoring, has a webserver for managing the 3ware RAID controller, etc. The containers are leased to customers, who can't entirely be trusted, especially if they get hacked. As such, our scenario is one in which the HN must be protected from all access (even from the containers) except for a few trusted hosts (e.g. my home-office).&lt;br /&gt;
&lt;br /&gt;
The exception to this is the nameserver, which we want open to the world. We use it as a caching nameserver for our containers and also to host DNS for a few customer domain.&lt;br /&gt;
&lt;br /&gt;
== Simple firewall configuration independent to IP addresses ==&lt;br /&gt;
&lt;br /&gt;
''Vzfirewall'' tool allows you to open/close ports for incoming connections with no dependencies to foreign IP addresses. E.g. you may allow a hostname ''release.prod.example.com'' to connect to VE 1234 by modifying 1234.conf file adding multiline ''FIREWALL'' directive into it:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
...&lt;br /&gt;
PRIVVMPAGES=&amp;quot;300000:300000&amp;quot;&lt;br /&gt;
HOSTNAME=&amp;quot;example.com&amp;quot;&lt;br /&gt;
...&lt;br /&gt;
FIREWALL=&amp;quot;&lt;br /&gt;
    ...&lt;br /&gt;
    # Allow access to PostgreSQL port only from release.prod machine.&lt;br /&gt;
    # Note that you may use domain names here.&lt;br /&gt;
    [5432]&lt;br /&gt;
    release.prod.example.com&lt;br /&gt;
    release.test.example.com&lt;br /&gt;
    ...&lt;br /&gt;
&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note that you may use hostnames instead of IP addresses, so the configuration is persistent fore VE movements to different IP-address.&lt;br /&gt;
&lt;br /&gt;
Vzfirewall and its documentation tool is available at [http://en.dklab.ru/lib/dklab_vzfirewall/ vzfirewall homepage.&lt;br /&gt;
&lt;br /&gt;
== An alternative from the author of Shorewall ==&lt;br /&gt;
&lt;br /&gt;
For those who might find the solution provided in this wiki article unsatisfactory (for whatever reason), the creator of Shorewall (Tom Eastep) has written a nice article explaining how to use Shorewall on an OpenVZ host node to manage the host node, containers, and more... with quite a complex setup as an example.  The article IS NOT an introduction to Shorewall for beginners, so some pre-existing knowledge and understanding of Shorewall may be required.&lt;br /&gt;
&lt;br /&gt;
Shorewall and OpenVZ by Tom Eastep - http://www.shorewall.net/OpenVZ.html&lt;br /&gt;
&lt;br /&gt;
See also this OpenVZ Forum posting - http://forum.openvz.org/index.php?t=msg&amp;amp;goto=16406&amp;amp;&lt;br /&gt;
&lt;br /&gt;
== Setting up a HN-based firewall ==&lt;br /&gt;
&lt;br /&gt;
This setup emulates (to the containers anyway) an external hardware firewall. It protects the HN from any access and then defines what services and ports are allowed/banned for individual containers. This leaves the firewall controlled by the site administrator, not be individual containers and the hackers who've gotten into them. ;)&lt;br /&gt;
&lt;br /&gt;
First off, let's disable Fedora's existing &amp;lt;code&amp;gt;iptables&amp;lt;/code&amp;gt; service:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
service iptables stop&lt;br /&gt;
chkconfig iptables off&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now create the new &amp;lt;code&amp;gt;firewall&amp;lt;/code&amp;gt; service. This code should be &amp;lt;code&amp;gt;/etc/init.d/firewall&amp;lt;/code&amp;gt; and then should be chmod'd 755.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/bin/sh&lt;br /&gt;
# firewall      Start iptables firewall&lt;br /&gt;
# chkconfig: 2345 08 92&lt;br /&gt;
# description:  Starts, stops and saves iptables firewall&lt;br /&gt;
# This script sets up the firewall for the INPUT chain (which is for&lt;br /&gt;
# the HN itself) and then processes the config files under&lt;br /&gt;
# /etc/firewall.d to set up additional rules in the FORWARD chain&lt;br /&gt;
# to allow access to containers' services.&lt;br /&gt;
&lt;br /&gt;
. /etc/init.d/functions&lt;br /&gt;
&lt;br /&gt;
# the IP block allocated to this server&lt;br /&gt;
SEGMENT=&amp;quot;192.168.0.0/24&amp;quot;&lt;br /&gt;
# the IP used by the hosting server itself&lt;br /&gt;
THISHOST=&amp;quot;192.168.0.1&amp;quot;&lt;br /&gt;
# services that should be allowed to the HN;&lt;br /&gt;
# services for containers are configured in /etc/firewall.d/*&lt;br /&gt;
OKPORTS=&amp;quot;53&amp;quot;&lt;br /&gt;
# hosts allowed full access through the firewall,&lt;br /&gt;
# to all containers and to this server&lt;br /&gt;
DMZS=&amp;quot;12.34.56.78 90.123.45.67&amp;quot;&lt;br /&gt;
&lt;br /&gt;
purge() {&lt;br /&gt;
  echo -n &amp;quot;Firewall: Purging and allowing all traffic&amp;quot;&lt;br /&gt;
  iptables -P OUTPUT ACCEPT&lt;br /&gt;
  iptables -P FORWARD ACCEPT&lt;br /&gt;
  iptables -P INPUT ACCEPT&lt;br /&gt;
  iptables -F&lt;br /&gt;
  success ; echo&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
setup() {&lt;br /&gt;
  echo -n &amp;quot;Firewall: Setting default policies to DROP&amp;quot;&lt;br /&gt;
  iptables -P INPUT DROP&lt;br /&gt;
  iptables -P FORWARD DROP&lt;br /&gt;
  iptables -I INPUT   -j ACCEPT -m state --state ESTABLISHED,RELATED&lt;br /&gt;
  iptables -I FORWARD -j ACCEPT -m state --state ESTABLISHED,RELATED&lt;br /&gt;
  iptables -I INPUT -j ACCEPT -i lo&lt;br /&gt;
  iptables -I FORWARD -j ACCEPT --source $SEGMENT&lt;br /&gt;
  success ; echo&lt;br /&gt;
&lt;br /&gt;
  echo &amp;quot;Firewall: Allowing access to HN&amp;quot;&lt;br /&gt;
  for port in $OKPORTS ; do&lt;br /&gt;
    echo -n &amp;quot;          port $port&amp;quot;&lt;br /&gt;
    iptables -I INPUT -j ACCEPT -s $SEGMENT -d $THISHOST --protocol tcp --destination-port $port&lt;br /&gt;
    iptables -I INPUT -j ACCEPT -s $SEGMENT -d $THISHOST --protocol udp --destination-port $port&lt;br /&gt;
    success ; echo&lt;br /&gt;
  done&lt;br /&gt;
  for ip in $DMZS ; do&lt;br /&gt;
    echo -n &amp;quot;          DMZ $ip&amp;quot;&lt;br /&gt;
    iptables -I INPUT   -i eth0 -j ACCEPT -s $ip&lt;br /&gt;
    iptables -I FORWARD -i eth0 -j ACCEPT -s $ip&lt;br /&gt;
    success ; echo&lt;br /&gt;
  done&lt;br /&gt;
&lt;br /&gt;
  CTSETUPS=`echo /etc/firewall.d/*`&lt;br /&gt;
  if [ &amp;quot;$CTSETUPS&amp;quot; != &amp;quot;/etc/firewall.d/*&amp;quot; ] ; then&lt;br /&gt;
  echo &amp;quot;Firewall: Setting up container firewalls&amp;quot;&lt;br /&gt;
  for i in $CTSETUPS ; do&lt;br /&gt;
    . $i&lt;br /&gt;
    echo -n &amp;quot;          $CTNAME CT$CTID&amp;quot;&lt;br /&gt;
    if [ -n &amp;quot;$BANNED&amp;quot; ]; then&lt;br /&gt;
      for source in $BANNED ;  do iptables -I FORWARD -j DROP --destination $CTIP --source $source ; done&lt;br /&gt;
    fi&lt;br /&gt;
    if [ -n &amp;quot;$OPENPORTS&amp;quot; ]; then&lt;br /&gt;
      for port in $OPENPORTS ; do iptables -I FORWARD -j ACCEPT --protocol tcp --destination $CTIP --destination-port $port ; done&lt;br /&gt;
      for port in $OPENPORTS ; do iptables -I FORWARD -j ACCEPT --protocol udp --destination $CTIP --destination-port $port ; done&lt;br /&gt;
    fi&lt;br /&gt;
    if [ -n &amp;quot;$DMZS&amp;quot; ]; then&lt;br /&gt;
      for source in $DMZS ; do iptables -I FORWARD -j ACCEPT --protocol tcp --destination $CTIP --source $source ; done&lt;br /&gt;
      for source in $DMZS ; do iptables -I FORWARD -j ACCEPT --protocol udp --destination $CTIP --source $source ; done&lt;br /&gt;
    fi&lt;br /&gt;
    [ $? -eq 0 ] &amp;amp;&amp;amp; success || failure&lt;br /&gt;
    echo&lt;br /&gt;
  done&lt;br /&gt;
  fi&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
case &amp;quot;$1&amp;quot; in&lt;br /&gt;
  start)&lt;br /&gt;
    echo &amp;quot;Starting firewall...&amp;quot;&lt;br /&gt;
    purge&lt;br /&gt;
    setup&lt;br /&gt;
    ;;&lt;br /&gt;
  stop)&lt;br /&gt;
    echo &amp;quot;Stopping firewall...&amp;quot;&lt;br /&gt;
    purge&lt;br /&gt;
    ;;&lt;br /&gt;
  restart)&lt;br /&gt;
    $0 stop&lt;br /&gt;
    $0 start&lt;br /&gt;
    ;;&lt;br /&gt;
  status)&lt;br /&gt;
    iptables -n -L&lt;br /&gt;
    ;;&lt;br /&gt;
  *)&lt;br /&gt;
    echo &amp;quot;Usage: $0 &amp;lt;start|stop|restart|status&amp;gt;&amp;quot;&lt;br /&gt;
    ;;&lt;br /&gt;
esac&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note: This will only allow access to the HN from the hosts/networks defined in SEGMENT. If you'd like to open up the OKPORTS on the HN to everybody, you can remove the ''-s $SEGMENT'' parameters from the iptables commands under the &amp;quot;Firewall: Allowing access to HN&amp;quot; section. The modified lines would look like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
iptables -I INPUT -j ACCEPT -d $THISHOST --protocol tcp --destination-port $port&lt;br /&gt;
iptables -I INPUT -j ACCEPT -d $THISHOST --protocol udp --destination-port $port&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above script can be called like this:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
service firewall start&lt;br /&gt;
service firewall stop&lt;br /&gt;
service firewall restart&lt;br /&gt;
service firewall status&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It will set up the firewall for the HN according to the parameters you specified for OKPORTS, DMZs, etc. and then it will call each file under /etc/firewall.d and process  its configuration.&lt;br /&gt;
&lt;br /&gt;
So create a file under /etc/firewall.d The exact filename isn't important, as long as it's meaningful to you, e.g. &amp;lt;code&amp;gt;ExampleCompany&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;ve12&amp;lt;/code&amp;gt; and give it content like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# This file is processed by /etc/init.d/firewall&lt;br /&gt;
CTID=&amp;quot;1&amp;quot;			# the container's ID#&lt;br /&gt;
CTNAME=&amp;quot;Customer1&amp;quot;		# A human-friendly label for the container&lt;br /&gt;
CTIP=&amp;quot;192.168.1.34&amp;quot;		# the IP address for this container &lt;br /&gt;
OPENPORTS=&amp;quot;80 443&amp;quot;		# ports that should be universally opened&lt;br /&gt;
				# to the entire Internet&lt;br /&gt;
DMZS=&amp;quot;1.2.3.0/24 5.6.7.8/32&amp;quot;	# IPs and blocks that should have full access&lt;br /&gt;
				# to the container's services&lt;br /&gt;
BANNED=&amp;quot;&amp;quot;			# IPs and blocks that should be entirely&lt;br /&gt;
				# blocked from the container's services&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And there you go. Go ahead and start the firewall and check its status:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
service firewall restart&lt;br /&gt;
service firewall status&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As you can see, you can now add and edit the configurations for individual containers very easily. This method proves a lot easier to manage than Fedora's iptables-config mechamism!&lt;br /&gt;
&lt;br /&gt;
To make the firewall service automatically start when the HN boots, run&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
chkconfig --add firewall&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Debian Notes ===&lt;br /&gt;
&lt;br /&gt;
The setup above works fine for Debian as well, however /etc/init.d/functions is missing. Here is a very simple version that you can use:&lt;br /&gt;
&lt;br /&gt;
  # /etc/init.d/functions&lt;br /&gt;
  &lt;br /&gt;
  success() {&lt;br /&gt;
    echo -n &amp;quot;...success&amp;quot;&lt;br /&gt;
  } &lt;br /&gt;
  failure() {&lt;br /&gt;
    echo -n &amp;quot;...failure&amp;quot;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
== Setting up a firewall that allows per-container configuration ==&lt;br /&gt;
&lt;br /&gt;
This setup configures iptables on the HN to disallow access to all hosts, including the containers. However, it allows all traffic into the containers so they may define their own iptables rules and therefore manage their own firewall.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
iptables -P FORWARD ACCEPT&lt;br /&gt;
iptables -F FORWARD&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This will remove all rules for the FORWARD chain so all packets can pass back and forth between containers and the outside world.&lt;br /&gt;
&lt;br /&gt;
If you want to use a firewall inside a container, please load these modules BEFORE starting the container:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
modprobe xt_tcpudp&lt;br /&gt;
modprobe ip_conntrack&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you do not, you will get an error like this: &amp;quot;iptables: No chain/target/match by that name&amp;quot;&lt;br /&gt;
&lt;br /&gt;
If you want to use stateful firewall rules (and you should!) you will also need to make sure that 'ipt_state' is in the 'IPTABLES' option in your vz.conf file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
IPTABLES=&amp;quot;ipt_REJECT ipt_tos ipt_limit ipt_multiport iptable_filter iptable_mangle ipt_TCPMSS ipt_tcpmss ipt_ttl ipt_length ipt_state&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Also make sure the 'xt_state' module is loaded on the host:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
modprobe xt_state&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
&lt;br /&gt;
* [[Traffic accounting with iptables]]&lt;br /&gt;
&lt;br /&gt;
[[Category: Networking]]&lt;/div&gt;</summary>
		<author><name>DmitryKoterov</name></author>
		
	</entry>
</feed>