Main Content

Configure Network Settings of Raspberry Pi Hardware

You can configure the network settings of your Raspberry Pi™ hardware by inspecting and editing the IP configuration of the Ethernet port.

You may need to reconfigure the IP settings if your Raspberry Pi board:

  • Has unknown IP settings

  • Is unreachable using a network connection

  • Is being moved to a network or direct Ethernet connection that uses static IP settings

  • Is being moved from a network that used static IP settings to one that uses DHCP services

There are several conditions under which networks use DHCP or static IP settings.

  • Use DHCP services — If your Raspberry Pi board is connected to a network with DHCP services, such as an office LAN or a home network connected to the Internet. DHCP is a network service that automatically configures the IP settings of Ethernet devices connected to a network.

  • Use static IP settings — If your Raspberry Pi board is directly connected to an Ethernet port on your host computer or connected to an isolated network without DHCP services.

To configure the Raspberry Pi board to use DHCP or static IP settings:

  1. You can use a terminal window after accessing the Linux® desktop. See Access Linux on Raspberry Pi Using Computer Peripherals.

  2. To display the content of the /etc/dhcpcd.conf file, enter this command in the terminal.

    cat /etc/dhcpcd.conf

    Be default, the IP address obtained from the DHCP server that you set in the dhcp.conf file, configures the DHCP client running on the Raspberry Pi board.

    By default, the DHCP client running on the Raspberry Pi board is configured to obtain an IP address from the DHCP server.

    DHCP Server IP Address

    You can configure a static IP address for your Raspberry Pi board by updating the dhcpcd.conf file. See the sample that shows how to configure the IP address of your Raspberry Pi board as 172.31.164.194.

    # Static IP configuration for eth0:
    interface eth0
    static ip_address=172.31.164.194/24
    static routers=172.31.164.1
    

    Static IP address for Raspberry Pi

  3. To create a backup of the /etc/dhcpcd.conf file, enter this command in the terminal.

    sudo cp /etc/dhcpcd.conf /etc/dhcpcd.backup

    If prompted, enter the root password.

  4. To edit the /etc/dhcpcd.conf file, enter this command in the terminal.

    sudo nano /etc/dhcpcd.conf
  5. To configure the static IP address for your Raspberry Pi board, add these lines of code for ip_address and router at the end of the dhcpcd.conf file.

    # Static IP configuration for eth0:
    interface eth0
    static ip_address=172.31.164.194/24
    static routers=172.31.164.1
    

  6. Save the changes and exit nano:

    1. Press Ctrl+X.

    2. Enter Y to save the modified buffer.

    3. For “File Name to Write: /etc/dhcpcd.conf”, press Enter.

    4. The nano editor confirms that it “Wrote # lines” and returns control to the command line.

  7. Reboot the board. In MATLAB® Command Window, enter:

    h = raspberrypi
    h.execute('sudo shutdown -r now')

  8. Test the IP settings by logging in to the board over a telnet session.

Tip

You can use the ifconfig command to temporarily change the IP settings. Rebooting the board removes the ifconfig settings and restores the /etc/dhcpcd.conf settings.

To change the IP settings temporarily, open a Linux command line. Enter ifconfig, the device id, a valid IP address, netmask, and the appropriate network mask. For example:

ifconfig eth0 192.168.45.12 netmask 255.255.255.0

Related Topics