Score:1

Assign multiple Static IP address to one NIC

ky flag

Please, I need help on to assign multiple static IP's with different on one NIC for 2 different IP like 192.168.xx.xx/24 and 172.16.xx.xx/24. Any idea of how to configure it on Ubuntu Server 22.04.

I tried creating the static ip in etc/netplan/00-install

network:
  version: 2
  renderer: networkd
  ethernets:
    eth0:
      addresses: 
      - 192.168.1.212/24 
      nameservers: 
        addresses: [8.8.8.8, 8.8.4.4]
      routes:
      - to: default 
        via: 192.168.1.2
zwets avatar
us flag
Your configuration doesn't show you setting both addresses.
zwets avatar
us flag
What are you trying to achieve? Technically, it's easy to put two IP addresses on a single NIC, even if they are in different subnets: just add them to the addresses list. But you'll also need routing rules to that make sure that traffic leaving your system has the desired origin IP.
Score:1
nu flag

You can use nmcli to assign the IP addresses.

For multiple interfaces

nmcli con add con-name static1 type ethernet ipv4.addresses <IP_ADDRESS>/<SUBNET_CIDR> ipv4.method manual ipv4.gateway <GATEWAY_IP> connection.autoconnect yes ifname <INTERFACE> ipv4.dns "8.8.8.8 8.8.4.4" 

route add -net <Network> netmask <subnet> dev <interface>
route add default gw <gateway>

Example with data filled...

nmcli con add con-name static1 type ethernet ipv4.addresses 192.168.0.101/24 ipv4.method manual ifname eth0 ipv4.gateway 192.168.0.1 ipv4.dns "8.8.8.8 8.8.4.4" connection.autoconnect yes

nmcli con add con-name static2 type ethernet ipv4.addresses 172.16.0.101/24 ipv4.method manual ifname eth1 ipv4.gateway 172.16.0.1 ipv4.dns "8.8.8.8 8.8.4.4" connection.autoconnect yes

route add -net 192.168.0.0 netmask 255.255.255.0 dev eth1
route add default gw 192.168.0.1
route add -net 172.16.0.0 netmask 255.255.255.0 dev eth0

For a single interface

# For routes nmcli has a ipv4.routes option that is "<IP_NETWORK>/<CIDR> <GATEWAY>, <IP_NETWORK_2> <GATEWAY_2>,..."
nmcli con add con-name static2 type ethernet ipv4.addresses 172.16.0.101/24,192.168.0.101/24 ipv4.method manual ifname eth0 ipv4.gateway 172.16.0.1 ipv4.dns "8.8.8.8 8.8.4.4" connection.autoconnect yes

route add -net 192.168.0.0 netmask 255.255.255.0 dev eth0
route add default gw 192.168.0.1
route add -net 172.16.0.0 netmask 255.255.255.0 dev eth0

Documentation on some of the items shown:

Using route for setting routes https://www.techrepublic.com/article/understand-the-basics-of-linux-routing/

Basic nmcli documentation https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/7/html/networking_guide/sec-configuring_ip_networking_with_nmcli

Related to using ipv4.routes with nmcli

https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/7/html/networking_guide/sec-configuring_static_routes_using_nmcli

https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/8/html/configuring_and_managing_networking/configuring-static-routes_configuring-and-managing-networking

zwets avatar
us flag
Your answer assumes the machine has two NICs. The question is how to do this on _a single NIC_.
Killian Fortman avatar
nu flag
@zwets I can change my answer for a single interface.
jesushova avatar
ky flag
Thanks let me give it a try
jesushova avatar
ky flag
All this configuration can be done on the /etc/netplan ?
Killian Fortman avatar
nu flag
@jesushova this is done via command line. This will create essentially a network profile called "static2" in the single interface example. The configuration should be possible via /etc/netplan but I do not know how to configure that, only know how to configure via `nmcli`
jesushova avatar
ky flag
Okay I will install network-manager and try it.
jesushova avatar
ky flag
after installing Network Manager, i used the nmcli device show and it showed me all what is on and all that. I will go ahead and use your code and see if it work. Thank you once again
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.