Follow by Email

Blog Archive

Search This Blog

Raspberry Pi Static / fixed IPv4 address for Raspbian Jessie and Stretch

Share it:
Raspberry Pi: Static / fixed IPv4 address for Raspbian Jessie and Stretch
RaspberryPi-Static-IPv4

If you are using Raspberry Pi for the first time and would like to access it via SSH, you first have to find out the IPv4 address. Cleverly,  Raspberry Pi displays its IPv4 address on the screen after the boot process, provided it is put into operation with the keyboard and screen. Unfortunately, the IPv4 address changes again and again, because the IPv4 address assignment by the DHCP server in the local network is not always assigned the same address, but dynamically any of its address pool.

If you use Raspberry Pi as a client, that's no problem. But if you want to operate Raspberry Pi as a server within the local network or often want to access it via SSH, then a fixed IPv4 address is an advantage. Note here: You can also connect via the host name z. B. "raspberrypi.local" build. Only that does not always work.
Note: In technical terminology, this is not a "fixed IP address" but a "static IP address" because, in contrast, there are dynamic IP addresses assigned by a DHCP server. In the case of a "static IP address" one speaks of a manual IP configuration.

Note: If you configure static IP addresses, they should not come from the DHCP pool of a DHCP server.
task
  • Determine a free IPv4 address and further IP configuration.
  • Change the IPv4 configuration to static.
  • Check the IPv4 configuration.
  • Check the network connection for function.
Overview
There are not just one, but several variants of how to make a working network configuration. Which one one uses depends on the requirements and personal preference.
  • IPv4 configuration in the file / etc / network / interfaces
  • IPv4 configuration via DHCP client Deamon (DHCPCD)
  • Exclude interface by DHCPCD (combination)
  • Assign IPv4 configuration in the router (central IP configuration)
  • IPv4 configuration via systemd-networkd
Officially, IP configuration is recommended for Raspbian Jessie via the DHCPCD (solution 2). In principle, there is nothing wrong with making a static IP configuration via the file "/ etc / network / interfaces" (solution 1). You can also combine both solutions (solution 3).
The future route is via "systemd-networkd", which can also be used to configure the IP (solution 5). The easiest way would certainly be to make the IP allocation in the router (solution 4). You would not have to make any changes to Raspberry Pi.

Troubleshooting: Network configuration
Problems with network configuration are not excluded. So right at the beginning a few hints for problem solving.
Note: Name of the network interfaces
Since Raspbian Stretch, the Ethernet and WiFi network interfaces have different names. So no longer "eth0" and "wlan0", but "enx ..." and "wlx ...". This concerns USB-connected network adapters whose names differ from the designations mentioned here. This means that one must first determine the individual name or change the naming to the old method.
Solution (variant 1): IPv4 configuration in the file "/ etc / network / interfaces"
Open the configuration file for the network or IP configuration:
sudo nano / etc / network / interfaces
A complete IPv4 configuration looks like this:
# Ethernet
car eth0
allow-hotplug eth0
iface eth0 inet static
address 192.168.1.2
netmask 255.255.255.0
gateway 192.168.1.1
dns-nameservers 192.168.1.1
This is an example configuration. This can work, but does not have to. Just trying it out does not make sense. You should first clarify what you have to enter here and not somehow play around. It is helpful if you look up what other clients have in their own network or what, for example, has already been assigned by DHCP.

When assigning the IPv4 address, however, you must make sure that you select an address that is NOT yet used and is NOT in the address pool of a DHCP server (eg Internet router). Otherwise there can be connection problems in the network.

After making the changes you can save and close the file: Ctrl + O, Return, Ctrl + X.

Note: In Raspbian Wheezy as of 2015-05-05 and in Raspbian Jessie, a DHCP Client Daemon (DHCPCD) is by default active, which can become a problem if you do the IPv4 configuration this way.

If you definitely do not need this DHCP client, then you should disable it.

At Raspbian Wheezy:
sudo service dhcpcd stop
sudo update-rc.d -f dhcpcd remove
At Raspbian Jessie:
sudo service dhcpcd stop
sudo systemctl disable dhcpcd
To apply the changes, a reboot is recommended.
sudo reboot
It is less radical to restart the "networking". But that only makes sense if you are NOT connected to Raspberry Pi via SSH.
sudo service networking restart
Alternatively, you can switch the interface "eth0" off and on again. This is only useful if you are NOT connected via SSH, but sit locally with the screen and keyboard on Raspberry Pi.
sudo ifdown eth0
sudo ifup eth0
This terminates the eth0 interface and restarts. At startup, the settings are applied. After that,  Raspberry Pi would have to be reachable with its static IPv4 address.
Then check the network configuration.

Solution (variant 2): IPv4 configuration via DHCP client Deamon (DHCPCD)
In Raspbian Jessie, a DHCP Client Daemon (DHCPCD) is enabled by default. The officially recommended way to configure an IPv4 is through the DHCPCD, which has a configuration file.

