Single static IP address for eth0


54 posts   Page 1 of 3   1, 2, 3
by DarrenHill » Tue Aug 04, 2015 12:22 pm
Hi,

I'm trying to set my Pi (under Raspbian) up with a single static IP address for eth0. I've found many on-line tutorials for this, but the /etc/network/interfaces file that my Pi (model B) has seems slightly different to the ones listed there, and if I try to follow them my Pi ends up with two IP addresses, one by DHCP from the router and the other the fixed address I'm trying to give it.

In all the tutorials it says to change the line
Code: Select all
iface eth0 inet dhcp
to
Code: Select all
iface eth0 inet static
and then add all the other entries (address, netmask, gateway, network and broadcast) below it. But on my Pi (with a fresh Raspbian install) the interfaces file has
Code: Select all
iface eth0 inet manual
rather than dhcp, and if I change the manual to static and add the other entries I get the dual IP address. There are other lines about auto eth0 as well which don't feature in the tutorials at all, although commenting those out don't seem to make a difference.

Could someone advise what is the current best set-up for the interfaces file so that I just get the one single static IP address set on the Pi and not assigned by DHCP from the router at all?
Posts: 130
Joined: Fri Oct 03, 2014 3:03 pm
by knute » Wed Aug 05, 2015 7:49 pm
Somewhere along the way an upgrade modified my /etc/network/interfaces file with the 'manual' word instead of dhcp or static and I ended up with two IP addresses, my static one and a dhcp address. I finally had time to play with it and found out that dhcpcd5 works differently than whatever was in it before. To get just your static address, do not modify /etc/network/interfaces. Put back the 'manual' word if you changed it and instead modify /etc/dhcpcd.conf as shown in the example from the docs:

Code: Select all
     static value
             Configures a static value.  If you set ip_address then dhcpcd
             will not attempt to obtain a lease and just use the value for the
             address with an infinite lease time.

             Here is an example which configures a static address, routes and
             dns.
                   interface eth0
                   static ip_address=192.168.0.10/24
                   static routers=192.168.0.1
                   static domain_name_servers=192.168.0.1


See more at: http://www.daemon-systems.org/man/dhcpcd.conf.5.html
Posts: 241
Joined: Thu Oct 23, 2014 12:14 am
by KLL » Thu Aug 06, 2015 8:45 am
thank you @knute, works great even for eth0 AND wlan0 static IP now!
( only my wlan0 hot plug after boot still not ok )
Posts: 621
Joined: Wed Jan 09, 2013 3:05 pm
Location: thailand
by DarrenHill » Thu Aug 06, 2015 1:32 pm
Thanks for the tips.

