<?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=Mmh</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=Mmh"/>
	<link rel="alternate" type="text/html" href="https://wiki.openvz.org/Special:Contributions/Mmh"/>
	<updated>2026-05-15T18:50:30Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.31.1</generator>
	<entry>
		<id>https://wiki.openvz.org/index.php?title=Using_NAT_for_container_with_private_IPs&amp;diff=6310</id>
		<title>Using NAT for container with private IPs</title>
		<link rel="alternate" type="text/html" href="https://wiki.openvz.org/index.php?title=Using_NAT_for_container_with_private_IPs&amp;diff=6310"/>
		<updated>2008-08-11T21:37:46Z</updated>

		<summary type="html">&lt;p&gt;Mmh: typo correction&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Usually you supply public IP addresses to your containers. Sometimes you don't want to do it (lack of IPs, etc.). This article describes how to use private IP addresses for containers.&lt;br /&gt;
&lt;br /&gt;
== Prerequisites ==&lt;br /&gt;
&lt;br /&gt;
=== IP forwarding ===&lt;br /&gt;
IP forwarding should be turned on, on the hardware node in order for container networking to work. Make sure it is turned on:&lt;br /&gt;
&lt;br /&gt;
 $ cat /proc/sys/net/ipv4/ip_forward &lt;br /&gt;
 1&lt;br /&gt;