For this it is important to determine if the "dhcpcd" is active at all.
sudo service dhcpcd status
If the status provides an installed but disabled "dhcpcd", then it is recommended to turn it on. Only then should one make the configuration over the "dhcpcd".
sudo service dhcpcd start 
sudo systemctl enable dhcpcd
Before you start the configuration with the "dhcpcd", you should restore the original state of the file "/ etc / network / interfaces" . The interfaces must be set to "manual" in the "iface" option.

For static IPv4 configuration, open the file "/etc/dhcpcd.conf".
sudo nano /etc/dhcpcd.conf
Here you enter as an example the following lines:
interface eth0
static ip_address = 192.168.1.2 / 24
static routers = 192.168.1.1
static domain_name_servers = 192.168.1.1
This is an example configuration. This can work, but does not have to. Just trying it out does not make sense. You should first clarify what you have to enter here and not somehow play around. It is helpful if you look up what other clients have in their own network or what, for example, has already been assigned by DHCP.

When assigning the IP address, however, you must make sure that you choose one that is NOT yet used and are not in the address pool of a DHCP server. Otherwise there will be connection problems in the network.

The line "static ip_address = 192.168.1.2 / 24" specifies the IPv4 address "192.168.1.2" with the subnet mask "255.255.255.0". The term "24" is a shorthand notation for the subnet mask.

After making the changes you can save and close the file: Ctrl + O, Return, Ctrl + X.

One changed only the file now. However, the changes have not yet been incorporated into the current network configuration. Basically, a reboot is recommended here, if you have made the configuration via SSH.
sudo reboot
It is less radical to restart the "networking". But that only makes sense if you are NOT connected to Raspberry Pi via SSH.
sudo service networking restart
Alternatively, you can switch the interface "eth0" off and on again. This is only useful if you are NOT connected via SSH, but sit locally with the screen and keyboard on the Raspberry Pi.
sudo ifdown eth0
sudo ifup eth0
This terminates the eth0 interface and restarts. At startup, the settings are applied. After that, Raspberry Pi would have to be reachable with its static IPv4 address.

Solution (variant 3): exclude interface by DHCPCD (combination)
If you do not want to disable the DHCPCD because you need it for an interface, then you can exclude or explicitly release the configuration through the DHCPCD for an interface.

To do this we open the configuration file of the DHCPCD and enter a line there.
sudo nano /etc/dhcpcd.conf
Exclude network interface from the configuration by the DHCPCD:
deny interfaces eth0
This line excludes the relevant interface from the network configuration. The network configuration for this interface must then be done in the file "/ etc / network / interfaces".

Explicitly release interface:
allow interfaces eth0
After making the changes you can save and close the file: Ctrl + O, Return, Ctrl + X.

However, the changes have not yet been incorporated into the current network configuration. Basically, a reboot is recommended here, if you have made the configuration via SSH.
sudo reboot
It is less radical to restart the "networking". But that only makes sense if you are NOT connected to Raspberry Pi via SSH.
sudo service networking restart
Alternatively, you can switch the interface "eth0" off and on again. This is only useful if you are NOT connected via SSH, but sit locally with the screen and keyboard on the Raspberry Pi.
sudo ifdown eth0
sudo ifup eth0
This terminates the eth0 interface and restarts. At startup, the settings are applied. After that,  Raspberry Pi would have to be reachable with its static IPv4 address.

Solution (variant 4): Assign IPv4 configuration in the router (central IP configuration)
If you miss Raspberry Pi with a static IP configuration with a fixed IPv4 address, but do not want to touch its network configuration, then you can also use a function in the Internet router. Some of these devices have a feature that allows the IPv4 address to be set to the MAC address. In that case, the MAC address of the network interface would have to be determined, which is to assign an IPv4 address in the router. The network configuration continues to get the Raspberry Pi via DHCP. Only then he always gets the same IPv4 address assigned.
This solution has the inescapable charm of not having to touch Raspberry Pi. Disadvantage of this solution is that for this purpose the DHCP server must be active when the Raspberry Pi gets the IP configuration via DHCP. If not, then there is the Raspberry Pi without IPv4 address.

Solution (variant 5): IPv4 configuration via systemd-networkd
In addition to the old configuration via "/ etc / network / interfaces" or "dhcpcd", there is also the systemd variant. In future versions of Raspbian, "systemd" will take over the network configuration.

One should not make the switch to "systemd-networkd" via SSH. If there is a connection break, then you may not get the connection rebuilt.

First we check if the service "systemd-networkd" exists. That is the condition that this solution is possible at all. It should work for Raspbian Jessie.
systemctl status systemd-networkd
There should be "Loaded: loaded".

