How to configure network in Linux


When you’re new to Linux, configuring your network can be a tedious job, and if you do it wrong, your internet won’t work. And we all know, how important internet is to us 🙂
So in this post, we will check how to configure network in Linux Systems, both RedHat based and Debian Based Operating Systems.
The prerequisites for this post is :

  • You should Know which type of IP you’re using. Static or Dynamic? If you don’t know check out this post.
  • If you’re using static IP, you should know your IP address, Subnet Mask, Gateway address and DNS of your ISP.
    If you don’t know ask them. They’ll tell you, and note it down.
  • Commands :
    ifconfig : used to see Mac Address and IP address of Ethernet devices that are active.
    ifconfig -a : shows all the devices connected to your machine.
    /etc/init.d/network restart : To restart network in RedHat/Debian based OS.
    service networking restart : To restart network in RedHat/Debian based OS.

1. Redhat Based Operating Systems (Fedora, CentOS , RedHat)

  • Static IP.

    If you’re using static IP, you can assign it to an Ethernet device in 2 ways.
    – By assigning IP in the terminal itself. This IP would be for temporary use only.
    Once you restart the network or your machine, the settings would be back to default.
    To set IP via terminal write

    ifconfig eth0 192.168.0.1 netmask 255.255.255.0
    That is ifconfig [ Device Name ] [ IP ] [ Netmask ].

    Configure static IP using console.

    Configure static IP using the terminal.

     

    – By changing the network File.
    Each time machine or network restarts, you will get the same IP. To give IP using this method, follow these steps :

    vi /etc/sysconfig/network-scrips/ifcfg-eth0
    [ Press ‘i’ to go into insert mode’ ]
    DEVICE=eth0
    BOOTPROTO=static
    HWADDR = 08:00:27:92:8C:C6
    IPADDR = 192.168.0.110

    NETMASK = 255.255.255.0
    ONBOOT =yes

    Assign static IP in ifcfg-eth3

    Assign static IP in ifcfg-eth3

    Restart service and check.

    Restart service and check.

 

  • Dynamic IP

    You can configure your LAN card to take an IP from DHCP server by changing the network file of that device.
    Follow these steps :

    vi /etc/sysconfig/network-scrips/ifcfg-eth0
    [ Press ‘i’ to go into insert mode’ ]
    DEVICE=eth0
    BOOTPROTO=dhcp
    ONBBOOT=yes

     

    Configuring dhcp in client

    Configuring dhcp in client

    Restart the network and check.

    Restart the network and check.

     

2. Debian Based Operating Systems (Ubuntu, Backtrack)

  • Static IP

    If you’re using static IP, you can assign it to an Ethernet device in 2 ways.
    – Same as in RedHat based OS.
    ifconfig eth0 192.168.0.1 netmask 255.255.255.0   That is
    ifconfig [Device Name ] [ IP ] [ Netmask ]

     

    Configure IP via console.

    Configure IP via console.

     

    -By changing the network File. Here the network file is in some other place. In that file, you have to define the LAN cards you have. By default you’ll have
    auto lo
     iface lo inet loopback
    To make an entry for device ‘eth0’ follow these steps :

    vi /etc/network/interfaces
    [ Press ‘i’ to go into insert mode and add ]
    auto eth0
    iface eth0 inet static
    address 192.168.0.110
    netmask 255.255.255.0
    gateway 192.168.0.1

     

    Interface file in Debian OS

    Interface file in Debian OS

     

    Restart service and check new IP

    Restart service and check new IP

     

     

  • Dynamic IP

    To configure Dynamic IP in Debian OS:

     vi /etc/network/interfaces 
    [ Press ‘i’ to go into insert mode and add  ]
    auto eth0
    iface eth0 inet dhcp

    Interface file. For DHCP

    Interface file. For DHCP

    Restart the network using: /etc/init.d/networking restart

    Restart network

    Restart network

    IP changed.

    IP changed.

     

In Static IP configuration of both Operating systems, you need to specify the DNS also.
DHCP server automatically assigns the DNS server.
To add DNS server make an entry in resolv.conf using  ‘vim /etc/resolv.conf‘ as
nameserver 192.168.0.1  that is
nameserver [IP of DNS server]

Leave a Reply