I had a play with it the other night, and kinda went the opposite direction, based on a few other things I found by trawling the net. I actually removed dhcpcd5 completely by apt-get, and then set everything up static in the interfaces file (the Pi only has fixed static ethernet, no wifi as it's a media player) and that worked fine too.

I'm about to set up an OpenVPN server with another Pi though, so I will look at your way of doing things there as I guess perhaps removing dhcpcd5 in that case may cause other issues as it will be acting as a dhcp server there for the new subnet for the incoming VPN client connections.

Still good to know new stuff and learn other ways of doing this kind of thing :D

Editted to add - This was the other thread I found on this topic, in the beginners section where I got the removal tip. Looks like we have multiple ways to do this (as normal).
Posts: 130
Joined: Fri Oct 03, 2014 3:03 pm
by KLL » Fri Aug 07, 2015 7:29 am
@knute, this settings are also possible for noobs, prior to installation,
( like wifi only, silent install... ) by incl in /os/Raspbian/partition_setup.sh
Code: Select all
# for a short ethernet connection PC RPI
#sed /tmp/1/cmdline.txt -i -e "s|rootwait|rootwait ip=192.168.10.101::192.168.10.10|"
#sed /tmp/2/etc/network/interfaces -i -e "/iface eth0/a\dns-nameservers 8.8.8.8 8.8.4.4"
# for WiFi with fix ssid in interfaces
sed /tmp/2/etc/network/interfaces -i -e '/wpa-conf/d'
sed /tmp/2/etc/network/interfaces -i -e '/iface wlan0/a\wpa-ssid "my-wlan" '
sed /tmp/2/etc/network/interfaces -i -e '/wpa-ssid/a\wpa-psk "We_Love_RPi" '
#sed /tmp/2/etc/network/interfaces -i -e '/wpa-psk/a\address 192.168.1.101'
#sed /tmp/2/etc/network/interfaces -i -e '/address/a\gateway 192.168.1.1'
# using dhcpcd.conf for fix ip now
sed /tmp/2/etc/dhcpcd.conf -i -e '$a\ interface eth0'
sed /tmp/2/etc/dhcpcd.conf -i -e '$a\ static ip_address=192.168.1.101/24 '
sed /tmp/2/etc/dhcpcd.conf -i -e '$a\ static routers=192.168.1.1 '
sed /tmp/2/etc/dhcpcd.conf -i -e '$a\ static domain_name_servers=192.168.1.1 '
sed /tmp/2/etc/dhcpcd.conf -i -e '$a\ interface wlan0'
sed /tmp/2/etc/dhcpcd.conf -i -e '$a\ static ip_address=192.168.1.201/24 '
sed /tmp/2/etc/dhcpcd.conf -i -e '$a\ static routers=192.168.1.1 '
sed /tmp/2/etc/dhcpcd.conf -i -e '$a\ static domain_name_servers=192.168.1.1 '

and noobs headless comes up on WiFi..
Posts: 621
Joined: Wed Jan 09, 2013 3:05 pm
Location: thailand
by RobinGreig » Sat Aug 15, 2015 3:05 am
knute wrote:Somewhere along the way an upgrade modified my /etc/network/interfaces file with the 'manual' word instead of dhcp or static and I ended up with two IP addresses, my static one and a dhcp address. I finally had time to play with it and found out that dhcpcd5 works differently than whatever was in it before. To get just your static address, do not modify /etc/network/interfaces. Put back the 'manual' word if you changed it and instead modify /etc/dhcpcd.conf as shown in the example from the docs:

Code: Select all
     static value
             Configures a static value.  If you set ip_address then dhcpcd
             will not attempt to obtain a lease and just use the value for the
             address with an infinite lease time.

             Here is an example which configures a static address, routes and
             dns.
                   interface eth0
                   static ip_address=192.168.0.10/24
                   static routers=192.168.0.1
                   static domain_name_servers=192.168.0.1



Thanks knute!!!!
Fixed the issue for me as well
Robin
www.robingreig.ca
Arduino + Raspi + Amateur Radio enthusiast since 2012
VE6RBN Canadian Amateur Radio Callsign
Posts: 13
Joined: Sat Oct 27, 2012 2:40 am
Location: Calgary, Alberta, Canada
by Master68 » Tue Aug 18, 2015 1:56 pm
Hello all,

thanks for this solution! This also puzzled me for quite some time. However while searching for a solution to this problem, I tampered a lot with my Interfaces file - now it looks like a mess and I don't have the original version any more (yes, I know I should have made a backup before trying, but every time I found a "solution" in some Forum I thought this is it now and it turned out later that it didn't work... :-( )

Could anyone please post the complete "default" of the interfaces file here (the new version with the "manual" keyword instead of the "dhcp" keyword)?
Since I did some other configurations already I would rather not need to reinstall everything just to become "clean" again.

Thanks a lot!
Posts: 4
Joined: Tue Aug 18, 2015 1:44 pm
by Joe Schmoe » Tue Aug 18, 2015 2:25 pm
Could anyone please post the complete "default" of the interfaces file here...


Did you install from NOOBS or from a "raw image" ?

(Yes, this question is relevant.)
And some folks need to stop being fanboys and see the forest behind the trees.

(One of the best lines I've seen on this board lately)
Posts: 4277
Joined: Sun Jan 15, 2012 1:11 pm
by Master68 » Tue Aug 18, 2015 2:29 pm
Did you install from NOOBS or from a "raw image" ?


I installed Raspbian Wheezy 2015-05-05

Ah, yes - and if you also happen to have the unmodified Version of the "dhcpcd.conf" (and even better also the correctly modified one for comparison), that would be really help! :-)

Thanks!
Last edited by Master68 on Tue Aug 18, 2015 2:45 pm, edited 1 time in total.
Posts: 4
Joined: Tue Aug 18, 2015 1:44 pm
by Joe Schmoe » Tue Aug 18, 2015 2:40 pm
Master68 wrote:
Did you install from NOOBS or from a "raw image" ?


I installed Raspbian Wheezy 2015-05-05

Thanks!


OK - then all you have to do is mount the original raw image file as a "loop" device and you will have access to the original /etc/interfaces file.

Note that if you installed via NOOBS (middleweight), the same basic idea will work but the details are a little bit more complicated.
And some folks need to stop being fanboys and see the forest behind the trees.

(One of the best lines I've seen on this board lately)
Posts: 4277
Joined: Sun Jan 15, 2012 1:11 pm
by Master68 » Tue Aug 18, 2015 3:03 pm
Joe Schmoe wrote:OK - then all you have to do is mount the original raw image file as a "loop" device and you will have access to the original /etc/interfaces file.


Ah, ok! Thanks for this tip! Sorry for dumb questions, I'm still at the beginning of my learning curve...
Posts: 4
Joined: Tue Aug 18, 2015 1:44 pm
by KLL » Tue Aug 18, 2015 3:32 pm
if i may add some more confusion, but as you see is the access to parameters
in /etc/network/interfaces not easy from the "outside" but for a raspbian only / non noobs / SD
it is possible,
here some nice trick you could use:
sudo nano /etc/network/interfaces
Code: Select all
auto lo
iface lo inet loopback

auto eth0
allow-hotplug eth0

auto wlan0
allow-hotplug wlan0

source /boot/interfaces.conf

sudo nano /boot/interfaces.conf
Code: Select all
# interfaces.conf
# RASPBIAN BOOT NETWORK CONFIG
# this file is included into the /etc/network/interfaces

iface eth0 inet manual

iface wlan0 inet manual
wpa-ssid "my-wlan"
wpa-psk "We_Love_RPi"

but for the fix IP now must use:
sudo nano /etc/dhcpcd.conf
Code: Select all
interface eth0
static ip_address=192.168.1.101/24
static routers=192.168.1.1
static domain_name_servers=192.168.1.1
interface wlan0
static ip_address=192.168.1.201/24
static routers=192.168.1.1
static domain_name_servers=192.168.1.1
Posts: 621
Joined: Wed Jan 09, 2013 3:05 pm
Location: thailand
by hippy » Tue Aug 18, 2015 5:42 pm
On a somewhat related note; does anyone know how to properly set up two IP addresses on eth0; one DHCP, one static ?

I have been using the quirk to have 192.168.0.206 (DHCP) and 192.168.0.100 (static) which has worked well so far but I now want to move the static to 192.168.1.100. If I change it to that in /etc/network/interfaces and reboot I end up with only the DHCP address. It gets both if I change it back and reboot.

I am happy to have DHCP plus two static if that's easier to set up.
Posts: 1879
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK
by rpdom » Tue Aug 18, 2015 8:03 pm
You can set up a virtual port as eth0:0 and assign the address to that in the interfaces file (and another to eth0:1 etc)
User avatar
Posts: 10585
Joined: Sun May 06, 2012 5:17 am
Location: Essex, UK
by Master68 » Wed Aug 19, 2015 7:16 am
KLL wrote:if i may add some more confusion, but as you see is the access to parameters
in /etc/network/interfaces not easy from the "outside" but for a raspbian only / non noobs / SD
it is possible,
here some nice trick you could use: [...]


Hello KLL,

thanks! That's a really useful tip! :-) makes it much more comfortable to customize the network setup.
This should be integrated into the default Raspbian images, so one could easily preconfigure the network setup from the Windows machine before writing the image to the SD Card.

Is this "source" keyword trick possible with any config files in Linux, or only with the "Interfaces" file? I mean: could I replace any *.conf file in the Linux file system by simply one line "source /boot/name_of_the_conf.conf" and place all the "real" conf files together in one place into the boot partition?
Posts: 4
Joined: Tue Aug 18, 2015 1:44 pm
by KLL » Wed Aug 19, 2015 9:07 am
Master68 wrote:really useful tip! :-) makes it much more comfortable to customize the network setup.
This should be integrated into the default Raspbian images, so one could easily preconfigure the network setup from the Windows machine before writing the image to the SD Card.

yes, that's why i mentioned it here, actually i found it in the config files for "octopi" from Guy Sheffer.

Master68 wrote:Is this "source" keyword trick possible with any config files in Linux, or only with the "Interfaces" file? I mean: could I replace any *.conf file in the Linux file system by simply one line "source /boot/name_of_the_conf.conf" and place all the "real" conf files together in one place into the boot partition?

i would not expect that!

but actually for noobs ( noobs noir ) headless silent install
i do exactly that by a hack, in the
/os/Raspbian/partition_setup.sh i add:
Code: Select all
# for a short ethernet connection PC RPI
#sed /tmp/1/cmdline.txt -i -e "s|rootwait|rootwait ip=192.168.10.101::192.168.10.10|"
#sed /tmp/2/etc/network/interfaces -i -e "/iface eth0/a\dns-nameservers 8.8.8.8 8.8.4.4"
# for WIFI with fix ssid in interfaces
sed /tmp/2/etc/network/interfaces -i -e '/wpa-conf/d'
sed /tmp/2/etc/network/interfaces -i -e '/iface wlan0/a\wpa-ssid "my-wlan" '
sed /tmp/2/etc/network/interfaces -i -e '/wpa-ssid/a\wpa-psk "We_Love_RPi" '
#sed /tmp/2/etc/network/interfaces -i -e '/wpa-psk/a\address 192.168.1.101'
#sed /tmp/2/etc/network/interfaces -i -e '/address/a\gateway 192.168.1.1'
# using dhcpcd.conf for fix ip now
sed /tmp/2/etc/dhcpcd.conf -i -e '$a\ interface eth0'
sed /tmp/2/etc/dhcpcd.conf -i -e '$a\ static ip_address=192.168.1.101/24 '
sed /tmp/2/etc/dhcpcd.conf -i -e '$a\ static routers=192.168.1.1 '
sed /tmp/2/etc/dhcpcd.conf -i -e '$a\ static domain_name_servers=192.168.1.1 '
sed /tmp/2/etc/dhcpcd.conf -i -e '$a\ interface wlan0'
sed /tmp/2/etc/dhcpcd.conf -i -e '$a\ static ip_address=192.168.1.201/24 '
sed /tmp/2/etc/dhcpcd.conf -i -e '$a\ static routers=192.168.1.1 '
sed /tmp/2/etc/dhcpcd.conf -i -e '$a\ static domain_name_servers=192.168.1.1 '

SD in RPI, powerup
and 30min later raspbian comes up even on WIFI..
Posts: 621
Joined: Wed Jan 09, 2013 3:05 pm
Location: thailand
by Jasimo » Thu Aug 20, 2015 7:46 pm
Thx for this tip about the static configuration in dhcpcd.conf.
One question, what will happen if a configured IP in the dhcpcd.conf file is already in use by another device in the network, will then a free ip assigned to the Pi?
Posts: 50
Joined: Mon Apr 27, 2015 11:50 am
by rpdom » Thu Aug 20, 2015 7:51 pm
If a static IP is assigned to an interface then it will use that IP even if it is also assigned to something else (it doesn't know!) this can cause network chaos. Been there... etc.
User avatar
Posts: 10585
Joined: Sun May 06, 2012 5:17 am
Location: Essex, UK
by Jasimo » Thu Aug 20, 2015 7:53 pm
Means that there is no way to configure something like a fallback, i read something about that in the man pages (fallback profile).
But you will be right, thx for the info.
Posts: 50
Joined: Mon Apr 27, 2015 11:50 am
by DanielBarker » Tue Sep 15, 2015 4:18 pm
I regret I have not managed to configure a static IP. I would appreciate advice.

The SD card is based on Raspbian 2015-05-05-raspbian-wheezy.zip.

Based on above posts, I added the following to the end of /etc/dhcpcd.conf. The settings are correct, as verified separately under OS X.

# Added by Daniel

interface eth0
static ip_address=138.251.156.122
static subnet-mask 255.255.255.0
static routers=138.251.156.199
static domain_name_servers=138.251.66.46
static domain_search=st-andrews.ac.uk

On reboot, the network still does not work.

On start-up, the Pi says wpasupplicant daemon failed to start.

I also see

ifup: interface eth0 already configured

and

eth0: using static address 138.251.156.122/16

I do not know what the /16 means - is it relevant?

Under earlier systems, I had a nice set-up, where both eth0 and eth0:1 were configured in /etc/network/interfaces. This gave both a static IP (that worked in the office and failed at home) and DHCP (that failed in the office but worked at home). I realise changing /etc/network/interfaces is no longer correct. Were it possible to somehow achieve the same effect with DHCPCD, that would be a bonus.

The main problem is to get the static IP working at all, though.

Advice will be very welcome.

Thanks,

Daniel
Posts: 71
Joined: Tue May 29, 2012 7:53 am
by DanielBarker » Tue Sep 15, 2015 5:17 pm
May have solved my own problem - with help from further posts, above!

(1) sudo apt-get remove dhcpcd5
(2) put what I used to put in /etc/network/interfaces in /etc/network/interfaces (i.e. as under earlier Rasbpians)
(3) put the nameserver, which I used to put in /etc/resolve.conf, in /etc/resolvconf.conf

I get wired network with a static IP!

I haven't tried the two-interfaces idea, but I assume it will work (and it's not too important).

- Daniel
Posts: 71
Joined: Tue May 29, 2012 7:53 am
by ozpete999 » Mon Nov 30, 2015 11:10 am
Hi
So, to get a static ip address under Jessie, do I edit the /etc/dhcpcd.conf file by merely ADDING the following code to it at the bottom of the file, or by completely REPLACING everything in it with the following code ?

interface eth0
static ip_address=192.168.x.x/n
static routers=192.168.x.x
static domain_name_servers=192.168.x.x

Thanks
Posts: 9
Joined: Sun Nov 29, 2015 4:08 am
by DougieLawson » Mon Nov 30, 2015 2:08 pm
Add that stuff at the bottom.
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

Since 2012: 1B*5, 2B*2, B+, A+, Zero*2, 3B*3

Please post ALL technical questions on the forum. Do not send private messages.
User avatar
Posts: 27604
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
by ozpete999 » Tue Dec 01, 2015 8:10 am
Thanks, Dougie. :D
Posts: 9
Joined: Sun Nov 29, 2015 4:08 am
by cyttorak » Sat Jan 02, 2016 1:38 pm
what's mean "/24" in "static ip_address=192.168.100.21/24"?
Posts: 73
Joined: Wed Dec 26, 2012 10:34 pm