First, it's about at least disabling old network or networking components. Uninstalling and deleting is not recommended because otherwise it will be difficult to return.
sudo update-rc.d networking remove
sudo systemctl stop dhcpcd
sudo systemctl disable dhcpcd
Enable and start the name-resolved service "systemd-resolved":
sudo systemctl enable systemd-resolved
sudo systemctl start systemd-resolved
Check if " systemd-resolved " is running:
sudo systemctl status systemd-resolved
The line "Loaded: loaded" should be "enabled" at the back.

Then we put a symbolic link for the file with the address of the name server. If this link is missing, then the name resolution may not work.
sudo ln -sf /run/systemd/resolve/resolv.conf /etc/resolv.conf
Then it's about creating a network configuration. This can be distributed in several files, which are all in the same directory.
ls / etc / systemd / network /
If the directory is empty, then you have to create the necessary files yet. The file name is irrelevant. He just has to end up with .network. Nevertheless, it is advisable to choose meaningful file names in order to find your way easier in case of an error.

Example of a static IPv4 configuration for the eth0 interface:
sudo nano /etc/systemd/network/eth0.network
Entry for a static IPv4 configuration:

[Match]
Name = eth0

[Network]
Address = 192.168.1.2 / 24
Gateway = 192.168.1.1 
DNS = 192.168.1.1
Alternatively: Entry for an IP configuration with DHCP:
[Match]
Name = eth0

[Network]
DHCP = yes

Save and close with Ctrl + O, Return, Ctrl + X.

Activate and start the service "systemd-networkd":
sudo systemctl enable systemd-networkd
sudo systemctl start systemd-networkd
Check if "systemd-networkd" is running:
sudo systemctl status systemd-networkd
In the line "Loaded:" the back should be "enabled" and the interface should be configured. In this case "eth0: link configured".
Then restart the system:
sudo reboot
After the restart, the system should run with "systemd-networkd".

Solution: Check network configuration
Whether the IPv4 settings were correctly adopted, you should check.

The IP address can be checked as follows.
ip a
The interface "eth0" must then have received the static configured IPv4 address in the line with "inet address".

Then check whether the default gateway or the default route has been entered. That is important, so that you even come to the Internet.
ip r
If "default" points to the correct IPv4 address of the default gateway, then everything is fine.

Now the DNS server is missing. This is necessary for the name resolution to work and for connections to the Internet via the determined IP addresses via the standard gateway.
cat /etc/resolv.conf
Is behind "nameserver" the IPv4 address of the DNS server, then everything is fine here.

Troubleshooting: Double IPv4 address
Basically, an interface (eth0, wlan0, etc.) may only have one IPv4 address. The network configuration check revealed that the IPv4 address was duplicated. That can lead to problems.
ip a
An exemplary output:
inet 192.168.1.2/24 brd 192.168.1.255 scope global eth0
   valid_lft forever preferred_lft forever
inet 192.168.1.132/24 brd 192.168.1.255 scope global secondary eth0
   valid_lft forever preferred_lft forever
As you can see, the eth0 interface has two IPv4 addresses, which can cause problems on the network. Some routers are allergic if a host is reachable under two IPv4 addresses.

Here the DHCP client daemon spits us into the soup. If you definitely do not need this DHCP client, then you should disable it.
sudo systemctl stop dhcpcd
sudo systemctl disable dhcpcd
To adopt the changes, we recommend a reboot.
Troubleshooting: No hostname anymore
If you set the network configuration after solution 1, 2 or 3, then there is the problem that you no longer get the host name of the Raspberry Pi in the Internet router. Only if you leave the settings in the original state appears in the router, the host name "raspberrypi" (default). 
The reason for this is the static network configuration. In that case, there is no active communication between Raspberry Pi and the router. And thus, the router does not experience the host name of the Raspberry Pi. 
You can solve the problem with variant 4. Here then the communication with DHCP takes place and Raspberry Pi communicates in this way its host name.
The prerequisite for this to work is that in the file "/etc/dhcpcd.conf" in a line "hostname" is.

Extension: IPv4 configuration with the network manager "wicd-curses"
"wicd-curses" is a graphical network manager that presents all the important information in a user interface that must be operated with the keyboard. If you find it difficult to edit text files, this can be an alternative.
Solution: install wicd-curses
First you have to install "wicd-curses":
sudo apt-get install wicd-curses
Then you simply run the tool.
sudo wicd-curses
Extension: access Raspberry Pi via hostname (Zeroconf / Bonjour / Avahi)
If you do not want to work with IP addresses and their configuration, you can also solve the problem with Zeroconf. With Zeroconf, also called Bonjour or Avahi, one can establish within the local network a connection to the Raspberry Pi with its host name. In other words, instead of the IP address that you may not even know, you simply take the hostname, which is much easier to remember. This works both in the browser and via SSH.
Share it:

Raspberry Pi

Post A Comment:

0 comments: