WelcomeWelcome | FAQFAQ | DownloadsDownloads | WikiWiki

Author Topic: No way to get ethernet connection automatically  (Read 3604 times)

Offline iFreilicht

  • Newbie
  • *
  • Posts: 4
No way to get ethernet connection automatically
« on: October 15, 2020, 02:09:12 PM »
I'm trying to use piCore 11 on a RaspberryPi 3B+ Rev1.3.  I've downloaded the piCore-11.0.img and followed the instructions in this README to write the image to SD, resize the second partition, and store the ssh-keys generated on first boot.

Now, to get a connection via ethernet, there's two options:

1. Setting a static IP with a script
2. Getting a dynamic IP via DHCP

The problem is that I can make neither of those work automatically. This RPi will be sitting at home, so a static IP is fine, but I will turn it off and on often, so I don't want to type in commands on every boot. Here's what I tried (all code blocks are commands executed directly after boot):

1.  Setting a static IP with a script

On my main machine, I ran arp-scan --local-network to find a free IP address, ifconfig to determine that the netmask is 255.255.255.0 and route followed by ns-lookup fritz.box to determine that the default gateway's IP is 192.168.178.1.

So, to get a static IP, all I should have to do is:

Code: [Select]
$ sudo ifconfig eth0 192.168.178.61
$ sudo route add default gw 192.168.178.1

And I did, and the subsequent outputs of ifconfig and route look good, see attached "static_notworking.txt", but ping 8.8.8.8 or a ping to the gateway or my main machine just stays silent and reports 100% packet loss when I Ctrl+C it.

2. Getting a dynamic IP via DHCP

For DHCP to work, the RPi needs to have the correct time. I found that out the hard way. Trying to make udhcpc do its thing without the correct time has no effect:

Code: [Select]
$ sudo pkill dhcp
$ sudo udhcpc -i eth0 -x hostname:box
udhcpc: started, v1.28.4
udhcpc: sending discover
udhcpc: sending discover
udhcpc: sending discover
udhcpc: sending discover
...

But, this will actually give me a working ethernet connection:

Code: [Select]
$ export TZ=CEST+2
$ sudo date -s "2020-10-15 19:37:16"
$ sudo ifconfig eth0 down
# Unplug ethernet cable, then plug it back in
$ sudo ifconfig eth0 up
# Wait for a few seconds

Now, everything is working perfectly. /etc/resolv.conf got populated, I can ping URLs, external and internal IPs, ssh into the RPi, everything I want. This also is the nicer solution, because it allows me to easily move the setup and not do any additional setup.

But, this always requires me manually typing in the correct time. I know that this is somewhat due to the RPi not having an RTC, but Raspbian somehow manages to handle this, as my other pi with Raspbian on it can easily be shutdown for days and still connect to the network automatically.

I know that NTP allows to get the time from an external source, but of course DHCP would have to already have obtained a lease for that to work. It seems like DHCP can actually supply time as well via options 004 and 042, but I guess udhcpc is not querying those or they do something else?

What do?

So, I really don't know where to go from here. Should I just get an RTC module for the RPi and be done with it? Or is there some solution to this problem?

Offline Greg Erskine

  • Sr. Member
  • ****
  • Posts: 404
Re: No way to get ethernet connection automatically
« Reply #1 on: October 15, 2020, 03:45:47 PM »
Your RPi should connect to your network without doing anything other than connecting a ethernet cable and connecting the power.

I don't think your assumption about "for DHCP to work, the RPi needs to have the correct time" is correct.

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11514
Re: No way to get ethernet connection automatically
« Reply #2 on: October 15, 2020, 03:55:45 PM »
Hi iFreilicht
I'm not running any RaspberryPi boards, but the way I set up my static addresses is by calling this script from bootlocal.sh:
Code: [Select]
tc@E310:~$ cat /opt/eth0.sh
#!/bin/sh
pkill udhcpc
ifconfig eth0 192.168.1.35 netmask 255.255.255.0 broadcast 192.168.1.255 up
route add default gw 192.168.1.1
echo nameserver 192.168.1.1 > /etc/resolv.conf            # My router
echo nameserver 68.237.161.12 >> /etc/resolv.conf         # DNS server from my ISP
tc@E310:~$

Offline iFreilicht

  • Newbie
  • *
  • Posts: 4
Re: No way to get ethernet connection automatically
« Reply #3 on: October 15, 2020, 05:01:11 PM »
Holy shit Greg, that was a good call!

The thing that actually brought DHCP to life is the TZ environment variable! Apparently that's sometimes important for DHCP, but not used by all clients. I guess my router just throws away requests that don't contain that option.

And wouldn't you know it, there's a topic about setting the timezone properly in this forum already. So in my case:

Code: [Select]
$ sudo sh -c 'echo TZ=CET-1CEST,M3.5.0,M10.5.0/3 > /etc/sysconfig/timezone' # Adjust timezone if necessary
$ echo etc/sysconfig/timezone >> /opt/.filetool.lst
$ filetool.sh -b

After that and rebooting, I at least get an IP once I unplug and replug the ethernet cable. Without that, DHCP doesn't try to get a lease, but this is good enough for my case.

To avoid this in the future, I see three options:

  • Modify /etc/profile to set export TZ=UTC0 if /etc/sysconfig/timezone is not present
  • Have /etc/sysconfig/timezone be present by default with the content TZ=UTC0
  • Ask the BusyBox devs to handle a missing TZ variable

I know a lot of other time-related programs will default to UTC, so maybe udhcpc should as well?

Offline Paul_123

  • Administrator
  • Hero Member
  • *****
  • Posts: 1225
Re: No way to get ethernet connection automatically
« Reply #4 on: October 15, 2020, 05:36:05 PM »
Use the TZ bootcode to set the timezone.

http://tinycorelinux.net/faq.html#bootcodes

Offline iFreilicht

  • Newbie
  • *
  • Posts: 4
Re: No way to get ethernet connection automatically
« Reply #5 on: October 17, 2020, 07:14:16 PM »
Ah nice, that is even easier! Do you think it would make sense to change the default cmdline.txt and cmdline3.txt files to include tz=UTC0?

This way, a problem like this could never occur for new users.