Setting up a Static IP

Here I want to cover how to set up a static IP address for your machine.

This is pretty simple. Right now, your server will be using an IP address dynamically assigned to it from your DHCP server which is more than likely your router/gateway that connects your LAN to the outside world (the internet/WAN). Setting up your server as a DHCP server is a topic unto itself which I plan on covering later. For now, the idea is to set your machine so that it always has the same IP address even though it probably wouldn’t change otherwise (typical lease time is around 2 weeks for most devices, and if it’s a “tower-type” computer, you probably won’t be moving it around), none-the-less, we’ll set a static IP for peace of mind.

First up, figure out what address you want the machine to have and addition information about your network such as:

  • Network interface name (eth0, eth1, wlan0, wlan1, etc.)
  • IP address (example: 192.168.1.8 — this must be in the private network IP range)
  • Netmask (typically 255.255.255.0 unless you are subnetting)
  • Network (usually the lowest address in your subnet, example: 192.168.1.0)
  • Broadcast (usually the highest address in your subnet, example: 192.168.1.255)
  • Gateway (IP address for your gateway, usually 1 higher than the network IP)
  • DNS-nameservers (optional, 8.8.8.8 8.8.4.4 are Google’s public dns nameservers)
  • DNS-search (optional, www.example.net)

If you don’t know this information the command below can help

ifconfig

Next, back up your network interfaces file

sudo cp -v /etc/network/interfaces{,.orig}

Remember that the command above is the same as typing

sudo cp -v /etc/network/interfaces /etc/network/interfaces.orig

Once you have that information and the file is backed up, open your network interface configuration file for editing with your favorite editor

sudo vi /etc/network/interfaces

As a default, you probably have something that looks somewhat similar to this:

Default contents of /etc/network/interfaces

Default contents of /etc/network/interfaces

and add your new information. Note that your values will differ somewhat from mine depending on your LAN. If you are using a different port on your network card, simply change eth0 to match. Mine when all finished looks like this:

Completed /etc/network/interfaces

Completed /etc/network/interfaces

Now that you’re done, restart the networking service and check that everything is working properly

sudo /etc/init.d/networking restart

Now you will always be able to access your machine using the same IP address.

Tagged , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published.

Protected by WP Anti Spam