Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to configure multiple IP for a single network card in Ubuntu system

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how to configure multiple IP for a single network card in the Ubuntu system". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to configure multiple IP for a single network card in the Ubuntu system".

Temporarily add an IP address

First, let's find the IP address of the network card. In my Ubuntu 15.10 server version, I only used one network card.

Run the following command to find the IP address:

The code is as follows:

Sudo ip addr

Sample output:

1: lo: mtu 65536 qdisc noqueue state UNKNOWN group default

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: enp0s3: mtu 1500 qdisc pfifo_fast state UP group default qlen 1000

Link/ether 08:00:27:2a:03:4b brd ff:ff:ff:ff:ff:ff

Inet 192.168.1.103/24 brd 192.168.1.255 scope global enp0s3

Valid_lft forever preferred_lft forever

Inet6 fe80::a00:27ff:fe2a:34e/64 scope link

Valid_lft forever preferred_lft forever

Or

The code is as follows:

Sudo ifconfig

Sample output:

Enp0s3 Link encap:Ethernet HWaddr 08:00:27:2a:03:4b

Inet addr:192.168.1.103 Bcast:192.168.1.255 Mask:255.255.255.0

Inet6 addr: fe80::a00:27ff:fe2a:34e/64 Scope:Link

UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1

RX packets:186 errors:0 dropped:0 overruns:0 frame:0

TX packets:70 errors:0 dropped:0 overruns:0 carrier:0

Collisions:0 txqueuelen:1000

RX bytes:21872 (21.8KB) TX bytes:9666 (9.6KB)

Lo Link encap:Local Loopback

Inet addr:127.0.0.1 Mask:255.0.0.0

Inet6 addr: 1/128 Scope:Host

UP LOOPBACK RUNNING MTU:65536 Metric:1

RX packets:217 errors:0 dropped:0 overruns:0 frame:0

TX packets:217 errors:0 dropped:0 overruns:0 carrier:0

Collisions:0 txqueuelen:0

RX bytes:38793 (38.7 KB) TX bytes:38793 (38.7 KB)

As you can see in the output above, my network card name is enp0s3 and its IP address is 192.168.1.103.

Now let's add a new IP address to the network card, for example, 192.168.1.104.

Open your terminal and run the following command to add additional IP.

The code is as follows:

Sudo ip addr add 192.168.1.104/24 dev enp0s3

Use the command to check if the new IP is enabled:

The code is as follows:

Sudo ip address show enp0s3

Sample output:

The code is as follows:

2: enp0s3: mtu 1500 qdisc pfifo_fast state UP group default qlen 1000

Link/ether 08:00:27:2a:03:4e brd ff:ff:ff:ff:ff:ff

Inet 192.168.1.103/24 brd 192.168.1.255 scope global enp0s3

Valid_lft forever preferred_lft forever

Inet 192.168.1.104/24 scope global secondary enp0s3

Valid_lft forever preferred_lft forever

Inet6 fe80::a00:27ff:fe2a:34e/64 scope link

Valid_lft forever preferred_lft forever

Similarly, you can add as many IP addresses as you want.

Let's ping this IP address and verify it.

The code is as follows:

Sudo ping 192.168.1.104

Sample output

PING 192.168.1.104 (192.168.1.104) 56 (84) bytes of data.

64 bytes from 192.168.1.104: icmp_seq=1 ttl=64 time=0.901 ms

64 bytes from 192.168.1.104: icmp_seq=2 ttl=64 time=0.571 ms

64 bytes from 192.168.1.104: icmp_seq=3 ttl=64 time=0.521 ms

64 bytes from 192.168.1.104: icmp_seq=4 ttl=64 time=0.524 ms

Great, it works!

To delete an IP, simply run:

The code is as follows:

Sudo ip addr del 192.168.1.104/24 dev enp0s3

Check again to see if IP has been deleted.

The code is as follows:

Sudo ip address show enp0s3

Sample output:

2: enp0s3: mtu 1500 qdisc pfifo_fast state UP group default qlen 1000

Link/ether 08:00:27:2a:03:4e brd ff:ff:ff:ff:ff:ff

Inet 192.168.1.103/24 brd 192.168.1.255 scope global enp0s3

Valid_lft forever preferred_lft forever

Inet6 fe80::a00:27ff:fe2a:34e/64 scope link

Valid_lft forever preferred_lft forever

You can see that it is gone!

As you know, these settings will fail after rebooting the system. So how do you set it to last forever? It's simple, too.

Add permanent IP address

The network card configuration file for the Ubuntu system is / etc/network/interfaces.

Let's take a look at the details of the above document.

The code is as follows:

Sudo cat / etc/network/interfaces

Sample output:

# This file describes the network interfaces available on your system

# and how to activate them. For more information, see interfaces (5).

Source / etc/network/interfaces.d/*

# The loopback network interface

Auto lo

Iface lo inet loopback

# The primary network interface

Auto enp0s3

Iface enp0s3 inet dhcp

As you can see in the output above, the network card has DHCP enabled.

Now, let's assign an additional address, such as 192.168.1.104Universe 24.

Edit / etc/network/interfaces:

The code is as follows:

Sudo nano / etc/network/interfaces

Add an additional IP address below.

The code is as follows:

# This file describes the network interfaces available on your system

# and how to activate them. For more information, see interfaces (5).

Source / etc/network/interfaces.d/*

# The loopback network interface

Auto lo

Iface lo inet loopback

# The primary network interface

Auto enp0s3

Iface enp0s3 inet dhcp

Iface enp0s3 inet static

Address 192.168.1.104/24

Save and close the file.

Run the following command to make the changes take effect without a restart.

The code is as follows:

Sudo ifdown enp0s3 & & sudo ifup enp0s3

Sample output:

Killed old client process

Internet Systems Consortium DHCP Client 4.3.1

Copyright 2004-2014 Internet Systems Consortium.

All rights reserved.

For info, please visit https://www.isc.org/software/dhcp/

Listening on LPF/enp0s3/08:00:27:2a:03:4e

Sending on LPF/enp0s3/08:00:27:2a:03:4e

Sending on Socket/fallback

DHCPRELEASE on enp0s3 to 192.168.1.1 port 67 (xid=0x225f35)

Internet Systems Consortium DHCP Client 4.3.1

Copyright 2004-2014 Internet Systems Consortium.

All rights reserved.

For info, please visit https://www.isc.org/software/dhcp/

Listening on LPF/enp0s3/08:00:27:2a:03:4e

Sending on LPF/enp0s3/08:00:27:2a:03:4e

Sending on Socket/fallback

DHCPDISCOVER on enp0s3 to 255.255.255.255 port 67 interval 3 (xid=0xdfb94764)

DHCPREQUEST of 192.168.1.103 on enp0s3 to 255.255.255.255 port 67 (xid=0x6447b9df)

DHCPOFFER of 192.168.1.103 from 192.168.1.1

DHCPACK of 192.168.1.103 from 192.168.1.1

Bound to 192.168.1.103-renewal in 35146 seconds.

Note: if you connect to the server remotely, it is important to put the above two commands on one line, because the first command will disconnect you. In this way, you can keep your ssh session.

Now, let's check if a new IP has been added with the following command:

Sudo ip address show enp0s3

Sample output:

2: enp0s3: mtu 1500 qdisc pfifo_fast state UP group default qlen 1000

Link/ether 08:00:27:2a:03:4e brd ff:ff:ff:ff:ff:ff

Inet 192.168.1.103/24 brd 192.168.1.255 scope global enp0s3

Valid_lft forever preferred_lft forever

Inet 192.168.1.104/24 brd 192.168.1.255 scope global secondary enp0s3

Valid_lft forever preferred_lft forever

Inet6 fe80::a00:27ff:fe2a:34e/64 scope link

Valid_lft forever preferred_lft forever

Fine! We have added additional IP.

Verify the ping IP address again.

The code is as follows:

Sudo ping 192.168.1.104

Sample output:

PING 192.168.1.104 (192.168.1.104) 56 (84) bytes of data.

64 bytes from 192.168.1.104: icmp_seq=1 ttl=64 time=0.137 ms

64 bytes from 192.168.1.104: icmp_seq=2 ttl=64 time=0.050 ms

64 bytes from 192.168.1.104: icmp_seq=3 ttl=64 time=0.054 ms

64 bytes from 192.168.1.104: icmp_seq=4 ttl=64 time=0.067 ms

That's great! It works properly. okay.

Thank you for your reading, the above is the content of "how to configure multiple IP for a single network card in the Ubuntu system". After the study of this article, I believe you have a deeper understanding of how to configure multiple IP for a single network card in the Ubuntu system, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report