Multiple IPs

From LinodeWiki

Jump to: navigation, search

Please see the article in the Linode Library on Configuring Static Networking for Adding Additional IP Addresses to your Linode for more information and additional resources.

Contents

[edit] An explanation

Linodes have one network interface, eth0. When you add IP addresses, you create virtual network interfaces named eth0:1, eth0:2... eth0:n.

There are several ways to add multiple IP addresses, dependent on your chosen distribution...

After you add a new IP address, you'll need to reboot your Linode because the IP can't be routed to your virtual machine instance on the fly while it's running.

If you're currently using DHCP for your IPs, it's a lot easier to switch to all static IPs, rather than a mix of DHCP and static. DHCP will always assign the lowest IP, and if your new one is in a lower subnet than your old one, then you'd better watch out.

NOTE: When using DHCP, your node's hostname is set automatically. So when you switch fully to static to add a new IP, make sure you also add a hostname before rebooting your system to avoid issues.

[edit] The temporary way, works on all distros

From a root prompt:

ifconfig eth0:1 up [IP address] netmask [chosen netmask, if unsure use 255.255.255.0].

You can add commands like this to your startup scripts to have them come up on reboot, but there are cleaner ways which follow.

[edit] Debian way for permanent setup

Edit /etc/network/interfaces

I turned off dhcp because you must use static assignment if you are configuring multiple IPs, so basically this file is:

auto lo
iface lo inet loopback

auto eth0
iface eth0 inet static
    address 99.99.99.99
    netmask 255.255.255.0
    gateway 99.99.99.1
auto eth0:0
iface eth0:0 inet static
    address 23.99.99.100
    netmask 255.255.255.0
#Assuming this is your private IP, you need a different netmask 
auto eth0:1 
iface eth0:1 inet static
    address 192.168.99.101
    netmask 255.255.128.0

Just repeat per ip address and replace 99.99.99.99 with your IP Address and gateway. You should only need to set one default gateway, but if it doesn't work, try adding the gateway to the second IP. (Please update this with your experiences!)

NB: you must reboot your Linode in order to get the second IP up and running, even apart from dhcpclient and other service bindings. Several forums posts confirm this. [1] [2] [3]

Changed the netmask for the private IP to get working, tested on Ubuntu Server 9.04 and 8.04 --Dereks 13:51, 24 July 2009 (EDT)

[edit] Simpler configuration, and controlling the outbound IP

Suppose you want your outbound traffic to go through a specific IP, for example if you SSH tunnel into your node, use it for web browsing, and don't want your primary IP to appear in web server logs because it can be traced back to your domain name via reverse DNS. The /etc/network/interfaces below will configure two IP addresses and direct outgoing traffic to the secondary IP address. You will not have to change anything (e.g. web server virtual host configurations) with regards to incoming connections.

# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).

# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
auto eth0
iface eth0 inet static
    address <the outbound IP>
    netmask <the netmask for the outbound IP, usually 255.255.255.0>
    gateway <the gateway for the outbound IP, find it at https://www.linode.com/members/linode/network.cfm>
    up /sbin/ip addr add <the primary IP>/24 dev eth0

[edit] CentOS/RHEL way for permanent setup

This method describes the basics of setting up the ethernet interface configuration for adding multiple IP addresses to a CentOS Linode distribution. It is taken mainly from Ch. 8 of the RedHat Enterprise Linux Reference Guide.

All interface configuration files are located in /etc/sysconfig/network-scripts/

Each interface is represented by a file corresponding to ifcfg-eth<x> where <x> represents the unique interface number for that card (e.g. the first interface card is represented by ifcfg-eth0).

To create an alias for that interface, you need to create a file in the format of ifcfg-eth0:<y> where <y> represents the alias number (e.g. the 2nd ip for the first card would be ifcfg-eth0:1).

To set up two ip addresses on a linode edit/create the following files as noted (substituting your ip addresses where denoted by x's):

[edit] /etc/sysconfig/network-scripts/ifcfg-eth0

DEVICE=eth0
BOOTPROTO=none
ONBOOT=yes
IPADDR=XXX.XXX.XXX.XXX
NETMASK=255.255.255.0
BROADCAST=XXX.XXX.XXX.255
GATEWAY=XXX.XXX.XXX.1

[edit] /etc/sysconfig/network-scripts/ifcfg-eth0:0

DEVICE=eth0:0
BOOTPROTO=none
ONPARENT=yes
IPADDR=XXX.XXX.XXX.XXX
NETMASK=255.255.255.0
BROADCAST=XXX.XXX.XXX.255

This assumes all ip addresses are in the same range. See the RedHat Enterprise Linux Reference Guide for details on the configurable parameters. Finally, running service network restart will restart the network interfaces (probably best to do this from lish). Hope it helps.

[edit] Gentoo way for permanent setup

  • Make sure you know your IP addresses and default route. This information may be found in the Linode members site by going to Members -> Remote Access.
  • Login via lish (at one point we'll take down the network interface, so this is the easiest way).
  • Edit /etc/conf.d/net.
    • Delete the contents of this file (unless you already have custom changes)
    • There are two configuration directives we are concerned with: config_eth0 and routes_eth0.
    • config_eth0 takes a list of IP addresses in CIDR notation. Assuming your Remote Access page lists your netmask as 255.255.255.0, the CIDR notation for each address is x.x.x.x/24. If you have a different netmask, do a search for 'cidr calcualtor' and find out the /xx part.
    • routes_eth0 requires your gateway IP address. I assume this changes depending on your data center. This IP is also listed on the Remote Access page.

/etc/conf.d/net should look like this (with the appropriate IPs substituted):

config_eth0=(
   "x.x.x.x/24"
   "x.x.x.x/24"
)
routes_eth0=( "default via x.x.x.x" )

Tip: Some more verbose examples can be seen in this article.

  • Stop networking services: /etc/init.d/net.eth0 stop
  • Start networking services: /etc/init.d/net.eth0 start
  • run ifconfig. You should see both 'eth0' and 'eth0:1'.
eth0      Link encap:Ethernet  HWaddr ...
          inet addr:your.first.IP  ...  Mask:255.255.255.0

...

eth0:1    Link encap:Ethernet  HWaddr ...
          inet addr:your.second.IP  ...  Mask:255.255.255.0
  • You're done.

[edit] Slackware Way

Edit /etc/rc.d/rc.inet1.conf to resemble the following:

IFNAME[0]="eth0"            # Your primary IP... You don't really need  to
                            # declare the interface name, but it looks pretty
IPADDR[0]="67.18.208.100"   # Your public IP
NETMASK[0]="255.255.255.0"  # Said IP's netmask
USE_DHCP[0]=""              # The DHCP options should be blanked out, or they
DHCP_HOSTNAME[0]=""         # can be omitted from the file all together

IFNAME[1]="eth0:0"          # Your first alias interface. These need to be specified
                            # because otherwise rc.inet1 will attempt to use "eth$I"
                            # where $I is the array index 
IPADDR[1]="192.168.130.100" # Linode is now recommending that you put your private
                            # IP in as an alias on eth0 due to a bug in Xen
NETMASK[1]="255.255.128.0"  # Netmask

GATEWAY="67.18.208.1"       # Your default gateway. The init script does not seem to
                            # support multiple gateways, so if you need to, you should
                            # add them to rc.local

[edit] Multiple IPs on different subnets

Note: At least one person believes you don't need to do this. Linux is smart enough to just deal with it. In fact, if you add a second gateway, it may well break stuff.
If questions or doubt exist on the below instructions please elaborate, in detail and with specifics, on recommendations for multiple IPs on different subnets on the Talk page or with corrections and explanation in the below text. An anonymous belief regarding a generic distro description is not helpful.

When purchasing additional IPs for your Linode, you may receive an IP on another subnet. This is "luck of the draw" and not intended to make configuration difficult. Two options exist in this event:

[edit] "The Linode Method"

Follow the notes above for adding interfaces and interface aliases for your distribution.

Add the following route commands for each network gateway to /etc/rc.local (or your distros simliar "run command" file):

route add default gw xxx.xxx.xxx.1
route add default gw xxx.xxx.xxx.1

This will likely result in two "defaults." One will always be used, the other, a failover...of sorts. The other default is not really a failover because if the main interface goes down, the aliased interface will go down with it.

See more at Network Settings/Information (linode account and login required).

[edit] "The Load Balanced Method"

This method comes from examples provided in the Linux Advanced Routing and Traffic Control, Chapter 4 HOWTO. "Load Balancing" the two routes assures that both gateways are available to the interface in a default configuration. The routes can be weighted, i.e., "use this route more than the other route.". To add the routes, use the more powerful ip command (arp,route, and ifconfig already use the more advanced syscalls of ip, but with default settings):

# ip route add default scope global \
> nexthop via xxx.xxx.xxx.1 dev eth0 weight 1 \
> nexthop via xxx.xxx.xxx.1 dev eth0 weight 1

The hazard here is that routes are cached. If the aliased interface comes down, you might end up with some difficult to debug behavior, like "route to host not found" errors. The route cache can be flushed however:

# ip route flush cache

If your aliased interface should, for some reason, come down, this is a problem beyond the scope of this entry. You likely will have to restart your network services in a distro specific manner to bring your main interface back with all its aliases.

All of these commands will need to be run as root (#).

[edit] Complications

[edit] Outbound connections

In some circumstances, the new IP on your linode may become the default IP that outgoing connections bind to. This can cause problems if, for example:

  • your mail daemon binds to an address not in your SPF record
  • your DNS daemon binds to an address not configured for notifies and zone transfers on other NS servers
  • you run afoul of any IP ACLs configured elsewhere
  • your reverse DNS is no longer valid
  • your OpenVPN server sends packets from a different address your client connected to

In general, the best solution is to configure your daemons to bind to the specific address you want. Consult the documentation for specifics. Examples of configuration directives for common daemons:

[edit] Postfix

In main.cf:

inet_interfaces = 127.0.0.1, a.b.c.d
smtp_bind_address = a.b.c.d

[edit] BIND

In named.conf in the "options" block:

listen-on {
  a.b.c.d;
};

query-source a.b.c.d;
notify-source a.b.c.d;
transfer-source a.b.c.d;

[edit] OpenVPN

In vpn-name.conf (a.b.c.d matches the address in the "remote" option of the client):

local a.b.c.d
Personal tools