Change IP address Linux

As Virtual Machine tools (VMware, VirtualBox, Xen, KVM, qemu, … ) is getting more and more popular, changing IP address of Linux has become a frequent task for Linux users, not only administrators. There are several ways to change IP address of a Linux machine:

+ Use GUI tools

+ Use command line directly

+ Edit configuration files

Edit configuration files

.

For Ubuntu Linux (version 10.x, 11.x)

Check current IP address :

$ ifconfig | grep inet

Check network interface cards (NICs) :

$ sudo lshw | grep -E "eth|wlan"

Assumming that your active NIC is eth0, now we edit config files:

$ sudo gedit /etc/network/interfaces

If eth0 is using DHCP, there will be some lines like:

iface eth0 inet dhcp

We change it to similar like below to set static IP address:

#iface eth0 inet dhcp

iface eth0 inet static
address 192.168.1.100
netmask 255.255.255.0

gateway 192.168.1.1

network 192.168.1.0

 #broadcast 192.168.1.255

If there is some problem with MAC address (hardware address), we may have to change udev-rules:

$ sudo gedit /etc/udev/rules.d/70-persistent-net.rules

Restart the network daemon to apply the change:

$ sudo service networking restart ## sudo /etc/init.d/networking restart

Bonus: change hostname

$ sudo gedit /etc/hostname
$ sudo hostname new-host-name
$ sudo service hostname restart ## sudo /etc/init.d/hostname restart

Check the hosts file and DNS servers to make approriate changes:

$ sudo gedit /etc/hosts
$ sudo gedit /etc/resolv.conf

.

For CentOS Linux (version 5.x, 6.x)

Check current IP address :

$ /sbin/ifconfig | grep inet

Check network interface cards (NICs) :

$ dmesg | grep -E "eth|wlan"

Assumming that your active NIC is eth0, now we edit config files:

$ sudo gedit /etc/sysconfig/network-scripts/ifcfg-eth0

If eth0 is using DHCP, there will be some lines like:

DEVICE=eth0

ONBOOT=yes

BOOTPROTO=dhcp

We change it to like below to set static IP address:

DEVICE=eth0

ONBOOT=yes

#HWADDR=00:30:48:56:A6:2E

#BOOTPROTO=static

BOOTPROTO=none

IPADDR=192.168.1.100
NETMASK=255.255.255.0
GATEWAY=192.168.1.1
NETWORK=255.255.255.0

#BROADCAST=192.168.1.255

If there is problem with MAC address (HWADDR), we may have to change udev-rules:

$ sudo gedit /etc/udev/rules.d/70-persistent-net.rules

Restart the network daemon to apply the change:

$ sudo service network restart ## sudo /etc/init.d/network restart

Bonus: change hostname

$ sudo gedit /etc/sysconfig/network
$ sudo hostname new-host-name
$ sudo service hostname restart ## sudo /etc/init.d/hostname restart

Check the hosts file and DNS servers to make approriate changes:

$ sudo gedit /etc/hosts
$ sudo gedit /etc/resolv.conf
.

For other major distrubutions

Debian-based distros (Mint, PCLinuxOS, …) configuration is very similar to Ubuntu’s .

Redhat-based distros (SUSE, FedoraCore, …) configuration is very similar to CentOS’s .

.

Use GUI tools

.

We have NetworkManager/KNetworkManager , nm-applet, system-config-network , gnome-network-admingnome-nettoolwicd , etc . . .

( your homework :D )

.

Use command directly

.

This is the where Linux shines (and confuses ppl with too much options, sometimes) . There you go: ifconfig, ip, route, . . .

./.

About DucQuoc.wordpress.com

A coder, brother, and player at times...
This entry was posted in Linux. Bookmark the permalink.

7 Responses to Change IP address Linux

  1. Pingback: Synchronize time in Linux | DucQuoc's Blog

  2. Pingback: Linux Directory Structure | DucQuoc's Blog

  3. Pingback: Shrink VirtualBox image | DucQuoc's Blog

  4. A common issue with RedHat-based Linux (such as CentOS, FedoraCore, …) is the *sendmail* service may hangup several minutes when booting (very common when you run Linux in VM). The root cause of that is DNS lookup problem.
    You can fix that issue by simply adding to your hosts file (/etc/hosts ) some IP for your hostname (if you are in a network using “domain” , apply the IP for your fully qualified domain name too):

    127.0.0.1 localhost localhost.localdomain
    192.168.56.104 myserver myserver.mydomain.net

    (If you don’t know the domain, you can look at “search” section in /etc/resolv.conf , or lookup the DNS server listed there).
    See also: http://www.redhat.com/support/resources/faqs/RH-sendmail-FAQ/x139.html

  5. This website was… how do I say it? Relevant!! Finally I’ve found something that helped me.
    Appreciate it!

  6. Cookware says:

    What’s Taking place i’m new to this, I stumbled upon this I have found It positively helpful and it has aided me out loads.
    I am hoping to contribute & aid different customers like its helped
    me. Good job.

  7. Tarik Tunai says:

    great put up, very informative. I ponder why the other experts of this sector do not notice this.
    You should continue your writing. I am sure, you’ve a great readers’ base already!

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