现在的位置: 首页 > 综合 > 正文

About IP Alias in Linux (Esp. CentOS)

2013年09月20日 ⁄ 综合 ⁄ 共 4828字 ⁄ 字号 评论关闭
Linux allows you to add additional network address using alias
feature. Please note that all additional network IP address must be in
same subnet. For example if your eth0 using 192.168.1.5 IP address then
alias must be setup using 192.168.1.0/24 subnet.

ifconfig command line

You can use ifconfig command to configure a network interface and alias. For example:

  • eth0 NIC IP 192.168.1.5
  • eth0:0 first NIC alias: 192.168.1.6

To setup eth0:0 alias type the following command as the root user:
# ifconfig eth0:0 192.168.1.6 up

Verify alias is up and running using following command:
# ifconfig -a
# ping 192.168.1.6

However, if you reboot the system you will lost all your alias. To make
it permanent you need to add it network configuration file.

Debian / Ubuntu Linux Instructions

You can configure the additional IP addresses automatically at boot with another iface statement in /etc/network/interfaces:

# vi /etc/network/interfaces

Append text as follows:

auto eth0:1
iface eth0:1 inet static
name Ethernet alias LAN card
address 192.168.1.7
netmask 255.255.255.0
broadcast 192.168.1.255
network 192.168.1.0

Save and close the file. Restart the network:
# /etc/init.d/networking restart

Red Hat / RHEL / CentOS / Fedora Linux Instructions

Copy etc/sysconfig/network-scripts/ifcfg-eth0 file as /etc/sysconfig/network-scripts/ifcfg-eth0:0
# cp /etc/sysconfig/network-scripts/ifcfg-eth0 /etc/sysconfig/network-scripts/ifcfg-eth0:0
Open file /etc/sysconfig/network-scripts/ifcfg-eth0:0 using vi text editor:
# vi /etc/sysconfig/network-scripts/ifcfg-eth0:0
Find entry that read as follows:

DEVICE=eth0

Replace with:

DEVICE=eth0:0

Find entry that read as follows:

IPADDR=xxx.xxx.xxx.xxx

Replace it with your actual IP address:

IPADDR=192.168.1.7

At the end your file should like as follows:

DEVICE=eth0:0
IPADDR=192.168.1.7
NETMASK=255.255.255.0
NETWORK=192.168.1.0
ONBOOT=yes
NAME=eth0:0

Open file /etc/sysconfig/network-scripts/ifcfg-eth0 and make sure file does not have a GATEWAY= entry:
# vi /etc/sysconfig/network-scripts/ifcfg-eth0
Find the entry that read as follows:

GATEWAY=your-ip

Remove or comment it out by prefixing # (hash) :

 # GATEWAY=192.168.1.254

Save the file. Add the GATEWAY= to your /etc/sysconfig/network:

# vi /etc/sysconfig/network

Append or modify GATEWAY entry:

GATEWAY=192.168.1.254

Save the file. Reboot the system or run the following command:

# ifup eth0:0

OR

# service network restart

Red Hat / CentOS / Fedora Multiple IP address range

You can assign multiple ip address range as follows to eth0:
vi /etc/sysconfig/network-scripts/ifcfg-eth0-range0
Append following code from 202.54.112.120 to 202.54.112.140:

IPADDR_START=202.54.112.120
IPADDR_END=202.54.112.140
CLONENUM_START=0
NETMASK=255.255.255.0

Save and close the file.

Caution: Alias interfaces do not support DHCP.

A clone interface configuration file should use the following naming convention: ifcfg-<if-name>-<clone-name>.
While an alias file allows multiple addresses for an existing
interface, a clone file is used to specify additional options for an
interface. For example, a standard DHCP Ethernet interface called eth0, may look similar to this:

DEVICE=eth0 ONBOOT=yes BOOTPROTO=dhcp

Since the default value for the USERCTL directive is no
if it is not specified, users cannot bring this interface up and down.
To give users the ability to control the interface, create a clone by
copying ifcfg-eth0 to ifcfg-eth0-user and add the following line to ifcfg-eth0-user:

USERCTL=yes

This way a user can bring up the eth0 interface using the /sbin/ifup eth0-user command because the configuration options from ifcfg-eth0 and ifcfg-eth0-user are combined. While this is a very basic example, this method can be used with a variety of options and interfaces.

The easiest way to create alias and clone interface configuration files is to use the graphical Network Administration Tool. For more information on using this tool, refer to Chapter 15, Network Configuration. or the following:

15.11. Device Aliases

Device aliases
are virtual devices associated with the same physical hardware, but
they can be activated at the same time to have different IP addresses.
They are commonly represented as the device name followed by a colon
and a number (for example, eth0:1). They are useful if you want to have
multiple IP addresses for a system that only has one network card.

After configuring the Ethernet device —such as eth0 —to use a static IP address (DHCP does not work with aliases), go to the Devices tab and click New. Select the Ethernet card to configure with an alias, set the static IP address for the alias, and click Apply to create it. Since a device already exists for the Ethernet card, the one just created is the alias, such as eth0:1.

Warning

If you are configuring an Ethernet device to have an alias, neither the
device nor the alias can be configured to use DHCP. You must configure
the IP addresses manually.

Figure 15.22, “Network Device Alias Example” shows an example of one alias for the eth0 device. Notice the eth0:1 device — the first alias for eth0. The second alias for eth0 would have the device name eth0:2,
and so on. To modify the settings for the device alias, such as whether
to activate it at boot time and the alias number, select it from the
list and click the Edit button.

Network Device Alias Example

Figure 15.22. Network Device Alias Example


Select the alias and click the Activate button to activate the alias. If you have configured multiple profiles, select which profiles in which to include it.

To verify that the alias has been activated, use the command /sbin/ifconfig. The output should show the device and the device alias with different IP addresses:

抱歉!评论已关闭.