Add Multiple IP Addresses into One Single Network Interface. (Server)
Adding an additional; secondary or third IP is a thing to think of when you are a Linux system administrator. Some of you may be curious as to why we would allocate numerous IP addresses to a single network card...
The first reason is just because you can ..., and yes Ubuntu server allows you to add multiple virtual IP addresses on a single network interface card without having to buy an additional network adapter.
Let's take the scenario where you are testing a Linux system and need two or more network cards.
Would you purchase a new one?
...
..
.
No, ...not necessary.
You may configure a network card with various IP series, such as 192.168.1.4, 192.168.2.2, 192.168.3.12, etc., and utilize each of them simultaneously.
Sounds helpful? – Of course, it is!
Permanently add an additional IP Address
Ubuntu uses the Netplan utility to configure networking.
We will permanently add an additional IP address to our system by editing the file /etc/netplan/50-cloud-init.yaml
.
Let's find the IP address we are currently using:
$ ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
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 mq state UP group default qlen 1000
link/ether xx:xx:32:xx:4x:x4 brd ff:ff:ff:ff:ff:ff
inet 192.168.1.93/24 brd 192.168.1.255 scope global eth0
valid_lft forever preferred_lft forever
inet6 xxzz::xxx:3xxf:fxx5:2xx7/64 scope link
valid_lft forever preferred_lft forever
3: wlan0: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN group default qlen 1000
link/ether xx:xx:xx:xx:xx:xx brd ff:ff:ff:ff:ff:ff
4: tun0: <POINTOPOINT,MULTICAST,NOARP,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UNKNOWN group default qlen 500
link/none
inet 10.8.0.1/24 scope global tun0
valid_lft forever preferred_lft forever
inet6 fxxx::xxx:cxxx:xxxx:axxx/64 scope link stable-privacy
valid_lft forever preferred_lft forever
As you see above, the IP address of my network card eth0 is 192.168.1.19.
Now; what if we need to add another IP address, for example, 192.168.1.4?
Let's take a look at our current Netplan configuration and apply the change.
$ sudo cat 50-cloud-init.yaml
[sudo] password for snubmonkey:
# This is the network config written by 'subiquity'
network:
ethernets:
eth0:
dhcp4: no
dhcp6: no
addresses:
- 192.168.1.93/24
- 192.168.1.4/24 <<<
routes:
- to: default
via: 192.168.1.1
nameservers:
addresses:
- 9.9.9.9
search:
- 208.67.222.222, 1.0.0.1
version: 2
Here’s a brief explanation of each parameter:
- eth0: A device name to be configured.
- dhcp4: Used to enable or disable dhcp4.
- dhcp6: Used to enable or disable dhcp6.
- addresses: The IP address of the device.
- gateway4: The IP address of your gateway.
- nameservers: The IP address of your DNS server.
Save and close the file when done.
Enable the new configuration with the following command.
$ netplan apply
Restart the networking service.
$ sudo systemctl status systemd-networkd.service
Run the below command to check whether the new IP has been taken into account:
$ ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
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 mq state UP group default qlen 1000
link/ether xx:xx:xx:xx:xx:ax brd ff:ff:ff:ff:ff:ff
inet 192.168.1.93/24 brd 192.168.1.255 scope global eth0
valid_lft forever preferred_lft forever
inet 192.168.1.4/24 brd 192.168.1.255 scope global secondary eth0
valid_lft forever preferred_lft forever
inet6 fe80::xxxx:xxxx:fxx4:4xxx/64 scope link
valid_lft forever preferred_lft forever
3: wlan0: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN group default qlen 1000
link/ether xc:x6:3x:x4:4x:xx brd ff:ff:ff:ff:ff:ff
4: tun0: <POINTOPOINT,MULTICAST,NOARP,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UNKNOWN group default qlen 500
link/none
inet 10.8.0.1/24 scope global tun0
valid_lft forever preferred_lft forever
inet6 fe80::xbxx:cxxx:x4xx:ax7x/64 scope link stable-privacy
valid_lft forever preferred_lft forever2
Let's double-check by pinging our new assignment address.
-a
: the system plays a sound when there is a response from a host. An audible ping is useful when you are troubleshooting network issues and do not want to look at the screen until there is a response.
-c
: automatically stops after it sends a certain number of packets, use -c
, and a number. This sets the desired amount of ping requests.
ps: as we may have noticed we used the ip addr
in favor of the ifconfig
command to view the network interface and IP address.
ip addr
and ip a
do the same.
BONUS
Pros and cons of ip and ipconfig
Since it employs Netlink
sockets rather than ioctl
system calls, the ip
command is more flexible and technically effective than the ifconfig
.
We will talk about this more in another of our posts; so stay tuned.
Here is an example:
$ ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.1.93 netmask 255.255.255.0 broadcast 192.168.1.255
inet6 xxxx::xxxx:xxxx:xx1x:xxax prefixlen 64 scopeid 0x20<link>
ether dc:a6:32:14:4e:a7 txqueuelen 1000 (Ethernet)
RX packets 9713030 bytes 6030523854 (6.0 GB)
RX errors 0 dropped 89 overruns 0 frame 0
TX packets 8998304 bytes 6420042303 (6.4 GB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
...
..
.
Have you spotted it?
Using ifconfig
command does not allow us to see whether or not our NIC is bound to multiple IP addresses.