Little Girl's Mostly Linux Blog

StaticIP

Static LAN IP

This page was last updated on March 01, 2017.

Table Of Contents

Introduction

This document hopes to guide you through the basic process of setting up a static LAN IP address for your computer in Ubuntu. It is intended to be a basic guide for a very simple wired network. Wireless and advanced networking are not covered.

Requirements

  • A computer with a network interface controller so that it can be connected to a network.
  • The Ubuntu operating system or one of its derivatives) on the computer. Note that these instructions have been tested on Ubuntu and have been written for Kubuntu and Ubuntu, but the command for opening your editor will vary depending on which derivative you use. These instructions may also work with other operating systems with or without adjustment.
  • A hardware router connected to the computer (and to a modem if the computer is going to use the internet) with ethernet cable.

Understanding Your Local Network

  • An interface is a connection between hardware (like a computer or printer or router, etc.) and/or software (like a virtual machine).
  • A private network or Local Area Network (LAN) is two or more interfaces connected together locally (often in a building or buildings) by hardware and software.
  • Each interface is given a unique number – an IP address – so that it can be found and identified in the network.
  • An IP address is made of four sections separated by periods, with each section containing a number between 0 and 255.
  • There are three classes of private networks. Each class provides a different range of IP addresses for use with your interfaces. Your router (or operating system, if you have no router) determines which one is used.

    CLASS ADDRESS RANGE
    A from 10.0.0.0 to 10.255.255.255
    B from 172.16.0.0 to 172.31.255.255
    C from 192.168.0.0 to 192.168.255.255

    The first two sections of a LAN IP address specify its class and the second two sections identify an interface. For example, 192.168.1.100 represents the 1.100 interface (which could be a computer, printer, virtual machine, etc.) in the 192.168 class (which is a class C network).

  • The router reserves some numbers for its own use:

    • 0 is usually reserved for the network identifier.
    • 1 is usually reserved for the gateway (the router’s static LAN IP address).
    • 255 is usually reserved for broadcasting to all interfaces on the network.
    • A specific range of addresses is reserved for Dynamic Host Configuration Protocol (DHCP) which is used by the router for assigning IP addresses to your interfaces as they are needed.
  • A dynamic LAN IP address assigned by DHCP can change, making an interface difficult to find in your network.
  • A static LAN IP address assigned manually makes sure the interface can always be found in the same place in your network. You can use a static LAN IP address in configuration files once and that configuration will work from then on.
  • You can use more than one class for your LAN IP addresses, but using the same class will ensure that your interfaces can find and communicate with each other using basic configuration.
  • The most commonly used IPv4 address is 127.0.0.1 and represents your system’s loopback or localhost (its way of referring to itself).
  • Your network information is kept in the /etc/network/interfaces file, which can be edited to give you a static LAN IP.

Get Your Network Information

