如何让我在Linux和OS X本地机器的主IP地址? [英] How to I get the primary IP address of the local machine on Linux and OS X?
问题描述
我要寻找一个命令行的解决方案,将返回我的主(第一个)本地主机的IP地址,不是127.0.0.1其他
I am looking for a command line solution that would return me the primary (first) IP address of the localhost, other than 127.0.0.1
该解决方案应该至少工作的Linux(Debian和红帽)和OS X 10.7 +
The solution should work at least for Linux (Debian and RedHat) and OS X 10.7+
据我所知,使用ifconfig上都可用,但它的输出并不在这些平台之间如此一致。
I am aware that ifconfig is available on both but it's output is not so consistent between these platforms.
推荐答案
使用的grep
过滤从IP地址使用ifconfig
:
使用ifconfig | grep的-eoINET(地址:)([0-9] * \\。){3} [0-9] *?| grep的-eo([0-9] * \\。){3} [0-9] *'| grep的-v127.0.0.1
,使用 SED
:
使用ifconfig | ?SED -En的/ 127.0.0.1 //; S /.* INET(地址:)(。([0-9] * \\){3} [0-9] *)* / \\ 2 / P
如果你只对某些接口,为wlan0,为eth0,等有兴趣的话:
If you are only interested in certain interfaces, wlan0, eth0, etc. then:
使用ifconfig为wlan0 | ...
您可以在的.bashrc
别名命令的创建的自己的命令叫 MYIP
的实例。
You can alias the command in your .bashrc
to create your own command called myip
for instance.
别名MYIP =使用ifconfig | sed的-En的/ 127.0.0.1 //; S /.* INET(地址:)(([0-9] * \\){3 } [0-9] *)* / \\ 2 / p'
有一个更简单的方法是主机名-I
然而,这仅仅是在Linux上。
A much simpler way is hostname -I
however this is on Linux only.
这篇关于如何让我在Linux和OS X本地机器的主IP地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!