从 Linux 命令行查找我的公网 IP 地址


IP 地址代表互联网协议地址。设备被分配了指定的 IP 地址,以识别连接到互联网的设备。公网 IP 地址可以在互联网上路由,通常由 ISP(互联网服务提供商)提供,并且可以通过互联网访问。

您知道如何从 Linux 命令行查找公网 IP 地址吗?有多种方法可以查找和识别公网 IP 地址。例如,我们可以使用第三方网站或“**shell**”命令。本文提供了一些简单的方法,用于从 Linux 命令行查找公网 IP 地址。

谁使用公网 IP 地址?

公网 IP 地址用于互联网 Web 服务器、DNS 服务器、网络路由器,并且它们直接连接到使用公网 IP 地址的计算机。IANA(互联网号码分配机构)负责将 IP 地址分配给 ISP(互联网服务协议),并且这些提供商有权为每个连接的设备分配特定的 IP 地址。

方法 1 - 使用“dig”命令

这是从 Linux 命令行识别公网 IP 地址的最快方法。要识别公网 IP 地址,请使用以下命令 -

$ dig +short myip.opendns.com @resolver1.opendns.com

输出应如下所示 -

$ dig +short myip.opendns.com @resolver1.opendns.com
183.82.108.59

在以上输出中,183.82.108.59 是系统 IP 地址。

方法 2 - “host”命令

以下是使用 host 命令识别公网 IP 地址的另一种替代方法。

使用以下命令识别公网 IP 地址。

$ host myip.opendns.com resolver1.opendns.com

示例输出应如下所示 -

$ host myip.opendns.com resolver1.opendns.com
Using domain server:
Name: resolver1.opendns.com
Address: 208.67.222.222#53
Aliases:
myip.opendns.com has address 183.82.108.59
Host myip.opendns.com not found: 3(NXDOMAIN)
Host myip.opendns.com not found: 3(NXDOMAIN)

在以上输出中,183.82.108.59 是系统 IP 地址。

方法 3 - 使用 Google 服务器的“dig”命令

使用“dig”命令和 Google 服务器,我们可以找到 IP 地址。

要查找 IP 地址,请使用以下命令 -

$ dig TXT +short o-o.myaddr.l.google.com @ns1.google.com | awk -F'"' '{ print $2}'

输出应如下所示 -

$ dig TXT +short o-o.myaddr.l.google.com @ns1.google.com | awk -F'"' '{ print $2}'
183.82.108.59

在以上输出中,183.82.108.59 是系统 IP 地址。

方法 4 - 使用第三方网站

我们可以使用第三方网站从 Linux 命令行 Linux 查找 IP 地址。

要查找 IP 地址,请使用以下命令中的任何一个 -

$ curl ifconfig.me
$ curl icanhazip.com
$ curl ipecho.net/plain
$ curl ifconfig.co

示例输出应如下所示 -

tp@linux:~$ curl ifconfig.me
183.82.108.59
tp@linux:~$ curl icanhazip.com
183.82.108.59
tp@linux:~$ curl ipecho.net/plain
183.82.108.59
tp@linux:~$ curl ifconfig.co
183.82.108.59

在以上输出中,183.82.108.59 是系统 IP 地址。

如何将我的 IP 地址存储在“Shell 变量”中?

要将 IP 地址存储在 shell 变量中,请使用以下命令 -

$ myip="$(dig +short myip.opendns.com @resolver1.opendns.com)"
$ echo "My WAN/Public IP address: ${myip}"

示例输出应如下所示 -

My WAN/Public IP address: 183.82.108.59

在以上输出中,183.82.108.59 是系统 IP 地址。

不喜欢命令行界面?

这是通过点击以下链接查找 IP 地址的最简单解决方案 -

https://www.google.co.in/search?q=what+is+my+IP+address
https://duckduckgo.com/?q=ip
http://www.wolframalpha.com/input/?i=what+is+my+ip+address

恭喜!现在,您知道了“如何使用命令行界面以及不使用命令行界面查找公网 IP 地址”。我们将在下一篇 Linux 文章中详细了解这些类型的命令。请继续阅读!

更新于: 2020-07-07

3K+ 浏览量

开启您的 职业生涯

通过完成课程获得认证

开始
广告

© . All rights reserved.