16

Trying to set two static ip in dhcpcd.conf, but second ip is not active.

dhcpcd.conf

interface eth0
    static ip_address=192.168.3.99/24
    static routers=192.168.3.61
    static domain_name_server=192.168.3.61

interface eth0:1
    static ip_address=192.168.4.55/24
    static routers=192.168.4.50
    static domain_name_server=192.168.4.50

ifconfig output:

inet addr:192.168.3.99  Bcast:192.168.3.255  Mask:255.255.255.0

Does anyone faced similar situation.

Avin Varghese
  • 261
  • 1
  • 2
  • 6

4 Answers4

7

Although it is said that /etc/network/interfaces is deprecated (read it everywhere online) so far the only way I have been able to make it work is in fact through /etc/network/interfaces.

The following should work for you, just put it in /etc/network/interfaces (you can leave out the gateway)

auto eth0:1
allow-hotplug eth0:1
iface eth0:1 inet static
    address 192.168.4.55
    netmask 255.255.255.0
    gateway 192.168.4.50

if you just want it to be temporary (not lasting after reboot) you could also use

sudo ifconfig eth0:1 192.168.4.55/24 
goldilocks
  • 58,859
  • 17
  • 112
  • 227
F. Pareto
  • 191
  • 1
  • 5
  • 5
    Instead of modifying /etc/network/interfaces, I leave the file /etc/network/interfaces untouched to keep dhcpcd working as intended, while adding a file such as /etc/network/interfaces.d/eth0-mydb to have the additional IP address configured. – VCD Jun 27 '18 at 14:09
  • "The 'modern' way described in official Debian documentation in fact states that this new method is dangerous." -> You've badly misinterpreted that; I am going to edit it out (the link was: https://wiki.debian.org/NetworkConfiguration#Multiple_IP_addresses_on_one_Interface). What it says is dangerous has nothing to do with dhcpcd (which is what the question is actually about); the two methods described there ("legacy" and "iproute2") are both /etc/network/interfaces based. Beyond that your answer is reasonable enough. – goldilocks Jul 06 '23 at 16:56
3

I suggest you use ip address show instead of ifconfig to see your ips, anyway with new debian/raspbian Jessie's versions, you may solve using multiple static configuration in this way:

interface eth0
arping 192.168.2.1
arping 192.168.4.50

profile 192.168.2.1
static ip_address=192.168.2.44/24
static routers=192.168.2.1
static domain_name_servers=192.168.2.1

profile 192.168.4.50
static ip_address=192.168.0.44/24
static routers=192.168.4.50
static domain_name_servers=192.168.4.50

source and more: Static IP address templates for dhcpcd.conf

lunix15
  • 31
  • 3
  • 1
    I could not get this approach to work, while adding a clause to /etc/network/interfaces.d/eth0-xxx (as per @F. Pareto and answer) worked fine. – askvictor Jan 16 '19 at 00:22
  • ok but you have to change that manually if you change network.. On the other hand, in my way, you need to know the addresses of the servers becouse If none of the arpings find an active machine then you will get a DHCP allocation – lunix15 Jan 17 '19 at 16:43
  • 4
    arping is for selecting one out of several possible static configurations, not for setting multiple IP addresses on one interface – Adrian W Feb 16 '19 at 16:08
1

I got my raspberry pi(jessie) to run two IPs on the same interface and on the same vlan by appending "denyinterfaces eth0" to dhcpcd.conf, and running an @reboot crontab job using "ip" commands.

$ sudo nano /etc/dhcpcd.conf  
-------------------------
denyinterfaces eth0


$ sudo nano /etc/ipstartup
-----------------------
ip address flush dev eth0
ip address add 10.0.0.2/24 dev eth0
ip address add 10.0.0.3/24 dev eth0
ip link set eth0 up
ip route add default via 10.0.0.1
printf "nameserver 8.8.8.8\nnameserver 8.8.4.4" > /etc/resolv.conf


$ sudo chmod 740 /etc/ipstartup


$ sudo crontab -e
-----------------
@reboot /etc/ipstartup
Jaredk
  • 11
  • 2
  • 1
    Although pleasingly simple,the trouble with this approach is that it doesn't do anything to make the address assingments happen before daemons (which want to bind these addresses!) are started. Myself I ended up writing my own initscript, so that it can be properly sequenced. – q.undertow Oct 16 '20 at 00:14
-1

yes, and a long time ago. Try /etc/network/interfaces : it's a proper place to make a config. DHCP client can assign only one IP, as DHCP protocol itself. Even more : DHCP is for getting a config from server. If you have a local per-host config, then use a proper place for it in your system

Alexey Vesnin
  • 926
  • 9
  • 16