cw84
Posts: 3
Joined: Fri Aug 25, 2017 2:32 am

Setting Multiple Static IPs

Fri Aug 25, 2017 2:56 am

Hello,

I am wondering how I go about setting multiple static IPs on the same subnet to the same interface?

I have tried editing the /etc/dhcpcd.conf file as such..
interface eth0
static ip_address=192.168.1.10/24
static routers=192.168.1.1
static domain_name_servers=127.0.0.1

interface eth0:1
static ip_address=192.168.1.15/24
static routers=192.168.1.1
static domain_name_servers=127.0.0.1

User avatar
rpdom
Posts: 18727
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Setting Multiple Static IPs

Fri Aug 25, 2017 5:24 am

I may be wrong, but I think this may be one time that you can actually put the definition in /etc/network/interfaces or in a file in /etc/network/interfaces.d as dhcpcd won't know about the interface.

I just tested this and it works, except that ip addr doesn't show the interface and ifconfig does?

cw84
Posts: 3
Joined: Fri Aug 25, 2017 2:32 am

Re: Setting Multiple Static IPs

Sat Aug 26, 2017 4:19 am

I'm not sure what the syntax should look like in that file...

I did a

Code: Select all

ifconfig eth0:0 192.168.1.15
and that appeared to do the trick. But... it doesn't persist beyond a restart?

Code: Select all

$> ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1
    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 pfifo_fast state UP group default qlen 1000
    link/ether b8:27:eb:9c:a4:29 brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.10/24 brd 192.168.1.255 scope global eth0
       valid_lft forever preferred_lft forever
    inet 192.168.1.15/24 brd 192.168.1.255 scope global secondary eth0:0
       valid_lft forever preferred_lft forever
    inet6 fe80::1dcf:330c:d3c9:f493/64 scope link
       valid_lft forever preferred_lft forever
3: wlan0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether b8:27:eb:c9:f1:7c brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.11/24 brd 192.168.1.255 scope global wlan0
       valid_lft forever preferred_lft forever
    inet6 fe80::f513:52d8:ca02:8509/64 scope link
       valid_lft forever preferred_lft forever

cw84
Posts: 3
Joined: Fri Aug 25, 2017 2:32 am

Re: Setting Multiple Static IPs

Sat Aug 26, 2017 4:20 am

My current /etc/network/interfaces file..

Code: Select all

# 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

User avatar
rpdom
Posts: 18727
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Setting Multiple Static IPs

Sat Aug 26, 2017 5:40 am

Ok, it looks like it's not that simple. I will work on this and see if I can come up with a way to do it. I'll be needing this myself as I plan to upgrade all of my Raspberrys to Stretch at some point and some of them have aliased intefaces.

hippy
Posts: 9974
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Setting Multiple Static IPs

Sat Aug 26, 2017 10:16 am

I have been battling this issue also. Did much as the OP did, added static 'eth0' and 'eth0:1' entries to /etc/dhcpcd.conf, rebooted to ensure they took, but 'ifconfig', 'ip addr', 'hostname -I', 'netstat -nr' are all only showing the one 'eth0' address.

I have had it working in the past to some degree; it worked when both IP were on the same network ( eg 192.168.0.110 and 192.168.0.210 ), though not when they were not ( eg 192.168.0.110 and 172.16.0.110 ), which is why I had reverted back to using /etc/network/interfaces.

No need to "guess what?"; it works perfectly well with /etc/network/interfaces ...

Code: Select all

pi@Pi3B:~$ hostname -I
192.168.0.210 172.16.0.210 192.168.0.110 172.16.0.110
pi@Pi3B:~$
My /etc/network/interfaces in its entirety is ...

Code: Select all

auto lo
iface lo inet loopback

auto eth0
allow-hotplug eth0

iface eth0 inet static
address 192.168.0.210
gateway 192.168.0.1
netmask 255.255.255.0
broadcast 192.168.0.255 

auto eth0:1

iface eth0:1 inet static
address 192.168.0.110
gateway 192.168.0.1
netmask 255.255.255.0
broadcast 192.168.0.255 

auto eth0:2

iface eth0:2 inet static
address 172.16.0.210
gateway 172.16.0.1
netmask 255.255.255.0
broadcast 172.16.0.255  

auto eth0:3

iface eth0:3 inet static
address 172.16.0.110
gateway 172.16.0.1
netmask 255.255.255.0
broadcast 172.16.0.255  

auto wlan0
allow-hotplug wlan0
iface wlan0 inet manual
wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf
I don't have a problem with using 'dhcpcd' and would be using it and recommending it if it actually worked and would do what I want it to do. Unfortunately it doesn't. Despite people saying it will, and no end of suggestions and searching for information, it never has for what I want. I am tired of hearing "this should work" when it doesn't, and it now seems more broken than it was before.

It took just a few minutes to edit /etc/network/interfaces and get things working; I have wasted hours on dhcpcd, quite possibly days in total.

User avatar
rpdom
Posts: 18727
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Setting Multiple Static IPs

Sat Aug 26, 2017 10:55 am

hippy, I agree. I suspect I will end up removing dhcpcd from the Pis where I need non-standard interface definitions and just using the interfaces file instead.

Just to emphasise though, this is a bit of a special case and normall I would just edit dhcpcd.conf for a simple static address assignment.

hippy
Posts: 9974
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Setting Multiple Static IPs

Sat Aug 26, 2017 11:11 am

rpdom wrote:
Sat Aug 26, 2017 10:55 am
Just to emphasise though, this is a bit of a special case and normall I would just edit dhcpcd.conf for a simple static address assignment.
I agree. Most Pi users will simply use DHCP or assign one static IP address, and it seems fine for that.

It does however appear that the unusual cases crop up fairly regularly and people are often told 'don't edit /etc/network/interfaces - that's not the way to do it!'. And I would agree with that; it's not. But the way it should be done needs to work.

User avatar
KLL
Posts: 1453
Joined: Wed Jan 09, 2013 3:05 pm
Location: thailand
Contact: Website

Re: Setting Multiple Static IPs

Sat Aug 26, 2017 12:31 pm

hippy wrote:
Sat Aug 26, 2017 10:16 am
It took just a few minutes to edit /etc/network/interfaces and get things working; I have wasted hours on dhcpcd, quite possibly days in total.
thanks @hippy, i would like to follow you, but have 2 questions:
-1- how you disable dhcpcd?
-2- what is the way to change back to the eth0 name under STRETCH?
OR is this the way?
because i still have on a RPI2 with wlan adapter using STRETCH ( new install, not upgrade from JESSIE )
pi@RPI2:~ $ ls /sys/class/net/
enxb827eb6e15fc lo wlx48022a53056d
pi@RPI2:~ $
would be nice if you could add that two things to your post pls.
Last edited by KLL on Sat Aug 26, 2017 2:46 pm, edited 2 times in total.

SurferTim
Posts: 2054
Joined: Sat Sep 14, 2013 9:27 am
Location: Miramar Beach, Florida

Re: Setting Multiple Static IPs

Sat Aug 26, 2017 1:24 pm

There must be something I am missing here. I tried your suggestion with an RPi3 and Stretch. My ethernet interface will not even start. It doesn't appear in ifconfig. What am I missing?
My advice applies to RaspiOS only. Please mention if you use another OS.

Agung Setiawan
Posts: 2
Joined: Mon Mar 04, 2019 8:11 am

Re: Setting Multiple Static IPs

Tue Apr 02, 2019 4:06 am

Hello,

I'm beginner for using it, would you help me what first i can do to set up multiple IP for my raspberry pi. I mean, i wanna set up one from PC and one from Wireless ? Please help, Thanks Before.

User avatar
Chupo_cro
Posts: 53
Joined: Mon Dec 14, 2015 8:45 pm
Location: Hrvatska

Re: Setting Multiple Static IPs

Wed Dec 18, 2019 10:45 pm

Agung Setiawan wrote:
Tue Apr 02, 2019 4:06 am
Hello,

I'm beginner for using it, would you help me what first i can do to set up multiple IP for my raspberry pi. I mean, i wanna set up one from PC and one from Wireless ? Please help, Thanks Before.

This is what I did in dhcpcd.conf for setting different static IPs for LAN and WiFi:

Code: Select all

interface eth0
static ip_address=192.168.1.aaa/24
static routers=192.168.1.1
static domain_name_servers=xxx.xxx.xxx.xxx

interface wlan0
static ip_address=192.168.1.bbb/24
static routers=192.168.1.1
static domain_name_servers=xxx.xxx.xxx.xxx

Just replace aaa and bbb and asign the DNS. If your router has a different IP range (e.g. 192.168.0.xxx) then just edit static_routers and change the IP to have the first 3 numbers the same as router's IP.
Chupo_cro

User avatar
Chupo_cro
Posts: 53
Joined: Mon Dec 14, 2015 8:45 pm
Location: Hrvatska

Re: Setting Multiple Static IPs

Wed Dec 18, 2019 10:54 pm

cw84 wrote:
Fri Aug 25, 2017 2:56 am
Hello,

I am wondering how I go about setting multiple static IPs on the same subnet to the same interface?

I have tried editing the /etc/dhcpcd.conf file as such..
interface eth0
static ip_address=192.168.1.10/24
static routers=192.168.1.1
static domain_name_servers=127.0.0.1

interface eth0:1
static ip_address=192.168.1.15/24
static routers=192.168.1.1
static domain_name_servers=127.0.0.1

I am curious what is the use case of having two different IP addresses when using one router?

I've found this thread because I am looking for a solution for specifying different static IP addresses for using them with different routers which work in different IP subnets (e.g. one router is at 192.168.1.1 and the other one is at 192.168.0.1). I am looking for a way to setup the static IP address which depends on the connection - if Pi is connected to the router which has IP 192.168.1.1 then static IP should be 192.168.1.xx and if it is connected to the router which has IP 192.168.0.1 then static IP should be 192.168.0.xx
Chupo_cro

mw0sec
Posts: 17
Joined: Thu Oct 25, 2018 2:31 pm
Contact: Website

Re: Setting Multiple Static IPs

Sat Mar 07, 2020 10:10 pm

I would like to add my thanks to Hippy for his working solution. Had exactly the same problem - i.e. the recommended method just did not work. Wasted hours on it. :D

thunderbird985
Posts: 5
Joined: Mon Aug 24, 2020 5:00 pm

Re: Setting Multiple Static IPs

Thu Dec 03, 2020 7:03 am

FYI, in Raspbian Buster, dhcpcd runs as a service, and editing the /etc/dhcpcd.conf file requires reloading the service configuration file.

Make your changes in /etc/dhcpcd.conf to have separate static IPs for various interfaces. Here's how the relevant section in the dhcpcd.conf file should look:

Code: Select all

# Example static IP configuration:
interface eth0
static ip_address=192.168.10.25/25
static routers=192.168.10.1
static domain_name_servers=8.8.8.8

interface wlan0
static ip_address=192.168.10.26/25
static routers=192.168.10.1
static domain_name_servers=8.8.8.8
Then, have systemctl update the changes with:

Code: Select all

sudo systemctl daemon-reload
Then, restart the dhcpcd service with:

Code: Select all

sudo systemctl restart dhcpcd
You may need to reboot if you're dealing with a wireless interface, but this will work.

Return to “Beginners”