&lt;br /&gt;
Output should be '1'. If it is '0', enable IP forwarding as it is described in [[Quick installation#sysctl]].&lt;br /&gt;
&lt;br /&gt;
NOTE: '''Ubuntu''' made some changes to the syntax for NAT. See this link if you are needing to enable NAT on an Ubuntu host :&lt;br /&gt;
&lt;br /&gt;
[https://bugs.launchpad.net/ubuntu/+source/procps/+bug/84537 Launchpad]&lt;br /&gt;
&lt;br /&gt;
The syntax of /etc/sysctl.conf has changed to :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;net.ipv4.conf.default.forwarding=1&lt;br /&gt;
net.ipv4.conf.all.forwarding=1&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== IP conntracks ===&lt;br /&gt;
IP connection tracking should be enabled for CT0.&lt;br /&gt;
&lt;br /&gt;
'''For OpenVZ kernels 2.6.8''', put the following line into /etc/modprobe.conf:&lt;br /&gt;
&lt;br /&gt;
 modprobe ip_conntrack ip_conntrack_enable_ve0=1&lt;br /&gt;
&lt;br /&gt;
and reboot.&lt;br /&gt;
&lt;br /&gt;
'''For OpenVZ kernels later than 2.6.8''', connection tracking for CT0 is enabled by default. '''However''', make sure there is '''no''' line like&lt;br /&gt;
&lt;br /&gt;
 options ip_conntrack ip_conntrack_disable_ve0=1&lt;br /&gt;
&lt;br /&gt;
in /etc/modules.conf or /etc/modprobe.conf. If there is such line, comment it out (or remove) and reboot.&lt;br /&gt;
&lt;br /&gt;
== How to provide access for container to Internet ==&lt;br /&gt;
&lt;br /&gt;
To enable the [[container]]s, which have only internal IP addresses, to access the Internet, SNAT (Source Network Address Translation, also known as IP masquerading) should be configured on the [[Hardware Node]]. This is ensured by the standard Linux &amp;lt;tt&amp;gt;iptables&amp;lt;/tt&amp;gt; utility. To perform a simple SNAT setup, execute the following command on the [[Hardware Node]]:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# iptables -t nat -A POSTROUTING -s src_net -o eth0 -j SNAT --to ip_address&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where &amp;lt;tt&amp;gt;src_net&amp;lt;/tt&amp;gt; is a range of IP addresses of containers to be translated by SNAT, and &amp;lt;tt&amp;gt;ip_address&amp;lt;/tt&amp;gt; is the external IP address of your [[Hardware Node]]. The format of src_net is xx.xx.xx.xx/xx.  For example to specify IP addresses from 192.168.2.12 through 192.168.2.25 use:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# iptables -t nat -A POSTROUTING -s 192.168.2.12/25 -o eth0 -j SNAT --to ip_address&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Multiple rules are allowed, for example, in case you wish to specify several ranges of IP addresses. If you are using a number of physical network interfaces on the [[Hardware Node|Node]], you may need to specify a different interface for outgoing connections, e.g. &amp;lt;tt&amp;gt;-o eth2&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
To make all IP addresses to be translated by SNAT (not only the ones of [[container]]s with private addresses), you should type the following string:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# iptables -t nat -A POSTROUTING -o eth0 -j SNAT --to ip_address&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{Note|If the above is not working then check if one of the following solutions does the trick.}}&lt;br /&gt;
1. If you are using stable (currently 2.6.8-based) kernel, then to enable SNAT for the containers on your local network you need to explicitly enable connection tracking in [[CT0]]. Make sure that the following string is present in the &amp;lt;tt&amp;gt;/etc/modprobe.conf&amp;lt;/tt&amp;gt; file:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
options ip_conntrack ip_conntrack_enable_ve0=1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{Note|in kernels later than 2.6.8, connection tracking is enabled by default}}&lt;br /&gt;
&lt;br /&gt;
In case it is not, add this string to the file by means of any text editor (for example, vi). This setting is not needed for kernels more recent than 2.6.8, since connection tracking for [[CT0]] is enabled by default in those kernels.&lt;br /&gt;
&lt;br /&gt;
2. For unknown reasons the above didn't work on a Debian host. The solution is to do it in an init.d script as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
modprobe ip_conntrack ip_conntrack_enable_ve0=1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Make sure that this module is loaded before any of the other iptables-modules are loaded! Also remember that if this module is loaded without the option, unloading and reloading doesn't work! You need to reboot the computer.&lt;br /&gt;
&lt;br /&gt;
{{Note|in kernels later than 2.6.8, connection tracking is enabled by default}}&lt;br /&gt;
&lt;br /&gt;
{{Note| you may need to modify the /etc/vz/vz.conf file to include:}} &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
IPTABLES=&amp;quot;iptable_filter iptable_mangle ipt_limit ipt_multiport ipt_tos ipt_TOS ipt_REJECT ipt_TCPMSS ipt_tcpmss ipt_ttl ipt_LOG ipt_length ip_conntrack ip_conntrack_ftp ip_conntrack_irc ipt_conntrack ipt_state ipt_helper iptable_nat ip_nat_ftp ip_nat_irc ipt_REDIRECT&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== How to provide access from Internet to a container ==&lt;br /&gt;
&lt;br /&gt;
In addition, to make some services in container with private IP address be accessible from the Internet, DNAT (Destination Network Address Translation) should be configured on the [[Hardware Node]]. To perform a simple DNAT setup, execute the following command on the [[Hardware Node]]:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# iptables -t nat -A PREROUTING -p tcp -d ip_address --dport port_num \&lt;br /&gt;
  -i eth0 -j DNAT --to-destination ve_address:dst_port_num &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where &amp;lt;tt&amp;gt;ve_address&amp;lt;/tt&amp;gt; is an IP address of the container, &amp;lt;tt&amp;gt;dst_port_num&amp;lt;/tt&amp;gt; is a tcp port which requires service use, &amp;lt;tt&amp;gt;ip_address&amp;lt;/tt&amp;gt; is the external (public) IP address of your [[Hardware Node]], and &amp;lt;tt&amp;gt;port_num&amp;lt;/tt&amp;gt; is a tcp port of [[Hardware Node]], which will be used for Internet connections to private container service. Note that this setup makes the service which is using &amp;lt;tt&amp;gt;port_num&amp;lt;/tt&amp;gt; on the [[Hardware Node]] be unaccessible from the Internet. Also note that SNAT translation is required too.&lt;br /&gt;
&lt;br /&gt;
For example, if you need a web server in a container to be accessible from outside and, at the same time, keep a web server on the [[Hardware Node]] be accessible, use the following config:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# iptables -t nat -A PREROUTING -p tcp -d ip_address --dport 8080 \&lt;br /&gt;
  -i eth0 -j DNAT --to-destination ve_address:80&lt;br /&gt;
# iptables -t nat -A POSTROUTING -s ve_address -o eth0 -j SNAT --to ip_address&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
After applying this, you'll see container' web server at &amp;lt;code&amp;gt;&amp;lt;nowiki&amp;gt;http://ip_address:8080/&amp;lt;/nowiki&amp;gt;&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
{{Note|this rule will only work for external clients, i.e. connections originating from a different host — so you can not test if it works locally.}}&lt;br /&gt;
&lt;br /&gt;
{{Note|If you get any errors relating to: &lt;br /&gt;
&amp;lt;code&amp;gt;iptables: No chain/target/match by that name&amp;lt;/code&amp;gt;&lt;br /&gt;
double check to see if you have all the iptables/netfilter modules loaded properly. I had to &amp;lt;code&amp;gt; modprobe xt_tcpudp &amp;lt;/code&amp;gt; before getting it to work.}}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;tt&amp;gt;iptables&amp;lt;/tt&amp;gt; utility allows to set up more complex rules for Network Address Translation, involving various protocols and ports. If you wish to get more information on this, consult the numerous Internet sites (e.g. [http://www.netfilter.org netfilter.org]) and tutorials devoted to this issue.&lt;br /&gt;
&lt;br /&gt;
== External Links ==&lt;br /&gt;
* [http://www.netfilter.org netfilter.org]&lt;br /&gt;
* [[w:Private network]]&lt;br /&gt;
&lt;br /&gt;
[[Category: HOWTO]]&lt;br /&gt;
[[Category: Networking]]&lt;/div&gt;</summary>
		<author><name>Mmh</name></author>
		
	</entry>
</feed>