How to add a NIC in ubuntu 16.04.1 server?

14,253

Solution 1

I am not sure I understand what you wish to do, add a new virtual interface or rename an existing one. In any case, you and Zina are both using obsolete commands, please switch to using iproute2 commands, for exactly the reason pointed out by Zina, i.e., that some features (like detecting virtual interfaces) are not available to ifconfig and equally old utilities like route.

Add a new virtual interface. You do it as follows:

ip link add link usb0 myeth0 type macvlan
dhclient myeth0
ip addr show

where usb0 is my ethernet device (if yours is called differently, please adjust accordingly). In a concrete case (my laptop) yields what follows:

# ip link add link usb0 myeth0 type macvlan
# dhclient -v myeth0
Internet Systems Consortium DHCP Client 4.3.3
Copyright 2004-2015 Internet Systems Consortium.
All rights reserved.
For info, please visit https://www.isc.org/software/dhcp/

Listening on LPF/myeth0/be:6b:e7:1a:f6:50
Sending on   LPF/myeth0/be:6b:e7:1a:f6:50
Sending on   Socket/fallback
DHCPDISCOVER on myeth0 to 255.255.255.255 port 67 interval 3 (xid=0x99d2cf3e)
DHCPREQUEST of 192.168.73.41 on myeth0 to 255.255.255.255 port 67 (xid=0x3ecfd299)
DHCPOFFER of 192.168.73.41 from 192.168.73.1
DHCPACK of 192.168.73.41 from 192.168.73.1
bound to 192.168.73.41 -- renewal in 19941 seconds.
# ip addr show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1
    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: usb0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 9c:eb:e8:2c:32:a5 brd ff:ff:ff:ff:ff:ff
    inet 192.168.73.74/24 brd 192.168.73.255 scope global dynamic usb0
       valid_lft 38149sec preferred_lft 38149sec
    inet6 fe80::7470:2421:63cd:c4c1/64 scope link 
       valid_lft forever preferred_lft forever
3: wlan0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    link/ether 60:57:18:58:03:db brd ff:ff:ff:ff:ff:ff
    inet 192.168.73.45/24 brd 192.168.73.255 scope global dynamic wlan0
       valid_lft 1240sec preferred_lft 1240sec
    inet6 fe80::489e:3c3b:4b1b:6cb4/64 scope link 
       valid_lft forever preferred_lft forever
4: vboxnet0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 0a:00:27:00:00:00 brd ff:ff:ff:ff:ff:ff
    inet 192.168.56.1/24 brd 192.168.56.255 scope global vboxnet0
       valid_lft forever preferred_lft forever
    inet6 fe80::800:27ff:fe00:0/64 scope link 
       valid_lft forever preferred_lft forever
5: myeth0@usb0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UNKNOWN group default qlen 1
    link/ether be:6b:e7:1a:f6:50 brd ff:ff:ff:ff:ff:ff
    inet 192.168.73.41/24 brd 192.168.73.255 scope global myeth0
       valid_lft forever preferred_lft forever
    inet6 fe80::bc6b:e7ff:fe1a:f650/64 scope link 
       valid_lft forever preferred_lft forever

Here, my ethernet card is called usb0, and I have added a new virtual interface onto it of the macvlan type. The command ip addr show displays also the interface MAC address (another possible command to identify the MAC address would be ip link show). The expression myeth0@usb0 shows that this is a virtual interface built on the hardware of the usb0 interface. Also, you may notice that the two interfaces have distinct MAC addresses, which makes it very easy to handle ARP traffic between the two.

Rename your current interface. You may do this with a udev rule, by creating the file /etc/udev/rules.d/70-persistent-net.rules manually, which means that you do not have to worry about any update script overwriting it. Add to it the following line:

SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="01:02:03:04:05:06", ATTR{dev_id}=="0x0", ATTR{type}=="1", NAME="eth0"

where you will have to change the MAC address to the one of your card.

But, if you are like me (i.e., very impatient), then you may wish a more radical solution: I modified the following line in /etc/default/grub to look as follows:

GRUB_CMDLINE_LINUX="net.ifnames=0 biosdevname=0"

This will restore the naming conventions of yore (eth0, usb0, wlan0,...), which I found just good enough.

EDIT:

the answer to 7wp's question below (how do I make this permanent), depends on distro. In debian and derivatives (Ubuntu, Mint, MXLinux,...) you add to the ethernet stanza in /etc/network/interfaces, assuming eth0 to be the name of your ethernet NIC:

auto eth0
iface eth0 inet dhcp
       post-up ip link add link eth0 mynewNICname type macvlan
       post-up dhclient mynewNICname
       post-down dhclient -r mynewNICname

That's all.

Solution 2

