Linux create network interface

How can I create a virtual ethernet interface on a machine without a physical adapter?

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?

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.

3 Answers 3

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 
$ ip link show eth10 6: eth10: 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: 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 
$ ifconfig -a eth10 eth10: flags=130 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 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) 
$ 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

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

Читайте также:  Узнать версию suse linux

@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/….

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.

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)

@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.

1. The basics:

# 1. Install the "dummy" Linux kernel module. sudo modprobe dummy # 2. Ensure the "dummy" Linux kernel module is installed. sudo lsmod | grep dummy # 3. Create a virtual (dummy) interface named `eth10`. sudo ip link add eth10 type dummy # 4. Change this new interface's IP address to whatever you like # (10.0.0.1 in this case). sudo ip address change dev eth10 10.0.0.1 # 5. See the newly-created device and the IP address you just # assigned to it. ip address 

And if you ever need to delete this device:

# 6. Delete this `eth10` dummy device you created. sudo ip link delete eth10 type dummy # 7. Ensure 'eth10' is deleted and doesn't show up here now. ip address 

2. More details

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

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 eth10 type dummy command above to create the virtual interface using the dummy kernel module. If you don’t have it, see @slm’s answer.

Читайте также:  Что такое plasma linux

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

You can also take a look at this:

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 dev eth10 10.0.0.1 , in place of sudo ip address change dev eth10 10.0.0.1 (notice addr in place of address ). Or, maybe they ran sudo ip a change dev eth10 10.0.0.1 (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 ip address help (or man ip address ), to see the existence of the ip address change command!:

$ ip address help Usage: ip address IFADDR dev IFNAME [ LIFETIME ] [ CONFFLAG-LIST ] ip address del IFADDR dev IFNAME [mngtmpaddr] ip address [ 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 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

Источник

How to create a virtual network interface in Ubuntu?

I'm testing a network program on Ubuntu. Is there any way to create a virtual network interface in Ubuntu. Something like lo is good, but unfortunately there is bunch of packets on lo with 127.0.0.1 as source and destination IP addrress.

2 Answers 2

USE CASE: To create a persistent VIP address in a server to function as a 'loopback' address

Change to /etc/systemd/network/ directory

user@server:~$ cd /etc/systemd/network/ 

Create two files, 'vip.netdev' and 'vip.network'

user@server:/etc/systemd/network$ touch vip.netdev vip.network 

Show the newly created files

user@server:/etc/systemd/network$ ls vip.netdev vip.network 

Edit the two files with your favorite editor to reflect the detail below

user@server:/etc/systemd/network$ more vip.netdev [NetDev] Name=vip Kind=dummy 
user@server:/etc/systemd/network$ more vip.network [Match] Name=vip [Network] Address=172.16.1.23 (or whatever address you so choose) Mask=255.255.255.255 Broadcast=172.16.1.255 (match the Address x.x.x.255) 

Enable the newly created VIP interface w/out rebooting the server

user@server:/etc/systemd/network$ systemctl restart systemd-networkd 

Show the newly create VIP interface

user@server:/etc/systemd/network$ ifconfig ens33: flags=4163 mtu 1500 inet 172.16.1.16 netmask 255.255.255.0 broadcast 172.16.1.255 inet6 fe80::20c:29ff:fe9b:703f prefixlen 64 scopeid 0x20 ether 00:0c:29:9b:70:3f txqueuelen 1000 (Ethernet) RX packets 244 bytes 32605 (32.6 KB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 653 bytes 51807 (51.8 KB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 lo: flags=73 mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 inet6 ::1 prefixlen 128 scopeid 0x10 loop txqueuelen 1000 (Local Loopback) RX packets 411 bytes 66247 (66.2 KB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 411 bytes 66247 (66.2 KB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 vip: flags=195 mtu 1500 inet 172.16.1.23 netmask 255.255.0.0 broadcast 172.16.255.255 inet6 fe80::d0a7:56ff:fe83:95ff prefixlen 64 scopeid 0x20 ether d2:a7:56:83:95:ff txqueuelen 1000 (Ethernet) RX packets 0 bytes 0 (0.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 7 bytes 490 (490.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 
user@server:/etc/systemd/network$ ip a 1: lo: mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 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: ens33: mtu 1500 qdisc fq_codel state UP group default qlen 1000 link/ether 00:0c:29:9b:70:3f brd ff:ff:ff:ff:ff:ff inet 172.16.1.16/24 brd 172.16.1.255 scope global ens33 valid_lft forever preferred_lft forever inet6 fe80::20c:29ff:fe9b:703f/64 scope link valid_lft forever preferred_lft forever 3: vip: mtu 1500 qdisc noqueue state UNKNOWN group default qlen 1000 link/ether d2:a7:56:83:95:ff brd ff:ff:ff:ff:ff:ff inet 172.16.1.23/16 brd 172.16.255.255 scope global vip valid_lft forever preferred_lft forever inet6 fe80::d0a7:56ff:fe83:95ff/64 scope link valid_lft forever preferred_lft forever 

Источник

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