30

Is there a bash command to find the IP address for an Ubuntu box? I need to find the IP address so I can ssh into the machine later.

Brad Gilbert
  • 2,503
  • 2
  • 21
  • 19
opierce
  • 413
  • 1
  • 4
  • 5

19 Answers19

36

/sbin/ifconfig -a

mpbloch
  • 982
  • 9
  • 14
  • Yup and if you're planning on remotely administering the server, `vi /etc/network/interfaces` and set the interface to `static` (see https://help.ubuntu.com/8.10/serverguide/C/network-configuration.html) – gravyface Apr 28 '10 at 13:26
  • Running this command reveals a whole list of numbers. Which one should we use? The `inet addr`? – ComputerScientist Jun 22 '18 at 16:26
27

You can use:

/bin/ip addr
Robert Swisher
  • 1,147
  • 7
  • 14
  • 3
    @opierce You have marked mpbloch's answer as correct, but as an FYI, you should be using this answer, as this is part of the iproute2 suite. `ifconfig` is being phased now. – jwbensley Jun 19 '13 at 15:03
  • 4
    You can make that shorter by just running `ip a`. – gak Dec 31 '13 at 20:42
22

If you have an internal address in use, checking

curl http://myip.dnsomatic.com

might be a good idea on unix shells.
Or, just plonk that URL into your browser.


If you get a different answer from the "ifconfig -a" result,
the ifconfig gave your internal address -- which will probably not work from outside.


Even if all seems fine, you could have a firewall in place that will disallow incoming ssh connections.
At which time you should try the port of interest from a browser on the machine at,

http://www.canyouseeme.org/

That will confirm connectivity through,

  • external IP address (showing it to you on that page)
  • NAT, Port Forwards
  • Firewalls
nik
  • 7,100
  • 2
  • 25
  • 30
21

/bin/hostname -i

James
  • 627
  • 7
  • 14
  • 3
    In this case I would suggest using ```hostname --all-ip-addresses```. Display all network addresses of the host. This option enumerates all configured addresses on all network interfaces. The loopback interface and IPv6 link-local addresses are omitted. Contrary to option -i, this option does not depend on name resolution. Do not make any assumptions about the order of the output. – lrkwz Jan 24 '14 at 11:36
  • 5
    Shorter alternative to @lrkwz's comment `/bin/hostname -I` – hanxue Apr 09 '14 at 08:58
  • Thanks @hanxue - this ought to be an answer of it's own - the shortest command with the cleanest output - just what I was looking for! In my experience (Ubuntu), `/bin/hostname -i` just gives `127.0.1.1`, which is useless. – Roger Dueck Jun 27 '17 at 14:50
  • This is the correct answer – serveraddict Feb 08 '23 at 22:11
13
 curl icanhazip.com

tee hee!

From http://www.commandlinefu.com/commands/view/2966/return-external-ip

chickeninabiscuit
  • 1,104
  • 6
  • 20
  • 33
10
/sbin/ifconfig|grep inet|head -1|sed 's/\:/ /'|awk '{print $3}'

If you need your internal adress append your interface after ifconfig, e.g.

 /sbin/ifconfig eth0|grep inet|head -1|sed 's/\:/ /'|awk '{print $3}'
Johan
  • 101
  • 3
  • +1, this is exactly what *I* needed to solve a problem. Hurray for one-liners! – J. Polfer Feb 26 '10 at 18:53
  • +1 Without the head -1: /sbin/ifconfig|grep inet|sed 's/\:/ /'|awk 'NR==1 {print $3}' – joeslice Jul 07 '10 at 15:40
  • Thanks, it works on Debian but on OS X it doesn't work, returning some garbage. It would be great if we could come of with a line that works on both. Here is the output of ifconfig on OS X: https://gist.github.com/ssbarnea/5814657 – sorin Jun 19 '13 at 14:16
6

If you are behind a NAT, and need the public IP, use this:

wget -q -O - checkip.dyndns.org|sed -e 's/.Current IP Address: //' -e 's/<.$//'

taken from: http://www.go2linux.org/what-is-my-public-ip-address-with-linux

JakeRobinson
  • 2,904
  • 18
  • 26
5

If you have multiple interfaces, could be useful to specify which one you want IP. if you want IPV4 address of interface 'eth0':

ip addr show dev eth0 | grep "inet " | awk '{ print $2 }' 

if you want IPV6 address of interface 'eth0':

ip addr show dev eth0 | grep "inet6 " | awk '{ print $2 }' 

if you want to search for an IP between two common interfaces of a laptop, wlan0 and eth0:

CURRENT_IP=''
for INTERFACE in wlan0 eth0; do
    if [ -z $CURRENT_IP ]; then
        CURRENT_IP=$(ip addr show dev $INTERFACE | grep "inet " | awk '{ print $2 }')
    fi
done
LoreLLo
  • 51
  • 1
  • 3
