Skip to content

Instantly share code, notes, and snippets.

@brunogama
Created March 31, 2021 15:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brunogama/7853523ffc22335f7132b416607393d0 to your computer and use it in GitHub Desktop.
Save brunogama/7853523ffc22335f7132b416607393d0 to your computer and use it in GitHub Desktop.
Setup static ip on pi
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.
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. Adapted from Foundation Network Tutorial
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment