Multiple IPs

From LinodeWiki

Jump to: navigation, search

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 UML instance on the fly while it's running.

[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
    gateway 23.99.99.1
auto eth0:1
iface eth0:1 inet static
    address 99.48.99.101
    netmask 255.255.255.0
    gateway 99.48.99.1


Just repeat per ip address and replace 99.99.99.99 with your IP Address and gateway.

If you have a ip address that is on a different range, you'll have to add gateway for each different ip.

[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:0

DEVICE=eth0:0
BOOTPROTO=none
ONBOOT=yes
IPADDR=XXX.XXX.XXX.XXX
NETMASK=255.255.255.0
GATEWAY=XXX.XXX.XXX.1

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

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

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.250, 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):

<pre>
config_eth0=(
   "x.x.x.x/24"
   "x.x.x.x/24"
)

routes_eth0=( "default via x.x.x.x" )
  • 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] Multiple IPs on different subnets

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

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;
Personal tools