-4

After researching a lot on the internet, I found that there is no easy/standard/official way to assign a static IP to a Raspberry Pi.

Why is so much tweaking needed in the config files even if I can assign a static IP using my network router?

If I assign a static IP using my Router (ethernet and WiFi each), then the Raspberry Pi won’t be able to connect to the internet. Although I can:
ping 8.8.8.8

pi@raspberrypi:~ $ ping google.com
ping: google.com: Temporary failure in name resolution

pi@raspberrypi:~ $ ping 8.8.8.8 PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data. 64 bytes from 8.8.8.8: icmp_seq=1 ttl=53 time=64.9 ms 64 bytes from 8.8.8.8: icmp_seq=2 ttl=53 time=115 ms ^C --- 8.8.8.8 ping statistics --- 2 packets transmitted, 2 received, 0% packet loss, time 1001ms rtt min/avg/max/mdev = 64.886/89.857/114.828/24.971 ms

pi@raspberrypi:~ $ ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.1.200  netmask 255.255.255.0  broadcast 192.168.1.255
        inet6 2402:ad80:f0:3ea8:39ca:f0c2:8ff8:c6cb  prefixlen 64  scopeid 0x0<global>
        inet6 fe80::d65e:5364:a0db:1705  prefixlen 64  scopeid 0x20<link>
        ether dc:a6:32:84:a8:e8  txqueuelen 1000  (Ethernet)
        RX packets 22976  bytes 3758615 (3.5 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 28514  bytes 18563719 (17.7 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 94006  bytes 8755506 (8.3 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 94006  bytes 8755506 (8.3 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

wlan0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        ether dc:a6:32:84:a8:e9  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
pi@raspberrypi:~ $ route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.1.1     0.0.0.0         UG    202    0        0 eth0
192.168.1.0     0.0.0.0         255.255.255.0   U     202    0        0 eth0
pi@raspberrypi:~ $ cat /etc/networks
default     0.0.0.0
loopback    127.0.0.0
link-local  169.254.0.0
pi@raspberrypi:~ $ cat /etc/network/interfaces
# interfaces(5) file used by ifup(8) and ifdown(8)
# Include files from /etc/network/interfaces.d:
source /etc/network/interfaces.d/*

pi@raspberrypi:~ $ cat /etc/network/interfaces.new
# 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
pi@raspberrypi:~ $ cat /etc/dhcpcd.conf
# A sample configuration for dhcpcd.
hostname

persistent
option rapid_commit
option domain_name_servers, domain_name, domain_search, host_name
option classless_static_routes
option interface_mtu
require dhcp_server_identifier
slaac private

# Example static IP configuration:
#interface eth0
#static ip_address=192.168.0.10/24
#static ip6_address=fd51:42f8:caae:d92e::ff/64
#static routers=192.168.0.1
#static domain_name_servers=192.168.0.1 8.8.8.8 fd51:42f8:caae:d92e::1

# It is possible to fall back to a static IP if DHCP fails:
# define static profile
#profile static_eth0
#static ip_address=192.168.1.23/24
#static routers=192.168.1.1
#static domain_name_servers=192.168.1.1

# fallback to static profile on eth0
#interface eth0
#fallback static_eth0
interface eth0
        static ip_address=192.168.1.200/24
        static routers=192.168.1.1
        static domain_name_servers=

Resources:

https://windowsreport.com/raspberry-pi-no-connect-internet/

Raspberry Pi Won't Connect Through Ethernet

https://thepihut.com/blogs/raspberry-pi-tutorials/tutorial-how-to-give-your-raspberry-pi-a-static-ip-address

https://forums.raspberrypi.com/viewtopic.php?t=70047

https://forums.raspberrypi.com/viewtopic.php?t=228820

Dürrani
  • 93
  • 4
  • Why is so much tweaking needed in the config files even if I can assign a static IP using my network router? So much tweaking?? What did you have to tweak outside of dhcpcd.conf? – Seamus Sep 28 '23 at 21:48
  • The real question is WHY so many Pi users WANT to assign static IP addresses which Windows and macOS don't. – Milliways Sep 28 '23 at 22:35
  • @Milliways - at a guess, more users would use a Pi as some sort of server, and in a NATed LAN, port forwarding requires the target device to have a known IP – Jaromanda X Sep 28 '23 at 23:29
  • @JaromandaX This would only apply to those few or those running an Access Point but NOT the vast bulk of users (most of whom do not seem to understand networking). – Milliways Sep 29 '23 at 00:00
  • @Seamus If I reserve an IP for my Pi MAC address using the network router, why do I've to tweak the config files of the Pi OS and hard code it there? Otherwise, the internet won’t work. Don’t you think it's a redundant tweak? – Dürrani Sep 29 '23 at 12:56
  • @Milliways In my case, I'm running multiple services on Pi e.g. uptime-kuma, plex, pi-hole, transmission, pm2, IoTs manager, etc. Until now the board is handling it fairly well. – Dürrani Sep 29 '23 at 13:02

3 Answers3

0

You can use the same methods to assign a static IP address to a Raspberry Pi as you can with any Linux desktop or NAS. You can assign a static IP on your router using DHCP, based on a network interface's MAC address (do note that each interface has its own MAC and IP address, so eth0 and wlan0 cannot use the same IP address). You can get your MAC address with either ip address or ifconfig:

skynet:~$ ip address
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000
    link/ether dc:a6:32:4c:b0:ff brd ff:ff:ff:ff:ff:ff
    inet 192.168.0.201/24 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::dea6:32ff:fe4c:b0ff/64 scope link 
       valid_lft forever preferred_lft forever
3: wlan0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc pfifo_fast state DOWN qlen 1000
    link/ether dc:a6:32:4c:b1:00 brd ff:ff:ff:ff:ff:ff
    inet 192.168.0.202/24 scope global wlan0
       valid_lft forever preferred_lft forever

The MAC addresses are on the lines that begin with link/ether.

skynet:~$ ifconfig
eth0      Link encap:Ethernet  HWaddr DC:A6:32:4C:B0:FF  
          inet addr:192.168.0.201  Bcast:0.0.0.0  Mask:255.255.255.0
          inet6 addr: fe80::dea6:32ff:fe4c:b0ff/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:226512751 errors:0 dropped:112 overruns:0 frame:0
          TX packets:214004248 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:181790639116 (169.3 GiB)  TX bytes:181372680174 (168.9 GiB)

lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0 inet6 addr: ::1/128 Scope:Host UP LOOPBACK RUNNING MTU:65536 Metric:1 RX packets:14856 errors:0 dropped:0 overruns:0 frame:0 TX packets:14856 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:2346907 (2.2 MiB) TX bytes:2346907 (2.2 MiB)

wlan0 Link encap:Ethernet HWaddr DC:A6:32:4C:B1:00
inet addr:192.168.0.202 Bcast:0.0.0.0 Mask:255.255.255.0 UP BROADCAST MULTICAST MTU:1500 Metric:1 RX packets:0 errors:0 dropped:0 overruns:0 frame:0 TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)

ifconfig shows the MAC address after the HWaddr labels.

Here's a handy function that shows the MAC address of an interface:

mac()
{
    ip a show "$1" | awk '/link\/ether/{print $2}'
}

You can put this in your .bashrc and just write mac eth0.

zorleone
  • 53
  • 1
  • 4
  • 1
    did you read the question? OP states if they assign a static IP using the router (i.e. DHCP), somehow, DNS messes up – Jaromanda X Sep 28 '23 at 23:27
0

Is is NOT "difficult to assign a static IP to Raspberry Pi" although I still think for most users it is a bad idea.

See How to set up Static IP Address which explains how using the (current) default network manager dhcpcd (and recommends alternatives).

If done correctly it does work, but this is only one way.
It can be done from the networking icon on the GUI toolbar and there are methods for other network managers.

You can NOT set a static IP using a Router - although you can reserve an address and assign to specific MAC.

Milliways
  • 59,890
  • 31
  • 101
  • 209
0

I'm able to achieve it by binding the MAC-IP in the Router for both WiFi and Ethernet.
And then added the following details to /etc/dhcpcd.conf file:

interface eth0
        static ip_address=192.168.1.200/24
        static routers=192.168.1.1
        static domain_name_servers=8.8.8.8 8.8.4.4

interface wlan0 static ip_address=192.168.1.201/24 static routers=192.168.1.1 static domain_name_servers=8.8.8.8 8.8.4.4

192.168.1.1 is the IP of my Router.
192.168.1.200 and 192.168.1.201 are the static IPs for Ethernet and WiFi.
/24 is because my local network is using class C subnet (netmask 255.255.255.0).
8.8.8.8 8.8.4.4 is DNS and Alternate DNS respectively.

My final /etc/dhcpcd.conf file looks something link this (comments removed):

hostname
clientid
persistent
option rapid_commit
option domain_name_servers, domain_name, domain_search, host_name
option classless_static_routes
option interface_mtu
require dhcp_server_identifier
slaac private

interface eth0 static ip_address=192.168.1.200/24 static routers=192.168.1.1 static domain_name_servers=8.8.8.8 8.8.4.4

interface wlan0 static ip_address=192.168.1.201/24 static routers=192.168.1.1 static domain_name_servers=8.8.8.8 8.8.4.4

Dürrani
  • 93
  • 4
  • 1
    You should probably list one (but not both!) of those as nogateway. otherwise the default route is going to be arbitrary and may be subject to change, which may create a problem at some point. – goldilocks Sep 30 '23 at 20:41