I have just tested this on a vSphere 6 VM with Ubuntu Server 16.04 installed:

  • added an additional vNIC
  • ifconfig showed only the first vNIC

    root@zinatest:~# ifconfig
    ens160    Link encap:Ethernet  HWaddr 00:50:56:02:53:53
              inet addr:10.140.253.53  Bcast:10.140.253.255  Mask:255.255.255.0
              inet6 addr: fe80::250:56ff:fe02:5353/64 Scope:Link
              UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
              RX packets:1981 errors:0 dropped:0 overruns:0 frame:0
              TX packets:997 errors:0 dropped:0 overruns:0 carrier:0
              collisions:0 txqueuelen:1000
              RX bytes:172503 (172.5 KB)  TX bytes:601251 (601.2 KB)
    
    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:2 errors:0 dropped:0 overruns:0 frame:0
              TX packets:2 errors:0 dropped:0 overruns:0 carrier:0
              collisions:0 txqueuelen:1
              RX bytes:100 (100.0 B)  TX bytes:100 (100.0 B)
    
    root@zinatest:~#
    
  • see name of added vNIC

    > root@zinatest:~# cat /proc/net/dev
    Inter-|   Receive                                                |  Transmit
     face |bytes    packets errs drop fifo frame compressed multicast|bytes    packets errs drop fifo colls carrier compressed
        lo:   10216     136    0    0    0     0          0         0    10216     136    0    0    0     0       0          0
    ens160:  119990    1406    0    0    0     0          0         0    40101     339    0    0    0     0       0          0
    ens224:   76542    1068    0    0    0     0          0         0     1332      10    0    0    0     0       0          0
    root@zinatest:~#
    
  • edit /etc/network/interface (added dhcp config for ens224

    root@zinatest:~# cat /etc/network/interfaces
    source /etc/network/interfaces.d/*
    
    auto lo
    iface lo inet loopback
    
    auto ens160
    iface ens160 inet dhcp
    
    auto ens224
    iface ens224 inet dhcp
    root@zinatest:~#
    
  • restart network

    root@zinatest:~# service networking restart

  • and new vNIC working

     root@zinatest:~# ifconfig
    
     ens160    Link encap:Ethernet  HWaddr 00:50:56:02:53:53
               inet addr:10.140.253.53  Bcast:10.140.253.255  Mask:255.255.255.0
               inet6 addr: fe80::250:56ff:fe02:5353/64 Scope:Link
               UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
               RX packets:36 errors:0 dropped:0 overruns:0 frame:0
               TX packets:24 errors:0 dropped:0 overruns:0 carrier:0
               collisions:0 txqueuelen:1000
               RX bytes:4124 (4.1 KB)  TX bytes:4002 (4.0 KB)
    
     ens224    Link encap:Ethernet  HWaddr 00:50:56:bd:a5:e2
               inet addr:10.140.253.127  Bcast:10.140.253.255  Mask:255.255.255.0
               inet6 addr: fe80::250:56ff:febd:a5e2/64 Scope:Link
               UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
               RX packets:13 errors:0 dropped:0 overruns:0 frame:0
               TX packets:8 errors:0 dropped:0 overruns:0 carrier:0
               collisions:0 txqueuelen:1000
               RX bytes:1370 (1.3 KB)  TX bytes:1192 (1.1 KB)
    
     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:136 errors:0 dropped:0 overruns:0 frame:0
               TX packets:136 errors:0 dropped:0 overruns:0 carrier:0
               collisions:0 txqueuelen:1
               RX bytes:10216 (10.2 KB)  TX bytes:10216 (10.2 KB)
    
     root@zinatest:~#
    

EDIT:

From ubuntu.help.com - Finding your network interface

 ls /sys/class/net
 # or 
 ip addr

Where the ls output is the cleanest (not so messy as the cat output found above).

Share:
14,253

Related videos on Youtube

user1810087
Author by

user1810087

still alive.

Updated on September 18, 2022

Comments

  • user1810087
    user1810087 over 1 year

    I installed a new network card into my server. Since the naming differs from ethX to (something like) enp3sX i am a bit confused. Using lspci -vv i can see the new network card, but i cannot see the MAC address (in Capabilities) to put into a custom /etc/udev/rules.d/... file. BTW. the /etc/udev/rules.d/70-usb-storage-custom.rules file does not exist.

    So how can i add this new network card into my system? Any suggestion is welcome.

  • Zina
    Zina over 7 years
    Thanks for pointing me to use the new iproute2 commands. The problem I have with your solution is that the OP said he added a new network card (I was the one mention vNIC as I tested on a virtualised server). Your answer is working if the usb0 device is already working (I assume). If you add an additional network card it will not be configured. I tried it your way (with a PCI plugged vNIC) and it did not work - it created a virtual adapter but I got no IP on it (even when I configured the underlying device) and those settings did not survive a reboot.
  • Zina
    Zina over 7 years
    So I would be grateful if you could expand you answer. I assume it worked for you as you were using an USB NIC which might be discovered & configured differently then PCI devices. (had to put two comments as it was a bit too long).
  • MariusMatutiae
    MariusMatutiae over 7 years
    @Zina The new network card is working already, since in the OP it is said it is detected by lspci. The fact that I used usb0 is irrelevant, the commands above would have worked on an lspci card as well. If he cannot install the new card, then he would have had no driver for it, which is not stated in the question. You can get a virtual interface to persist thru reboots by adding the following stanza to /etc/network interfaces: auto myeth0 (newline) iface myeth0 inet dhcp (newline) pre-up ip link add link usb0 myeth0 type macvlan, et voila.
  • MariusMatutiae
    MariusMatutiae over 7 years
    @Zina As for this not working on a VM, I agree: in *any Hypervisor, all interfaces need to be declared to the Hypervisor, one cannot add virtual interfaces from within the VM. But this was not what the OP was about, I think.
  • 7wp
    7wp over 4 years
    How do I make this permanent? After rebooting I lose this new virtual network interface
  • MariusMatutiae
    MariusMatutiae over 4 years
    @7wp See my edit.