<?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=Luminousbit</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=Luminousbit"/>
	<link rel="alternate" type="text/html" href="https://wiki.openvz.org/Special:Contributions/Luminousbit"/>
	<updated>2026-05-15T14:52:11Z</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=3321</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=3321"/>
		<updated>2007-07-17T16:18:39Z</updated>

		<summary type="html">&lt;p&gt;Luminousbit: Should be 'service iptables stop' not 'off'&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 VEs. The effect would emulate, as far as the VEs 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 VEs, thus allowing individual VEs 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+ VEs 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;
&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 VEs. 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 VEs 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 VEs) 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 VEs and also to host DNS for a few customer domain.&lt;br /&gt;
&lt;br /&gt;
== Setting up a HN-based firewall ==&lt;br /&gt;
&lt;br /&gt;
This setup emulates (to the VEs anyway) an external hardware firewall. It protects the HN from any access and then defines what services and ports are allowed/banned for individual VEs. This leaves the firewall controlled by the site administrator, not be individual VEs 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 the HN itself)&lt;br /&gt;
# and then processes the config files under /etc/firewall.d to set up additional rules&lt;br /&gt;
# in the FORWARD chain to allow access to VEs' 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; services for VEs 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, to all VEs 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;
  VESETUPS=`echo /etc/firewall.d/*`&lt;br /&gt;
  if [ &amp;quot;$VESETUPS&amp;quot; != &amp;quot;/etc/firewall.d/*&amp;quot; ] ; then&lt;br /&gt;
  echo &amp;quot;Firewall: Setting up VE firewalls&amp;quot;&lt;br /&gt;
  for i in $VESETUPS ; do&lt;br /&gt;
    . $i&lt;br /&gt;
    echo -n &amp;quot;          $VENAME VE$VEID&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 $VEIP --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 $VEIP --destination-port $port ; done&lt;br /&gt;
      for port in $OPENPORTS ; do iptables -I FORWARD -j ACCEPT --protocol udp --destination $VEIP --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 $VEIP --source $source ; done&lt;br /&gt;
      for source in $DMZS ; do iptables -I FORWARD -j ACCEPT --protocol udp --destination $VEIP --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;
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;
VEID=&amp;quot;1&amp;quot;                      # the VE's ID#&lt;br /&gt;
VENAME=&amp;quot;Customer1&amp;quot;            # A human-friendly label for the VE&lt;br /&gt;
VEIP=&amp;quot;192.168.1.34&amp;quot;           # the IP address for this VE&lt;br /&gt;
OPENPORTS=&amp;quot;80 443&amp;quot;            # ports that should be universally opened 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 to the VE's services&lt;br /&gt;
BANNED=&amp;quot;&amp;quot;                     # IPs and blocks that should be entirely blocked from the VE'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 VEs very easily. This method proves a lot easier to manage than Fedora's iptables-config mechamism!&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Setting up a firewall that allows per-VE configuration ==&lt;br /&gt;
&lt;br /&gt;
This setup configures iptables on the HN to disallow access to all hosts, including the VEs. However, it allows all traffic into the VEs so they may define their own iptables rules and therefore manage their own firewall.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;This content is missing. You are invited to fill it in, if you get to it before I do. :)&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&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;br /&gt;
[[ Category: Firewalls ]]&lt;/div&gt;</summary>
		<author><name>Luminousbit</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.openvz.org/index.php?title=Resource_management&amp;diff=3302</id>
		<title>Resource management</title>
		<link rel="alternate" type="text/html" href="https://wiki.openvz.org/index.php?title=Resource_management&amp;diff=3302"/>
		<updated>2007-07-11T00:33:52Z</updated>

		<summary type="html">&lt;p&gt;Luminousbit: Added link to traffic shaping&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''OpenVZ resource management''' is a set of controls to provide per-[[VE]] limits and guarantees in a way that multiple VEs will coexist on a single physical server. It consists three primary controllers:&lt;br /&gt;
&lt;br /&gt;
* [[User beancounters]]&lt;br /&gt;
* [[OpenVZ quota system]]&lt;br /&gt;
* [[CPU Fairscheduler]]&lt;br /&gt;
* [[Traffic shaping with tc]]&lt;br /&gt;
&lt;br /&gt;
 FIXME: add more text&lt;br /&gt;
 FIXME: create a right-hand navigation menu for all of the resource management stuff&lt;br /&gt;
&lt;br /&gt;
[[Category:Definitions]]&lt;br /&gt;
[[Category:Kernel]]&lt;/div&gt;</summary>
		<author><name>Luminousbit</name></author>
		
	</entry>
</feed>