Linux 如何查找网络接口名称
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12474172/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to find network interface name
提问by artfulrobot
I have a bash script that runs on a variety of different Ubuntu Linux machines. Its job is to find out the LAN IPv4 address of the localhost.
我有一个 bash 脚本,可以在各种不同的 Ubuntu Linux 机器上运行。它的工作是找出本地主机的 LAN IPv4 地址。
The script is using
该脚本正在使用
ip addr show eth0 | sed -n '/inet /{s/^.*inet \([0-9.]\+\).*$//;p}'
which is fine, but some machines for some reason use eth1 instead of eth0. I would like to be able to discover the LAN iface name, so I can substitute it in here instead of eth0.
这很好,但某些机器出于某种原因使用 eth1 而不是 eth0。我希望能够发现 LAN iface 名称,因此我可以在此处替换它而不是 eth0。
Of course, if you can come up with a different oneliner that does the same thing, all good.
当然,如果你能想出一个不同的oneliner来做同样的事情,那就太好了。
采纳答案by gatoatigrado
Not sure if this helps, but it seems that ip route get
will show which interface it uses to connect to a remote host.
不确定这是否有帮助,但似乎ip route get
会显示它使用哪个接口连接到远程主机。
ubuntu@ip-10-40-24-21:/nail/srv/elasticsearch$ ip route get 8.8.8.8
8.8.8.8 via <gateway address> dev eth0 src <eth0 IP Address>
of course you could automate that in shell script with something like,
当然,你可以在 shell 脚本中使用类似的东西来自动化,
ip route get 8.8.8.8 | awk '{ print $NF; exit }'
回答by artfulrobot
I'd still like to know if there was an easier way to do this, but this is my workaround: I know the LAN subnet, so...
我仍然想知道是否有更简单的方法来做到这一点,但这是我的解决方法:我知道 LAN 子网,所以......
ip addr show | grep "inet 10.67.5." \
| sed -n '/inet /{s/^.*inet \([0-9.]\+\).*$//;p}'
回答by Aaron Digulla
How about searching for the string inet
and brd
(for broadcast)? That would give you:
如何搜索字符串inet
和brd
(用于广播)?那会给你:
ip addr show|egrep '^ *inet'|grep brd|awk -- '{ print ; }'|sed -e 's:/[0-9]*$::'
Note that I'm using more commands than necessary; you can probably achieve the same thing with sed
and a more complex regexp but I prefer a command that makes it obvious by which steps I arrive at the result.
请注意,我使用的命令比必要的多;您可能可以sed
使用更复杂的正则表达式来实现相同的目的,但我更喜欢一个命令,该命令可以使我通过哪些步骤得出结果。
If you want to run it in a single command, I suggest to try awk
:
如果你想在单个命令中运行它,我建议尝试awk
:
ip addr show|awk -- ' == "inet" && == "brd" { split(,a,"/"); print a[1]; }'
which isn't much longer than the sed
version but more readable.
这并不比sed
版本长多少,但更具可读性。
+1 Slightly more readable:
+1 更易读:
ip addr show | awk ' == "inet" && == "brd" { sub (/\/.*/,""); print }'
回答by Neolo
1) This one print only interface names (I needed that for handing upcoming and downcoming PPP links):
1) 这一个只打印接口名称(我需要它来处理即将到来的和即将到来的 PPP 链接):
for i in $( ifconfig | grep 'ppp' | awk '{print }' );
do
printf "$i "; ## Or echo
done
Result:
结果:
ppp0 ppp1 ppp2
ppp0 ppp1 ppp2
2) This one prints interface names and IP
2)这个打印接口名称和IP
declare -a IPADDR
index=0
for i in $( ifconfig | grep 'inet addr' | awk '{print }'| sed 's#addr:##g' );
do
IPADDR[$index]=$i
let "index += 1"
done
index=0
for i in $( ifconfig | grep 'ppp' | awk '{print }' );
do
echo $i
let "index += 1"
done
Result:
结果:
ppp0 addr:IP
ppp1 addr:IP
ppp2 addr:IP
ppp0 地址:IP
ppp1 地址:IP
ppp2 地址:IP
回答by David W.
Believe it or not, there is no standard, easy way to get this information. There is no standard give me the current IP and Interface Namecommand. There isn't even a standard format for the information returned by ifconfig
.
信不信由你,没有标准的、简单的方法来获取这些信息。没有标准的给我当前的 IP 和接口名称命令。返回的信息甚至没有标准格式ifconfig
。
I was going to recommend forgoing pure shell and go with a scripting language like Python where you can do this:
我将建议放弃纯 shell 并使用像 Python 这样的脚本语言,您可以在其中执行以下操作:
import socket
socket.gethostbyname(socket.gethostname())
Except it doesn't work on most Linux systems because there's usually an entry in the /etc/host
file pointing to 127.0.0.1
, the loopback address. Perlhas the same issues.
除非它在大多数 Linux 系统上不起作用,因为/etc/host
文件中通常有一个条目指向127.0.0.1
,即环回地址。Perl也有同样的问题。
You have a firm grasp on the scripting involved, and you've seen the issues. The only thing I can recommend is to test this on each machine you're going to run it on, and see what pops out. There isn't going to be a general purpose one liner that works with all operating systems, or even with different systems on the same operating system because of the way the network is setup and the way interfaces may be named by each location.
您已经牢牢掌握了所涉及的脚本,并且已经看到了问题所在。我唯一能推荐的是在你要运行它的每台机器上测试它,看看会弹出什么。由于网络的设置方式以及每个位置可能对接口进行命名的方式,不会有通用的单线程可以适用于所有操作系统,甚至适用于同一操作系统上的不同系统。
回答by r00txs
Most recenttly systemd/udev has automatically started to assign interface names for all local Ethernet, WLAN and WWAN interfaces to something that we're all accustomed to . This is a departure from the traditional interface naming scheme ("eth0", "eth1", "wlan0", ...) .. now we have to check first what the local interface name is before we can use it while previously we it was a pretty accurate guess that "eth0" was the right name. What you're asking for is the network NAME .. Here's a small script to solve the problem
最近,systemd/udev 自动开始为所有本地以太网、WLAN 和 WWAN 接口分配接口名称,这些名称是我们都习惯的。这与传统的接口命名方案(“eth0”,“eth1”,“wlan0”,...)背道而驰.. 现在我们必须先检查本地接口名称是什么才能使用它非常准确地猜测“eth0”是正确的名称。你要的是网络 NAME .. 这是一个解决问题的小脚本
Use "ip route get 8.8.8.8 " to figure out which ACTIVE interface has the route to internet ( or currently being used ) Output should look like :
8.8.4.4 via 10.10.1.1 dev enp0s3 src 10.10.1.118 cache
Use awk to print the 5th text block for Interface NAME
]# ip route get 8.8.8.8 | awk -- '{print $5}' Output : enp0s3
Use awk to print the 7th text block for Interface Address
]# ip route get 8.8.8.8 | awk -- '{print $7}' Output : 10.10.1.118
使用“ip route get 8.8.8.8”来确定哪个 ACTIVE 接口具有到互联网的路由(或当前正在使用)输出应如下所示:
8.8.4.4 通过 10.10.1.1 dev enp0s3 src 10.10.1.118 缓存
使用 awk 打印 Interface NAME 的第 5 个文本块
]# ip 路由获取 8.8.8.8 | awk -- '{print $5}' 输出:enp0s3
使用 awk 打印接口地址的第 7 个文本块
]# ip 路由获取 8.8.8.8 | awk -- '{print $7}' 输出:10.10.1.118
回答by user150471
The main NIC will usually have a default route. So:
主网卡通常会有一个默认路由。所以:
ip -o -4 route show to default
The NIC:
网卡:
ip -o -4 route show to default | awk '{print }'
The gateway:
网关:
ip -o -4 route show to default | awk '{print }'
Unlike ifconfig, ip has a consistent & parsable output. It only works on Linux; it won't work on other Unixen.
与 ifconfig 不同,ip 具有一致且可解析的输出。它只适用于 Linux;它不适用于其他 Unixen。