Command
Usage: ip link add [link DEV] [ name ] NAME
[ txqueuelen PACKETS ]
[ address LLADDR ]
[ broadcast LLADDR ]
[ mtu MTU ]
type TYPE [ ARGS ]
ip link delete DEV type TYPE [ ARGS ]
ip link set { dev DEVICE | group DEVGROUP } [ { up | down } ]
[ arp { on | off } ]
[ dynamic { on | off } ]
[ multicast { on | off } ]
[ allmulticast { on | off } ]
[ promisc { on | off } ]
[ trailers { on | off } ]
[ txqueuelen PACKETS ]
[ name NEWNAME ]
[ address LLADDR ]
[ broadcast LLADDR ]
[ mtu MTU ]
[ netns PID ]
[ netns NAME ]
[ alias NAME ]
[ vf NUM [ mac LLADDR ]
[ vlan VLANID [ qos VLAN-QOS ] ]
[ rate TXRATE ] ]
[ master DEVICE ]
[ nomaster ]
ip link show [ DEVICE | group GROUP ]
TYPE := { vlan | veth | vcan | dummy | ifb | macvlan | can | bridge }
|
Virtual ethernet interface (veth) pair
sudo ip link add <veth-name-1> type veth peer name <veth-name-2>
|
VLAN interface (802.1Q)
ip link add link <dev> name <name> type vlan id <ID>
|
Demo
First case: Two PCs (network namespaces) in the same vlan)
sudo ip link add eth1 type veth peer name eth2
sudo ip netns add pc1
sudo ip netns add pc2
sudo ip link set eth1 netns pc1
sudo ip link set eth2 netns pc2
sudo ip netns exec pc1 ip link add link eth1 name eth1.10 type vlan id 10
sudo ip netns exec pc2 ip link add link eth2 name eth2.10 type vlan id 10
sudo ip netns exec pc1 ip addr add 10.0.0.1/24 dev eth1.10
sudo ip netns exec pc2 ip addr add 10.0.0.2/24 dev eth2.10
sudo ip netns exec pc1 ip link set eth1 up
sudo ip netns exec pc2 ip link set eth2 up
sudo ip netns exec pc1 ip link set eth1.10 up
sudo ip netns exec pc2 ip link set eth2.10 up
sudo ip netns exec pc1 ping 10.0.0.2
sudo ip netns exec pc2 ping 10.0.0.1
|
Result:
vdkmai@Ubuntu32bit:~$ sudo ip netns exec pc1 ping 10.0.0.2
PING 10.0.0.2 (10.0.0.2) 56(84) bytes of data.
64 bytes from 10.0.0.2: icmp_req=1 ttl=64 time=0.000 ms
vdkmai@Ubuntu32bit:~$ sudo ip netns exec pc2 ping 10.0.0.1
PING 10.0.0.1 (10.0.0.1) 56(84) bytes of data.
64 bytes from 10.0.0.1: icmp_req=1 ttl=64 time=0.029 ms
|
Second case: Two PCs (network namespaces) in the different vlans)
sudo ip netns del pc1
sudo ip netns del pc2
sudo ip link add eth1 type veth peer name eth2
sudo ip netns add pc1
sudo ip netns add pc2
sudo ip link set eth1 netns pc1
sudo ip link set eth2 netns pc2
sudo ip netns exec pc1 ip link add link eth1 name eth1.10 type vlan id 10
sudo ip netns exec pc2 ip link add link eth2 name eth2.10 type vlan id 20
sudo ip netns exec pc1 ip addr add 10.0.0.1/24 dev eth1.10
sudo ip netns exec pc2 ip addr add 10.0.0.2/24 dev eth2.10
sudo ip netns exec pc1 ip link set eth1 up
sudo ip netns exec pc2 ip link set eth2 up
sudo ip netns exec pc1 ip link set eth1.10 up
sudo ip netns exec pc2 ip link set eth2.10 up
sudo ip netns exec pc1 ping 10.0.0.2
sudo ip netns exec pc2 ping 10.0.0.1
|
Result
vdkmai@Ubuntu32bit:~$ sudo ip netns exec pc1 ping 10.0.0.2
PING 10.0.0.2 (10.0.0.2) 56(84) bytes of data.
^C
--- 10.0.0.2 ping statistics ---
6 packets transmitted, 0 received, 100% packet loss, time 5009ms
vdkmai@Ubuntu32bit:~$ sudo ip netns exec pc2 ping 10.0.0.1
PING 10.0.0.1 (10.0.0.1) 56(84) bytes of data.
^C
--- 10.0.0.1 ping statistics ---
5 packets transmitted, 0 received, 100% packet loss, time 3999ms
|
802.1q VLAN is supported by Linux kernel driver.
Check whether the Linux kernel driver (module) called 8021q is loaded:
# lsmod | grep 8021q
If the module is not loaded automatically, load it manually with the following modprobe command:
# modprobe 8021q
Macvlan interface
Macvlan allows a single physical interface to have multiple mac and ip addresses using macvlan sub-interfaces. This is different from creating sub-interface on a physical interface using vlan. With vlan sub-interfaces, each sub-interface belongs to a different L2 domain using vlan and all sub-interfaces have same mac address. With macvlan, each sub-interface will get unique mac and ip address and will be exposed directly in underlay network.
sudo ip link add <interface-name> link eth0 type macvlan mode <bridge/..>
|
Topology
This kind of interface is similar with the Bridge type network interface in Virtualbox or VMWare.
Configuration (Note: it may not work on a Virtual machine)
sudo ip link add eth1 link eth0 type macvlan mode bridge
ip link list
sudo ip netns add pc1
sudo ip link set eth1 netns pc1
sudo ip netns exec pc1 ip link set eth1 up
sudo ip netns exec pc1 dhclient -nw -v eth1
sudo ip netns exec pc1 ping 8.8.8.8
sudo ip netns exec pc1 ifconfig
|
Result
vdkmai@Ubuntu32bit:~$ sudo ip netns exec pc1 dhclient -nw -v eth1
Internet Systems Consortium DHCP Client 4.1-ESV-R4
Copyright 2004-2011 Internet Systems Consortium.
All rights reserved.
For info, please visit https:
Listening on LPF/eth1/36:aa:75:c9:e0:38
Sending on LPF/eth1/36:aa:75:c9:e0:38
Sending on Socket/fallback
DHCPREQUEST of 192.168.0.38 on eth1 to 255.255.255.255 port 67
vdkmai@Ubuntu32bit:~$ sudo ip netns exec pc1 ping 8.8.8.8
PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
64 bytes from 8.8.8.8: icmp_req=1 ttl=53 time=41.4 ms
vdkmai@Ubuntu32bit:~$ sudo ip netns exec pc2 ifconfig
eth1 Link encap:Ethernet HWaddr 36:aa:75:c9:e0:38
inet addr:192.168.0.38 Bcast:192.168.0.255 Mask:255.255.255.0
inet6 addr: fe80::34aa:75ff:fec9:e038/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:1354 errors:0 dropped:0 overruns:0 frame:0
TX packets:44 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:421112 (421.1 KB) TX bytes:4632 (4.6 KB)
|
If You Enjoyed This, Take 5 Seconds To Share It
0 comments:
Post a Comment