Linux Get the Interface name of ethernet
Linux 获取网卡接口名称
ip link | grep '2:' | head -1 | awk '{print $2}'| sed 's/://'
直接上测试: 测试1:多网卡虚拟机
root@crazy:~# ip link
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq state UP mode DEFAULT group default qlen 1000
link/ether aa:aa:00:1a:e4:f6 brd ff:ff:ff:ff:ff:ff
altname enp0s18
altname ens18
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq state UP mode DEFAULT group default qlen 1000
link/ether ba:aa:00:1a:e4:f6 brd ff:ff:ff:ff:ff:ff
altname enp0s19
altname ens19
19: tailscale0: <POINTOPOINT,MULTICAST,NOARP,UP,LOWER_UP> mtu 1280 qdisc fq state UNKNOWN mode DEFAULT group default qlen 500
link/none
root@crazy:~# ip link | grep '2:' | head -1 | awk '{print $2}'| sed 's/://'
eth0
测试2:多网卡物理机
[root@crazy ~]# ip link
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
3: enp1s0f0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP mode DEFAULT group default qlen 1000
link/ether a0:36:9f:00:00:00 brd ff:ff:ff:ff:ff:ff
4: enp1s0f1: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc mq state DOWN mode DEFAULT group default qlen 1000
link/ether a0:36:9f:00:00:00 brd ff:ff:ff:ff:ff:ff
5: enp1s0f2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP mode DEFAULT group default qlen 1000
link/ether a0:36:9f:00:00:00 brd ff:ff:ff:ff:ff:ff
7: tailscale0: <POINTOPOINT,MULTICAST,NOARP,UP,LOWER_UP> mtu 1280 qdisc fq state UNKNOWN mode DEFAULT group default qlen 500
link/none
17: enp1s0f3: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc mq state DOWN mode DEFAULT group default qlen 1000
link/ether a0:36:9f:00:00:00 brd ff:ff:ff:ff:ff:ff
20: enp3s0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc fq state DOWN mode DEFAULT group default qlen 1000
link/ether 00:e0:4c:00:00:00 brd ff:ff:ff:ff:ff:ff
22: macvtap10@enp1s0f2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP mode DEFAULT group default qlen 500
link/ether 52:54:00:82:bc:27 brd ff:ff:ff:ff:ff:ff
[root@crazy ~]# ip link | grep '2:' | head -1 | awk '{print $2}'| sed 's/://'
enp1s0f2
测试3:虚拟机内运行Docker, TAP等多种服务
root@crazy:~# ip link
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: ens5: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP mode DEFAULT group default qlen 1000
link/ether 52:54:00:aa:ae:b3 brd ff:ff:ff:ff:ff:ff
3: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN mode DEFAULT group default
link/ether 02:42:23:8f:df:23 brd ff:ff:ff:ff:ff:ff
7: tap-udp: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UNKNOWN mode DEFAULT group default qlen 100
link/ether 6a:c9:a3:e2:99:f9 brd ff:ff:ff:ff:ff:ff
root@sha2:~# ip link | grep '2:' | head -1 | awk '{print $2}'| sed 's/://'
ens5
全部正确
最后修改于 Wed, 25 Jan 2017