通过 Linux 命令行查找我的公网 IP 地址
IP 地址是 Internet 协议地址 (IP Address) 的缩写。设备被分配特定的 IP 地址来识别连接到互联网的设备。公网 IP 地址可在互联网上路由,通常由 ISP(Internet 服务提供商)提供,可通过互联网访问。
您知道如何通过 Linux 命令行查找公网 IP 地址吗?查找和识别公网 IP 地址的方法有很多种。例如,我们可以使用第三方网站或"shell"命令。本文提供了一些在 Linux 命令行上查找公网 IP 地址的简单方法。
谁使用公网 IP 地址?
公网 IP 地址用于 Internet Web 服务器、DNS 服务器、网络路由器,它们直接连接到使用公网 IP 地址的计算机。 IANA(互联网号码分配机构)负责为 ISP(互联网服务协议)分配 IP 地址,这些提供商有权为每个连接的设备分配特定的 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 "我的 WAN/公网 IP 地址:${myip}"
示例输出应如下所示 –
我的 WAN/公网 IP 地址: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 文章中进一步学习这些类型的命令。继续阅读!
相关文章
有用资源
linux 参考教程 - 该教程包含有关 linux 的更多信息:https://www.cainiaomax.com/linux/

