Page 1 of 1

If DHCP fails then set static IP

Posted: Mon Aug 20, 2012 8:27 pm
by ddupuis
Hi,

I use my RPi in 2 network configurations and access to it from my laptop via VNC:

1- Connected to a local network via a router (DHCP is fine)
2- Connected directly to the laptop using crossover cable (need to set a static IP)

I would like to configure the networking onto raspbian to try first to get an IP address via DHCP , then if DHCP fails (e.g. in situation #2) use a pre-defined static IP. This is useful for me when I travel and only have the screen and keyboard of my laptop to work onto my RPi. This way, I can power up my RPi, connect it to my laptop using my crossover cable and connect with VNC since the address will be the pre-defined static IP.

Does anybody knows how to configure the networking to be able to do it?

Thanks!

Re: If DHCP fails then set static IP

Posted: Mon Aug 20, 2012 10:36 pm
by RaspberryPiBeginners
You would have to write a script that ran at start up and modified "/etc/network/interfaces"

Re: If DHCP fails then set static IP

Posted: Tue Aug 21, 2012 1:58 am
by SirLagz
RaspberryPiBeginners wrote:You would have to write a script that ran at start up and modified "/etc/network/interfaces"
Not quite
All the script has to do is run

Code: Select all

ifconfig eth0 <ipaddress> 
to set an ip address on the fly.
No need to modify /etc/network/interfaces.

Re: If DHCP fails then set static IP

Posted: Tue Aug 21, 2012 2:50 am
by SirLagz
I also just realised that you could just give it a 2nd IP address statically like this

Re: If DHCP fails then set static IP

Posted: Tue Aug 21, 2012 5:04 pm
by ddupuis
Thanks SirLagz!

Your second solution works perfect! First one also I imagine, but the second one is really simple to implement.

In case it may be useful to someone else, here is an example of my /etc/network/interfaces file:
----------
auto lo
iface lo inet loopback
iface eth0 inet dhcp

auto eth0:1
iface eth0:1 inet static
address 123.123.123.123
netmask 255.255.255.0
----------

Cheers!

Re: If DHCP fails then set static IP

Posted: Wed Aug 22, 2012 3:24 pm
by brs
The "proper" way to do this might be to use the new IPv4 link-local addresses (http://tools.ietf.org/html/rfc3927). There is a special block of addresses reserved for this (169.254.0.0/16) , along with rules how hosts should behave to avoid address clashes. Avahi-autoipd (http://avahi.org/wiki/AvahiAutoipd) seems to be an implementation of this, which would hopefully work on the Raspberry Pi.

I am waiting for a second board to arrive, where I'll try to get this kind of zero-conf networking to work. So maybe more to come later...

Re: If DHCP fails then set static IP

Posted: Sun Aug 26, 2012 9:55 pm
by william1616
Dum Question but do you still have to use a startup script with this interfaces file and if so what
auto lo
iface lo inet loopback
iface eth0 inet dhcp

auto eth0:1
iface eth0:1 inet static
address 123.123.123.123
netmask 255.255.255.0
Sorry for the stupid question - linux is fairly new to me - windows and browser scripting are really where my coding talents lie

Re: If DHCP fails then set static IP

Posted: Sun Aug 26, 2012 11:37 pm
by ripping
william1616 wrote:Dum Question but do you still have to use a startup script with this interfaces file and if so what
No. You just edit the /etc/network/interfaces file (as above) with your desired static IP and it runs on startup every time. No further work required.

In my case I have no requirement for DHCP at all so I delete the line that has DHCP on it totally, and just use the static IP values.

Re: If DHCP fails then set static IP

Posted: Mon Aug 27, 2012 7:57 am
by william1616
Thanks - i've already changed to a static ip but didnt think it way possible to automatically see if dhcp fails - i thought i would have to do it manually

Thanks

Re: If DHCP fails then set static IP

Posted: Mon Aug 27, 2012 10:36 am
by william1616
is there any reason why ssh doesnt work with this?

@ddupuis have you had this problem?

nb i use debian squeeze not wheezy

Re: If DHCP fails then set static IP

Posted: Mon Aug 27, 2012 11:24 am
by william1616
I've been playing around and have got some more detailed info

SSH is defiantly running all of the time
SSH works when my Ethernet cable is plugged into the router through the DHCP address 192.168.1.80 and through the static address 192.168.1.77. This means that even when DHCP works its setting up a static IP and when the Ethernet cable is plugged into the router at boot it says "ip address is 192.168.1.77" (not exact wording here)
SSH works when eth0:1 commented out and DHCP changed to static for eth0

Any help would be appreciated

Re: If DHCP fails then set static IP

Posted: Mon Aug 27, 2012 1:52 pm
by william1616
ive done an apt-get upgrade but still no luck unless eth0:1 is commented out

Re: If DHCP fails then set static IP

Posted: Tue Aug 28, 2012 6:25 pm
by ddupuis
Everything works fine for me without commenting any line of /etc/network/interfaces. Here is a summary of what I tested:

RPi connected to router -> DHCP address assigned:

1- SSH to DHCP address works fine
2- SSH to static IP doesn't work

and

RPi not connected to DHCP server -> static IP assigned:

1- SSH to static address works fine
2- No DHCP address so can only try SSH to static address

I'm using the wheezy-raspbian released on 2012-08-16 distro.

Hope it helps!

Re: If DHCP fails then set static IP

Posted: Wed Aug 29, 2012 9:19 am
by william1616
I think i've found the problem but i've no idea how to fix it. The rpi dhcp gets my laptops ip address 192.168.1.73 so doesn't set a static ip - this creates an ip address conflict which my router doesn't recognise but windows does.

Any suggestions?

Re: If DHCP fails then set static IP

Posted: Wed Aug 29, 2012 9:47 am
by bredman
william1616 wrote:The rpi dhcp gets my laptops ip address 192.168.1.73
Have you any idea how both your RPi and your laptop are receiving the same address?

If you are using static addressing on the laptop, you should change to DHCP or else use an address which is outside the DHCP range.

You could also configure the router (normally by a web page). You can set the router to recognise the MAC address of each computer and always give each of them a special address.

Re: If DHCP fails then set static IP

Posted: Wed Aug 29, 2012 11:17 am
by castalla
If you set a static address, then it's advisable to add the lines:

gateway 192.168.1.1 (= your router ip address)
dns-nameservers 192.168.1.1

otherwise the pi fails to get the correct date and time.

Re: If DHCP fails then set static IP

Posted: Fri Apr 19, 2013 2:53 pm
by ngoctuct
auto lo
iface lo inet loopback
iface eth0 inet dhcp

auto eth0:1
iface eth0:1 inet static
address 10.10.10.2
netmask 255.255.255.0