Add second ip linux

Add Secondary IP Address on Ubuntu 22.04|20.04|18.04

In this tutorial, I’ll teach you how to install a secondary IP address on Ubuntu 22.04|20.04|18.04, both temporarily and permanently. Transmission Control Protocol/Internet Protocol (TCP/IP) is a set of protocols that allows computers to communicate with one another. Despite its name, the stack includes a variety of protocols, including IP, TCP, UDP, ICMP, DNS, SMTP, ARP, and many others. The IP protocol is in charge of a host’s logical addressing, which allows packets to be transferred from one host to another.

Each device on the network is given a unique IP address for this purpose, and many addresses might be issued to the same device. In addition to the IP addressing protocol at the network layer, a protocol at the transport layer, such as the TCP and UDP protocols, will be required for a host to access a service offered on another host. This communication is carried out via these protocols through network ports. To access a service, in addition to establishing a source and destination IP, source and destination ports will be used.

Need for Secondary IPs in a Linux system?

Here are the reasons why you need multiples IP addresses:

  • To set up name servers.
  • Assign separate IP addresses to different services.
  • Installing an SSL certificate on a website and hosting numerous SSL sites without the use of virtual hosts is possible.
  • Separate IP addresses are used to host programs.

There are three main options to configure IP addresses on Ubuntu 22.04|20.04|18.04

1. Configuring Secondary IP Address Permanently on Ubuntu 22.04|20.04|18.04

2. Configuring Secondary IP Addresses Temporarily on Ubuntu 22.04|20.04|18.04

You can temporarily add a secondary IP address to your Ubuntu 22.04|20.04|18.04 system using the syntax below. When you use this approach to install a secondary IP, it will persist until the next system reboot.

ip addr add ip-address> dev interface-name>

Delete the above IP as follows:

sudo ip addr del ip-address> dev interface-name>

Configuring Secondary IP Addresses Permanently on Ubuntu 22.04|20.04|18.04

Configuring IP addresses permanently on Ubuntu can be achieved through the following ways:

Option 1: Configure Secondary IP Address on Ubuntu using Netplan

When using Netplan, an static or secondary IP address can be added to your Ubuntu system by editing /etc/netplan/01-netcfg.yaml file.

Читайте также:  Какая файловая система нужна linux

First of all, issue the following command to check current IP address of the system:

$ ip a 1: lo: 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: enp0s8: mtu 1500 qdisc fq_codel state UP group default qlen 1000 link/ether 08:00:27:e0:af:44 brd ff:ff:ff:ff:ff:ff inet 192.168.33.10/24 brd 192.168.33.255 scope global enp0s8 valid_lft forever preferred_lft forever inet6 fe80::a00:27ff:fee0:af44/64 scope link valid_lft forever preferred_lft forever

Or you can also use the following:

$ ifconfig enp0s8: flags=4163 mtu 1500 inet 192.168.33.10 netmask 255.255.255.0 broadcast 192.168.33.255 inet6 fe80::a00:27ff:fee0:af44 prefixlen 64 scopeid 0x20 ether 08:00:27:e0:af:44 txqueuelen 1000 (Ethernet) RX packets 16 bytes 1400 (1.4 KB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 51 bytes 4286 (4.2 KB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 lo: flags=73 mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 inet6 ::1 prefixlen 128 scopeid 0x10 loop txqueuelen 1000 (Local Loopback) RX packets 306 bytes 31120 (31.1 KB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 306 bytes 31120 (31.1 KB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

As you can see from the output above, the current IP address of the system is 192.168.33.10 on NIC (Network Interface Controller) enp0s8.

If you don’t have Netplan tool installed on your system, you can the following command to install it:

sudo apt install netplan.io

Now you can use vim editor or any other editor of your choice to edit /etc/netplan/01-netcfg.yaml file. If the vim editor is not installed you can also install as shown below:

#install vim editor sudo apt install vim #edit the file sudo vim /etc/netplan/01-netcfg.yaml

Now you can add the new IP address as shown below. In this guide, I will add secondary IP 192.168.33.15 .

network: version: 2 renderer: networkd ethernets: enp0s8: addresses: - 192.168.33.10/24 - 192.168.33.15/24 nameservers: addresses: [8.8.8.8, 1.1.1.1]

Now, confirm if the IP address has been added:

$ ip a 1: lo: 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: enp0s8: mtu 1500 qdisc fq_codel state UP group default qlen 1000 link/ether 08:00:27:e0:af:44 brd ff:ff:ff:ff:ff:ff inet 192.168.33.10/24 brd 192.168.33.255 scope global enp0s8 valid_lft forever preferred_lft forever inet 192.168.33.15/24 brd 192.168.33.255 scope global secondary enp0s8 valid_lft forever preferred_lft forever inet6 fe80::a00:27ff:fee0:af44/64 scope link valid_lft forever preferred_lft forever

Option 2: Configuring Secondary IP address Manually

No technical experience is required to manually set up a secondary IP address on Ubuntu 20.04/18.04 Desktop PCs.

Читайте также:  Antelope zen go linux

Search for “ settings ” on the Activities screen and click on the icon. This will bring up the GNOME preferences panel. Select the “ Network ” tab depending on the interface you wish to change. Click the “ + ” symbol under “ Wired ” interface name to enter the interface options.

Select “ Manual ” from the “ IPV4 ” Method” option and input your static IP Address , Netmask , and Gateway :

Now set the DNS as shown below and click on the “ Apply ” button:

Now confirm the IP address set above:

$ ip a 1: lo: 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: enp0s8: mtu 1500 qdisc fq_codel state UP group default qlen 1000 link/ether 08:00:27:e0:af:44 brd ff:ff:ff:ff:ff:ff inet 192.168.33.10/24 brd 192.168.33.255 scope global enp0s8 valid_lft forever preferred_lft forever inet 192.168.33.20/24 brd 192.168.33.255 scope global secondary enp0s8 valid_lft forever preferred_lft forever inet6 fe80::a00:27ff:fee0:af44/64 scope link valid_lft forever preferred_lft forever

Option 3: Configure Secondary Ip Address on Ubuntu using NMCLI

On an Ubuntu 22.04/20.04/18.04 system, we can also utilize the Network Manager CLI tool to add a secondary IP address.

Run the following to install NMCLI tool:

sudo apt install network-manager

Examine the network connection profiles that are already in place:

$ nmcli device show GENERAL.DEVICE: enp0s8 GENERAL.TYPE: ethernet GENERAL.HWADDR: 02:70:58:87:E8:B4 GENERAL.MTU: 1500 GENERAL.STATE: 10 (unmanaged) GENERAL.CONNECTION: -- GENERAL.CON-PATH: -- WIRED-PROPERTIES.CARRIER: on IP4.ADDRESS[1]: 10.0.2.15/24 IP4.GATEWAY: 10.0.2.2 IP4.ROUTE[1]: dst = 0.0.0.0/0, nh = 10.0.2.2, mt = 100 IP4.ROUTE[2]: dst = 10.0.2.0/24, nh = 0.0.0.0, mt = 0 IP4.ROUTE[3]: dst = 10.0.2.2/32, nh = 0.0.0.0, mt = 100 IP6.ADDRESS[1]: fe80::70:58ff:fe87:e8b4/64 IP6.GATEWAY: -- IP6.ROUTE[1]: dst = fe80::/64, nh = . mt = 256 GENERAL.DEVICE: lo GENERAL.TYPE: loopback GENERAL.HWADDR: 00:00:00:00:00:00 GENERAL.MTU: 65536 GENERAL.STATE: 10 (unmanaged) GENERAL.CONNECTION: -- GENERAL.CON-PATH: -- IP4.ADDRESS[1]: 127.0.0.1/8 IP4.GATEWAY: -- IP6.ADDRESS[1]: ::1/128 IP6.GATEWAY: -- IP6.ROUTE[1]: dst = ::1/128, nh = . mt = 256

After locating the interface for adding a secondary IP address, we proceed to add the IP address as shown below:

$ sudo nmcli con add con-name "Network2" \ type ethernet \ ifname enp0s8 \ ipv4.address 192.168.33.20/24 \ ipv4.gateway 192.168.33.1 \ ipv4.method manual \ connection.autoconnect yes Connection 'Network2' (35d0b0a5-edbc-4aec-93c3-41326c4ee8dd) successfully added.

Now check network creation:

$ nmcli connection show NAME UUID TYPE DEVICE Network2 35d0b0a5-edbc-4aec-93c3-41326c4ee8dd ethernet -- 

The secondary IP is assigned using the +ipv4.addresses option:

sudo nmcli con mod Network2 +ipv4.addresses 192.168.33.20/24

For the IP address to be added, restart the network:

sudo nmcli connection down "Network2" && sudo nmcli connection up "Network2"

Check updated IP addresses:

$ ip a 1: lo: 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: enp0s8: mtu 1500 qdisc fq_codel state UP group default qlen 1000 link/ether 08:00:27:e0:af:44 brd ff:ff:ff:ff:ff:ff inet 10.0.2.15/24 brd 10.0.2.255 scope global dynamic enp0s8 valid_lft forever preferred_lft forever inet 192.168.33.20/24 brd 192.168.33.255 scope global secondary enp0s8 valid_lft forever preferred_lft forever inet6 fe80::a00:27ff:fee0:af44/64 scope link valid_lft forever preferred_lft forever

Conclusion

That concludes the discussion. You’ve added your Ubuntu 20.04/18.04 system a secondary IP address. I hope you found this information to be useful.

Читайте также:  Посмотреть символические ссылки linux

Источник

How do I add an additional IP address to /etc/network/interfaces?

I have an extra IP address available to my server, and so I need to assign it in the interfaces file. At the moment, I’ve got this:

auto lo iface lo inet loopback auto eth0 iface eth0 inet static address aaa.aaa.aaa.aaa netmask 255.255.254.0 gateway bbb.bbb.bbb.bbb dns-nameservers ccc.ccc.ccc.ccc ddd.ddd.ddd.ddd eee.eee.eee.eee dns-search vps-number.com 

What do I add/assign my new IP address ( fff.fff.fff.fff )? And how do I then restart it to accept the new configuration?

3 Answers 3

auto lo iface lo inet loopback auto eth0 iface eth0 inet static address aaa.aaa.aaa.aaa netmask 255.255.254.0 gateway bbb.bbb.bbb.bbb dns-nameservers ccc.ccc.ccc.ccc ddd.ddd.ddd.ddd eee.eee.eee.eee dns-search vps-number.com auto eth0:0 iface eth0:0 inet static address fff.fff.fff.fff netmask 255.255.254.0 

Then you can run sudo ifup eth0:0 to bring it up and sudo ifdown eth0:0 to bring it down.

It may be old (and maybe outdated), but there’s nothing wrong with using this method, since it was not marked ‘deprecated’ and there’s no plan for it being removed in the near future. IMHO this method is cleaner and safer than the others (see the Wilmer’s answer and its comments).

The legacy solution didn’t work for me on Debian Stretch, I ended up with just the second IP. See the iproute2 method, it worked.

You can just have repeated iface stanzas for the same interface. Example from https://wiki.debian.org/NetworkConfiguration#Multiple_IP_addresses_on_one_ :

auto eth0 allow-hotplug eth0 iface eth0 inet static address 192.168.1.42/24 gateway 192.168.1.1 iface eth0 inet static address 192.168.1.43/24 iface eth0 inet static address 192.168.1.44/24 # adding IP addresses from different subnets is also possible iface eth0 inet static address 10.10.10.14/24 

So just the solution above but drop the 😡 suffix, which as Heihachi points out is outdated.

(The ip addr suggestion is the worst. It’s ugly and incomplete, as you’ll have to also add a down variant or ifdown won’t work very cleanly.)

Источник

Оцените статью
Adblock
detector