LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Networking
User Name
Password
Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.

Notices


Reply
  Search this Thread
Old 07-25-2023, 03:32 AM   #1
Jason.nix
Member
 
Registered: Feb 2023
Posts: 567

Rep: Reputation: 10
Post How to set multiple IP addresses on a NIC?


Hello,
To set the multiple IP addresses on a NIC, I must create a virtual NIC?
For example, If my real NIC name is enp0s3, then I must:
Code:
# touch /etc/network/interfaces.d/enp0s3:0
# nano /etc/network/interfaces.d/enp0s3:0

auto enp0s3:0
iface enp0s3:0 inet static
address 10.0.5.20
netmask 255.255.255.0

#
# systemctl restart networking
Is it mandatory? Can I add the above lines to the real NIC file (/etc/network/interfaces.d/enp0s3)?


Thank you.
 
Old 07-25-2023, 07:26 AM   #2
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,453

Rep: Reputation: 448Reputation: 448Reputation: 448Reputation: 448Reputation: 448
Hi

You normally add ip addresses in a file in /etc/network/interfaces.d - with the name of the device.

For example:

Code:
auto eth0
iface eth0 inet static
address 10.0.5.20
netmask 255.255.255.0
gateway 10.0.5.1

auto eth0:1
iface eth0:1 inet static
address 10.0.5.21
netmask 255.255.255.0
Here the name is eth0. I think :0 is not really needed - it's the same as no suffix. You need to add :1 for the second one, :2 for the third and so on.

Add gateway to the first one - outgoing traffic will come from this one.
 
Old 07-25-2023, 07:36 AM   #3
Jason.nix
Member
 
Registered: Feb 2023
Posts: 567

Original Poster
Rep: Reputation: 10
Quote:
Originally Posted by Guttorm View Post
Hi

You normally add ip addresses in a file in /etc/network/interfaces.d - with the name of the device.

For example:

Code:
auto eth0
iface eth0 inet static
address 10.0.5.20
netmask 255.255.255.0
gateway 10.0.5.1

auto eth0:1
iface eth0:1 inet static
address 10.0.5.21
netmask 255.255.255.0
Here the name is eth0. I think :0 is not really needed - it's the same as no suffix. You need to add :1 for the second one, :2 for the third and so on.

Add gateway to the first one - outgoing traffic will come from this one.
Hello,
Thank you so much for your reply.
What I have done is wrong?
 
Old 07-25-2023, 07:42 AM   #4
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,453

Rep: Reputation: 448Reputation: 448Reputation: 448Reputation: 448Reputation: 448
Hi

It looks like you set it up in a different file. I usually just edit the file and append. In your case, it's /etc/network/interfaces.d/enp0s3

Try to add this:

Code:
auto enp0s3:1
iface enp0s3:1 inet static
address 10.0.5.20
netmask 255.255.255.0
I think using :0 will just overwrite the other one.
 
1 members found this post helpful.
Old 07-26-2023, 03:06 PM   #5
Jason.nix
Member
 
Registered: Feb 2023
Posts: 567

Original Poster
Rep: Reputation: 10
Quote:
Originally Posted by Guttorm View Post
Hi

It looks like you set it up in a different file. I usually just edit the file and append. In your case, it's /etc/network/interfaces.d/enp0s3

Try to add this:

Code:
auto enp0s3:1
iface enp0s3:1 inet static
address 10.0.5.20
netmask 255.255.255.0
I think using :0 will just overwrite the other one.
Hello,
Thank you so much for your reply.
If the NIC receives the IP from the DHCP service, then there is no file under the /etc/network/interfaces.d/ directory and just a /etc/network/interfaces file.
I have two questions:

1- Are you sure enp0s3:0 is wrong?

2- Is it possible for enp0s3 to get the address from DHCP but enp0s3:1 manually?
 
Old 07-26-2023, 03:19 PM   #6
jefro
Moderator
 
Registered: Mar 2008
Posts: 22,008

Rep: Reputation: 3629Reputation: 3629Reputation: 3629Reputation: 3629Reputation: 3629Reputation: 3629Reputation: 3629Reputation: 3629Reputation: 3629Reputation: 3629Reputation: 3629
https://wiki.debian.org/NetworkConfi..._one_Interface

Those changes have to be followed by stopping networking and downing and up.
 
Old 07-26-2023, 03:23 PM   #7
Jason.nix
Member
 
Registered: Feb 2023
Posts: 567

Original Poster
Rep: Reputation: 10
Quote:
Originally Posted by jefro View Post
https://wiki.debian.org/NetworkConfi..._one_Interface

Those changes have to be followed by stopping networking and downing and up.
Hello,
Thanks.
I restarted the networking service.
So, the enp0s3:0 is correct. Am I right?
Is it possible for enp0s3 to get the address from DHCP but enp0s3:0 manually?
 
Old 07-28-2023, 01:58 PM   #8
Jason.nix
Member
 
Registered: Feb 2023
Posts: 567

Original Poster
Rep: Reputation: 10
Hello all,
Any idea?
Is it difficult to set multiple IP addresses on a NIC in Linux? In Windows OS, this is easily done: https://tunecomp.net/windows-10-multiple-ip-addresses/

Last edited by Jason.nix; 07-28-2023 at 01:59 PM.
 
Old 07-28-2023, 02:17 PM   #9
Ser Olmy
Senior Member
 
Registered: Jan 2012
Distribution: Slackware
Posts: 3,345

Rep: Reputation: Disabled
Quote:
Originally Posted by Jason.nix View Post
Is it difficult to set multiple IP addresses on a NIC in Linux?
No, it's dead easy:
Code:
ip addr add <address>/<CIDR> dev <interface>
The problems you're having are related to your distribution, not the Linux kernel. It seems your network scripts require that you use aliases, an old and deprecated mechanism where you would assign additional addresses to an interface alias called <interface>:<alias>.

This is rather confusing, as it looks like you're creating something akin to a virtual interface (like you stated in your other thread), but that's not at all the case.

You should check the documentation for your distribution to see if the scripts support adding multiple addresses to the same interface using a more recent (non-deprecated) mechanism.
 
1 members found this post helpful.
Old 07-28-2023, 04:20 PM   #10
jefro
Moderator
 
Registered: Mar 2008
Posts: 22,008

Rep: Reputation: 3629Reputation: 3629Reputation: 3629Reputation: 3629Reputation: 3629Reputation: 3629Reputation: 3629Reputation: 3629Reputation: 3629Reputation: 3629Reputation: 3629
Do you have gui on linux?
 
Old 07-28-2023, 04:47 PM   #11
Jason.nix
Member
 
Registered: Feb 2023
Posts: 567

Original Poster
Rep: Reputation: 10
Quote:
Originally Posted by Ser Olmy View Post
No, it's dead easy:
Code:
ip addr add <address>/<CIDR> dev <interface>
The problems you're having are related to your distribution, not the Linux kernel. It seems your network scripts require that you use aliases, an old and deprecated mechanism where you would assign additional addresses to an interface alias called <interface>:<alias>.

This is rather confusing, as it looks like you're creating something akin to a virtual interface (like you stated in your other thread), but that's not at all the case.

You should check the documentation for your distribution to see if the scripts support adding multiple addresses to the same interface using a more recent (non-deprecated) mechanism.
Hello,
Thank you so much for your reply.
The Debian documentation says this: https://wiki.debian.org/NetworkConfi..._one_Interface

Does the following command add the IP address permanently and is it not deleted by restarting the system?
Code:
# ip addr add <address>/<CIDR> dev <interface>
In which file does this command write the settings?
How do I delete the added IP?

Last edited by Jason.nix; 07-28-2023 at 04:48 PM.
 
Old 07-28-2023, 09:38 PM   #12
JJJCR
Senior Member
 
Registered: Apr 2010
Posts: 2,162

Rep: Reputation: 449Reputation: 449Reputation: 449Reputation: 449Reputation: 449
to create a sub interface:

Quote:
Create subinterface on Ubuntu – Debian permanently. Add eth0:0 interface to /etc/network/interfaces

