If I check with google, I can see my public IP. Is there something on the Ubuntu command-line which will yield me the same answer?
23 Answers
If you are not behind a router, you can find it out using ifconfig.
If you are behind a router, then your computer will not know about the public IP address as the router does a network address translation. You could ask some website what your public IP address is using curl or wget and extract the information you need from it:
curl -s https://checkip.dyndns.org | sed -e 's/.*Current IP Address: //' -e 's/<.*$//'
or shorter
curl https://ipinfo.io/ip
For finding the external ip, you can either use external web-based services, or use system based methods. The easier one is to use the external service, also the ifconfig based solutions will work in your system only if you're not behind a NAT. the two methods has been discussed below in detail.
Finding external IP using external services
The easiest way is to use an external service via a commandline browser or download tool. Since wget is available by default in Ubuntu, we can use that.
To find your ip, use-
$ wget -qO- https://ipecho.net/plain ; echo
Courtesy:
- https://hawknotes.blogspot.in/2010/06/finding-your-external-ip-address.html
- https://ipecho.net/plain
You could also use lynx(browser) or curl in place of wget with minor variations to the above command, to find your external ip.
Using curl to find the ip:
$ curl https://ipecho.net/plain
For a better formatted output use:
$ curl https://ipecho.net/plain ; echo
A faster (arguably the fastest) method using dig with OpenDNS as resolver:
The other answers here all go over HTTP to a remote server. Some of them require parsing of the output, or rely on the User-Agent header to make the server respond in plain text. They also change quite frequently (go down, change their name, put up ads, might change output format etc.).
- The DNS response protocol is standardised (the format will stay compatible).
- Historically DNS services (OpenDNS, Google Public DNS, ..) tend to survive much longer and are more stable, scalable and generally looked after than whatever new hip whatismyip.com HTTP service is hot today.
- (for those geeks that care about micro-optimisation), this method should be inherently faster (be it only by a few micro seconds).
Using dig with OpenDNS as resolver:
$ dig +short myip.opendns.com @resolver1.opendns.com
111.222.333.444
Copied from: https://unix.stackexchange.com/a/81699/14497
Finding external IP without relying on external services
- If you know your network interface name
Type the following in your terminal:
$ LANG=c ifconfig <interface_name> | grep "inet addr" | awk -F: '{print $2}' | awk '{print $1}'
In the above, replace <interface_name> with the name of your actual interface, e.g: eth0, eth1, pp0, etc...
Example Usage:
$ LANG=c ifconfig ppp0 | grep "inet addr" | awk -F: '{print $2}' | awk '{print $1}'
111.222.333.444
- If you don't know your network interface name
Type the following in your terminal (this gets the name and ip address of every network interface in your system):
$ LANG=c ifconfig | grep -B1 "inet addr" |awk '{ if ( $1 == "inet" ) { print $2 } else if ( $2 == "Link" ) { printf "%s:" ,$1 } }' |awk -F: '{ print $1 ": " $3 }'
Example Usage:
$ LANG=c ifconfig | grep -B1 "inet addr" |awk '{ if ( $1 == "inet" ) { print $2 } else if ( $2 == "Link" ) { printf "%s:" ,$1 } }' |awk -F: '{ print $1 ": " $3 }'
lo: 127.0.0.1
ppp0: 111.222.333.444
N.B: Outputs are indicative and not real.
Courtesy: https://www.if-not-true-then-false.com/2010/linux-get-ip-address/
UPDATE
LANG=chas been added toifconfigbased usages, so that it always gives the english output, irrespective of locale setting.
My favorite has always been :
curl ifconfig.me
simple, easy to type.
The command above might output IPv6, if you want just IPv4:
curl -4 ifconfig.me
You will have to install curl first ;)
If ifconfig.me is down try icanhazip.com and or ipecho.net
curl icanhazip.com
or
curl ipecho.net
icanhazip.com is my favorite.
curl icanhazip.com
You can request IPv4 explicitly:
curl ipv4.icanhazip.com
If you don't have curl you can use wget instead:
wget -qO- icanhazip.com
- 943
Running my own service, designed to be simple and stupid, ident.me.
Its API and implementation are documented at https://api.ident.me/
Examples from the terminal (add https:// for security at the expense of speed):
curl ident.me
curl v4.ident.me
curl v6.ident.me
- 1,215
- 10
- 11
You could use a DNS request instead of HTTP request to find out your public IP:
$ dig +short myip.opendns.com @resolver1.opendns.com
It uses resolver1.opendns.com dns server to resolve the magical myip.opendns.com hostname to your ip address.
- 4,078
Amazon AWS
curl https://checkip.amazonaws.com
Sample output:
123.123.123.123
Also works on browser: http://checkip.amazonaws.com
I like it because:
- it returns just the plaintext IP in the reply body, nothing else
- it is from a well known provider which is unlikely to go offline anytime soon
- 31,312
For this, STUN was invented. As a client you can send a request to a publicly available STUN server and have it give back the IP address it sees. Sort of the low level whatismyip.com as it uses no HTTP and no smartly crafted DNS servers but the blazingly fast STUN protocol.
Using stunclient
If you have stunclient installed (apt-get install stuntman-client on debian/ubuntu) you can simply do:
$stunclient stun.services.mozilla.com
Binding test: success
Local address: A.B.C.D:42541
Mapped address: W.X.Y.Z:42541
where A.B.C.D is the IP address of your machine on the local net and W.X.Y.Z is the IP address servers like websites see from the outside (and the one you are looking for). Using sed you can reduce the output above to only an IP address:
stunclient stun.services.mozilla.com |
sed -n -e "s/^Mapped address: \(.*\):.*$/\1/p"
However, your question was how to find it using the command line, which might exclude using a STUN client. So I wonder...
Using bash
A STUN request can be handcrafted, sent to an external STUN server using netcat and be post-processed using dd, hexdump and sed like so:
$echo -en '\x00\x01\x00\x08\xc0\x0c\xee\x42\x7c\x20\x25\xa3\x3f\x0f\xa1\x7f\xfd\x7f\x00\x00\x00\x03\x00\x04\x00\x00\x00\x00' |
nc -u -w 2 stun.services.mozilla.com 3478 |
dd bs=1 count=4 skip=28 2>/dev/null |
hexdump -e '1/1 "%u."' |
sed 's/\.$/\n/'
The echo defines a binary STUN request (0x0001 indicates Binding Request) having length 8 (0x0008) with cookie 0xc00cee and some pasted stuff from wireshark. Only the four bytes representing the external IP are taken from the answer, cleaned and printed.
Working, but not recommended for production use :-)
P.S. Many STUN servers are available as it is a core technology for SIP and WebRTC. Using one from Mozilla should be safe privacy-wise but you could also use another: STUN server list
- 301
Type in this exactly, press Enter where indicated:
telnet ipecho.net 80Enter
GET /plain HTTP/1.1Enter
HOST: ipecho.net Enter
BROWSER: web-kitEnter
Enter
This manually submits a HTTP request, which will return your IP at the bottom of a HTTP/1.1 200 OK reply
Example output:
$ telnet ipecho.net 80
Trying 146.255.36.1...
Connected to ipecho.net.
Escape character is '^]'.
GET /plain HTTP/1.1
HOST: ipecho.net
BROWSER: web-kit
HTTP/1.1 200 OK
Date: Tue, 02 Jul 2013 07:11:42 GMT
Server: Apache
Expires: Mon, 26 Jul 1997 05:00:00 GMT
Cache-Control: no-cache
Pragma: no-cache
Vary: Accept-Encoding
Transfer-Encoding: chunked
Content-Type: text/html
f
111.222.333.444
0
You can read a web page using only bash, without curl, wget:
$ exec 3<> /dev/tcp/icanhazip.com/80 && # open connection
echo 'GET /' >&3 && # send http 0.9 request
read -u 3 && echo $REPLY && # read response
exec 3>&- # close fd
- 4,078
These will get the local IPs:
ifconfig
or for shorter output:
ifconfig | grep inet
also
ip addr show
and probably:
hostname -I
This should get the external IP
wget http://smart-ip.net/myip -O - -q ; echo
N.B. If you don't mind to installing curl, this as well:
curl http://smart-ip.net/myip
- 30,732
For those of us with login access to our routers, using a script to ask the router what its' WAN IP address is is the most efficient way to determine the external IP address. For instance the following python script prints out the external IP for my Medialink MWN-WAPR300N router:
import urllib, urllib2, cookielib
import re
from subprocess import check_output as co
cookie_jar = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie_jar))
urllib2.install_opener(opener)
def get(url, values=None):
data = None
if values: data = urllib.urlencode(values)
req = urllib2.Request(url, data)
rsp = urllib2.urlopen(req)
return rsp.read()
router = co(['ip', '-o', 'ro', 'list', '0.0.0.0/0']).split()[2]
url = "http://" + router
get(url+"/index.asp")
get(url+"/LoginCheck", dict(checkEn='0', Username='admin', Password='admin'))
page = get(url+"/system_status.asp")
for line in page.split("\n"):
if line.startswith("wanIP = "):
print line.split('"')[1]
exit(1)
Note that this is not very secure (as is the case with plaintext credentials & logging in to most routers), and is certainly not portable (needs to be changed for each router). It is however very fast and a perfectly reasonable solution on a physically secure home network.
To customize the script for another router, I recommend using the tamperdata addon in firefox to determine what HTTP requests to make.
- 178
Many home routers can be queried by UPnP:
curl "http://fritz.box:49000/igdupnp/control/WANIPConn1" -H "Content-Type: text/xml; charset="utf-8"" -H "SoapAction:urn:schemas-upnp-org:service:WANIPConnection:1#GetExternalIPAddress" -d "<?xml version='1.0' encoding='utf-8'?> <s:Envelope s:encodingStyle='http://schemas.xmlsoap.org/soap/encoding/' xmlns:s='http://schemas.xmlsoap.org/soap/envelope/'> <s:Body> <u:GetExternalIPAddress xmlns:u='urn:schemas-upnp-org:service:WANIPConnection:1' /> </s:Body> </s:Envelope>" -s
Then, grep the ip address from the answer.
grep -Eo '\<[[:digit:]]{1,3}(\.[[:digit:]]{1,3}){3}\>'
If you are using DD-WRT then this works for me:
curl -s 192.168.1.1 | grep "ipinfo" | awk -v FS="(IP: |</span)" '{print $2}'
or
curl -s -u your_ddwrt_username:your_ddwrt_password http://192.168.1.1 | grep "ipinfo" | awk -v FS="(IP: |</span)" '{print $2}'
Where 192.168.1.1 is the Gateway/Router LAN IP Address of the DD-WRT router.
The -s component means silent (i.e. don't show the curl progress information).
- Oh, I should mention that I use the above with "DD-WRT v24-sp2 (01/04/15) std".
- 191
- 1
- 3
Maybe I am a little late, but inxi can do it fairly easy.
Install inxi
sudo apt install inxi
Then run the following command
inxi -i
Example with my information blocked out using the z option for copy and paste to sites like this:
~$ inxi -iz
Network: Card: NVIDIA MCP77 Ethernet driver: forcedeth
IF: eth0 state: up speed: 1000 Mbps duplex: full mac: <filter>
WAN IP: <filter>
IF: eth0 ip-v4: <filter> ip-v6-link: N/A
Where it says <filter> is where your WAN IP, IPv4, MAC address etc will appear
- 43,712
use ip!
ip addr show
then look for the relevant adapter (not lo, and usually eth0), and locate the ip address near inet.
- 1,245
- 18
- 36
to make this fast, use stunclient with a timeout
"fast" means about 2 times faster than a http request
based on the answer by Victor Klos
#!/bin/sh
whatismyip.sh
get my public IP address from a STUN server
check dependencies
for cmd in timeout stunclient; do
if command -v $cmd &>/dev/null; then continue; fi
echo "error: missing command: $cmd" >&2
exit 1
done
if false; then
find nearby STUN servers
serverlist_url=https://github.com/pradt2/always-online-stun/raw/master/valid_ipv4s.txt
sudo netselect -s 50 -t 10 $(curl -s -L $serverlist_url) |
awk '{ print $2 }' |
xargs sudo netselect -s 10 -t 100 |
awk '{ print $2 }'
fi
function get_public_ipaddr() {
get my public IP address from a STUN server
based on https://askubuntu.com/a/683488/877276
TODO update the server list for your location
local stun_server_list="5.9.87.18:3478 74.125.250.129:19302 212.18.0.14:3478 109.68.96.189:3478"
local stun_server=
local host=
local port=
local res=
local addr=
for stun_server in $stun_server_list; do
IFS=: read host port <<<"$stun_server"
res="$(
# fast response: 0.05 seconds
# slow response: 5 seconds
#time
timeout 0.2
stunclient $host $port
)"
if [ $? = 0 ]; then
# get the "Mapped address"
addr=$(echo "$res" | tail -n1)
addr=${addr:16}
addr=${addr%:*}
echo $addr
return
#else echo "server failed: $stun_server" >&2 # debug
fi
done
return 1 # all servers failed
}
set -e
get_public_ipaddr
- 356
Simply issue a traceroute for any website or service..
sudo traceroute -I google.com
Line 2 always seems to be my public IP address after it gets past my router gateway.
user@user-PC ~ $ sudo traceroute -I google.com
traceroute to google.com (173.194.46.104), 30 hops max, 60 byte packets
1 25.0.8.1 (25.0.8.1) 230.739 ms 231.416 ms 237.819 ms
2 199.21.149.1 (199.21.149.1) 249.136 ms 250.754 ms 253.994 ms**
So, make a bash command.
sudo traceroute -I google.com | awk -F '[ ]' '{ if ( $2 ="2" ) { print $5 } }'
And the output...
(199.21.149.1)
I don't think relying on PHP scripts and the sort is good practice.
- 592
A command with no dependencies except 8.8.8.8 being a GOogle DNS:
echo $(ip route get 8.8.8.8 | awk '{print $NF; exit}')
- 625