» iptables in a Ubuntu OpenVZ container

Install iptables using apt-get.
 
apt-get install iptables

Any changes you make to iptables, such as adding new rules, will be lost each time the service is restarted. This is obviously not ideal as all the rules will be lost every time the container reboots. To get round this we need to add a script to save the rules each time the network interface goes down, and one to load the rules when the interface starts up.
 
Create an iptables script to run when the network is started:

vi /etc/network/if-pre-up.d/iptables
 
And add the below script to load the rules into iptables:

#!/bin/sh
iptables-restore < /etc/iptables.rules
exit 0
 
And when the network goes down:

vi /etc/network/if-post-down.d/iptables
 
To save the rules:

#!/bin/sh
iptables-save -c > /etc/iptables.rules
 exit 0

After your network is restarted, the current rules will be saved to /etc/iptables.rules. To add new rules, you can edit this file directly and load the settings or you can use the iptables commands to create the rules you require.

Was this answer helpful?

Add to favorites Add to Favourites
Print Print this Article