Multiple IP addresses for a single NIC

So, you have a computer with a single network interface card. But for a certain reason you want to be able to connect to several various networks.

Let us imagine, you have the computer with a network switch connected to it. To this switch there are connected two cables. One cable leads to an Internet router with address 192.168.1.1, the other cable leads to a network switch connected to a local group of computers and a server with IP address 192.168.2.1.

You want your computer to be able to connect BOTH to the router 192.168.1.1 AND the server 192.168.2.1.

To achieve this is extremely simple! Just edit your /etc/network/interfaces (remember to create a backup copy before you do).

I suggest you run ifconfig before that to see what your current configuration is. Write it down!

When you open the file you may see something like this (let’s assume your network card is called eth0):

auto lo
iface lo inet loopback

auto eth0
iface eth0 inet dhcp

To assign two static IP addresses to the single physical NIC eth0, edit the file to look like this:

auto lo
iface lo inet loopback

auto eth0 eth0:1
iface eth0 inet static
address 192.168.1.101
netmask 255.255.255.0
gateway 192.168.1.1

iface eth0:1 inet static
address 192.168.2.101
netmask 255.255.255.0
gateway 192.168.2.1

Restart networking by : /etc/init.d/networking restart

Now your computer will be able to connect to both networks as easy as if it would have two real network interface cards.

P.S.

If you experience connectivity problems, see what gateway your computers are using:
route -n

You may need to add a different gateway, e.g. to a computer at 192.168.2.x to be able to browse the Internet via router at 192.168.1.1:

route add default gw 192.168.1.1

You may also have to edit file /etc/NetworkManager/NetworkManager.conf and set:
[ifupdown]
managed=false

 

Advertisements


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s