Linux Crumbs

How To and Why Set Up a Static IP Address on GNU/Linux

Summary

This tutorial explains what a static IP address is, and why to use one. It also outlines how to set up a static IP address on GNU/Linux.

The steps in this tutorial have been tested on kubuntu 12.04, mythbuntu 12.04, and mythbuntu 14.04, and should work with many other GNU/Linux distributions.

This tutorial assumes that your computer is on a private home network that is connected to the Internet via a router.

Contents

What is a Static IP Address?

A static Internet Protocol Address (IP address) is a fixed location at which a computer can be contacted on a network.

This fixed location must be unique on the network so that network traffic meant for the computer is sent only to that computer. This is similar to the way your home address is unique, so that your postal mail is delivered only to your home address.

Why Use a Static IP Address?

A static IP address is needed when a computer is to provide a service to other computers. Examples of services are a web page server, a MythTV backend server, and a calendar and address server.

In order for other computers to reliably contact the service on the computer, the computer must have a unique static (unchanging) IP address.

Advantages of a static IP address are:

  • Can provide services for other computers because computer is accessible at a known, unchanging IP address
  • Faster boot because no time spent waiting for a dynamic IP address

Disadvantages of a static IP address are:

  • More difficult to move computer to other networks because the computer network configuration will require changing
  • Requires more knowledge of networking to support

If other computers do not rely on services from your computer, or you often move your computer and use it on different networks, then staying with a dynamically assigned IP address is probably best.

Acquiring Network Information

Before we can set up a static IP address, we need to determine some information about your computer and the network to which the computer is connected.

The items that we will need to set up a static IP address are:

  • Network interface name
  • Network your computer is on
  • Gateway address
  • Network mask
  • Broadcast address
  • A range of addresses set aside for static IP addresses

Fortunately much of this information can be readily determined.

  1. Open a terminal window.

    In the K Desktop Environment (kubuntu) use menu option: K -> System -> Konsole (Terminal)

    In XFCE (mythbuntu) use menu option: Applications -> System -> Xfce Terminal

    TIP:   Opening a terminal in other desktop environments

    You can often open a terminal window using the following steps:

    a) Press Alt+F2 to open a run command prompt
    b) Enter "xterm" without the quotes


  2. Enter the network routing information command.

    sudo netstat -rn

    Following is a sample of output from this command:
    
    Kernel IP routing table
    Destination Gateway Genmask Flags MSS Window irtt Iface 0.0.0.0 192.168.1.1 0.0.0.0 UG 0 0 0 eth0 169.254.0.0 0.0.0.0 255.255.0.0 U 0 0 0 eth0 192.168.1.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0

  3. Determine the required networking details.

    From the netstat -rn output we can learn the following:

    eth0 - network interface name
    192.168.1.1 - gateway address and address to use for Domain Name Server address (same as home router IP address)
    192.168.1.0 - network provided by the home router (the first three numbers match the gateway)
    255.255.255.0 - network mask (a class C network for up to 254 devices)
    192.168.1.255 - broadcast address (replace the last 0 in the network with a 255)

    The above values are colour coded to help later when placing these values in the network interfaces file.

    TIP:   Other Private Class C Networks

    Private class C networks start with 192.168. Many home routers commonly use 192.168.1.x addresses, but values from 0 to 254 can be used (not just 1).

    For example 192.168.0.254 is a valid class C network address on the private network 192.168.0.0.


  4. Choose a static IP address.

    To learn the range of IP addresses set aside by the router for static IP addresses, refer to your router documentation or login to your home router to check these settings. In general these are IP address outside of the range that is dynamically assigned by your home router.

    From my experience with Linksys and D-Link routers, addresses with a last number of 51 to 64 have been outside the range of dynamically assigned addresses and are safe to use as a static IP address.

    Choose a number that will be unique on your network (no two computers can use the same IP address).

    For example, I chose the following static IP addresses for two different computers on my home network.

    192.168.1.53 - My desktop computer
    192.168.1.55 - My MythTV personal video recorder

Setting Up Static IP Address

We are now ready to set up the static IP address on the computer. For this example I used the network information determined above and the following IP address:

192.168.1.53 - static IP address.

  1. Create a backup of your existing network configuration.

    In a terminal window, enter the following commands:

    cd /etc/network
    sudo cp -p interfaces interfaces.backup

  2. Edit the /etc/network/interfaces file to define a static IP address.

    TIP:   Use any text editor you prefer.

    Many text editors are available.

    kate is commonly used with KDE desktops
    gedit is often used on GNOME desktops
    leafpad is a graphical editor I install and use on XFCE desktops
    nano is an editor often available on all GNU/Linux distributions.


    Edit the file with a command similar to the following:

    sudo nano /etc/network/interfaces

    and make the content like the following lines.

    Remember to replace the values with the ones you determined above.

    ---------- begin /etc/network/interfaces ----------
    
    # This file describes the network interfaces available on your system
    # and how to activate them. For more information, see interfaces(5).
    
    # The loopback network interface
    # Note:  These two lines are the only lines needed for a
    #        dynamically assigned (DHCP) IP address.
    auto lo
    iface lo inet loopback
    
    # The primary network interface
    auto eth0
    iface eth0 inet static
    address 192.168.1.53
    network 192.168.1.0
    netmask 255.255.255.0
    broadcast 192.168.1.255
    gateway 192.168.1.1
    metric 0
    dns-nameservers 192.168.1.1
    

    ---------- end /etc/network/interfaces ----------

    Save file and exit editor.

  3. Release the current IP address from the network interface.

    sudo ip addr flush eth0

  4. Restart the networking service.

    Either use one of the following commands or reboot:

    sudo /etc/init.d/networking restart

    Or:

    sudo systemctl restart networking.service

  5. Verify the IP address is correct.

    sudo ifconfig eth0

    Following is a sample of output from this command:
    
     eth0     Link encap:Ethernet  HWaddr bc:ae:c5:e1:4b:f1
              inet addr:192.168.1.53  Bcast:192.168.1.255  Mask:255.255.255.0
              inet6 addr: fe80::beae:c5ff:fee1:4bf1/64 Scope:Link
              UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
              RX packets:54384 errors:0 dropped:0 overruns:0 frame:0
              TX packets:32697 errors:0 dropped:0 overruns:0 carrier:0
              collisions:0 txqueuelen:1000
              RX bytes:77236077 (77.2 MB)  TX bytes:2463869 (2.4 MB)
        
  6. Test you can access the Internet.

    In a terminal window enter:

    ping www.google.com

    Use CTRL+C to cancel the ping commmand.

    Sample output follows:
    
    PING www.google.com (74.125.196.99) 56(84) bytes of data.
    64 bytes from yk-in-f99.1e100.net (74.125.196.99): icmp_req=1 ttl=46 time=95.3 ms
    64 bytes from yk-in-f99.1e100.net (74.125.196.99): icmp_req=2 ttl=46 time=94.9 ms
    ^C
    --- www.google.com ping statistics ---
    2 packets transmitted, 2 received, 0% packet loss, time 1001ms
    rtt min/avg/max/mdev = 94.974/95.164/95.355/0.362 ms
    

    NOTE:   Cannot ping the google server?

    If the ping command cannot contact the google server and indicates packet loss, then something is wrong with the network configuration.

    If you are unable to resolve the problem, you can restore the backup you made earlier in /etc/network/interfaces.backup and reboot to return to your previous network configuration.

References

While setting up a static IP address, I found the following references useful:

Medusa Deception novel on sale now
News   Documentation     Copyright (c) 2013-2017 Curtis Gedak

Get GParted at SourceForge.net. Fast, secure and Free
	Open Source software downloads Valid HTML 4.01! Correct CSS! Privacy policy