108

I have a Dell XPS 13 ultrabook which has a wifi nic, but no physical ethernet nic (wlan0, but no eth0). I need to create a virtual adapter for using Vagrant with NFS, but am finding that the typical ifup eth0:1... fails with ignoring unknown interface eth0:1=eth0:1. I also tried creating a virtual interface against wlan0, but received the same result.

How can I create a virtual interface on this machine with no physical interface?

2
  • 1
    First off, make sure that your driver that's being used by wlan0 supports aliasing. That's the other name that virtual interfaces goes by. See how via my A to this Q: unix.stackexchange.com/questions/108396/…. BTW you cannot make an alias against eth0 if you do not have that physical device.
    – slm
    Commented Aug 27, 2014 at 3:03
  • 1
    You can also add additional IPs using the ip command too: xmodulo.com/2013/02/…
    – slm
    Commented Aug 27, 2014 at 3:20

3 Answers 3

146

Setting up a dummy interface

If you want to create network interfaces, but lack a physical NIC to back it, you can use the dummy link type. You can read more about them here: iproute2 Wikipedia page.

Creating eth10

To make this interface you'd first need to make sure that you have the dummy kernel module loaded. You can do this like so:

$ sudo lsmod | grep dummy
$ sudo modprobe dummy
$ sudo lsmod | grep dummy
dummy                  12960  0 

With the driver now loaded you can create what ever dummy network interfaces you like:

$ sudo ip link add eth10 type dummy

NOTE: In older versions of ip you'd do the above like this, appears to have changed along the way. Keeping this here for reference purposes, but based on feedback via comments, the above works now.

$ sudo ip link set name eth10 dev dummy0

And confirm it:

$ ip link show eth10
6: eth10: <BROADCAST,NOARP> mtu 1500 qdisc noop state DOWN mode DEFAULT group default 
    link/ether c6:ad:af:42:80:45 brd ff:ff:ff:ff:ff:ff

Changing the MAC

You can then change the MAC address if you like:

$ sudo ifconfig eth10 hw ether 00:22:22:ff:ff:ff
$ ip link show eth10
6: eth10: <BROADCAST,NOARP> mtu 1500 qdisc noop state DOWN mode DEFAULT group default 
    link/ether 00:22:22:ff:ff:ff brd ff:ff:ff:ff:ff:ff

Creating an alias

You can then create aliases on top of eth10.

$ sudo ip addr add 192.168.100.199/24 brd + dev eth10 label eth10:0

And confirm them like so:

$ ifconfig -a eth10
eth10: flags=130<BROADCAST,NOARP>  mtu 1500
        ether 00:22:22:ff:ff:ff  txqueuelen 0  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

$ ifconfig -a eth10:0
eth10:0: flags=130<BROADCAST,NOARP>  mtu 1500
        inet 192.168.100.199  netmask 255.255.255.0  broadcast 192.168.100.255
        ether 00:22:22:ff:ff:ff  txqueuelen 0  (Ethernet)

Or using ip:

$ ip a | grep -w inet
    inet 127.0.0.1/8 scope host lo
    inet 192.168.1.20/24 brd 192.168.1.255 scope global wlp3s0
    inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0
    inet 192.168.100.199/24 brd 192.168.100.255 scope global eth10:0

Removing all this?

If you want to unwind all this you can run these commands to do so:

$ sudo ip addr del 192.168.100.199/24 brd + dev eth10 label eth10:0
$ sudo ip link delete eth10 type dummy
$ sudo rmmod dummy

References

4
  • 1
    The Wikipedia page says nada about dummy interfaces
    – Daniel
    Commented May 1, 2020 at 16:13
  • I've expounded upon your answer and also shown how to set the IP address for the new virtual interface, here: unix.stackexchange.com/a/593142/114401 Commented Jun 16, 2020 at 1:49
  • 2
    Should i put this setup in rc.local so it persists across reboots? Commented Mar 26, 2021 at 21:56
  • @Ciastopiekarz it depends on your distro. I'd prob. look to make these via network-manager or some other mechanism vs. rc.local but it could live there. For e.g. w/ network-manager - access.redhat.com/documentation/en-us/red_hat_enterprise_linux/….
    – slm
    Commented Mar 27, 2021 at 16:43
40

You can create virtual interfaces using the iproute2 toolkit.

ip link add veth0 type veth peer name veth1

