Virtual ips on linux

Configuring virtual IPs in Linux

When you use a computer that is run on the Linux operating system and you want to configure virtual IP addresses so that the virtual IPs can then be configured as virtual clients in tests or as virtual servers in stubs.

Important: The steps for the task that are to be performed on the operating system are only indicative. The steps might vary depending on the version of the operating system. You might be able to use multiple methods to accomplish the task. You might already be aware of such methods. You can perform the task by following the on-screen instructions or by referring to the documentation provided by the operating system provider.

About this task

  • The IP addresses that you create are legitimate.
  • The IP addresses are available within the same subnet in the network.
  • The IP addresses are blocked for distribution outside of the network.

Procedure

For example, to add the IP alias 10.134.59.4 to the network interface ens192 , run the following command:

# ip address add 10.134.59.4 dev ens192

Note: After you complete runs of the tests and stubs that used the virtual IPs, you must remove the virtual IPs that you configured by running the following command:

For example, to remove the IP alias 10.134.59.4 from the network interface ens192 , run the following command:

# ip address del 10.134.59.4 dev ens192

Results

What to do next

Источник

Create Virtual/Secondary IP addresses on an Interface in Linux

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.

Читайте также:  What is linux ia32

Create Virtual/Secondary IP addresses on an Interface in Linux

You can create virtual/secondary IP addresses on an interface in Linux temporarily or permanently.

Create Temporary Virtual/Secondary IP addresses on an Interface in Linux

To create temporary virtual/secondary IP addresses on an interface in Linux, you can use commands such as ip , ifconfig .

To use ip command to create/add secondary IP addresses to an interface, see the examples below.

In our example server, we have an interface called enp0s8 .

Checking the current IP address of the interface;

 3: enp0s8: mtu 1500 qdisc fq_codel state UP group default qlen 1000 link/ether 08:00:27:c4:23:c9 brd ff:ff:ff:ff:ff:ff inet 192.168.56.108/24 brd 192.168.56.255 scope global noprefixroute enp0s8 valid_lft forever preferred_lft forever inet6 fe80::d524:3777:b321:5ed/64 scope link noprefixroute valid_lft forever preferred_lft forever 

The primary IP address assigned to the interface is 192.168.56.108 .

Assuming we want to temporarily assign a secondary IP address, 192.168.56.109 , to the interface using the ip command;

ip addr add 192.168.56.109/24 br 192.168.56.255 dev enp0s8

The addr and br are abbreviations for address and broadcast respectively.

Confirming the secondary IP address assignment;

 3: enp0s8: mtu 1500 qdisc fq_codel state UP group default qlen 1000 link/ether 08:00:27:c4:23:c9 brd ff:ff:ff:ff:ff:ff inet 192.168.56.108/24 brd 192.168.56.255 scope global dynamic noprefixroute enp0s8 valid_lft 394sec preferred_lft 394sec inet 192.168.56.109/24 brd 192.168.56.255 scope global secondary enp0s8 valid_lft forever preferred_lft forever inet6 fe80::d524:3777:b321:5ed/64 scope link noprefixroute valid_lft forever preferred_lft forever 

If you restart the networking/take the interface up and down or reboot your server, you will loose the assigned IP address.

Create Permanent Virtual/Secondary IP addresses on an Interface in Linux

Create Secondary IP addresses on CentOS and Similar RHEL derivatives.

Create secondary IP addresses using nmcli command

On CentOS and Similar derivatives, you can use simply use the NetworkManager command line tool, nmcli .

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 ( network-manager ).

First confirm the interface connection name;

con is an abbreviation for connection .

NAME UUID TYPE DEVICE enp0s8 e59e1c2f-bda2-4704-9f4a-67e8cce636d9 ethernet enp0s8 Wired connection 1 a7d294d4-05d9-3724-832e-6b80dc288a24 ethernet enp0s3

In this case, we want to add virtual/secondary IP to interface enp0s8 , connection name enp0s8 .

This can be done using nmcli command as follows. Pay attention to plus(+)ipv4.addresses .

nmcli con mod enp0s8 +ipv4.addresses 192.168.56.109/24

mod is an abbreviation for modify .

Читайте также:  Samp server on linux

Take down and bring up the interface. I Assume you are directly logged in via console and not via ssh.

nmcli con down enp0s8 && nmcli con up enp0s8

Confirm the secondary IP address assignment.

 3: enp0s8: mtu 1500 qdisc fq_codel state UP group default qlen 1000 link/ether 08:00:27:c4:23:c9 brd ff:ff:ff:ff:ff:ff inet 192.168.56.108/24 brd 192.168.56.255 scope global noprefixroute enp0s8 valid_lft forever preferred_lft forever inet 192.168.56.109/24 brd 192.168.56.255 scope global secondary noprefixroute enp0s8 valid_lft forever preferred_lft forever inet6 fe80::d524:3777:b321:5ed/64 scope link noprefixroute valid_lft forever preferred_lft forever 

To remove the secondary IP address using nmcli command, just use minus(-)ipv4.addresses .

nmcli con mod enp0s8 -ipv4.addresses 192.168.56.109/24
nmcli con down enp0s8 && nmcli con up enp0s8

Create Secondary IP addresses using nmtui (Network Manager GUI)

nmtui is available if you have installed Network manager package.

Launch nmtui from the terminal (you can use tab key to navigate through the settings);

Select Edit a connection and click Ok.

Create Virtual/Secondary IP addresses on an Interface in Linux

Select the Interface to edit, which in this example is enp0s8 and click Edit;

Create Virtual/Secondary IP addresses on an Interface in Linux

Scroll down to IPV4 configuration and click Add and enter your IP address.

add ip address

Next, click Ok at the bottom > Back > Activate a connection > Ok.

Select and deactivate the interface.

select n deactivate interface

Select the interface and activate it again.

select n activate interface

Select Back > Quit.

Confirm the IP address assignment.

 3: enp0s8: mtu 1500 qdisc fq_codel state UP group default qlen 1000 link/ether 08:00:27:c4:23:c9 brd ff:ff:ff:ff:ff:ff inet 192.168.56.108/24 brd 192.168.56.255 scope global noprefixroute enp0s8 valid_lft forever preferred_lft forever inet 192.168.56.109/24 brd 192.168.56.255 scope global secondary noprefixroute enp0s8 valid_lft forever preferred_lft forever inet6 fe80::d524:3777:b321:5ed/64 scope link noprefixroute valid_lft forever preferred_lft forever 

You can similarly remove this using nmcli command or right from the nmtui tool.

Create Secondary IP address on Ubuntu 18.04/Ubuntu 20.04 systems

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;

Check the current IP address for the interface;

 3: enp0s8: mtu 1500 qdisc fq_codel state UP group default qlen 1000 link/ether 08:00:27:56:39:94 brd ff:ff:ff:ff:ff:ff inet 192.168.59.14/24 brd 192.168.59.255 scope global enp0s8 valid_lft forever preferred_lft forever inet6 fe80::a00:27ff:fe56:3994/64 scope link valid_lft forever preferred_lft forever 

The current netplan configuration for my interfaces;

cat /etc/netplan/00-installer-config.yaml
 network: ethernets: enp0s3: dhcp4: true enp0s8: dhcp4: no addresses: [192.168.59.14/24] routes: - to: 0.0.0.0/0 via: 192.168.59.1 metric: 101 nameservers: addresses: [8.8.8.8] version: 2 

To add a secondary IP address to the interface, edit the configuration file

cp /etc/netplan/00-installer-config.yaml
vim /etc/netplan/00-installer-config.yaml

Such that it may look like;

addresses: [192.168.59.14/24, 192.168.59.15/24]

The config now looks like;

 network: ethernets: enp0s3: dhcp4: true enp0s8: dhcp4: no addresses: [192.168.59.14/24, 192.168.59.15/24] routes: - to: 0.0.0.0/0 via: 192.168.59.1 metric: 101 nameservers: addresses: [8.8.8.8] version: 2 

Apply the configuration changes;

Читайте также:  Linux mint пароль по умолчанию

The confirm the IP address assignment

Create Secondary IP address on Debian systems

Similarly, update the interfaces as follows, to add the secondary IP address.

Sample interface configurations;

 source /etc/network/interfaces.d/* # The loopback network interface auto lo iface lo inet loopback # The primary network interface allow-hotplug enp0s3 iface enp0s3 inet dhcp auto enp0s8 iface enp0s8 inet static address 192.168.58.22 netmask 255.255.255.0 gateway 192.168.58.1 broadcast 192.168.58.255 dns-nameservers 8.8.8.8 

To add a secondary IP address;

update the config as follows

 source /etc/network/interfaces.d/* # The loopback network interface auto lo iface lo inet loopback # The primary network interface allow-hotplug enp0s3 iface enp0s3 inet dhcp auto enp0s8 iface enp0s8 inet static address 192.168.58.22 netmask 255.255.255.0 gateway 192.168.58.1 broadcast 192.168.58.255 dns-nameservers 8.8.8.8 auto enp0s8:0 iface enp0s8:0 inet static address 192.168.58.23 netmask 255.255.255.0 
auto enp0s8:0 iface enp0s8:0 inet static address 192.168.58.23 netmask 255.255.255.0

Save and exit the config and restart the networking;

systemctl restart networking
 3: enp0s8: mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether 08:00:27:2b:b4:61 brd ff:ff:ff:ff:ff:ff inet 192.168.58.22/24 brd 192.168.58.255 scope global enp0s8 valid_lft forever preferred_lft forever inet 192.168.58.23/24 brd 192.168.58.255 scope global secondary enp0s8:0 valid_lft forever preferred_lft forever inet6 fe80::a00:27ff:fe2b:b461/64 scope link valid_lft forever preferred_lft forever 

And that is it on how to create virtual/secondary IP addresses on an Interface in Linux.

Other Tutorials

SUPPORT US VIA A VIRTUAL CUP OF COFFEE

We’re passionate about sharing our knowledge and experiences with you through our blog. If you appreciate our efforts, consider buying us a virtual coffee. Your support keeps us motivated and enables us to continually improve, ensuring that we can provide you with the best content possible. Thank you for being a coffee-fueled champion of our work!

Источник

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