Pages

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:192.168.1.117 Bcast:192.168.1.255 Mask:255.255.255.0
inet6 addr: fe80::2e0:1cff:fe40:7cf/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
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:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
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 127.0.0.1



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


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 192.168.1.11 on eth0, type the following command


ifconfig eth0 192.168.1.11 netmask 255.255.255.0


This command will assign ip address 192.168.1.11 with netmask 255.255.255.0 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 192.168.1.11 netmask 255.255.255.0



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

route add default gw 192.168.1.1
 

 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:

nameserver 192.168.1.1
namserver 4.2.2.2


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

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

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  






1 comments:

Abe Wisnu said...

Thak's for tutorial.. :D

Post a Comment

Pageviews past week