3

If you need to find out what the IP address of your router, you could run this command.

dig +short myip.opendns.com @208.67.222.222 @208.67.220.220

If you are using OpenDNS for your dns server, you could shorten it to:

dig +short myip.opendns.com

You could also use this command.

curl http://myip.dnsomatic.com
Brad Gilbert
  • 2,503
  • 2
  • 21
  • 19
3

Just curl this page:

$ curl wtfismyip.com/text

mansi
  • 31
  • 3
2

Here is a one line that works on Linux and OS X too, and it will return the first address that is not local:

ifconfig | sed -En 's/127.0.0.1//;s/.*inet (addr:)?(([0-9]*\.){3}[0-9]*).*/\2/p'

Send credits to https://stackoverflow.com/a/13322549/99834

sorin
  • 8,016
  • 24
  • 79
  • 103
2
LOCAL_IP=`/bin/hostname -I | sed 's/ //g'`
techraf
  • 4,243
  • 8
  • 29
  • 44
1

The simplest way to go about it is probably

ifconfig eth0

assuming the machine has a single IP address on the default wired interface - you might need

ifconfig wlan0

if it's on WiFi.

David North
  • 760
  • 1
  • 5
  • 12
1
ip address show scope link

It will show you the IP address of living - has link - interfaces. But it is not a bash command. Bash has no ability to know about IP and network at all.

Saabi
  • 371
  • 1
  • 3
1

What I understand is you want to connect a remote ubuntu machine which has dynamic ip. Go dyndns.org site and open a free account. Then on the remote machine you need to install a dynamic ip tool.

sudo aptitude install dyndns-client

so you can ssh remote machine via

ssh username@yourdynamicnamealias.dyndns.org

So after configuration you will never need the remote machine ip address.

rainman
  • 33
  • 6
1

Here is what I am using:

LC_ALL=C /sbin/ifconfig | awk '
    /inet addr/ {
        gsub("addr:","");
        if(($2!="127.0.0.1") && ($2!="0.0.0.0") && ($2!=""))
            { print $2 };
    }'
jlliagre
  • 8,861
  • 18
  • 36
1

if you need just a single IP of given interface you can do:

ifconfig eth0 | grep "inet " | awk '{gsub("addr:","",$2);  print $2 }' 
Michael Hampton
  • 244,070
  • 43
  • 506
  • 972
DmitrySemenov
  • 835
  • 2
  • 15
  • 27
0

I once golfed the extraction of IP address on Linux:

http://www.catonmat.net/blog/golfing-the-extraction-of-ip-addresses-from-ifconfig/

Peter Krumins
  • 3,500
  • 4
  • 22
  • 20
0

If you're looking for a public IP address of the box, you could use the following:

  • dig @ns1.google.com -t txt o-o.myaddr.l.google.com +short | tr -d \"

You could use dig(1) options like -4 or -6 to specifically look for an IPv4 or IPv6 address; Google will provide an answer in a record of TXT type, which will have quotes around it when presented by dig; if you want to subsequently use the variable with utilities like traceroute, you gotta use something like tr(1) to remove said quotes.

Other options include whoami.akamai.net and myip.opendns.com, which answer with A and AAAA records (instead of TXT as in the above example from Google), so, they don't require having the quotes removed:

  • dig -4 @ns1-1.akamaitech.net -t a whoami.akamai.net +short

  • dig -4 @resolver1.opendns.com -t any myip.opendns.com +short

  • dig -6 @resolver1.opendns.com -t any myip.opendns.com +short

Here's a sample script that uses all the options above to set the variables:

#!/bin/sh
IPANY="$(dig @ns1.google.com -t txt o-o.myaddr.l.google.com +short | tr -d \")"
GOOGv4="$(dig -4 @ns1.google.com -t txt o-o.myaddr.l.google.com +short | tr -d \")"
GOOGv6="$(dig -6 @ns1.google.com -t txt o-o.myaddr.l.google.com +short | tr -d \")"
AKAMv4="$(dig -4 @ns1-1.akamaitech.net -t a whoami.akamai.net +short)"
CSCOv4="$(dig -4 @resolver1.opendns.com -t a myip.opendns.com +short)"
CSCOv6="$(dig -6 @resolver1.opendns.com -t aaaa myip.opendns.com +short)"
printf '$GOOG:\t%s\t%s\t%s\n' "${IPANY}" "${GOOGv4}" "${GOOGv6}"
printf '$AKAM:\t%s\n$CSCO:\t%s\t%s\n' "${AKAMv4}" "${CSCOv4}" "${CSCOv6}"

If you're looking for a private IP address, or for a set of all IP addresses assigned to the box, you could use some combination of ifconfig (on BSD and GNU/Linux), ip addr (on GNU/Linux), hostname (options -i and -I on GNU/Linux) and netstat to see what's going on.

cnst
  • 13,848
  • 9
  • 54
  • 76