This will create 2 interfaces, veth0 and veth1. Think of them as 2 ends of a pipe. Any traffic sent into veth0 will come out veth1 and vice versa.

If you want the traffic to be routed, you can do:

sysctl -w net.ipv4.conf.veth0.forwarding=1

This will tell the kernel to forward traffic coming from veth0 (so use veth1 for the used endpoint).

Another option is to set up a bridge with veth0 and another interface. Then any traffic coming through the virtual interface will get routed out to the network as if your machine were simply acting as a switch.

There are many other things you can do with this traffic (masquerade it, redirect it, DNAT it, etc), but that depends on what you're trying to accomplish.

To tear it down:

ip link del veth0
4
  • Cool stuff, but not quite what I needed in this case (I really just needed a virtual interface so that a VM could mount an NFS share rather than using VBox file shares)
    – STW
    Commented Aug 28, 2014 at 3:17
  • How to set IP address veth0 in the example ?
    – MURATSPLAT
    Commented Jun 7, 2018 at 16:23
  • How can you make these persistent?
    – Daniel
    Commented May 1, 2020 at 16:16
  • @Daniel Short of creating a systemd service to run the commands, I don't know. After some brief googling, I does not seem apparent that NetworkManager will manage veth interface.
    – phemmer
    Commented May 1, 2020 at 20:18
10

Adding virtual (dummy) interface devices on Linux

...and an introduction to the ip address command and adding or deleting IP addresses and netmasks.

Tested on Ubuntu 18.04 and 22.04.

Quick summary

Short version:

# create a virtual `eth_dummy` interface
sudo ip link add eth_dummy type dummy
# Add some required IPs and netmasks to it
sudo ip address add 10.0.0.1/24 dev eth_dummy
sudo ip address add 192.168.2.1/24 dev eth_dummy
sudo ip address add 200.201.202.203/24 dev eth_dummy
# view it
ip a  
# delete it when all done using the interface and running your tests
sudo ip link delete eth_dummy

A slightly more-detailed version:

# See all network interface devices and their IPs and netmasks
ip address
ip a  # Or (same thing) short version

# Create a virtual (dummy) interface named `eth_dummy`.
sudo ip link add eth_dummy type dummy
# see that it exists now
ip a

# Give it 3 IP addresses with netmask `/24` (255.255.255.0)
sudo ip address add 10.0.0.1/24 dev eth_dummy
sudo ip address add 192.168.2.1/24 dev eth_dummy
sudo ip address add 200.201.202.203/24 dev eth_dummy
# See that it has these 3 IP addresses with netmask `/24` now
ip a

# Delete `200.201.202.203/24` on the `eth_dummy` virtual (dummy) interface
sudo ip address del 200.201.202.203/24 dev eth_dummy
# See that the above IP is gone now
ip a 

# Delete the whole `eth_dummy` interface
sudo ip link delete eth_dummy
# See that it is gone now
ip a

Details

