Monday, March 2, 2009

How to change/configure IP Address on Linux/ Fedora /Ubuntu

This post is related to unix networking and IP Address configuration. With this post you can both setup your network through command line and graphical user interface.

The unix command "ifconfig" is used to view the network interface information and configure the network interface.

Viewing Ethernet Interface Information
1)If you simply write ifconfig as a root user then it will display the status of the currently active interfaces.
By running ifconfig command as below we can tell that currently eth0, eth1 and lo network interface is active where "lo" is local loopback interface, "eth0" and "eth1" is ethernet interface.

[root@localhost ~]# ifconfig
eth0 Link encap:Ethernet HWaddr 00:1D:60:80:E9:A9
inet addr:203.88.110.9 Bcast:203.88.110.63 Mask:255.255.255.192
inet6 addr: fe80::21d:60ff:fe80:e9a9/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:17233 errors:0 dropped:71 overruns:0 frame:0
TX packets:1373 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:3692352 (3.5 MiB) TX bytes:118395 (115.6 KiB)
Interrupt:209 Base address:0x4800

eth1 Link encap:Ethernet HWaddr 00:15:E9:A5:88:1D
inet addr:10.4.12.5 Bcast:10.255.255.255 Mask:255.0.0.0
inet6 addr: fe80::215:e9ff:fea5:881d/64 Scope:Link
UP BROADCAST MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:0 (0.0 b) TX bytes:0 (0.0 b)
Interrupt:225 Base address:0x6c00

lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:2018 errors:0 dropped:0 overruns:0 frame:0
TX packets:2018 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:2673028 (2.5 MiB) TX bytes:2673028 (2.5 MiB)

2)In order to see just the status of a single network interface just with ifconfig write that network interface name. For example we want to see the status of ethernet "eth0" ,
[root@localhost ~]# ifconfig eth0
eth0 Link encap:Ethernet HWaddr 00:1D:60:80:E9:A9
inet addr:203.88.110.9 Bcast:203.88.110.63 Mask:255.255.255.192
inet6 addr: fe80::21d:60ff:fe80:e9a9/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:66691 errors:0 dropped:89 overruns:0 frame:0
TX packets:15293 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:39110857 (37.2 MiB) TX bytes:1055155 (1.0 MiB)
Interrupt:209 Base address:0x4800

3)If you want to see the status of all network interfaces then use "ifconfig -a" command. It displays the status of all interfaces, even those that are down.

[root@localhost ~]# ifconfig -a

Configure Network Interface
1)Change IP Address
IP Address can be set by using any of the three ways,
a)Using command Line tools
b)Modifying configuration files
c)Using GUI tools

a)Using command line tools: With ifconfig command itself you can set up IP Address like,
[root@localhost ~]# ifconfig eth1 10.4.12.6 netmask 255.255.255.0 up
format is #ifconfig {interface_name} {IP Address} netmask {network_mask} up

See the address by,
[root@localhost ~]# ifconfig eth1
eth1 Link encap:Ethernet HWaddr 00:15:E9:A5:88:1D
inet addr:10.4.12.6 Bcast:10.4.12.255 Mask:255.255.255.0
inet6 addr: fe80::215:e9ff:fea5:881d/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:691 errors:0 dropped:0 overruns:0 frame:0
TX packets:15 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:65496 (63.9 KiB) TX bytes:1110 (1.0 KiB)
Interrupt:225 Base address:0x6c00

If you are on 1)Cent OS Linux or 2) Fedora Core Linux or 3) Red Hat Enterprise Linux (RHEL) 3/4/5 then on console type,
#system-config-network-tui &

You will see text based GUI as above. Select your Ethernet card (eth0 or eth1) and press "Enter" or "F12" special key.

After that you can obtain IP address using DHCP or setup manually. Once IP address assigned, click on Ok button to save the changes.

b)Modifying configuration files: You can configure network by editing text file under /etc/sysconfig/network-scripts. You need to edit / create files as follows:

-Edit /etc/sysconfig/network-scripts/ifcfg-eth0 for Ethernet card eth0 configuration file.

-Edit /etc/sysconfig/network-scripts/ifcfg-eth1 for Ethernet card eth1 configuration file.

Below is the setting on my machine,
[root@localhost ~]# vi /etc/sysconfig/network-scripts/ifcfg-eth1
IPADDR=113.11.0.10
NETMASK=255.255.255.192
GATEWAY=113.11.0.1
DEVICE=eth1
HWADDR=00:15:E9:A5:88:1D
ONBOOT=yes
TYPE=Ethernet
BOOTPROTO=static

After editing ifcfg-eth1 file save it.

If you don't mention gateway inside ifcfg-eth1 you can define it under /etc/sysconfig/network. like,
# vi /etc/sysconfig/network
NETWORKING=yes
HOSTNAME=http://arjudba.blogspot.com
GATEWAY=113.11.0.1

Also make sure you have correct DNS server defined in /etc/resolv.conf file. Like,
#vi /etc/resolv.conf
nameserver 4.2.2.2

At last you need to restart you network by,
# /etc/init.d/network restart

c)Using GUI tools: On 1)Cent OS Linux 2)Fedora Core Linux or 3)Red Hat Enterprise Linux (RHEL) 3/4/5 issue,
#system-config-network &
or
#setup &
A GUI will appear and configure your network.

or,
#netconfig
or,
#neat

Ubuntu network configuration
In ubuntu, main network configuration file is /etc/network/interfaces.
1)Open network configuration file.
$ sudo vi /etc/network/interfaces

2)Find and remove dhcp entry. You may got like,
#iface eth0 inet dhcp
Just comment this line.

3)Add following lines to setup your new configuration.
iface eth0 inet static
address 172.16.16.16
netmask 255.255.0.0
network 172.16.16.0
broadcast 172.16.16.255
gateway 172.16.16.1


4)Save, close and restart network service.
$ sudo /etc/init.d/networking restart
Related Documents
Copy files between Unix and Windows with rcp
Screen -A very useful unix tool to work with remote system
Different ways to take Screenshot on Linux
How to change the hostname in Linux
Memory Usage of Solaris Operating System

No comments:

Post a Comment