What is my IP address? How can I determine my IP address reliably from the command line using Linux?

Introduction

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 ipconfig, ifconfig, and 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.

Solutions

SSH to the computer and use either one of the following commands:

$ curl ifconfig.me

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

Explanation

  • curl is a way to retrieve remote web page or files
  • ifconfig.me is an awesome service that allows you to look from the outside in
  • dig is used to perform name server queries
  • +short means that just the essential information (the IP address) must be returned instead of all output
  • myip.opendns.com is a remote service that determines the originating IP address and assigns it to the response
  • @resolver1.opendns.com provides this remote service

Reference

https://www.cyberciti.biz/faq/how-to-find-my-public-ip-address-from-command-line-on-a-linux/

Share this article

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to Top