Third party cookies may be stored when visiting this site. Please see the cookie information.

Configuring Linux / Raspberry Pi with a static IP address

To use Linux as a server or to access it remotely requires a static IP address. This is assuming a computer on a home network with a router that connects to the Internet over a broadband connection.

The following is based on the Raspberry Pi running Debian Linux, but will be similar for other distributions.

By default Linux will request a dynamic IP address which is issued by your router as required. Instead we need to provide it with an address that doesn’t change such as 192.168.1.4.

Note that this address can be used on the local network, but not on the Internet – later we’ll look at providing access through your router / firewall from the Internet.

First find out what DHCP address has been allocated by using the ifconfig command – see the extract below

...



eth0      Link encap:Ethernet  HWaddr b8:27:eb:8a:71:a0



          inet addr:192.168.1.110  Bcast:192.168.1.255  Mask:255.255.255.0



...

This is saying that the ethernet port 0 – has an IP address of 192.168.1.110

You will also need to find out what address your router is, using the route command

$ route

Kernel IP routing table



Destination     Gateway         Genmask         Flags Metric Ref    Use Iface



default         192.168.1.1     0.0.0.0         UG    0      0        0 eth0



192.168.1.0     *               255.255.255.0   U     0      0        0 eth0

This shows that the router IP address (Gateway) is 192.168.1.1 and all traffic is sent via that router.

At this point you will also need to check on what address range is being issued by the router. This depends upon the individual router. In my case I have a Belkin Wireless Router which can be reached by pointing a web browser to the IP address of the router 192.168.1.1

The LAN settings are shown below:

Belkin wireless router lan settings

In this case the local network has valid addresses from 192.168.1.1 to 192.168.1.254. The router is at address 192.168.1.1 and any DHCP requests will be given entries between 192.168.1.100 and 192.168.1.150 (you can change the range of the DHCP addresses if required). I have used 192.168.1.4 for this server.

To change to static IP address

cd /etc/networks



sudo nano interfaces

replace the line “iface eth0 inet dhcp” with

iface eth0 inet static



address 192.168.1.4



netmask 255.255.255.0



gateway 192.168.1.1

You should also take a look at the file /etc/resolv.conf

and check it has a nameserver entry (probably pointing at your default gateway) or direct to your ISP name servers.

nameserver 192.168.1.1

If the file has the following:

# Dynamic resolv.conf(5) file for glibc resolver(3) generated by resolvconf(8)

# DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN

then it is using resolvconf program which should instead be configured using the following two lines in /etc/network/interfaces

dns-nameservers

dns-search

Whilst you can dynamically reload the network interface I suggest a reboot at this stage to make sure that the configuration is correct.

sudo reboot

After logging in check using ifconfig to confirm that we have a static ip address

...

eth0      Link encap:Ethernet  HWaddr b8:27:eb:8a:71:a0



          inet addr:192.168.1.4  Bcast:192.168.1.255  Mask:255.255.255.0



...

Useful networking commands

To make dynamic changes to the network configuration use the ipconfig command.

The ipconfig command can also be used to activate and deactivate the interface, but that is better done using the ifup and ifdown commands which will provide details of any errors.

See the main Linux tutorials


» PenguinTutor Facebook page