Tuesday, August 6, 2013

How to change IP address in linux

There are many ways to change your IP address in linux.
Start by finding the current ip address by typing
or in some cases

The output should be something like this:
eth0      Link encap:Ethernet  HWaddr 00:0c:29:e8:23:ba
             inet addr:  Bcast:  Mask:
             inet6 addr: fe80::20c:29ff:fee8:23ba/64 Scope:Link
             UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
             RX packets:307505062 errors:0 dropped:2743 overruns:0 frame:0

in this case is my IP address.
Now I want to change my address to the static IP address
This is easily done with this command:
ifconfig eth0 netmask up

My IP address is now changed to the static ip

Persistant static IP address

Changing the IP address with ifconfig will only make the computer keep that address until a reboot.
To make your static IP address static you will need to change the config file for the network interface.
Edit this file with your favorite editor: /etc/network/interfaces

auto eth0
iface eth0 inet static

The above lines in the config file will make the network interface use the static IP address and use as gateway.
After changing this file you will need to restart the network service, this can usually be done by the command:
service networking restart
as root or by just rebooting your computer.