{"id":10526,"date":"2021-10-09T08:53:34","date_gmt":"2021-10-09T05:53:34","guid":{"rendered":"https:\/\/kifarunix.com\/?p=10526"},"modified":"2024-03-18T13:47:58","modified_gmt":"2024-03-18T10:47:58","slug":"create-virtual-secondary-ip-addresses-on-an-interface-in-linux","status":"publish","type":"post","link":"https:\/\/kifarunix.com\/create-virtual-secondary-ip-addresses-on-an-interface-in-linux\/","title":{"rendered":"Create Virtual\/Secondary IP addresses on an Interface in Linux"},"content":{"rendered":"\n<p>Follow through this tutorial to learn how to create virtual\/secondary IP addresses on an interface in Linux. This enables you to assign multiple IP addresses to a single interface.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Creatin Virtual IP addresses on a Linux Interface <\/h2>\n\n\n\n<p>You can create virtual IP addresses on an interface temporarily or permanently.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Create Temporary Virtual IP addresse<\/h3>\n\n\n\n<p>To create temporary virtual\/secondary IP addresses on an interface in Linux, you can use commands such as <strong><code>ip<\/code><\/strong>, <strong><code>ifconfig<\/code><\/strong>.<\/p>\n\n\n\n<p>To use <strong><code>ip<\/code><\/strong> command to create\/add secondary IP addresses to an interface, see the examples below.<\/p>\n\n\n\n<p>In our example server, we have an interface called <code>enp0s8<\/code>.<\/p>\n\n\n\n<p>Checking the current IP address of the interface;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ip add show dev enp0s8<\/code><\/pre>\n\n\n\n<pre class=\"scroll-box\"><code>\n3: enp0s8: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000\n    link\/ether 08:00:27:c4:23:c9 brd ff:ff:ff:ff:ff:ff\n    inet 192.168.56.108\/24 brd 192.168.56.255 scope global noprefixroute enp0s8\n       valid_lft forever preferred_lft forever\n    inet6 fe80::d524:3777:b321:5ed\/64 scope link noprefixroute \n       valid_lft forever preferred_lft forever\n<\/code><\/pre>\n\n\n\n<p>The primary IP address assigned to the interface is <code><strong>192.168.56.108<\/strong><\/code>.<\/p>\n\n\n\n<p>Assuming we want to temporarily assign a secondary IP address, <code><strong>192.168.56.10<\/strong>9<\/code>, to the interface using the <strong><code>ip<\/code><\/strong> command;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ip addr add 192.168.56.109\/24 br 192.168.56.255 dev enp0s8<\/code><\/pre>\n\n\n\n<p>The <strong><code>addr<\/code><\/strong> and <strong><code>br<\/code><\/strong> are abbreviations for <strong><code>address<\/code><\/strong> and <strong><code>broadcast<\/code><\/strong> respectively.<\/p>\n\n\n\n<p>Confirming the secondary IP address assignment;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ip add show dev enp0s8<\/code><\/pre>\n\n\n\n<pre class=\"scroll-box\"><code>\n3: enp0s8: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000\n    link\/ether 08:00:27:c4:23:c9 brd ff:ff:ff:ff:ff:ff\n    inet 192.168.56.108\/24 brd 192.168.56.255 scope global dynamic noprefixroute enp0s8\n       valid_lft 394sec preferred_lft 394sec\n    <strong>inet 192.168.56.109\/24 brd 192.168.56.255 scope global secondary enp0s8 <\/strong>\n       valid_lft forever preferred_lft forever\n    inet6 fe80::d524:3777:b321:5ed\/64 scope link noprefixroute \n       valid_lft forever preferred_lft forever\n<\/code><\/pre>\n\n\n\n<p>If you restart the networking\/take the interface up and down or reboot your server, you will loose the assigned IP address.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Create Permanent Virtual IP addresses<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">Create secondary IP addresses using nmcli command<\/h4>\n\n\n\n<p>On CentOS and Similar derivatives, you can use simply use the NetworkManager command line tool, <strong><code>nmcli<\/code><\/strong>.<\/p>\n\n\n\n<p>The command may not be available by default on Ubuntu\/Debian systems. If so and want to use it, then install the network manager package (<strong><code>network-manager<\/code><\/strong>).<\/p>\n\n\n\n<p>First confirm the interface connection name;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>nmcli con show<\/code><\/pre>\n\n\n\n<p><code>con<\/code> is an abbreviation for <code>connection<\/code>.<\/p>\n\n\n\n<p>Sample output;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>NAME                UUID                                  TYPE      DEVICE \nenp0s8              e59e1c2f-bda2-4704-9f4a-67e8cce636d9  ethernet  enp0s8 \nWired connection 1  a7d294d4-05d9-3724-832e-6b80dc288a24  ethernet  enp0s3<\/code><\/pre>\n\n\n\n<p>In this case, we want to add virtual\/secondary IP to interface <strong><code>enp0s8<\/code><\/strong>, connection name <strong><code>enp0s8<\/code><\/strong>.<\/p>\n\n\n\n<p>This can be done using <code><strong>nmcli<\/strong><\/code> command as follows. Pay attention to <code>plus(+)ipv4.addresses<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>nmcli con mod enp0s8 +ipv4.addresses 192.168.56.109\/24<\/code><\/pre>\n\n\n\n<p><code>mod<\/code> is an abbreviation for <code>modify<\/code>.<\/p>\n\n\n\n<p>Take down and bring up the interface. <strong>I Assume you are directly logged in via console<\/strong> and not via ssh.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>nmcli con down enp0s8 &amp;&amp; nmcli con up enp0s8<\/code><\/pre>\n\n\n\n<p>Confirm the secondary IP address assignment.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ip add show dev enp0s8<\/code><\/pre>\n\n\n\n<pre class=\"scroll-box\"><code>\n3: enp0s8: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000\n    link\/ether 08:00:27:c4:23:c9 brd ff:ff:ff:ff:ff:ff\n    inet 192.168.56.108\/24 brd 192.168.56.255 scope global noprefixroute enp0s8\n       valid_lft forever preferred_lft forever\n    inet 192.168.56.109\/24 brd 192.168.56.255 scope global secondary noprefixroute enp0s8\n       valid_lft forever preferred_lft forever\n    inet6 fe80::d524:3777:b321:5ed\/64 scope link noprefixroute \n       valid_lft forever preferred_lft forever\n<\/code><\/pre>\n\n\n\n<p>To remove the secondary IP address using nmcli command, just use <strong><code>minus(-)ipv4.addresses<\/code><\/strong>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>nmcli con mod enp0s8 -ipv4.addresses 192.168.56.109\/24<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>nmcli con down enp0s8 &amp;&amp; nmcli con up enp0s8<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Create Secondary IP addresses using nmtui (Network Manager  GUI)<\/h4>\n\n\n\n<p><strong><code>nmtui<\/code><\/strong> is available if you have installed Network manager package.<\/p>\n\n\n\n<p>Launch <strong><code>nmtui<\/code><\/strong> from the terminal (you can use tab key to navigate through the settings);<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>nmtui<\/code><\/pre>\n\n\n\n<p>Select <strong>Edit a connection<\/strong> and click <strong>Ok<\/strong>.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"752\" height=\"508\" src=\"https:\/\/kifarunix.com\/wp-content\/uploads\/2021\/10\/edit-connection.png\" alt=\"Create Virtual\/Secondary IP addresses on an Interface in Linux\" class=\"wp-image-10623\" title=\"\"><\/figure><\/div>\n\n\n<p>Select the Interface to edit, which in this example is <strong><code>enp0s8<\/code><\/strong> and click <strong>Edit<\/strong>;<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"747\" height=\"575\" src=\"https:\/\/kifarunix.com\/wp-content\/uploads\/2021\/10\/edit-interface.png\" alt=\"\" class=\"wp-image-10624\" title=\"\"><\/figure><\/div>\n\n\n<p>Scroll down to IPV4 configuration and click <strong>Add<\/strong> and enter your IP address.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"773\" height=\"510\" src=\"https:\/\/kifarunix.com\/wp-content\/uploads\/2021\/10\/add-ip-address.png\" alt=\"\" class=\"wp-image-10626\" title=\"\" srcset=\"https:\/\/kifarunix.com\/wp-content\/uploads\/2021\/10\/add-ip-address.png?v=1633779251 773w, https:\/\/kifarunix.com\/wp-content\/uploads\/2021\/10\/add-ip-address-768x507.png?v=1633779251 768w\" sizes=\"(max-width: 773px) 100vw, 773px\" \/><\/figure><\/div>\n\n\n<p>Next, click <strong>Ok<\/strong> at the bottom &gt; <strong>Back<\/strong> &gt; <strong>Activate a connection<\/strong> &gt; <strong>Ok<\/strong>.<\/p>\n\n\n\n<p>Select and deactivate the interface.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"756\" height=\"320\" src=\"https:\/\/kifarunix.com\/wp-content\/uploads\/2021\/10\/select-n-deactivate-interface.png\" alt=\"\" class=\"wp-image-10627\" title=\"\"><\/figure><\/div>\n\n\n<p>Select the interface and activate it again.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"746\" height=\"248\" src=\"https:\/\/kifarunix.com\/wp-content\/uploads\/2021\/10\/select-n-activate-interface.png\" alt=\"\" class=\"wp-image-10628\" title=\"\"><\/figure><\/div>\n\n\n<p>Select <strong>Back<\/strong> &gt; <strong>Quit<\/strong>.<\/p>\n\n\n\n<p>Confirm the IP address assignment.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ip add show dev enp0s8<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>ip add show dev enp0s8<\/code><\/pre>\n\n\n\n<pre class=\"scroll-box\"><code>\n3: enp0s8: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000\n    link\/ether 08:00:27:c4:23:c9 brd ff:ff:ff:ff:ff:ff\n    inet 192.168.56.108\/24 brd 192.168.56.255 scope global noprefixroute enp0s8\n       valid_lft forever preferred_lft forever\n    inet 192.168.56.109\/24 brd 192.168.56.255 scope global secondary noprefixroute enp0s8\n       valid_lft forever preferred_lft forever\n    inet6 fe80::d524:3777:b321:5ed\/64 scope link noprefixroute \n       valid_lft forever preferred_lft forever\n<\/code><\/pre>\n\n\n\n<p><strong>You can similarly remove this using nmcli command or right from the nmtui tool.<\/strong><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Create Secondary IP address on Ubuntu<\/h3>\n\n\n\n<p>To create a permanent IP address on Ubuntu 18.04\/Ubuntu 20.04 systems, which uses netplan to manage the network interfaces, then you can proceed as follows;<\/p>\n\n\n\n<p>Check the current IP address for the interface;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ip add show dev enp0s8<\/code><\/pre>\n\n\n\n<pre class=\"scroll-box\"><code>\n3: enp0s8: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000\n    link\/ether 08:00:27:56:39:94 brd ff:ff:ff:ff:ff:ff\n    inet 192.168.59.14\/24 brd 192.168.59.255 scope global enp0s8\n       valid_lft forever preferred_lft forever\n    inet6 fe80::a00:27ff:fe56:3994\/64 scope link \n       valid_lft forever preferred_lft forever\n<\/code><\/pre>\n\n\n\n<p>The current netplan configuration for my interfaces;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cat \/etc\/netplan\/00-installer-config.yaml<\/code><\/pre>\n\n\n\n<pre class=\"scroll-box\"><code>\nnetwork:\n  ethernets:\n    enp0s3:\n      dhcp4: true\n    <strong>enp0s8:\n      dhcp4: no\n      addresses: [192.168.59.14\/24]\n      routes:\n              - to: 0.0.0.0\/0\n                via: 192.168.59.1\n                metric: 101\n      nameservers:\n              addresses: [8.8.8.8]<\/strong>\n  version: 2\n<\/code><\/pre>\n\n\n\n<p>To add a secondary IP address to the interface, edit the configuration file<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cp \/etc\/netplan\/00-installer-config.yaml{,.old}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>vim \/etc\/netplan\/00-installer-config.yaml<\/code><\/pre>\n\n\n\n<p>and update the line;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>addresses: &#91;192.168.59.14\/24]<\/code><\/pre>\n\n\n\n<p>Such that it may look like;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>addresses: &#91;192.168.59.14\/24, 192.168.59.15\/24]<\/code><\/pre>\n\n\n\n<p>The config now looks like;<\/p>\n\n\n\n<pre class=\"scroll-box\"><code>\nnetwork:\n  ethernets:\n    enp0s3:\n      dhcp4: true\n    <strong>enp0s8:\n      dhcp4: no\n      addresses: [192.168.59.14\/24, 192.168.59.15\/24]\n      routes:\n              - to: 0.0.0.0\/0\n                via: 192.168.59.1\n                metric: 101\n      nameservers:\n              addresses: [8.8.8.8]<\/strong>\n  version: 2\n<\/code><\/pre>\n\n\n\n<p>Apply the configuration changes;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>netplan apply<\/code><\/pre>\n\n\n\n<p>The confirm the IP address assignment<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ip add show dev enp0s8<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Create Secondary IP address on Debian systems<\/h3>\n\n\n\n<p>Similarly, update the interfaces as follows, to add the secondary IP address.<\/p>\n\n\n\n<p>Sample interface configurations;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cat \/etc\/network\/interfaces<\/code><\/pre>\n\n\n\n<pre class=\"scroll-box\"><code>\nsource \/etc\/network\/interfaces.d\/*\n\n# The loopback network interface\nauto lo\niface lo inet loopback\n\n# The primary network interface\nallow-hotplug enp0s3\niface enp0s3 inet dhcp\n\nauto enp0s8\niface enp0s8 inet static\n\taddress 192.168.58.22\n\tnetmask 255.255.255.0\n\tgateway 192.168.58.1\n\tbroadcast 192.168.58.255\n\tdns-nameservers 8.8.8.8\n<\/code><\/pre>\n\n\n\n<p>To add a secondary IP address;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cp \/etc\/network\/interfaces{,.old}<\/code><\/pre>\n\n\n\n<p>update the config as follows<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>vim \/etc\/network\/interfaces<\/code><\/pre>\n\n\n\n<pre class=\"scroll-box\"><code>\nsource \/etc\/network\/interfaces.d\/*\n\n# The loopback network interface\nauto lo\niface lo inet loopback\n\n# The primary network interface\nallow-hotplug enp0s3\niface enp0s3 inet dhcp\n\nauto enp0s8\niface enp0s8 inet static\n\taddress 192.168.58.22\n\tnetmask 255.255.255.0\n\tgateway 192.168.58.1\n\tbroadcast 192.168.58.255\n\tdns-nameservers 8.8.8.8\n<strong>auto enp0s8:0\niface enp0s8:0 inet static\n\taddress 192.168.58.23\n\tnetmask 255.255.255.0<\/strong>\n<\/code><\/pre>\n\n\n\n<p>see the added configs;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>auto enp0s8:0\niface enp0s8:0 inet static\n\taddress 192.168.58.23\n\tnetmask 255.255.255.0<\/code><\/pre>\n\n\n\n<p>Save and exit the config and restart the networking;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>systemctl restart networking<\/code><\/pre>\n\n\n\n<p>Confirm IP address;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ip add show dev enp0s8<\/code><\/pre>\n\n\n\n<pre class=\"scroll-box\"><code>\n3: enp0s8: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000\n    link\/ether 08:00:27:2b:b4:61 brd ff:ff:ff:ff:ff:ff\n    inet 192.168.58.22\/24 brd 192.168.58.255 scope global enp0s8\n       valid_lft forever preferred_lft forever\n    inet 192.168.58.23\/24 brd 192.168.58.255 scope global secondary enp0s8:0\n       valid_lft forever preferred_lft forever\n    inet6 fe80::a00:27ff:fe2b:b461\/64 scope link \n       valid_lft forever preferred_lft forever\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Other Tutorials<\/h2>\n\n\n\n<p><a href=\"https:\/\/kifarunix.com\/connect-to-wifi-in-linux-using-nmcli-command\/\" target=\"_blank\" rel=\"noreferrer noopener\">Connect to WiFi in Linux Using NMCLI command<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/kifarunix.com\/assign-static-ip-addresses-for-openvpn-clients\/\" target=\"_blank\" rel=\"noreferrer noopener\">Assign Static IP Addresses for OpenVPN Clients<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/kifarunix.com\/how-to-configure-static-ip-addresses-on-ubuntu-18-04-server-using-netplan\/\" target=\"_blank\" rel=\"noreferrer noopener\">Configure Static IP Addresses using Netplan on Ubuntu 20.04\/18.04<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Follow through this tutorial to learn how to create virtual\/secondary IP addresses on an interface in Linux. This enables you to assign multiple IP addresses<\/p>\n","protected":false},"author":1,"featured_media":10623,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"rank_math_lock_modified_date":false,"footnotes":""},"categories":[63,121,68],"tags":[4150,4149,4153,4158,673,4155,4157,4156,4151],"class_list":["post-10526","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-networking","category-howtos","category-netplan","tag-create-secondary-ip-linux","tag-create-virtual-ip-address-in-linux-interface","tag-create-virtual-ip-in-linux-using-nmtui","tag-debian","tag-nmcli","tag-nmtui","tag-secondary-ip-centos","tag-virtual-ip","tag-virtual-ip-nmcli-command","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-50","resize-featured-image"],"_links":{"self":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/10526"}],"collection":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/comments?post=10526"}],"version-history":[{"count":6,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/10526\/revisions"}],"predecessor-version":[{"id":21648,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/10526\/revisions\/21648"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/media\/10623"}],"wp:attachment":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/media?parent=10526"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/categories?post=10526"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/tags?post=10526"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}