How to bind a range of ips to ubuntu interfaces

If you don't mind not having interface aliases (i.e. the :0, :1 etc.), you can add as many IP addresses as you want on enp2s0 with the ip addr add command on the up event:

auto enp2s0
iface enp2s0 inet static
address 200.20.39.1
netmask 255.255.248.0
up ip addr add 200.20.39.1/21 dev enp2s0
up ip addr add 200.20.39.2/21 dev enp2s0
...
up ip addr add 200.20.39.254/21 dev enp2s0
...

and so on.

It should be pretty easy to generate the 2048 lines programmatically and then a simple ifdown enp2s0/ifup enp2s0 should get you ready without multiple restarts of the network service.

DISCLAIMER

Please keep in mind that you should do ifdown only if locally connected or remotely connected through another interface, as you'll lose connectivity to the server if connecting to enp2s0.


If you add a subnet to your lo interface, linux will respond to any address within that subnet.

To illustrate this, say you have an IP address of 192.168.1.99 on your ethernet interface. Now add 10.1.2.0/21 to your lo interface:

ip addr add 10.1.2.0/21 dev lo

Now it will react to any arp request for any IP address within that range, on whatever interface.

To be able to reach those IP addresses from other systems in your 192.168.1.0/24 range, you will need to set a route on those systems to that subnet via its primary address, i.e. 192.168.1.99 in this case. Alternatively add this route to the default gateway, it should send ICMP redirects when necessary to that host.