You will need to know your network information to assign a static LAN IP. Below are several commands you can use to get the information. Each command is designed to obtain and display information without making any changes to your system, and a basic explanation is provided. See the man pages for the commands to get more details. Please write down the result you get for each step.

  1. Get your gateway
    • To get your gateway (your router’s static LAN IP address), type this command in a terminal window and press the Enter key:

      netstat -r | grep 'default' | awk '{ print $2}'
      1. The netstat command uses the -r option to get the kernel routing table and then passes its output to the awk command.
      2. The grep command searches for a line containing default and passes it to the awk command.
      3. The awk command displays the contents of the 2nd column, which is your gateway.

  2. Get your iface:
    • To get your iface (the network device [ethernet card or ethernet interface] you want to configure), type this command in a terminal window and press the Enter key:

      route | grep 'default' | awk '{print $8}'
      1. The route command gets the IP routing table and passes it to the grep command.
      2. The grep command searches for a line containing default and passes it to the awk command.
      3. The awk command displays the information in the 8th column of the line, which is your iface.

  3. Get your LAN IP address
    • To get your address (your computer’s LAN IP address), type this command in a terminal window and press the Enter key:

      hostname -I | cut -d' ' -f1
      1. The hostname command uses the -I option to display all network addresses of the computer and then passes its findings to the cut command.
      2. The cut command uses the -d option to specify spaces as delimiters and displays the first field, which is your LAN IP address.

  4. Get your loopback address
    • To get your loopback address (this is the first address in the /etc/network/interfaces file), type this command in a terminal window and press the Enter key:

      ifconfig | grep 'inet addr:'| grep -v 'Bcast' | cut -d: -f2 | awk '{print $1}'
      1. The ifconfig command gets your network interface information and passes it to the grep command.
      2. The first grep command searches for lines containing inet addr: and passes them to the second grep command.
      3. The second grep command searches for a line that doesn’t contain Bcast and passes it to the cut command.
      4. The cut command uses a colon as a delimiter and chooses the second field in the line and passes it to the awk command.
      5. The awk command displays the information in the first column of the line, which is your loopback address.

  5. Get your loopback netmask
    • To get your loopback netmask (this is the first netmask in the /etc/network/interfaces file), type this command in a terminal window and press the Enter key:

      ifconfig | grep 'Mask:'| grep -v 'Bcast' | cut -d: -f3
      1. The ifconfig command gets your network interface information and passes it to the grep command.
      2. The first grep command searches lines containing Mask: and passes them to the second grep command.
      3. The second grep command searches for a line that doesn’t contain Bcast and passes it to the cut command.
      4. The cut command uses a colon as a delimiter and displays the third field in the line, which is your loopback netmask.

  6. Get your nameserver
    • To get your dns-nameservers (the service you use to translate domain names into IP addresses), type this command in a terminal window and press the Enter key:

      cat /etc/resolv.conf | grep nameserver | awk '{print $2}'
      1. The cat command gets the contents of the /etc/resolv.conf file and passes them to the grep command.
      2. The grep command searches for a line containing nameserver and passes it to the awk command.
      3. The awk command displays the information in the second column of the line, which is your nameserver.

  7. Get your netmask
    • To get your netmask (filter used to determine the subnet [the class and range of IP addresses] your IP address belongs to), type this command in a terminal window and press the Enter key:

      ifconfig | grep 'Mask:'| grep -v '127.0.0.1' | cut -d: -f4 | awk '{ print $1}'
      1. The ifconfig command gets your network interface information and passes it to the grep command.
      2. The grep command searches for lines containing Mask: and passes them to the grep command.
      3. The grep command searches for a line that doesn’t contain 127.0.0.1 and passes it to the cut command.
      4. The cut command uses : as a delimiter and chooses the fourth field of the line, which it passes to the awk command.
      5. The awk command displays the information in the first column of the line, which is your netmask.

      Three common netmasks are:

      • 255.0.0.0 is the class A netmask.
      • 255.255.0.0 is the class B netmask.
      • 255.255.255.0 is the class C netmask.

  8. Get your private network class
    • To get your private network class, type this command in a terminal window and press the Enter key:

      result=$(netstat -r | grep 'default' | awk '{ print $2}'); case $result in 10.*) printf 'A\n' ;; 172.*) printf 'B\n' ;; 192.*) printf 'C\n' ;; *) printf 'UNKNOWN CLASS\n' ;; esac;
      1. The result= section runs the command you used to get your gateway and stores it in the result variable.
      2. The case command reads the result variable and tests for an IP starting with 10 or 172 or 192 or any other number and displays a message depending on which number it finds.
      3. The esac command closes the case.

Choose Your Static LAN IP Address

Before you can assign yourself a static LAN IP, you need to find out which IPs are available (not already assigned) on your network. In order to do this, you’ll need to access your router’s configuration.

  1. Log into your router:
    • Type your gateway into your browser’s address bar to log into the router and open its configuration interface. The router may have a default log-in name and password. If it does and you don’t know what it is, check its documentation.

  2. Check your router settings:
    • Find the section in your router’s configuration that refers to DHCP settings. The addresses used by your router for DHCP will either be displayed by using a starting and ending address or a range of addresses. In this example, both of these class C routers have reserved 100 through 150 for their own use:

      Starting and ending address Starting Address 192.168.1.100
      Ending Address 192.168.1.150
      Range of addresses Starting Address 192.168.1.100
      Number of Addresses: 51

  3. Choose your static LAN IP address:
    • Now that you know which numbers you can’t use, it’s time to choose one from those that are available. The available numbers for the example routers above are from 2 through 99 and from 151 through 254, so you could, for example, choose 192.168.1.3 or 192.168.1.155 as a valid static LAN IP.

If you have a stubborn router that is using all the IP addresses, see the Troubleshooting section for instructions on freeing up some IP addresses for you to use.

Assign Your Static LAN IP Address

