「Linux」- 如何确定公网网络地址? @20210311

问题描述

有时,我们需要向对方提供我们的公网地址,或者验证我们的请求是否通过代理服务器(此时应该返回代理服务器地址)

该笔记将记录:如何查看自己的公网地址。

解决方案

命令行获取(推荐)

dig +short myip.opendns.com @resolver1.opendns.com
dig TXT +short o-o.myaddr.l.google.com @ns1.google.com

使用 HTTP 服务,以返回公网地址(自建)

我们可以使用 Nginx 来返回公网地址(需要具有公网服务器)使用如下配置:

server {
   listen 80;
   server_name ifconfig.toolbox.d3rm.org;
   location / {
       return 200 $remote_addr;
   }
}

当在命令行中使用 curl 发送 http 请求是,将返回网络地址:

# curl ifconfig.toolbox.d3rm.org
1.2.3.4.5

使用第三方站点(HTTP)

curl "http://ifconfig.me"
curl "http://icanhazip.com"
curl "http://ipecho.net/plain"
curl "http://ifconfig.co"

################################################################################

curl checkip.amazonaws.com
curl ifconfig.me
curl icanhazip.com
curl ipecho.net/plain
curl ifconfig.co

## store output in $server_ip ##
server_ip="$(curl ifconfig.co)"

## Display it ##
printf "Server public ip4 %s\n" $server_ip

参考文献

How To Find My Public IP Address From Command Line On a Linux
How To Find My Public IP Address From Command Line On a Linux - nixCraft


posted @ 2021-03-11 22:50  研究林纳斯写的  阅读(136)  评论(0编辑  收藏  举报