Static IP address and internet access

I set a static IP address for my pi. ( 192.168.1.103) by adding these lines to the top of the /etc/dhcpcd.conf file:
interface eth0
static ip_address=192.168.1.103/24
After reboot this works fine and I can access the pi through the new IP address, BUT now I cannot connect to the internet anymore.

I tried also adding the lines:
static routers=192.168.1.254
static domain_name_servers=8.8.8.8
which is what the link I got this information suggested but it made no difference.

I change one line to :
static routers=192.168.1.1
And that did not make any difference either.

This aught to be simple if you know what you are doing - and I don’t in this particular area.

Can someone help me to find out what I need to keep the static IP address and also be able to have internet access on my network?

I have found another link explaining how to do this and have followed it carefully and I cannot get it to work either:

hash=68363f45a54300f1a62a4101e66294c7#comment-51015

I have added the lines to the top of the /etc/dhcpcd.conf file to be:
interface eth0
static ip_address=192.168.1.103/24
static routers = 192.168.1.1
static domain_name_servers=192.168.1.1
When I try to open a browser and put google in the URL I get:
This site can’t be reached http://www.google.com/ is unreachable.
ERR_ADDRESS_UNREACHABLE
Changing /etc/dhcpcd.conf file to:
interface eth0
static ip_address=192.168.1.103/24
static routers = 192.168.1.1
static domain_name_servers=8.8.8.8
I get:
No Internet Try: checking the network cables, modem and router, Reconfiguring your WiFi DNS_PROBE_FINISHED_NO_INTERNET

Changing /etc/dhcpcd.conf file to:

interface eth0

static ip_address=192.168.1.103/24

static routers = 192.168.1.1

static domain_name_servers=192.168.1.1

I have no problems, but I do not have a static IP anymore!

What is wrong? It seems that this simple thing is not so simple!

Please advise how I can do this.

Hey Clem,

DougieLawson on the Raspberry Pi forums made a good troubleshooting guide that can be found here.

To summarise his post, the main issues were connecting to the router (issues with the connection or using a conflicting/already used IP address

Dougie tested the connection using the following commands:

ip addr show
ip route
ping -c3 $(ip route | awk '/default/ {print $3}')
ping -c3 8.8.8.8

The static routers line indicates which address the router is connected to and must be found using the command ip r | grep default

PiMyLifeUp has an excellent and comprehensive guide on getting you up and running with a static IP which covers most of the bases.

Let us know how you go!
Liam.

Hi Liam,

I have already tried your second link and done precisely what it instructs and while it changes the Pi to a static IP address successfully, it at the same time stops the internet access from working.

Hey Clem,

Hmm, it sounds like it might be an issue using the DNS, those settings can be found in your router and will change from model to model. Coincidentally Dougie covers it again here: No internet Connection - Raspberry Pi Forums

Let us know how you go with the other steps!
Liam.

Top of the /etc/dhcpcd.conf file is:
interface eth0 static ip_address=192.168.1.103/24 static routers = 192.168.1.1 static domain_name_servers=192.168.1.1
Ping results:
ping 192.168.1.103 works
ping 192.168.1.1 works
ping 8.8.8.8 connect: Network is unreachable
ping www.google.com connect: Network is unreachable
cannot connect to internet

run command:
sudo ip route add default via 192.168.1.1

Ping results:
ping 192.168.1.103 works
ping 192.168.1.1 works
ping 8.8.8.8 works
ping www.google.com works
now CAN connect to internet

reboot

cannot access internet.

So how do I make the effect of this command permanent?: sudo ip route add default via 192.168.1.1

I found static IP addresses work better when assigned by the Router.
I find the MAC address of the Pi and then change the router configuration to assign an IP address to that MAC address.

It has worked successfully so far.
Cheers
Jim

2 Likes

The command sudo ip route add default via 192.168.1.1 would need to be added as one of the last commands upon boot up.
I use /etc/rc.local to add these type of commands. The & at the end of the line allows rc.local to end.
This might or might not work, I have not tested it.
But running my projects from /etc/rc.local certainly does work.

Regards
Jim

#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

# Print the IP address
_IP=$(hostname -I) || true
if [ "$_IP" ]; then
  printf "My IP address is %s\n" "$_IP"
fi

sudo ip route add default via 192.168.1.1 &

exit 0
1 Like

I shall try this soon,

I do not understand what this command actually does: ip route add default via 192.168.1.1
I would like to know,
further I would like to know why this cannot be set up in a configuration file so that this command does not need to be run?

1 Like

The ip command adds a default route gateway to the ip routing table.
The following is from a Raspberry Pi I have setup as an environment monitor.
My Router DHCP server assigns the MAC address of the Pi as 192.168.1.23 IP address and specifies the default gateway as 192.168.1.1. The Raspberry Pi then configures itself for these addresses. This happens automatically.

pi@pizero008:~ $ ip route show
default via 192.168.1.1 dev wlan0 proto dhcp src 192.168.1.23 metric 302
192.168.1.0/24 dev wlan0 proto dhcp scope link src 192.168.1.23 metric 302

As you are setting this up manually the routing table would not have default via 192.168.1.1 when the Pi boots up. The command ip route show should give a list of the routing table contents (ip route list also works). This might help you figure out what is going on.

In my dhcpcd.conf file the static assignment lines are commented out near the bottom of the file. Suggest placing your lines at the bottom not at the top.
Also there is are spaces around the equals in the line static routers = 192.168.1.1. Past experience has shown Linux can sometimes be fussy with spaces. Suggest removing the spaces. Below is the lines of the dhcpcd.conf on the Pi related to setting static addresses. The # means the line is treated like a comment and ignored, remove to enable the line.

# 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

Regards
Jim

2 Likes

How to set up Static IP Address

explains in detail how to set static, why you don’t need it and how to assign a predictable IP Address.

3 Likes

Hi Clem,

FYI, here’s a great overview of different network devices:

And here’s a really good article:

Hi Clem
I think Jame’s comment from 3 days ago should be revisited. You need to get your router to assign a static IP address on your local network to your RPi. Log into your router’s admin page and the right part for these assignments. -Its appearance varies with the brand of router. Good luck!

1 Like

Ian38390,

I tried this re your link:

I like the idea of requesting the router to give the desired IP address.
I tried this as per the link by editing the '/etc/dhcpcd.conf ’ file by adding the lines:

interface eth0
request 192.168.1.104

I also tried this with no different result:

interface eth0
request 192.168.1.104/24

To my disapointment this did not work it just allocated some other IP address: ‘192.168.1.4’

I like the idea of doing it this way - how can I get this to work?

Request will only work if you request an unused IP in the range served by your DHCP server (which we don’t know) otherwise it serves normally.

There is no need for the CIDR suffix /24 which at best will be ignored.

It is common practice to specify static IP outside the range served by your DHCP server.

1 Like

I understand that I used an unused IP address within the range served by the router. How can I know this for sure?

You have to ask the router unfortunately - but most applications won’t need the full 255 (or even close to it) so it’s become a bit of a defacto standard practice to only automatically IP addresses above 100. This gives you almost 100 IP addresses to use for static allocations - without needing to learn much of anything about networking.

I have used the line:
interface eth0 request 192.168.1.17
it does not allocate it! ??

Hi Clem,

Try this instead: sudo ifconfig eth0 192.168.1.17

Also, be sure to share the error message if you get one.

This command does not give any error, and it adds another Ethernet address to the pi.
command hostname -I prior returns:
192.168.1.4 192.168.1.15
.4 is the non static allocated ip for eth0, …15 is the wifi ip.
after command