Now that you’ve chosen an IP address, you can assign it by editing the /etc/network/interfaces file.

  1. Create a time-stamped backup copy of the /etc/network/interfaces file by typing this command in a terminal window and then pressing the Enter key and providing your password when asked:

    sudo cp /etc/network/interfaces /etc/network/interfaces-backed-up-on-$(date +%b-%d-%Y)
  2. Open the /etc/network/interfaces file in a text editor:

    • If you use Kubuntu, type this command in a terminal window and then press the Enter key:

      kdesudo kate /etc/network/interfaces
    • If you use Ubuntu, type this command in a terminal window and then press the Enter key:

      gksudo gedit /etc/network/interfaces
  3. Examine the contents of the file. It should have these contents by default:

    # interfaces(5) file used by ifup(8) and ifdown(8)
    auto lo
    iface lo inet loopback
    

    If it does, go to the next step. If it doesn’t, stop and do some research before continuing so you can make certain that the steps in this document won’t interfere with existing settings that you might need.

  4. Replace the contents of the /etc/network/interfaces file with these contents:

    # interfaces(5) file used by ifup(8) and ifdown(8)
    auto lo
    iface lo inet loopback
    address MY_LOOPBACK_ADDRESS
    netmask MY_LOOPBACK_NETMASK
    
    auto MY_IFACE
    iface MY_IFACE inet static
    address MY_LAN_IP_ADDRESS
    netmask MY_NETMASK
    gateway MY_GATEWAY
    dns-nameservers MY_NAMESERVER
    
  5. Replace the upper case labels in the /etc/network/interfaces file with your network information:

    1. Replace MY_LOOPBACK_ADDRESS with your loopback address.
    2. Replace MY_LOOPBACK_NETMASK with your loopback netmask.
    3. Replace both instances of MY_IFACE with your iface.
    4. Replace MY_LAN_IP_ADDRESS with the static LAN IP address you chose.
    5. Replace MY_NETMASK with your netmask.
    6. Replace MY_GATEWAY with your gateway.
    7. Replace MY_NAMESERVER with your nameserver.

    The result should look something like the example /etc/network/interfaces file in the Appendix.

  6. Save the /etc/network/interfaces file and close the text editor.
  7. Restart your network by rebooting your computer or by typing this command in a terminal window and press the Enter key and provide your password when asked:
  8. sudo service network-manager restart

Verify Whether It Worked

Check your LAN IP to test whether it’s been properly assigned. If it’s the static LAN IP you assigned above, then you’re finished. If it’s not, or if you get a network error when booting the computer, or if booting the computer takes a longer time than usual:

  1. Wait for your computer to fully boot up (if applicable) and follow the steps in the Troubleshooting section in the Appendix.
  2. If you can’t assign the static LAN IP you want and are not able to successfully troubleshoot the issue, you can restore your original settings by copying the backed up /etc/network/interfaces over the one you changed:
    1. Type this command in a terminal window and then press the Enter key:

      sudo cp /etc/network/interfaces-backed-up-on-$(date +%b-%d-%Y) /etc/network/interfaces
    2. Provide your password when asked.

Appendix

Example /etc/network/interfaces File

This is an example of what your /etc/network/interfaces file could look like after assigning a static LAN IP:

auto lo
iface lo inet loopback
address 127.0.0.1
netmask 255.0.0.0

auto enp0s25
iface enp0s25 inet static
address 192.168.0.100
netmask 255.255.255.0
gateway 192.168.1.1
dns-nameservers 192.168.1.1

Further Information

Troubleshooting

  • Make sure that you meet the requirements in the Requirements section of this document.
  • Make sure that your hardware has been properly installed and is properly connected.
  • Make sure that DHCP is enabled if your internet provider requires it for you to connect to the internet. Note that enabling DHCP will not interfere with valid static IP addresses.
  • Review the steps above and make sure that you substituted the correct information in the /etc/network/interfaces file, fixing any errors that you find.
  • If you have a stubborn router that is using all the IP addresses, there will be no valid IP addresses for you to pick from when assigning a static IP address. For example these class C routers are each using 192.168.1.2 through 192.168.1.254:

    Starting and ending address Starting Address 192.168.1.2
    Ending Address 192.168.1.254
    Range of addresses Starting Address 192.168.1.2
    Number of Addresses: 253

    In both examples, the router is using 2 through 254 for DHCP, leaving no addresses for you to use. In that case, you’ll need to change the settings of the router to make some addresses available for yourself. The information you’ll want to change is either the Ending Address or the Number of Addresses, depending on which method your router uses to display the addresses. For example:

    Starting and ending address Starting Address 192.168.1.2
    Ending Address 192.168.1.200
    Range of addresses Starting Address 192.168.1.2
    Number of Addresses: 199

    In both examples, the router is now using 2 through 200 for DHCP. It’s also using 0, 1 and 255 for its own purposes. This leaves 201 through 254 available for you to use (note that you may need to reboot the router for the change to take effect – see the router’s documentation). Examples of valid IP addresses for this computer are:

    • 192.168.1.201
    • 192.168.1.250
  • If you need additional help not covered in this section, see my GettingHelp page.

Obligatory Happy Ending

And they all lived happily ever after. The end.

Back to top

Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Comment:

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: