This article explains how to get the IP address of a computer as viewed from the outside. The reason why you might want to do this is because in spite of a computer’s actual configuration, the IP address on the outside could be a NATted address. The prerequisite is that you are running Linux/Unix and the
dig utility is available.
The typical way to obtain an IP address consists of commands such as
ip addr sh. These utilities report on the current IP address configuration as defined on the computer. However, there could be a catch. The caveat is that a NAT gateway might be on the outside of your network forwarding data from the outside to your IP address in the inside.
The solution in this article will assist Linux users to get their precise IP address as viewed from the outside in spite of any kind of gateway. It’s also a useful shorthand way to generically determine an IP address as Linux distributions have different ways of doing so.
SSH to the computer and use either one of the following commands:
$ curl ifconfig.me
$ dig +short myip.opendns.com @resolver1.opendns.com
curlis a way to retrieve remote web page or files
ifconfig.meis an awesome service that allows you to look from the outside in
digis used to perform name server queries
+shortmeans that just the essential information (the IP address) must be returned instead of all output
myip.opendns.comis a remote service that determines the originating IP address and assigns it to the response
@resolver1.opendns.comprovides this remote service