294

I have followed (any one of hundreds) tutorial, and it does not work.

How do I set up networking/WiFi/static IP address on the Raspberry Pi?


After the Foundation introduced the dhcpcd networking system to Raspbian in May 2015 there were hundreds of questions (partly because the Foundation didn't produce any documentation). After answering dozens of questions I decided to produce a canonical answer.
This proved to be more difficult than anticipated - especially as the implementation keeps changing - hopefully I have continued to keep it up-to-date.

(Originally I referred to this as a Dorothy Dixer, but this is apparently an Australian idiom.)

Milliways
  • 54,718
  • 26
  • 92
  • 182
  • FYI: You are probably aware of this, but ICYI the [RPi PTB have decided to dump `dhcpcd` in favor of `NetworkManager`](https://www.raspberrypi.com/news/the-latest-update-to-raspberry-pi-os/) – Seamus Sep 14 '22 at 00:58

6 Answers6

329

This tutorial describes how to setup networking using the default network manager dhcpcd included in Raspbian since 2015-05-05 and Raspberry Pi OS.
It applies to the Foundation releases of Raspberry Pi OS -Bullseye, Raspberry Pi OS -Buster, Raspbian Buster, Raspbian Stretch, Raspbian Jessie and the last Raspbian Wheezy.
Bullseye & Buster settings are identical to Stretch.

How to setup Raspbian Networking

If you are using an Ethernet connection with a router there should be no configuration required and it should work out of the box.

The Pi3B+ and Pi4 have Gigabit Ethernet interfaces and will only work on a Gigabit Ethernet (1000BASE-T) router if a 8 wire CAT 5E cable is used. Earlier Pi models which only supported 100BASE-T work over 6 wire cable.


NOTE The Pi3/Pi Zero W inbuilt WiFi does not support 5GHz networks, and may not connect to Ch 12,13 on 2.4GHz networks until wireless regulatory domain is set.

WiFi on 5GHz enabled devices is disabled until wireless regulatory domain is set (Pi4B, Pi3B+, Pi3A+)

  • The domain can be set through Raspberry Pi Configuration (rc_gui), raspi-config or by setting country= to an appropriate ISO 3166 alpha2 country code in /etc/wpa_supplicant/wpa_supplicant.conf.

If you use the latest Raspberry Pi Imager there are advanced options, invoked by the magic key sequence: Ctrl-Shift-X. These enable you to set many initial options; hostname, username, password, ssh, WiFi Country Code etc during imaging. This should enable you to setup a headless system, without the need to edit any files.

If you are using WiFi and the GUI set up by following Using the Desktop

If you are using WiFi from the Command Line set up by following Using the Command Line
This is the only way to set up a network which does not broadcast SSID.

This can also be used to setup a 'Headless' system e.g. using a serial console cable, but it MUCH easier if you can borrow a monitor and keyboard for setup. See below for another possible method using an Ethernet cable to a PC.


For most users READ NO FURTHER

The following are Advanced options

Headless Raspbian WiFi Setup

Raspbian, since May 2016, checks the contents of the boot directory for a file called wpa_supplicant.conf, and will copy the file into /etc/wpa_supplicant, replacing any existing wpa_supplicant.conf file that may be there. The file in the boot directory is then removed. This can be used to enable headless setup, using the wpa_supplicant.conf settings detailed below or Setting up a Raspberry Pi headless (You will probably want to enable ssh as well.)

NOTE the links to raspberrypi.org suggest using ifconfig - this is deprecated use ip a; ip r instead.

NOTE If you use the latest Raspberry Pi Imager the advanced options Ctrl-Shift-X make the above unnecessary.

What is my IP Address?

If you just want to know the IP Address your Pi is using enter hostname -I on the command line.

SSH

As of the November 2016 release, Raspbian has the SSH server disabled by default. You will have to enable it manually.

Enter sudo raspi-config in the terminal, first select advanced options, then navigate to ssh, press Enter and select Enable or disable ssh server.

For headless setup, SSH can be enabled by placing a file named 'ssh', without any extension, onto the boot partition of the SD card.

Networking Files

If you are running a recent Raspbian /etc/network/interfaces should be as below. If you have changed it PUT IT BACK. (Or on Stretch just delete it - it effectively does NOTHING.)

# interfaces(5) file used by ifup(8) and ifdown(8)

# Please note that this file is written to be used with dhcpcd
# For static IP, consult /etc/dhcpcd.conf and 'man dhcpcd.conf'

# Include files from /etc/network/interfaces.d:
source-directory /etc/network/interfaces.d

The file /etc/wpa_supplicant/wpa_supplicant.conf will be created/modified by the recommended setup methods, but can be setup by hand. Recent Raspbian have an option in raspi-config to enter SSID and Password. It should contain something like the following:-

ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
country=AU

network={
    ssid="ESSID"
    psk="Your_wifi_password"
}

If you need to connect to a private network (i.e. no broadcast SSID) include the line scan_ssid=1 inside network={⋯}.

NOTE If you want to connect to different networks (e.g. at work or home) you can include multiple network={⋯} entries.

If you have access to multiple networks and want to connect to a specific network, include the line priority=100 inside the network={⋯} entry.
You can select a different priority for each network, the highest priority accessible network will be selected; the default is 0.

There are many other options which can be used see man wpa_supplicant.conf.

Notes on obsolete Operating Systems

Jessie does not include the 10-wpa_supplicant hook to manage WiFi interfaces, so links to wpa_supplicant are needed. These settings are incompatible with Predictable Network Interface Names.

The /etc/network/interfaces used by Jessie should be:-

# interfaces(5) file used by ifup(8) and ifdown(8)

# Please note that this file is written to be used with dhcpcd
# For static IP, consult /etc/dhcpcd.conf and 'man dhcpcd.conf'

# Include files from /etc/network/interfaces.d:
source-directory /etc/network/interfaces.d

auto lo
iface lo inet loopback

iface eth0 inet manual

allow-hotplug wlan0
iface wlan0 inet manual
    wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf

allow-hotplug wlan1
iface wlan1 inet manual
    wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf

NOTE calling dhcp in /etc/network/interfaces will disable dhcpcd.

Predictable Network Interface Names

Stretch introduced1 predictable network interface names which have been used in other distributions for some time.

Network interfaces will have names formed from a prefix en — Ethernet or wl — wlan followed by x indicating MAC and the MAC e.g. enxb827eb123456 or wlx00c140123456. The onboard WiFi of the Pi4, Pi3 and PIZeroW which is connected over sdio will however use the name wlan0

This can be beneficial to those using multiple network interfaces, however for most Pi users, with a single Ethernet and WiFi interface will make little difference. The previous names eth0 and wlan0 can be restored if you pass net.ifnames=0 on the kernel command line in /boot/cmdline.txt.

There is an option in raspi-config to enable predictable network interface names.

1. For some incomprehensible reason the Foundation decided to "Disable predictable network interface names for Ethernet devices" only 3 weeks after introducing the long-awaited feature. There is an option under Advanced Options of raspi-config to toggle this setting.


Setup a Static IP Address

If you want your Pi to be assigned a predictable IP Address you can either reserve one in your router OR request the DHCP server to assign one.
E.g. the following will request an address on wlan0.

interface wlan0
request 10.1.2.99

request [address]
Request the address in the DHCP DISCOVER message. There is no guarantee this is the address the DHCP server will actually give. If no address is given then the first address currently assigned to the interface is used.

If you request an IP Address within the range managed by the DHCP server which is available this should be honoured, otherwise the DHCP server will allocate an address as normal.

If you want an IP Address outside the range managed by the DHCP server e.g. if you have a range of addresses reserved use the inform directive.

inform address[/cidr[/broadcast_address]]
This does not get a lease, just notifies the DHCP server of the address in use. You should also include the optional CIDR network number in case the address is not already configured on the interface.

If you REALLY need a static IP Address see separate answer Static IP Address


Advanced dhcpcd Configuration

Raspbian, by default, uses dhcpcd to manage network interfaces. This is automatic, and most users need do no more than specify the SSID and password for wireless networks.

It is possible to configure how dhcpcd works by entering options in /etc/dhcpcd.conf; see man dhcpcd.conf.There is a good article on dhcpcd at https://wiki.archlinux.org/index.php/dhcpcd

The following describe some of the more common configurations:-

Fallback profile

It is possible to configure a static profile within dhcpcd and fall back to it when DHCP lease fails. This is useful particularly for headless machines, where the static profile can be used as "recovery" profile to ensure that it is always possible to connect to the machine. The static profile is setup as any other Static IP Address

    # define static profile
    profile static_eth0
    static ip_address=⋯
    static routers=⋯
    static domain_name_servers=⋯

    # fallback to static profile on eth0
    interface eth0
    fallback static_eth0

Prevent dhcpcd from configuring an interface

This is often done to enable the Pi to act as an Access Point (which needs to be configured using other files), while allowing normal DHCP configuration on other interfaces.

Add denyinterfaces wlan0 to the end of the file (but above any other added interface lines).

Prevent dhcpcd from configuring a gateway on an interface

If you want an interface to not install any default routes (often used in conjunction with a static IP address) specify

    nogateway

Host Specific Configuration

dhcpcd can configure interfaces dependent on the host network. This uses the Address Resolution Protocol (ARP) to probe hosts based on MAC or IP address before attempting DHCP resolution.

    interface bge0
    arping 192.168.0.1

    # My specific 192.168.0.1 network
    profile dd:ee:aa:dd:bb:ee
    static ip_address=192.168.0.10/24

    # A generic 192.168.0.1 network
    profile 192.168.0.1
    static ip_address=192.168.0.98/24

You can speed up DHCP by disabling ARP probing if you are not using these features and have a simple network.

    noarp

Use different wpa_supplicant files

It is possible to configure dhcpcd to use different wpa_supplicant.conf files for a specific wireless interface.

Create a file named wpa_supplicant-"$interface".conf in /etc/wpa_supplicant/ e.g. wpa_supplicant-wlan0.conf will only be used by wlan0

/etc/wpa_supplicant/wpa_supplicant.conf will be used for any other wireless interfaces.


Connecting a Computer to the Pi

Recent versions of Raspbian (which use dhcpcd) allow ssh to work over a link-local address and avahi (which is a zeroconf implementation) enables programs to discover hosts running on a local network.

This means you can plug the Pi into a Computer (with an Ethernet cable) or a local network router and connect without knowing the IP address.

You can easily connect from Linux and OS X with ssh username@hostname.local (Prior to 2020-04-04 version of Bullseye the default username was 'pi', hostname raspberrypi. This is now set on initial boot.) This should work with popular GUI ssh programs. This is sometimes problematic with some versions of Windows and networks which use .local in a non-standard way. (See https://en.wikipedia.org/wiki/.local)

NOTE .local resolution does not always work e.g. in rsync. The following should resolve IP (and can be included in bash scripts)
RemotePi=$(getent hosts hostname.local | awk '{ print $1 }')

If your system does not have getent to query the hosts e.g. macOS you can use the following:-
RemotePi=$(arp -n hostname.local | awk '{x = $2; gsub(/[()]/, "", x); print x }')

If you have multiple Pi you need to make sure each Pi has a unique hostname.

You can use a crossover cable, but you don't need one (most modern interfaces automatically detect).

One drawback of direct connection is that the Pi will have no Internet access and the date will not be set. You can copy the date from the host by running ssh username@hostname.local sudo date -s$(date -Ins) before connection.

Scope:

This tutorial is about setting up a normal Raspbian/Raspberry Pi OS installation to access the internet using the included software. It also covers connections to other computers on the same network.

It is primarily aimed at helping new users struggling to get their Pi (especially WiFi) working .

It does NOT cover:-

  • Other Network Managers.
  • Running advanced networking on the Pi (e.g. DHCP servers, Tunnelling, VPN, Access Point).
  • Use of ipv6 (although if you have an ipv6 network this should work).
  • Alternate networking setups. (There are many different ways of setting up networking.)
Milliways
  • 54,718
  • 26
  • 92
  • 182
  • Shouldn't there be hashes (`#`) on the first few lines of `dhcpd.conf`? – Phil B. Nov 05 '15 at 12:48
  • @PhilB. This is an extract from man dhcpcd.conf (as stated) NOT something you would type into the file. – Milliways Nov 05 '15 at 22:32
  • Ok - made a formatting change to make that clearer. – Phil B. Nov 05 '15 at 22:49
  • What about `/etc/resolv.conf` etc? – Wilf Nov 26 '15 at 22:32
  • 1
    @Wilf "What about /etc/resolv.conf etc?" ⋯ "sources" for what in particular? This is mostly links to raspberrypi.org plus extracts from the man files. All tested by me (apart from the static IP). As always TIMTOWTDI. – Milliways Nov 29 '15 at 22:36
  • 2
    I got confused by the "this is NOT something you would type into the file". If I'm not mistaken, what Milliways means here is that you wouldn't *replace the entire file* with this bit of text. However, it's perfectly fine to add this bit at the end (modifying the parameters to your liking, of course). Also, the `routers` and `domain_name_servers` lines are optional if you don't want to set up routing through that interface. – Mark Dec 04 '15 at 18:34
  • 4
    So, now, to configure your ip address, you need to have the DHCP client installed and use its configuration to set a static ip?! It doesn't work if I want a DHCP server on my Raspbian! The DHCP client is run *after* the DHCP server launch, so the ip is not yet set, making the DHCP server (isc-dhcpd) complain. – Luc Stepniewski Dec 23 '15 at 10:31
  • 23
    Where is this *officially* documented? – Alastair McCormack Jan 16 '16 at 14:13
  • 7
    Where IS this documented? I mean, WTF. I have followed these directions as well as many others and cannot get connected. I can happily see and scan networks, but it will never connect or associate. I'm so cranky about this. – AaronJAnderson Feb 04 '16 at 01:09
  • @AaronJAnderson "this" is documented in the dictionary. If you have a question which can be answered ask it as a separate question. The tutorial above is mostly links to instructions or extracts from man files. – Milliways Feb 04 '16 at 02:23
  • In the "Networking Files" section why is there both wlan0 and wlan1? Also why use "iface {name} inet manual" instead of "iface {name} inet dhcp" like many other tutorials suggest? – Ralph Feb 13 '16 at 11:12
  • 1
    @milliways what if you want to run a dhcp server on the pi with a wired connection? This is quite difficult to follow. – Hugh Jones May 19 '16 at 16:15
  • I was redirected here from a forum where I explain my issue to create a router with a RPi. I followed your tutorial step by step with my Jessie deserved Raspberry but I'm still not able to connect to wifi when connecting with another computer to the RPi created WAP. Yet it seems that what I'm doing here isn't in the scope of the tutorial. However I followed a tutorial that was pre Jessie: http://raspberrypihq.com/how-to-turn-a-raspberry-pi-into-a-wifi-router/... Therefore, what should I do to finalize my raspberry router? – Revolucion for Monica Jun 09 '16 at 15:01
  • 1
    I'd be curious to know what the advantages and drawbacks are of using dhcpcd over the traditional interfaces file. Though if dhcpcd is the standard method in raspbian then I see no reason not to use it. – Mike D. Aug 06 '16 at 06:28
  • 4
    @MikeD. See https://www.raspberrypi.org/blog/another-raspbian-desktop-user-interface-update/ – Milliways Aug 06 '16 at 06:34
  • Thank you ! Finally, I found the right post with valuable information that helped me to make it work ! I've been browsing amid a lot of posts with poor information, people throwing some commands without explanation that surely works for them but not for all environments ! – alexserver Aug 18 '16 at 03:33
  • Very useful, worked like a charm (used dhcpcd method). – greuze Oct 17 '16 at 13:54
  • This is probably the best answer i've ever read here! Additionally I have to say that the option which worked best for me is the "Network Interfaces Method" without the wpa_supplicant.conf file. E.g.: iface wlan0 inet static address 192.168.0.198 netmask 255.255.255.0 gateway 192.168.0.254 network 192.168.0.0 broadcast 192.168.0.255 wpa-ssid "WIFINAME" wpa-psk "WIFIPASSWD" – scherzkrapferl Nov 22 '16 at 23:07
  • I tried disabling `dhcpcd` and enabling `networking`. I configured `/etc/network/interfaces` like in a normal debian but this didn't work, the network was not configured. If I did manually `ifup eth0` then the network git configured. Did I miss something? – Pablo Dec 06 '16 at 21:32
  • +1 for mentioning the fallback mechanism. I have some strange issues with my headless RPi where it fails to obtain an IP from the router. This is a real safer. – bcelary Jan 21 '17 at 21:55
  • If you've upgraded from an older distribution and don't have dhcpd, you need to run `sudo apt-get install raspberrypi-net-mods` to get the new networking package. – Jarett Millard Jan 29 '17 at 20:54
  • Only the network interface method worked for me, for static ip – Pini Cheyni Feb 03 '17 at 20:10
  • Godspeed, that's how you write an answer. – igauravsehrawat Feb 07 '17 at 10:45
  • Thank you. The useful part starts at "Network Interfaces method" – Tor Klingberg Apr 13 '17 at 10:38
  • 1
    @TorKlingberg You are welcome, but I still think using static addresses is not the best idea. `dhcpcd` is more complex to setup, but performs better, particularly if WiFi goes up and down. It is really only included for those used to the old method. – Milliways Apr 13 '17 at 11:36
  • Sure, most machines should just use dhcp, but there are lots of special cases where a static address is useful. – Tor Klingberg Apr 13 '17 at 14:27
  • 1
    When you're looking for a 30 second "What file do I edit?" answer, this is not the one to go to. Way way way too verbose! – DaveStephens Sep 29 '17 at 06:56
  • 2
    @DaveStephens When you are looking for a 30 second "What file do I edit?" the answer is **DO NOTHING** - it works out of the box; this is to help those who fiddle with the settings. – Milliways Sep 29 '17 at 07:13
  • 1
    I don't understand the problem with static IPs. For me its a lot easier to set a static so I can connect to it when I need it instead of trying to figure out what the address is. –  Oct 20 '17 at 14:06
  • I created a gist that generates the `/etc/dhcpcd.conf` using the process you described. https://gist.github.com/RichardBronosky/1526ec6a585dab8f1fc9990234ac4ed0 – Bruno Bronosky Oct 26 '17 at 22:05
  • Make names "predictable"... and then add the MAC address to the name. Predictable? Yes, if you know the MAC address. – Marc Apr 01 '18 at 09:27
  • Many people want strict control over (device) <---> (IP address) mappings. The advantage of using static IP addresses is that everything keeps its IP address even if you change routers. The advantage of using dynamic IP addresses is the flexibility of managing assignments in a centralized location, and device conflicts are easier to avoid. The latter is a much more common scenario than the former. So, yes, most people should be using DHCP, and hence dhcpcd. – David Stein Jul 03 '18 at 00:36
  • Following this tutorial for the Pi Zero W wasn't working until I discovered the Pi came with a default language region of GB. I changed it to US UTF-8, matching my country code in the wpa_supplicant.conf, and rebooted and now it works just fine. Kind of a "Gotcha!" – J E Carter II Apr 05 '19 at 18:04
  • I tried and tried and nothing worked until I DELETED the lease file under /var/lib/dhclient.wlan0.leases – Steffen Roller Oct 28 '19 at 03:50
  • «The Pi3B+ and Pi4 have Gigabit Ethernet interfaces and will only work on a Gigabit Ethernet (1000BASE-T) router if a 8 wire CAT 5E cable is used» What? This makes no sense, the Pi should negotiate whatever speed / duplex it needs... – Alba Mendez Jan 13 '20 at 17:38
  • I found this to be a vital find. Wish I found it days ago - I’d have more hair. For wifi where I have situations roaming across mesh zones or where wifi drops, I’m finding my rpiz to be very fault intolerant. What is the recommended at to recover once wlan0 carrier is lost? – leeprevost Sep 10 '22 at 21:59
  • @Milliways I am old enough to remember your excellent Forth compiler! I still use my HP 11c also with RPN. :) I wonder if you would be willing to comment on my problem references here: https://raspberrypi.stackexchange.com/questions/139061/wifi-carrier-lost-recovery-dhcpcd-and-avahi-problems?noredirect=1#comment237300_139061 – leeprevost Sep 11 '22 at 17:11
105

This tutorial describes how to setup networking using the default network manager dhcpcd included in Raspbian since 2015-05-05 and Raspberry Pi OS.
It applies to the Foundation releases of Raspberry Pi OS -Bullseye, Raspberry Pi OS -Buster, Raspbian Buster, Raspbian Stretch, Raspbian Jessie and the last Raspbian Wheezy.
Bullseye & Buster settings are identical to Stretch.

How to setup Raspbian Networking - Preliminaries

The Pi3B+ and Pi4 have Gigabit Ethernet interfaces and will only work on a Gigabit Ethernet (1000BASE-T) router if a 8 wire CAT 5E cable is used. Earlier Pi models which only supported 100BASE-T work over 6 wire cable.


NOTE The Pi3/Pi Zero W inbuilt WiFi does not support 5GHz networks, and may not connect to Ch 12,13 on 2.4GHz networks until wireless regulatory domain is set.

WiFi on 5GHz enabled devices is disabled until wireless regulatory domain is set (Pi4B, Pi3B+, Pi3A+)

  • The domain can be set through Raspberry Pi Configuration (rc_gui), raspi-config or by setting country= to an appropriate ISO 3166 alpha2 country code in /etc/wpa_supplicant/wpa_supplicant.conf.

If you use the latest Raspberry Pi Imager there are advanced options, invoked by the magic key sequence: Ctrl-Shift-X. These enable you to set many initial options; hostname, password, ssh, WiFi Country Code etc during imaging.

Setup a Static IP Address

Questions about setting Static IP Address are among the most common on this site. There are very many tutorials (many wrong, obsolete or incomplete).

If the reason you are contemplating a Static IP Address is you want your Pi to be assigned a predictable IP Address you can request the DHCP server to assign one.
E.g. Adding the following to /etc/dhcpcd.conf will request an address on wlan0 and on eth0.

interface wlan0
request 10.1.2.99

interface eth0
request 10.1.2.98

If you request an IP Address within the range managed by the DHCP server which is available this should be honoured, otherwise the DHCP server will allocate an address as normal.

If you want an IP Address outside the range managed by the DHCP server e.g. if you have a range of addresses reserved use the inform directive.

inform address[/cidr[/broadcast_address]]
This does not get a lease, just notifies the DHCP server of the address in use. You should also include the optional CIDR network number in case the address is not already configured on the interface.

Disclaimer

Before proceeding I feel obliged to state that setting up a static address is NOT recommended. Telecommunications Engineers do not do this. Static IP Addresses can be the bane of a Network Administrator's life. There are situations where Static IP Addresses are necessary e.g. if you are running a DHCP server, or running on an isolated network with no DHCP server.

If you want to have a known IP Address it is preferable to reserve one on your DHCP server - preferably outside the range served by DHCP (I do this for my network printers). This avoids the complication of determining gateways etc.

If you are determined to proceed anyway you should make sure you get it right.

Find the Settings of your local Network

This is most easily done with the Pi itself, using DHCP, but can be done on any computer on your network, although the commands may differ on other systems.

Run

ip -4 addr show | grep global

The above assumes IPV4 addressing - if your ISP uses IPV6 omit the "-4" parameter.

which should give an output like:

inet 10.1.1.30/24 brd 10.1.1.255 scope global eth0
inet 10.1.1.31/24 brd 10.1.1.255 scope global wlan0

The first address is the IP address of your Pi on the network, and the part after the slash is the network size. It is highly likely that yours will be a /24.

The second address is the brd (broadcast) address of the network.

Find the address of your router (or gateway)

ip route | grep default | awk '{print $3}'

10.1.1.1

Finally note down the address of your DNS server, which is often the same as your gateway.

cat /etc/resolv.conf

# Generated by resolvconf
nameserver 10.1.1.1

Then follow ONE of the following methods. (There are other methods not documented here. These are the most common on Raspbian.) (In either method substitute the appropriate network interface name for eth0, wlan0 or predictable network interface names.)

If you want to find the interface names, even if not connected, run the following command ls /sys/class/net/

In either method you should choose IP addresses which are not in use; ideally outside the range used by your DHCP server, within the same sub-network.

dhcpcd method

Leave /etc/network/interfaces at its default (as above).

Edit /etc/dhcpcd.conf as follows:-

 Here is an example which configures a static address, routes and dns.
       interface eth0
       static ip_address=10.1.1.30/24
       static routers=10.1.1.1
       static domain_name_servers=10.1.1.1

       interface wlan0
       static ip_address=10.1.1.31/24
       static routers=10.1.1.1
       static domain_name_servers=10.1.1.1

ip_address is the address and size from the command above (or another unused address on the same network), routers is the address of your router (or gateway). domain_name_servers is the DNS address(es) from /etc/resolv.conf. (see man dhcpcd.conf)

There is a good article on dhcpcd at https://wiki.archlinux.org/index.php/dhcpcd The Fallback profile is an alternative to static IP


Network Interfaces method

NOTE This method is NOT recommended (and only works if you disable the DHCP client daemon), particularly if you plan to use both interfaces.

Configure a static network adddress on your Pi in /etc/network/interfaces

auto eth0
iface eth0 inet static
        address 10.1.1.30
        netmask 255.255.255.0
        gateway 10.1.1.1

allow-hotplug wlan0
iface wlan0 inet static
        address 10.1.1.31
        netmask 255.255.255.0
        gateway 10.1.1.1
    wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf

address is the address from the command above (or another unused address on the same network), netmask 255.255.255.0 corresponds to network size/24. gateway is the address of your router (or gateway).

You can also specify dns-nameservers, but this is generally not necessary. The broadcast is automatically derived from address and netmask and need not be specified. For more detail see https://wiki.debian.org/NetworkConfiguration

You can set either or both eth0, wlan0 or one of the predictable network interface names

Then disable the DHCP client daemon and switch to standard Debian networking:

sudo systemctl disable dhcpcd
sudo systemctl enable networking

Reboot for the changes to take effect:

sudo reboot
Milliways
  • 54,718
  • 26
  • 92
  • 182
  • 1
    What are the pros and cons of these two methods you mention? – Elliott B Jun 24 '18 at 23:51
  • @ElliottB read the Disclaimer above. – Milliways Jun 25 '18 at 04:39
  • 2
    I did read that, I'm just wondering about the two methods dhcpcd vs networking. I want to see if a static IP lets it boot faster, because I see a 15s delay for dhcpcd in `systemd-analyze`. – Elliott B Jun 25 '18 at 04:42
  • 1
    I don't use either - this is only there because so many people try (for whatever reason - which eludes me) BUT get it wrong, because they don't understand networking. There is no simple answer because it depends on what else you are doing. **NOTE** setting a static IP is unlikely to have **ANY** impact on boot time, unless you have a very slow DHCP server. If you have questions you should ask a Question - this is not a discussion forum. – Milliways Jun 25 '18 at 05:11
  • I've added `static domain_search=example.local` to the dhcpcd.conf method. Feel free to revert/edit/contact me. – MadMike Sep 11 '18 at 09:57
  • I also had to add `static domain_search=.local` to be able access the Pi from the LAN using its hostname. – CrouZ Jan 06 '19 at 22:09
  • @CrouZ I have seen a few references to `domain_search=.local` but 1) it is not mentioned in the `dhcpcd` documentation, and 2) Should not be needed on a network which supports standard [Zero-conf](https://en.wikipedia.org/wiki/Zero-configuration_networking). I have had many comments that this answer is already too complex. You might like to add your own answer explaining the option and the environment in which it is needed. – Milliways Jan 07 '19 at 04:22
  • I'm connecting my Pi to a proprietary device via `eth0`, but also want to use `wlan0` as normal for an internet connection. Using the `/etc/network/interfaces` method, I tried adding ONLY the `eth0` section but it seems to kill my (previously working) Wifi connection. If I need to provide a `wlan0` section, how do I set it up to use dhcp as normal? – T3db0t Jan 23 '19 at 17:30
  • @T3db0t You should ask your own question, but if you use the obsolete method you have to accept its limitations. – Milliways Jan 23 '19 at 22:40
  • @Milliways I take it you mean via /etc/network/interfaces. Does this post say anywhere that it's obsolete? – T3db0t Jan 25 '19 at 18:33
  • @T3db0t The post starts by advising people not to do it; /etc/network/interfaces contains a warning to use dhcpcd. People continue to ignore these - adding another would be futile. This post is only there because those who ignore the warnings get it wrong. – Milliways Jan 25 '19 at 23:14
  • You can also use `wpa-ssid` and `wpa-psk` instead of the `wpa-conf` – Talal Mar 23 '19 at 07:04
  • Is using a static IP to setup a personal SFTP server an exception to the 'not recommended' warning? Everything I've found suggests static IPs – seadoggie01 Aug 06 '19 at 03:42
  • 1
    @seadoggie01 static IP addresses are a Pi disease - Windows & macOS users seem to get by without. If you are connected to a network with a DHCP server don't use a static IP address - let the server assign one. – Milliways Aug 06 '19 at 05:48
  • 2
    **Performance is better disabling dhcpd** I just tested the second non recommended approach, and it speeds up the boottime by 7 seconds on my RasPI Zero W. – Stefan Profanter Oct 28 '19 at 17:22
  • 3
    network administrators do not like this. That is not a reason to have dhcp addresses on rapsberry pi's and orange pi's and local printers. Almost everybody who reads this is is a home user, and is not served by smug comments about what "professionals" do. Albert – Albert van der Horst Nov 13 '19 at 21:29
  • "Finally note down the address of your DNS server, which is often the same as your gateway." I believe this is poor advice, because the DNS should not be your router, DNS should only be in your network if you have a dedicated DNS device. Routers generally stop working due to being used as a DNS. especially the routers provided by ISPs. Instead get your dns from your ISP, Google, Cloudfalre, Opendns – TheArchitecta Mar 03 '20 at 08:17
  • 1
    Who come up with the idea of setting static ip in `/etc/dhcpcd.conf` ? This makes no sense, and is just purely unacceptable at so many levels. This post is also wrong about "Telecommunications Engineers do not do this" They do it all the time, especially if there is no DHCP server, or static machines that are intended to be outside the range etc. – NiKiZe Jan 18 '22 at 16:02
  • As I read some of the comments here, it seems clear that a surprising number of the readership here is pathetically misinformed. The author and I disagree on many things, but he is correct here. I may take exception with a couple of points, but they are minor - mostly having to do with the use of `request` instead of `inform`. But we agree on this: Using the `static` option is *brain-dead*, and an invitation for trouble in the vast majority of cases. Strangely however, it is **the *"option of choice"* for those who *imagine* they know what they are doing.** – Seamus Apr 17 '22 at 00:03
41

For a static IP address on an Ethernet connection:

  1. sudo nano /etc/dhcpcd.conf
  2. Type in the following lines on the top of the file:

    interface eth0
    static ip_address=192.168.1.XX/24
    static routers=192.168.1.1
    static domain_name_servers=192.168.1.1
    
  3. sudo reboot

This needs to be done for the recent Jessie update. /etc/network/interfaces should be left alone. Open your browser and enter your router address (192.168.1.1 for most) and check your home network to make sure the Raspberry Pi shows up as 'Static'.

Peter Mortensen
  • 1,984
  • 2
  • 14
  • 17
Danica Panica
  • 429
  • 3
  • 7
  • 6
    If you are going to copy parts of the answer at least you should get it right. – Milliways Dec 15 '15 at 23:37
  • What did I get wrong? Was it the /24 after the IP address? Why do you need that? Things seem to be working fine without it. – Danica Panica Dec 17 '15 at 00:48
  • 2
    It probably did work in your case, but not in general because `dhcpcd` inferred the netmask, which it cannot for other address ranges. Read the `man` pages. – Milliways Dec 17 '15 at 00:58
  • 1
    You assert that /etc/network/interfaces should be left alone, but you do not say why. In fact, quite the opposite is true. See my answer below. – JayEye May 12 '16 at 23:10
  • 2
    If you want to restart networking services without reboot, make sure you have ``auto eth0`` above ``iface eth0 inet manual`` line in your ``/etc/network/interface`` file. Restart network via ``sudo service networking restart`` command – Barmaley Nov 02 '16 at 04:21
  • 1
    And if ``sudo service networking restart`` does not work, you can try ``sudo ip link set eth0 down && sudo ip link set eth0 up`` – Barmaley Nov 04 '16 at 22:52
  • 4
    This can be a copied answer but has everything you need to setup a static ip on raspberry, the accepted answer is unnecessarily long. – Pedro Lobito May 31 '17 at 03:57
  • I created a script to generate that conf based on your current network connection. https://gist.github.com/RichardBronosky/1526ec6a585dab8f1fc9990234ac4ed0 – Bruno Bronosky Oct 26 '17 at 22:08
  • What if the router is me? I mean I create a subnet. – Adam Mar 07 '18 at 17:23
  • This answer is more to the point than the other longer answers. In my case, only the line `static ip_address=` was relevant, because the dhcp over wifi does the rest. Only the IP address needed to be static. Key was that the config file as mentioned here. +1 – Roland Apr 13 '20 at 20:37
  • Readers should do their homework before adopting the `static ip` configuration advocated by this answer. For example, the author of `dhcpcd` discourages use of `static ip`!! In `man dhcpcd.conf` he states, `For IPv4, you should use the inform ipaddress option instead of setting a static address.` Ask yourself why you should follow the recommendation in this answer when the author of `dhcpcd` says otherwise? – Seamus Apr 03 '22 at 14:44
14

Short and foolproof method how to do this with:

Raspbian Jessie, Stretch, Buster

This will set a fixed IP and enable the ssh daemon:

  • Stick the sd card in your pc and find that it has two partitions; mount the smallest partition as /boot/
  • Open /boot/cmdline.txt and add ip=192.168.1.20 to the end of the line.
  • Create an empty file /boot/ssh
  • Unmount the sd card from the pc, insert in the Pi and boot your Raspberry Pi
  • On Linux start ssh pi@192.168.1.20 the password is raspberry. Use Putty on Windows to connect via ssh.
  • On the Pi, configure settings to enable SSH, e.g. with GUI or raspi-config, because /boot/ssh is automatically removed at first boot

I'm using to this to access my Raspbian Jessie and Stretch which boots without a attached monitor, with power and ethernet only. After accessing it with a ssh shell I can continue my setup.

After this treatment the Raspberry PI had two IPs: One from the fixed IP you specified and one from the dhcp-client running on the Raspberry Pi.

ifconfig will show you the fixed IP. Whereas the blue arrow-Icon on the top-right shows the dhcpd-IP.

Roland
  • 238
  • 1
  • 6
MadMike
  • 583
  • 4
  • 19
  • 2
    Please do NOT suggest disabling IPv6. There are NO need for that. – Anders Sep 07 '17 at 21:25
  • Why would I need 2 IPs? –  Oct 20 '17 at 14:04
  • @SiXandSeven8ths There is no need for 2 IPs. I have observed the behavior and am describing it here to minimize surprises. I would improve my answer, but hadn't time to do that. – MadMike Oct 20 '17 at 14:42
  • @MadMike I had observed that too, and it screwed things up for me, eventually I sorted it out but this is confusing. –  Oct 20 '17 at 15:05
  • 1
    @SiXandSeven8ths I'm sorry to hear that. It worked fine for me. Also this was the only way to do without attaching any monitor and keyboard to the raspberry pi. so I figured it could be useful to someone else. – MadMike Oct 20 '17 at 15:08
  • Does it work the same on stretch for a headless setup with a static ip? – sporc May 16 '18 at 14:32
  • @sporc this should work under stretch with a headless setup. – MadMike Jun 08 '18 at 09:00
  • yesss. After messing with a thousand methods, this simple one works perfect! Thanks – Ivan Ferrer Villa Sep 14 '18 at 18:56
  • @Roland I've tested it with Jessie, Stretch and Buster. I use this method when I created a new SD-Card before the first bootup with a headless-setup (no keyboard, no lcd). It never failed me. – MadMike Apr 15 '20 at 14:43
  • 1
    I found out what I did wrong. I tried to configure static IP after booting, with attached monitor mouse and keyboard. I edited your answer. If the edit gets accepted, I will remove my comments. +1 – Roland Apr 16 '20 at 22:45
  • Note there is a way to also change more than the ip, namely the netmask and I think dns. You can add this to the end of cmdline.txt (with a space before it to separate it): ip=192.168.1.200::192.168.1.1:255.255.255.0:rpi:eth0:off – Eradicatore Nov 03 '20 at 14:20
  • very good info at http://kr15h.github.io/RPi-Setup/index.html#titles.3 – Eradicatore Nov 03 '20 at 14:21
  • Please mention the caveats!! This is great advice for the initial boot on a new headless system. Setting a fixed IP in `/boot/cmdline.txt` allows one to connect to a new host with minimal dependencies. Having two IP addresses on an interface is called `IP Aliasing` & isn't a problem *per se*. **`But`** if your network employs DHCP, retaining that fixed IP may be problematic. *If you're not familiar with DHCP, and you don't like spending hours trying to fix strange problems, the fixed IP entry in `/boot/cmdline.txt` should be removed after first boot.* – Seamus Apr 03 '22 at 14:38
4

First thing you should do is make your Raspberry pi's IP static. So that whenever you power up your Raspberry pi it should connect to your access point(Hotspot).

Connect to your Access point. Type ifconfig in raspberry pi's terminal and enter that IP address below, in my case it was 192.168.43.233

Start by editing the dhcpcd.conf file

sudo nano /etc/dhcpcd.conf

Scroll all the way to the bottom and add this lines at the end as per your connection (wired or wireless).

interface eth0

static ip_address=192.168.0.10/24
static routers=192.168.0.1
static domain_name_servers=192.168.0.1


interface wlan0
static ip_address=192.168.43.233/24
static routers=192.168.0.1
static domain_name_servers=192.168.0.1

Press Ctrl+x to save and reboot. Now power up your raspberry pi and it will automatically connect to your access point.

SMshrimant
  • 217
  • 2
  • 4
  • Readers should do their homework before adopting the `static ip` configuration advocated by this answer. For example, the author of `dhcpcd` discourages use of `static ip`!! In `man dhcpcd.conf` he states, `For IPv4, you should use the inform ipaddress option instead of setting a static address.` Ask yourself why you should follow the recommendation in this answer when the author of `dhcpcd` says otherwise? – Seamus Apr 03 '22 at 14:13
3

SystemdNetworkd

Raspbian/Raspberry Pi OS initially used Debian Networking which was replaced by dhcpcd in 2015.
dhcpcd is the current default networking system.

Raspbian is now based on systemd which includes systemd-networkd, a system service that manages networks.
It detects and configures network devices as they appear, as well as creating virtual network devices.

This tutorial describes how to setup basic networking using systemd-networkd.
NOTE only basic networking using the in-built interfaces eth0 and wlan0 is described.

It is assumed you are familiar with networking and reasonably competent configuring Linux.
It is NOT a method recommended for beginners; it is more complex to setup.
Each interface needs to be explicitly configured.

WARNING this will disable the Networking tool on the Desktop Panel.

Ethernet Networking

You need to define a network by creating a file in /etc/systemd/network. This can be given any name with the extension .network

The following example tells systemd-networkd to use eth0 (which is set up by udev) and assign it an address using DHCP.

[Match]
Name=eth0

[Network]
DHCP=ipv4

[DHCP]
RouteMetric=201

An alternative using static IP Addresses

[Match]
Name=eth0

[Network]
Address=192.168.1.20/24
Gateway=192.168.1.1
DNS=192.168.1.1

[DHCP]
RouteMetric=201

Wireless Networking

In order to connect to a wireless network with systemd-networkd, a wireless adapter configured with another application such as WPA supplicant is required.
To use wlan0 You should create a suitable /etc/wpa_supplicant/wpa_supplicant-wlan0.conf e.g.

ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
country=<2 letter ISO 3166-1 country code>

network={
    ssid="ESSID"
    psk="Your_wifi_password"
}

(If you have existing WiFi setup you can copy the file /etc/wpa_supplicant/wpa_supplicant.conf.)

Create a .network file in /etc/systemd/network.

[Match]
Name=wlan0

[Network]
DHCP=ipv4

[DHCP]
RouteMetric=301

Having created the appropriate configuration files you NEED to disable existing networking and enable systemd-networkdservices.
NOTE there is NO NEED to purge existing network services. If disabled they will not interfere, and can be easily re-enabled if required.

sudo systemctl enable systemd-networkd
sudo systemctl disable dhcpcd
sudo systemctl disable networking

sudo systemctl enable systemd-resolved.service
sudo systemctl disable wpa_supplicant.service
sudo systemctl enable wpa_supplicant@wlan0.service

You can check the status using the following:-

systemctl status systemd-networkd
systemctl status systemd-resolved.service
systemctl status wpa_supplicant.service
systemctl status wpa_supplicant@wlan0.service
systemctl status dbus.service

Simplified Systemd-Networkd setup

Configuring networking using systemd-networkd requires multiple steps, and it is easy to get things wrong. The following 3 scripts simplify the process and enable swapping between systemd-networkd and dhcpcd.

The setup below is more general than that described above and should work with multiple interfaces (although if you have reason to have multiples you will probably want to customise the setup).

This should offer similar functionality to the dhcpcd setup (except this will disable the Networking tool on the Desktop Panel).

Step1 installs necessary files, and only needs to be run once. The other scripts activate/deactivate systemd-networkdbut do not delete anything merely changing which services are running - Reboot REQUIRED!
All script MUST be run by root or with sudo.

SetupSystemdNetworkd.sh

#! /bin/sh
# Script to install files for basic networking using systemd-networkd
# NOTE this does NOT swap networking systems - see separate scripts to activate
# 2022-06-24

# Assumes a working wpa_supplicant.conf file /etc/wpa_supplicant/wpa_supplicant.conf
# This script MUST be run by root or with sudo

# Create a network configuration for Ethernet
# Supports default eth0 or Predictable Network Interface Name en…
cat > /etc/systemd/network/02-eth.network <<EOF
[Match]
Name=e*

[Network]
DHCP=ipv4

[DHCP]
RouteMetric=201
EOF

# Create a network configuration for WiFi
# Supports default wlan0 or Predictable Network Interface Name wl…r
cat > /etc/systemd/network/03-wlan.network <<EOF
[Match]
Name=wl*

[Network]
DHCP=ipv4

[DHCP]
RouteMetric=301
EOF

# Copy existing wpa_supplicant.conf
cp /etc/wpa_supplicant/wpa_supplicant.conf /etc/wpa_supplicant/wpa_supplicant-wlan0.conf

ActivateSystemdNetworkd.sh

#! /bin/sh
# Script to Activate networking using systemd-networkd
# This DeActivates dhcpcd and Debian networking
# 2022-06-24

# Assumes network configuration files and wpa_supplicant.conf file have been setup
# This script MUST be run by root or with sudo
# Reboot REQUIRED!

# Copy existing wpa_supplicant.conf
cp /etc/wpa_supplicant/wpa_supplicant.conf /etc/wpa_supplicant/wpa_supplicant-wlan0.conf
systemctl enable systemd-networkd
systemctl disable dhcpcd
systemctl disable networking

systemctl enable systemd-resolved.service
systemctl disable wpa_supplicant.service
systemctl enable wpa_supplicant@wlan0.service

DeactivateSystemdNetworkd.sh

#! /bin/sh
# Script to DeActivates networking using systemd-networkd
# This Activates dhcpcd
# 2022-06-24

# This script MUST be run by root or with sudo
# Reboot REQUIRED!

# Copy existing wpa_supplicant.conf
cp /etc/wpa_supplicant/wpa_supplicant.conf /etc/wpa_supplicant/wpa_supplicant-wlan0.conf
systemctl enable dhcpcd
systemctl disable systemd-networkd

systemctl enable wpa_supplicant.service
systemctl disable systemd-resolved.service
systemctl disable wpa_supplicant@wlan0.service

References

  1. Debian Wiki
  2. The ArchLinux Wiki provides a comprehensive set of documentation.
Milliways
  • 54,718
  • 26
  • 92
  • 182