root@ubuntu:~# vi /etc/network/interfaces
# interfaces(5) file used by ifup(8) and ifdown(8)
auto lo
iface lo inet loopback
auto eth0
iface eth0 inet static
address 192.168.0.106
netmask 255.255.255.0
network 192.168.0.0
broadcast 192.168.0.255
gateway 192.168.0.1
auto eth0:0
iface eth0:0 inet static
address 192.168.0.107
netmask 255.255.255.0
root@ubuntu:~#
Link: https://github.com/muhamadfaiz/Creat...face-in-Debian

To set DHCP, just specify the config on the interface : https://wiki.debian.org/NetworkConfiguration

Quote:
auto eth0
allow-hotplug eth0
iface eth0 inet dhcp
If you need to delete it then just remove the config of the sub interface.

or try: ifconfig eth0:0 down
 
Old 07-28-2023, 11:59 PM   #13
Jason.nix
Member
 
Registered: Feb 2023
Posts: 567

Original Poster
Rep: Reputation: 10
Quote:
Originally Posted by JJJCR View Post
to create a sub interface:



Link: https://github.com/muhamadfaiz/Creat...face-in-Debian

To set DHCP, just specify the config on the interface : https://wiki.debian.org/NetworkConfiguration



If you need to delete it then just remove the config of the sub interface.

or try: ifconfig eth0:0 down
Hello,
Thanks, but as others have said, this is a Legacy Method and as the Debian documentation said, there is no need to gateway and netmask.

Last edited by Jason.nix; 07-29-2023 at 12:00 AM.
 
Old 07-29-2023, 06:36 AM   #14
Jason.nix
Member
 
Registered: Feb 2023
Posts: 567

Original Poster
Rep: Reputation: 10
Hello all,
The IP address that I added to my NIC via ip addr add <address>/<CIDR> dev <interface> command is not permanent:
Code:
# ip addr add 10.0.5.15/24 dev enp0s3
#
# ip addr show enp0s3
2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 08:00:27:7b:8f:51 brd ff:ff:ff:ff:ff:ff
    inet 10.0.4.15/8 brd 10.255.255.255 scope global enp0s3:1
       valid_lft forever preferred_lft forever
    inet 10.0.5.15/24 scope global enp0s3
       valid_lft forever preferred_lft forever
    inet6 fe80::a00:27ff:fe7b:8f51/64 scope link 
       valid_lft forever preferred_lft forever
But, when I restarted my networking service:
Code:
# systemctl restart networking
#
# ip addr show enp0s3
2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 08:00:27:7b:8f:51 brd ff:ff:ff:ff:ff:ff
    inet 10.0.4.15/8 brd 10.255.255.255 scope global enp0s3:1
       valid_lft forever preferred_lft forever
    inet6 fe80::a00:27ff:fe7b:8f51/64 scope link 
       valid_lft forever preferred_lft forever
How do I make it permanent?


Thank you.
 
Old 07-30-2023, 08:26 PM   #15
JJJCR
Senior Member
 
Registered: Apr 2010
Posts: 2,162

Rep: Reputation: 449Reputation: 449Reputation: 449Reputation: 449Reputation: 449
try:

Quote:
sudo ip route add default via 192.168.88.10 dev enp0s3
then restart network service

and try: ip r | grep default

see how it goes

or try exploring "netplan" see if it works on your distro.

Link: https://www.techrepublic.com/article...en-deprecated/

Last edited by JJJCR; 07-30-2023 at 08:50 PM. Reason: edit
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Trying to be figure out the total Usable IP Addresses using whatmask and adding multiple IP addresses in a text file with a script Shakespearr Linux - Newbie 7 11-02-2018 05:28 PM
multiple IP addresses, 1 NIC: debian woody mix_master_mike Linux - Networking 1 01-11-2004 03:19 AM
Binding multiple IP addresses to a nic card Natalie Linux - Networking 9 08-12-2003 08:27 PM
Multiple addresses for a single NIC grub Programming 1 08-04-2003 02:24 AM
Multiple IP addresses on one NIC hrr Linux - Networking 2 03-07-2003 12:04 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Networking

All times are GMT -5. The time now is 12:20 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration