Author Archives: James Coyle

Apache – redirect traffic to a different url

Get Social!

Some web applications I work with are only available on a URL similar to http://hostname/application. This causes problems when giving the URL to users as they sometimes forget the /application part and receive an unhelpful page they are not looking for or worse, an error.

Using mod_rewrite in Apache2 we can force any traffic matching a specific URL to another URL of our choosing. For this example, we want to direct users landing on / to /application. Notice these URL strings only need to include the path.

Make sure mod_rewrite is enabled in you Apache2 configuration. On Debian flavour distributions you can use

a2enmod rewrite

For Red Hat type distributions, you need to uncomment the line containing mod_rewrite.so in /etc/httpd/conf/httpd.conf.

A basic redirect matching rule has two components. What URL to look for when redirecting, and where to send the traffic.

Edit the vhost file which you would like to include the redirect. For example:

/etc/apache2/sites-available/default

And add the following inside the <VirtualHost> tags.

RewriteEngine  on
RewriteRule ^[FROM]$ [TO] [R=301,L]

You will need to replace [FROM] with the url you would like to direct and [TO] should be the URL of where to send the user. For example, the below rule redirects users going to / to /myapplication

RewriteEngine  on
RewriteRule ^/$ /myapplication [R=301,L]

 


iptables cheat sheet

Get Social!

Here are a few handy commands for using iptables. They are tailored for an OpenVZ container with a venet network interface but can easily be adapted to use your interface by replacing venet0 with your network interface.

For setting up iptables in an openVZ this blog post.

iptables console

Remove existing rules

You can easily delete all existing rules in iptables. Be careful using this command, there is no going back unless you have backed up your rules.

iptables -F

Backup and restore

Backup to file

Rules can easily be saved to an external file for backups or outputting for version control. This will save the rules to /etc/iptables.rules.

iptables-save -c > /etc/iptables.rules

Restore from file

Saved settings can be restored with the following command:

iptables-restore > /etc/iptables.rules

Change the default policy

The default policy can be changed to specify what should happen to traffic which doesn’t have a rule to explicitly define what to do. You can specify to ACCEPT, REJECT or DROP for INPUT, FORWARD and OUTPUT.

Change OUTPUT to ACCEPT

iptables -P OUTPUT ACCEPT

Change INPUT to DROP

iptables -P INPUT DROP

Apply the catchall rule

You can add a rule to the bottom of the rule book to choose what to do with traffic which doesn’t match any other rule. A common use would be to add a DROP as the last rule to drop any traffic which isn’t explicitly allowed by an earlier rule. You can specify to ACCEPT, REJECT or DROP for INPUT, FORWARD and OUTPUT as well as an interface.

ACCEPT INPUT on interface lo

iptables -A INPUT -i lo -j ACCEPT

BLOCK INPUT on interface venet0

iptables -A INPUT -i venet0 -j DROP

List active rules

You can list the active rules with -L, and -v for information on packets affected.

iptables -L -v

Enable established connections rule

Already established connections will not be affected by adding this inbound rule. Traffic affected by other outbound rules will also be honoured. If you add this rule, you won’t likely need to specify inbound rules for many outbound rules.

iptables -A INPUT -i venet0 -m state --state RELATED,ESTABLISHED -j ACCEPT

Adding new rules

New rules can be added to control how traffic passes through an interface. If you have not used the Established connections rule then you will also need to add an outgoing rule. If you do use the Established connections rule then you will only need the top command in each of the below headings.

Add rule for port 80 – such as Apache

iptables -A INPUT -i venet0 -p tcp --dport 80 -m state --state NEW,ESTABLISHED -j ACCEPT
iptables -A OUTPUT -o venet0 -p tcp --sport 80 -m state --state ESTABLISHED -j ACCEPT

Add rule for port 22 – SSH outbound connections

iptables -A OUTPUT -o venet0 -p tcp --dport 22 -m state --state NEW,ESTABLISHED -j ACCEPT
iptables -A INPUT -i venet0 -p tcp --sport 22 -m state --state ESTABLISHED -j ACCEPT

Add rule for 53 – DNS outbound

iptables -A OUTPUT -o venet0 -p udp -o eth0 --dport 53 -j ACCEPT
iptables -A INPUT -i venet0-p udp -i eth0 --sport 53 -j ACCEPT

Add rule for port 22 – SSH inbound connections

iptables -A INPUT -i venet0 -p tcp --dport 22 -m state --state NEW,ESTABLISHED -j ACCEPT
iptables -A OUTPUT -o venet0 -p tcp --sport 22 -m state --state ESTABLISHED -j ACCEPT

Add rule for outgoing http/ https

iptables -A OUTPUT -o venet0 -p tcp -m multiport --dport 80,443 -m state --state NEW,ESTABLISHED -j ACCEPT
iptables -A INPUT -i venet0 -p tcp -m multiport --sports 80,443 -m state --state ESTABLISHED -j ACCEPT

Add rule for ping from remote to local

iptables -A INPUT -i venet0 -p icmp --icmp-type echo-request -j ACCEPT
iptables -A OUTPUT -o ventet -p icmp --icmp-type echo-reply -j ACCEPT

 Add rule for ping from local to remote

iptables -A OUTPUT -o venet0 -p icmp --icmp-type echo-request -j ACCEPT
iptables -A INPUT -i venet0 -p icmp --icmp-type echo-reply -j ACCEPT

Delete rule

The easiest way to remove a rule is to delete it by it’s rule position in the list. To find out the rules position run iptables -L with the line-number argument. The below command is to delete an INPUT command however you can easily change INPUT to OUTPUT as required.

iptables -L INPUT --line-numbers

Then run the delete command for the relevent direction. This will delete the 7th inbound rule.

iptables -D INPUT 7

Example deleting rule 1 for INPUT:

# iptables -L INPUT --line-numbers
Chain INPUT (policy DROP)
num  target     prot opt source               destination
1    ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:ssh state NEW,ESTABLISHED
2    ACCEPT     all  --  anywhere             anywhere             state RELATED,ESTABLISHED
3    ACCEPT     icmp --  anywhere             anywhere             icmp echo-reply
4    ACCEPT     icmp --  anywhere             anywhere             icmp echo-request

# iptables -D INPUT 1

# iptables -L INPUT --line-numbers
Chain INPUT (policy DROP)
num  target     prot opt source               destination
1    ACCEPT     all  --  anywhere             anywhere             state RELATED,ESTABLISHED
2    ACCEPT     icmp --  anywhere             anywhere             icmp echo-reply
3    ACCEPT     icmp --  anywhere             anywhere             icmp echo-request

 

Let me know in the comments if you think anything is missing.


iptables in a Ubuntu OpenVZ container

Get Social!

proxmox logo gradIf you need a software firewall to shield containers on a Proxmox stack, you should always use a firewall on the host to decide what traffic is allowed for each container. This brings some obvious benefits such as it’s centrally managed – one configuration location for all containers on the node, and security as a compromised container cannot change firewall settings.

However, in Proxmox 3.0+ you can use iptables in a container which also has it’s own benefits under certain circumstances. For example, you can test firewall rules for a new development container without risking other containers on the same host, and you don’t need to give people access to the host to modify the rules.

I have tried iptables using a Ubuntu 12.04 container template. It works as expected but requires some setup on both the guest container and the Proxmox host.

Setup

Proxmox – steps to perform on the Proxmox host

You will need to enable containers access to the required kernel modules. To do this, edit the vz config file:

vi /etc/vz/vz.conf

And edit the IPTABLES= line as below.

IPTABLES="ipt_REJECT ipt_tos ipt_limit ipt_multiport iptable_filter iptable_mangle ipt_TCPMSS ipt_tcpmss ipt_ttl ipt_length ipt_state"

Make sure the required modules are loaded by running the following in a console window as root:

modprobe xt_state
modprobe xt_tcpudp
modprobe ip_conntrack

 Container – steps to perform in the Ubuntu container

First, you need a console window in the host. Either use the GUI console window or use vzctl enter [VMID] to login to the 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. More about that in my iptables cheat sheet.


Enable Proxmox installation debugger

Get Social!

Although installations of Proxmox usually go smoothly, sometimes you may have problems detecting storage arrays or other hardware. By default the installer doesn’t give much away about any problems, so in the event of a problem you need to enable debugging.

To enable debugging, you will need to type debug at the installation boot menu.

Proxmox boot install screen debug

You can mix this with other arguments to over ride the defaults. See Advanced install settings for more information.

You will then be taken to a console where you can run any pre diagnostic commands. Press CTRL + D to continue the installation once you have finished with the terminal. If the installer errors, click Abort to return to a console.

Proxmox boot install screen debug console


Proxmox advanced install settings

Get Social!

proxmox logo gradInstalling Proxmox is quick and easy – the installer GUI takes care of everything, such as installing the correct packages and partitioning the root hard disk.

You can download Proxmox from the Proxmox download page as an ISO which you will need to boot your server from.

See my  Install guide blog post for the basics on installing Proxmox.

On more advanced installs, there may be a need to override some of these options. For example, you may want to specify how much swap space is created, or the size of the root partition. On one of my installs using a 120GB disk as the install device, 28GB is used for my root (/) partition on a default install. As I only run the basic, required packages on the host this size is far too large. With larger disks the problem gets worse.

When installing proxmox, the first screen of the installer is the Boot menu. At this prompt, we can specify arguments to override the defaults.

Proxmox boot install screen

The above example linux ext4 maxroot=10 swapsize=20 sets the partition format to ext4 (ext3 is the default), creates a root partition of 10GB providing the disk is large enough and swapsize of 20GB.

The options available at the boot menu are:

  • linux ext4 – sets the partition format to ext4. The default is ext3.
  • hdsize=nGB – this sets the total amount of hard disk to use for the Proxmox installation. This should be smaller than your disk size.
  • maxroot=nGB – sets the maximum size to use for the root partition. This is the max size so if the disk is too small, the partition may be smaller than this.
  • swapsize=nGB – sets the swap partition size in gigabytes.
  • maxvz-nGB – sets the maximum size in gigabytes that the data partition will be. Again, this is similar to maxroot and the final partition size may be smaller.
  • minfree=nGB – sets the amount of free space to remain on the disk after the Proxmox instillation.

Proxmox 3.0 is now available – with updates!

Category : Tech News

Get Social!

proxmox logo gradThe guys building Proxmox have not been messing about; in the past couple of weeks we have seen a major release of Proxmox V3.0 and a few updates. We now have qemu templates which we can clone as many times as required, as a whole new guest or a thinly provisioned guest linked to the clone. With the latest updates we now have storage migration which enables us to move a qemu guests storage to a new storage type.

You can download the ISO from http://www.proxmox.com/downloads/proxmox-ve/17-iso-images

Highlights of the 3.0 release:

  • Based on Debian 7.0 (Wheezy)
  • new VM clone feature
  • new event driven API server (pveproxy)
    • completely replace apache2
    • efficient support for HTTP keep-alive
  • support bootlogd (boot log can be viewed on the GUI)
  • update qemu-kvm to 1.4.1

And the recent update:

  • Storage migration 

There is an upgrade script to upgrade from version 2.3 to 3 however as the new version is now based on Debian 7.0 instead of Debian 6. To make sure there are no left over packages or files floating around, I would recommend a fresh install of Proxmox version 3.0.

Don’t forget to update once you have installed Proxmox 3.0 with apt-get by using the below commands in a terminal. Before updating, make sure all your VM’s have been stopped. Run the below commands on each server in your cluster.

apt-get update
apt-get dist-upgrade

Restart all Proxmox servers to complete the installation.

If you would like to try the in-place upgrade path, see the Proxmox Wiki for more information.


Visit our advertisers

Quick Poll

What type of VPN protocol do you use?

Visit our advertisers