Sunday, March 31, 2019

Setting a Static IP Address on the Raspberry Pi Running Raspbian Lite Stretch

By default, a Raspberry Pi with Raspbian installed as the operating system is set to request an IP address via DHCP.  On most networks, this means that the gateway router, or other DHCP server, will assign the Pi an address in a predetermined range.  This is optimal if the Pi will be accessing multiple networks, but if the Pi will mainly reside on a single network then it is often preferable to set a static IP address so that the Pi can be accessed over the network via SSH.

Luckily setting a static IP address on the Raspberry Pi is an easy task.  In order to do this we will need to edit the file /etc/dhcpcd.conf.  (Do not edit /etc/network/interfaces, as it is no longer used to manage network interfaces)  Before editing any configuration files, it is always best to make a copy of the default configuration.

sudo cp /etc/dhcpcd.conf /etc/dhcpcd.conf.default

Now you can edit /etc/dhcpcd.conf with a text editor such as Vim.  If you are not familiar with how to use a command line text editor, take a look at this tutorial on the basics of using Vim.

sudo vim /etc/dhcpcd.conf

You need to add the following lines to the end of the dhcpcd.conf file, substituting the value for static ip_address with the IP address we wish to assign to the Pi.  static routers should be set to your gateway router's IP, and static domain_name_servers should also be set to your gateway router unless you have set up a separate DNS server.

#static IP configuration
interface eth0
static ip_address=192.168.1.2/24
static routers=192.168.1.1
static domain_name_servers=192.168.1.1

If you are setting a static IP for the Wifi, use interface wlan0 instead of eth0

*As of the writing of this tutorial (March 2019), Raspbian Stretch is no longer using Predictable Network Interface Names, and has reverted back to using eth0 for the Pi's built-in Ethernet interface, and wlan0 for the Pi's built-in Wifi.  Because of this it is not necessary to check the interface names before configuring the interfaces.  If this changes I will update this tutorial.

Once you have edited dhcpcd.conf, save your changes, and reboot the Pi in order for the new network setting to take effect.

sudo reboot


Checking Your Current IP Address



Whether you have just assigned a static IP, or you just want to see the address assigned to you by the DHCP server, there are several ways to check the IP address of your Pi.  The first is the simplest, and will only give you the IP address.

hostname -I

If you would like a little more information along with the IP address you can use ifconfig.

ifconfig

Finally, I would be remiss to not mention that the ifconfig we have all loved for years has become obsolete, and while still available on most systems, is being replaced by a new set of commands in a collection known as ip.  To use the ip command that most resembles ifconfig, try ip addr.

ip addr

No comments:

Post a Comment