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
ifconfig
or in some cases
/sbin/ifconfig

The output should be something like this:
eth0      Link encap:Ethernet  HWaddr 00:0c:29:e8:23:ba
             inet addr:192.168.114.3  Bcast:192.168.114.255  Mask:255.255.255.0
             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 192.168.114.3 is my IP address.
Now I want to change my address to the static IP address 192.168.1.3
This is easily done with this command:
ifconfig eth0 192.168.1.3 netmask 255.255.255.0 up

My IP address is now changed to the static ip 192.168.1.3

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
        address  192.168.1.3
        netmask 255.255.255.0
        gateway  192.168.1.1

The above lines in the config file will make the network interface use the static IP address 192.168.1.3 and use 192.168.1.1 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.