1. The basics

  1. Create a virtual (dummy) adapter/interface device

    # 1. Install the "dummy" socket interface Linux kernel module.
    sudo modprobe dummy
    
    # 2. Ensure the "dummy" Linux kernel module is installed.
    sudo lsmod | grep dummy
    
    # 3. View all existing socket interfaces/adapters, whether WiFi, Ethernet,
    # or virtual (dummy)
    ip address
    ip a  # or (short version of the command above)
    
    # 4. Create a virtual (dummy) interface named `eth_dummy`.
    sudo ip link add eth_dummy type dummy
    
    # 5. View it. You'll now see "eth_dummy" as one of your attached interfaces.
    ip address
    
    # 6. Add an IP address and netmask to this new dummy interface. Use any IP
    # address and netmask you like. Ex: `10.0.0.1/24` in this case. 
    # - Note that the `/24` means that the first (most-significant, or
    #   left-most) 24 bits of the 32-bit netmask will be set to 1's. This
    #   (`/24`) is the same as `255.255.255.0`. See below for details and a
    #   full list of possible netmasks.
    # - See `ip address help` for command syntax help. 
    sudo ip address add 10.0.0.1/24 dev eth_dummy
    
    # 7. See the newly-created device and the IP address you just
    # assigned to it.
    ip address
    

    That's it!

  2. Add IP addresses to your interface:

    You can add as many IP addresses as you want to your network interface.

    In case you are using sockets to receive from (bind to, and receive on) many different IP addresses at once, you can easily add them all to this one dummy interface.

    Here, I am adding two more IP addresses and netmasks as I see fit:

    sudo ip address add 192.168.2.1/24 dev eth_dummy
    sudo ip address add 10.5.4.1/8 dev eth_dummy
    

    ip address now shows the following for my eth_dummy virtual (type dummy) interface. So, I can have my C, C++, Python, etc. sockets bind to IP addresses 10.0.0.1, 192.168.2.1, and 10.5.4.1 on this interface. This is great for airplane travel when you need to test your network and simulated output packets without having a real network switch plugged into your computer (I'm on an airplane now as I write this. Pretty sure that puts me in the "mile high" coding club).

    10: eth_dummy: <BROADCAST,NOARP> mtu 1500 qdisc noop state DOWN group default qlen 1000
        link/ether 4a:e7:bc:8f:2e:2d brd ff:ff:ff:ff:ff:ff
        inet 10.0.0.1/24 scope global eth_dummy
           valid_lft forever preferred_lft forever
        inet 192.168.2.1/24 scope global eth_dummy
           valid_lft forever preferred_lft forever
        inet 10.5.4.1/8 scope global eth_dummy
           valid_lft forever preferred_lft forever
    
  3. Delete or update existing IP addresses on your interface:

    To change an existing IP address on your interface, you must delete and re-add it. The ip address change command is something else entirely--it changes flags/configuration parameters on an existing IP address. So, delete and re-add an IP address on your interface like this:

    # general form
    sudo ip address del <current_ip_address> dev <interface_device_name>
    sudo ip address add <new_ip_address> dev <interface_device_name>
    
    # Example: change `10.5.4.1/8` above to `32.42.52.62/28`
    sudo ip address del 10.5.4.1/8 dev eth_dummy
    sudo ip address add 32.42.52.62/28 dev eth_dummy
    
    # verify the change
    ip address
    
  4. Delete the entire dummy interface device:

    # 1. Delete this `eth_dummy` dummy device you created.
    sudo ip link delete eth_dummy
    
    # 2. Ensure 'eth_dummy' is deleted and doesn't show up here now.
    ip address
    

Done!

To learn more about the ip address add and ip address change type commands, see:

  1. ip address help, as well as:
  2. This answer here: Server Fault: understanding "ip addr change" and "ip addr replace" commands.

2. Non-standard forms

I have observed, as has @Martin on ServerFault, that:

If I execute ip addr change 10.11.12.6/24 dev eth0 or ip addr replace 10.11.12.6/24 dev eth0 then 10.11.12.6 is just added to eth0.

This is also the case with ip addr add 10.11.12.6/24 dev eth0.

So, you can also add a new IP address to your interface using ip address change or ip address replace instead of ip address add. Don't do that, however. That's just confusing. The add command is intended to add a new IP address, and the change command is intended to change config flags in an existing IP address. Note that change does not update nor change the IP address itself. To do that, use del to delete an existing IP address first, and then add to add a new one. Nobody really seems to know what replace is for or how it's different from change. The source code holds the answer I suppose.

So these technically all do the same thing:

# add IP address 10.11.12.6/24
sudo ip address add 10.11.12.6/24 dev eth_dummy     # standard way to add this IP (recommended)
sudo ip address change 10.11.12.6/24 dev eth_dummy  # non-standard way to add this IP
sudo ip address replace 10.11.12.6/24 dev eth_dummy # non-standard way to add this IP

And you can also put the IP address at the end of the command too, but that's also a non-standard form. ip address help shows the IP (as IFADDR, or "interface address") coming before the word dev. So, here are the alternative forms:

sudo ip address add 10.11.12.6/24 dev eth_dummy     # standard way to add this IP (recommended)
sudo ip address add dev eth_dummy 10.11.12.6/24     # non-standard way to add this IP

And you don't have to type out the whole word address. The ip command is smart enough that once it sees some of the address argument, it accepts it, since no other argument to ip in this position begins with a. So, these all work:

sudo ip address add 10.11.12.6/24 dev eth_dummy  # standard form
# "short", non-standard forms:
sudo ip addres add 10.11.12.6/24 dev eth_dummy
sudo ip addre add 10.11.12.6/24 dev eth_dummy
sudo ip addr add 10.11.12.6/24 dev eth_dummy
sudo ip add add 10.11.12.6/24 dev eth_dummy  # yeah that's confusing: `add add...`
sudo ip ad add 10.11.12.6/24 dev eth_dummy
sudo ip a add 10.11.12.6/24 dev eth_dummy

Lastly, don't forget the /number part at the end to specify the netmask. Consider the following:

# This:
sudo ip address add 10.11.12.6 dev eth_dummy
# Defaults to this, which isn't very useful
sudo ip address add 10.11.12.6/32 dev eth_dummy

# So, remember to specify your own useful netmask, such as 
# `/24` (255.255.255.0), instead, like this:
sudo ip address add 10.11.12.6/24 dev eth_dummy

3. Netmasks

Here is a full list of possible netmasks when setting the IP address and netmask as ip/netmask, such as 10.0.0.1/24. The /24 here means that the first (most-significant, or left-most) 24 bits of the 32-bit netmask will be set to 1s. Netmask /24 is the same as 255.255.255.0.

Here is a full list of possible netmasks:

Whole-byte netmasks:

/32 = 255.255.255.255
/24 = 255.255.255.0     # <== most common
/16 = 255.255.0.0
/8  = 255.0.0.0
/0  = 0.0.0.0           # (I don't even know if this is a valid option)

All netmasks:

/32 = 255.255.255.255
/31 = 255.255.255.254
/30 = 255.255.255.252
/29 = 255.255.255.248
/28 = 255.255.255.240
/27 = 255.255.255.224
/26 = 255.255.255.192
/25 = 255.255.255.128
/24 = 255.255.255.0     # <== most common
/23 = 255.255.254.0
/22 = 255.255.252.0
/21 = 255.255.248.0
/20 = 255.255.240.0
/19 = 255.255.224.0
/18 = 255.255.192.0
/17 = 255.255.128.0
/16 = 255.255.0.0
/15 = 255.254.0.0
/14 = 255.252.0.0
/13 = 255.248.0.0
/12 = 255.240.0.0
/11 = 255.224.0.0
/10 = 255.192.0.0
/9 = 255.128.0.0
/8 = 255.0.0.0
/7 = 254.0.0.0
/6 = 252.0.0.0
/5 = 248.0.0.0
/4 = 240.0.0.0
/3 = 224.0.0.0
/2 = 192.0.0.0
/1 = 128.0.0.0
/0 = 0.0.0.0            # (I don't even know if this is a valid option)

4. More details

lsmod shows "the status of modules in the Linux Kernel" (see man lsmod). Try it out! Just type in

lsmod

One of the modules is called dummy. Let's look to see it's there:

$ lsmod | grep dummy
dummy                  16384  0

Yep, it's there. Good. That Linux kernel module must be present for you to be able to run the sudo ip link add eth_dummy type dummy command above to create the virtual interface using the dummy kernel module. If you don't have it, see @slm's answer.

Before you create a new virtual interface, run this to see what IP addresses and interfaces you already have:

ip address

You can also take a look at this:

ifconfig

After you have created your new virtual interface, you will see it in the output of the ip address command above. Note: ifconfig may not show a virtual, dummy device you create, but ip address will.


Wait, but my coworker ran sudo ip addr change 10.0.0.1 dev eth_dummy, in place of sudo ip address change 10.0.0.1 dev eth_dummy (notice addr in place of address). Or, maybe they ran sudo ip a change 10.0.0.1 dev eth_dummy (notice a in place of address). What's up with that!?

Well, this particular command only needs enough of its characters to ensure it knows what you mean. In other words, once you have enough characters in the command for it to know you couldn't possibly mean any other command, it accepts it. Since no other subcommand after ip starts with the letter a, ip a is enough. Therefore, all of the below commands are equivalent:

ip address
ip addres
ip addre
ip addr
ip add
ip ad
ip a

Just be aware of this weird sort of thing when sharing information and looking at the help menus and man pages (shown in my references below). Otherwise, you'll be all sorts of confused, like I was, when no matter how hard you search you can't find the a (as in ip a) or addr (as in ip addr) commands listed anywhere in these page. Just realize both of those are short for address. Ah...now there it is in the help pages!

$ ip help
Usage: ip [ OPTIONS ] OBJECT { COMMAND | help }
       ip [ -force ] -batch filename
where  OBJECT := { link | address | addrlabel | route | rule | neigh | ntable |
                   tunnel | tuntap | maddress | mroute | mrule | monitor | xfrm |
                   netns | l2tp | fou | macsec | tcp_metrics | token | netconf | ila |
                   vrf | sr }
       OPTIONS := { -V[ersion] | -s[tatistics] | -d[etails] | -r[esolve] |
                    -h[uman-readable] | -iec |
                    -f[amily] { inet | inet6 | ipx | dnet | mpls | bridge | link } |
                    -4 | -6 | -I | -D | -B | -0 |
                    -l[oops] { maximum-addr-flush-attempts } | -br[ief] |
                    -o[neline] | -t[imestamp] | -ts[hort] | -b[atch] [filename] |
                    -rc[vbuf] [size] | -n[etns] name | -a[ll] | -c[olor]}
                    ```

And you can run `ip address help` (or `man ip address`), _to see the existence of the `ip address add` and `ip address change` commands!_:
```bash
$ ip address help
Usage: ip address {add|change|replace} IFADDR dev IFNAME [ LIFETIME ]
                                                      [ CONFFLAG-LIST ]
       ip address del IFADDR dev IFNAME [mngtmpaddr]
       ip address {save|flush} [ dev IFNAME ] [ scope SCOPE-ID ]
                            [ to PREFIX ] [ FLAG-LIST ] [ label LABEL ] [up]
       ip address [ show [ dev IFNAME ] [ scope SCOPE-ID ] [ master DEVICE ]
                         [ type TYPE ] [ to PREFIX ] [ FLAG-LIST ]
                         [ label LABEL ] [up] [ vrf NAME ] ]
       ip address {showdump|restore}
IFADDR := PREFIX | ADDR peer PREFIX
          [ broadcast ADDR ] [ anycast ADDR ]
          [ label IFNAME ] [ scope SCOPE-ID ]
SCOPE-ID := [ host | link | global | NUMBER ]
FLAG-LIST := [ FLAG-LIST ] FLAG
FLAG  := [ permanent | dynamic | secondary | primary |
           [-]tentative | [-]deprecated | [-]dadfailed | temporary |
           CONFFLAG-LIST ]
CONFFLAG-LIST := [ CONFFLAG-LIST ] CONFFLAG
CONFFLAG  := [ home | nodad | mngtmpaddr | noprefixroute | autojoin ]
LIFETIME := [ valid_lft LFT ] [ preferred_lft LFT ]
LFT := forever | SECONDS
TYPE := { vlan | veth | vcan | vxcan | dummy | ifb | macvlan | macvtap |
          bridge | bond | ipoib | ip6tnl | ipip | sit | vxlan | lowpan |
          gre | gretap | erspan | ip6gre | ip6gretap | ip6erspan | vti |
          nlmon | can | bond_slave | ipvlan | geneve | bridge_slave |
          hsr | macsec

References

  1. @slm's answer here
  2. ip help
  3. man ip
  4. ip link help
  5. man ip link
  6. ip address help
  7. man ip address
  8. Server Fault: understanding "ip addr change" and "ip addr replace" commands - very useful to understand the difference between ip address add, ip address change, and ip address replace, as well as ip address del.

See also

  1. [my answer] AskUbuntu: How to enable/disable networking (ethernet or wifi) devices, arbitrarily
  2. LinuxHandbook.com: How to Change IP Address in Linux
6
  • How can this all be persistent? So that on reboot ip address with nic interface is not lost. Commented Mar 26, 2021 at 21:54
  • @Ciastopiekarz, add whatever commands you need (to recreate the interface you desire) to your /etc/rc.local startup file so that they get automatically run at each boot. If this file doesn't exist, you can create it, as I mention and link to in my answer here: SuperUser: "How do I use magic sysreq keys on a Mac?". For another example, see my answer here as well: Raspberry Pi Stack Exchange: "Can't get a CIFS network drive to mount on boot". Commented Mar 26, 2021 at 22:54
  • How does a node in the same network know about the new ip? Or in other words, how does the router get informed? Commented Oct 7, 2023 at 19:00
  • @VincentGerris, I don't know, but I think the lower levels of the networking stack handle it automatically to build up the ARP (Address Resolution Protocol) table with the new addresses and share it to each router on the network. This is beyond my knowledge level. I'd have to go study. Commented Jan 5 at 19:30
  • @VincentGerris, are you sure you can even reach this ip from another device?
    – The Fool
    Commented Mar 14 at 21:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .