How to add secondary ip to linux
################################

NOTE: this is not a permanent method as we use ifconfig, to have a more permanent method we need to edit /etc/network/interfaces but thats another article that I have wrote yet (11/19/2013)

Good article: http://www.tecmint.com/ifconfig-command-examples/

To make a secondary ip address (so that we can reach another network for example) we need to make an alias interface.
first find out what interface you need to give this new ip address to, then make an alias and give it an ip address (you can make an alias and give an ip address all in one command)

First off though to set ip address in linux, the general format is:
# ifconfig INTERFACE IPADDRESS netmask MASK broadcast BCAST
Where every uppercase statement is a variable thgat needs to be filled in with its appropriate information

SIDENOTE: to set eth0 to ip address 172.20.18.10 with subnet 255.255.255.0 that has a broadcast of 172.20.18.255 we would do this:

# ifconfig eth0 172.20.18.10 netmask 255.255.255.0 broadcast 172.20.18.255

 

# ifconfig -a

find out what interface gives you internet?

lets pretend its eth0

lets pretened eth0 is currently set as:

ip address: 172.20.18.10
subnet: 255.255.255.0
etc…

so this gives us access to 172.20.18.0 network

lets pretend we need to also access 10.10.10.0 network so we want a secondary ip that

first we need to make an alias (just a make believe new interface that we can delete at any time)

their names of the form

INTERFACE:ALIAS

or

eth0:0 for example

Our example will use eth0:0

So to cause one into existance, we need to give it some of the typical properties of an interface (ip, subnet, broadcast)

then you can set its ip address and subnet mask and broadcast like this:

# ifconfig eth0:0 IP netmask MASK broadcast BCAST

for our examples case:

# ifconfig eth0:0 10.10.10.5 netmask 255.255.255.0 broadcast 10.10.10.255

Thats it.

You can see it with

# ifconfig -a

or

# ifconfig eth0:0

To get rid of it

# ifconfig eth0:0 down

To check default routes:

# netstat -nr

if you need to add a default route

# route add default gw GWIP

ex:

# route add default gw 10.10.10.1

More ways here:

http://www.cyberciti.biz/faq/howto-debian-ubutnu-set-default-gateway-ipaddress/

Leave a Reply

Your email address will not be published. Required fields are marked *