🤖 Ai回答: Of course、The command `ip link add` is a powerful utility from the `iproute2` package on Linux, used to create virtual network interfaces、These interfaces are not tied to physical hardware but exist entirely in software.
This is the foundation for modern networking on Linux, enabling everything from container networking (Docker, Podman) and virtual machine connectivity to complex tunnels and overlay networks.
TL;DR / Quick Summary
`ip link add` creates a new virtual network device、The most common syntax is:
bash
Syntax
sudo ip link add <name type <type [options...]
Example: Create a simple dummy interface for testing
sudo ip link add dev dummy0 type dummy
Bring it up
sudo ip link set dummy0 up
See it
ip link show dummy0
3: dummy0: <BROADCAST,NOARP,UP,LOWER_UP mtu 1500 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
link/ether 1e:5e:5e:7a:7a:7a brd ff:ff:ff:ff:ff:ff
Delete it when done
sudo ip link del dummy0
In-Depth Guide and Common Interface Types
The `type` is the most important part of the command、It determines what kind of virtual device you are creating、Here are the most common types and what they are used for.
1、`dummy`
A simple, software-only interface、It never sends or receives real traffic、It's perfect for testing routing, scripts, or for applications that require an interface to be up but don't need to talk to anyone.
bash
sudo ip link add dev my-test-interface type dummy
2、`veth` (Virtual Ethernet)
Creates a pair of connected virtual interfaces, like a pipe、Whatever goes into `veth0` comes out of `veth1`, and vice-versa、This is the cornerstone for connecting network namespaces (which Docker and other container technologies use).
bash
Create a pair named veth-a and veth-b
sudo ip link add veth-a type veth peer name veth-b
3、`bridge`
Creates a Layer 2 software switch, just like a physical network switch、You can plug other interfaces (physical or virtual) into it, and they will be able to communicate with each other at Layer 2.
bash
Create a bridge named br0
sudo ip link add name br0 type bridge
4、`vlan`
Creates a virtual Layer 2 interface for a specific VLAN ID、It's attached to an existing physical interface, which acts as the trunk.
bash
Create a VLAN interface for VLAN 100 on physical NIC eth0
sudo ip link add link eth0 name eth0.100 type vlan id 100
5、`macvlan`
Allows you to create a virtual interface with its own unique MAC address that is directly connected to your physical network、The virtual interface will appear on the same physical LAN as the host's NIC、This is great for giving containers their own IP on your local network.
bash
Create a macvlan interface on eth0
sudo ip link add link eth0 name macvlan0 type macvlan mode bridge
6、`vxlan` (Virtual Extensible LAN)
Creates a Layer 2 overlay network on top of a Layer 3 network、It's a key technology for modern data centers and cloud-native networking (e.g., Kubernetes networking with Flannel)、It encapsulates L2 frames inside UDP packets.
bash
Create a VXLAN tunnel with VNI 100, using multicast group 239.1.1.1
sudo ip link add name vxlan100 type vxlan id 100 group 239.1.1.1 dev eth0
Practical Examples
Example 1: Connecting Two Network Namespaces with `veth`
This is the classic use case for `veth`、We'll create two namespaces and connect them so they can ping each other.
bash
1、Create two network namespaces
sudo ip netns add ns1
sudo ip netns add ns2
2、Create the veth pair
sudo ip link add veth-ns1 type veth peer name veth-ns2
3、Move each end of the pair into its respective namespace
sudo ip link set veth-ns1 netns ns1
sudo ip link set veth-ns2 netns ns2
4、Inside each namespace:
Bring up the loopback interface
Bring up the veth interface
Assign an IP address
sudo ip netns exec ns1 ip link set lo up
sudo ip netns exec ns1 ip link set veth-ns1 up
sudo ip netns exec ns1 ip addr add 10.0.1.1/24 dev veth-ns1
sudo ip netns exec ns2 ip link set lo up
sudo ip netns exec ns2 ip link set veth-ns2 up
sudo ip netns exec ns2 ip addr add 10.0.1.2/24 dev veth-ns2
5、Test connectivity
sudo ip netns exec ns1 ping -c 3 10.0.1.2
PING 10.0.1.2 (10.0.1.2) 56(84) bytes of data.
64 bytes from 10.0.1.2: icmp_seq=1 ttl=64 time=0.048 ms
...
Cleanup (optional)
sudo ip netns del ns1
sudo ip netns del ns2
Example 2: Creating a Simple Bridge
Let's create a bridge and add a physical interface to it.
bash
NOTE: Be careful if doing this over SSH! You might lose connection.
It's best to do this from a local console.
Let's assume your physical interface is enp0s3 and has no IP.
1、Create the bridge
sudo ip link add name br0 type bridge
2、Bring up the bridge interface
sudo ip link set br0 up
3、Add the physical interface enp0s3 to the bridge
This makes enp0s3 a port on the br0 switch.
sudo ip link set enp0s3 master br0
sudo ip link set enp0s3 up
4、Assign an IP address to the bridge, not the physical port
sudo ip addr add 192.168.1.50/24 dev br0
Now, your host's IP is on br0, and any VM or container plugged
into br0 will be on the same L2 network.
Cleanup
sudo ip addr del 192.168.1.50/24 dev br0
sudo ip link set enp0s3 nomaster
sudo ip link set enp0s3 down
sudo ip link set br0 down
sudo ip link del br0
Managing the Links
After creating a link with `ip link add`, you typically need to perform a few more actions:
* Bring it Up: By default, new interfaces are in the `DOWN` state.
bash
sudo ip link set dev <device_name up
* Assign an IP Address: (Optional, but often necessary)
bash
sudo ip addr add <ip_address/<cidr dev <device_name
* Delete it: When you're finished.
bash
sudo ip link del dev <device_name
Finding More Help
* The Man Page: The ultimate source of truth.
bash
man ip-link
* Command-Line Help: A quick summary of types and options.
bash
ip link help
The `ip link add` command is an essential tool for anyone doing advanced networking, virtualization, or system administration on a modern Linux system.