Monday, July 11, 2011

Basic Linux Networking

This article is for people trying to get their Linux PC connect to internet or just the local network. Most linux distributions come with automatic DHCP clients and thus need minimal tinkering with configuration. But if you are in a situation where having a simple DHCP client is not working, read on. In this part I'll write about bare basics only.

First run ifconfig command in console window to check your ip configuration. ifconfg is linux counterpart of Windows ipconfig command. Most likely, the output will look like this:

root@jj:/# ifconfig
eth0 Link encap:Ethernet HWaddr 00:e0:1c:40:07:cf
inet addr: Bcast: Mask:
inet6 addr: fe80::2e0:1cff:fe40:7cf/64 Scope:Link
RX packets:90395 errors:0 dropped:0 overruns:0 frame:0
TX packets:68143 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:58988739 (58.9 MB) TX bytes:11107421 (11.1 MB)
Interrupt:22 Base address:0x3000

lo Link encap:Local Loopback
inet addr: Mask:
inet6 addr: ::1/128 Scope:Host
RX packets:377 errors:0 dropped:0 overruns:0 frame:0
TX packets:377 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:41747 (41.7 KB) TX bytes:41747 (41.7 KB)

Here eth0 identifies your LAN interface. If you have more than one, it's shown as eth1, eth2 and so on. Wirelss interfaces are shown as wlan0, wlan1 and so on.

lo is you default local interface which will always have ip

In this case, you can see that eth0 is correctly configured with ip and netmask

If you are sure that your network has a DHCP server and want to use only that, run the following command:

/etc/init.d/networking restart

Unless you had specified a permanent IP address, the interface on which DHCP client is running will get you an ip like this.

For cases where you don't have a DHCP server to provide you IP configuration automatically or you just want some other address, say on eth0, type the following command

ifconfig eth0 netmask

This command will assign ip address with netmask to your default network interface eth0. If you want to assign this ip to another ethernet interface, replace eth0 by that interface's identifier. For example if you want to assign this ip to your 2nd network card, type:

 ifconfig eth1 netmask

Now, that you've assigned an ip, it's time to provide gateway too. If your gateway address in,  type the following command.

route add default gw

 By now, you should be able to connect to your local network. But in order to browse internet, you still need DNS. In Linux, a file named resolv.conf stored in /etc/ contains address of your DNS server. To manually add DNS entries, open resolv.conf in any text editor and add your DNS ip in separate lines. For example:


Don't forget to add nameserver before each DNS ip. Save the file and start browsing.

Following above steps will enable you to  connect to the network for current session. But what if you need to save the settings permanently ? For this you'll need to edit a file named interfaces. It's path is /etc/network/interfaces and it's contents look like this:

auto lo
iface lo inet loopback

auto eth0
iface eth0 inet dhcp

iface eth1 inet dhcp

#auto eth2
#iface eth2 inet dhcp

#auto ath0
#iface ath0 inet dhcp

auto wlan0
iface wlan0 inet dhcp


Here,  auto eth0 , iface eth0 inet dhcp part shows that DHCP is enabled on eth0 interface.

iface eth1 inet static

This part is for eth1 and displays it's static ip address, netmask and gateway in different lines. If you want any of your interfaces to have a static permanent address, modify entry for that interface  just like this with address of your choice. In this case, I have 2 network cards and one wireless lan. eth0  and wlan get their ip from DHCP while eth1 has a static ip. You can either delete entries of interfaces that you don't have or just put a # before them to disable them.
Also you can disable a network interface by using command

ifconfig eth0 down

To enable it again type:

ifconfig eth0 up 

That's most of it that's usually needed in a normal computer. Thanks for reading  


Abe Wisnu said...

Thak's for tutorial.. :D

Post a Comment

Pageviews past week