Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
Maybe should be on superuser? – Kyle Brandt Jul 27 '09 at 18:23
Seemed like a network configuration question. Also, I'm not in the SuperUser beta. – opierce Jul 27 '09 at 18:28
@opierce, have you tried getting into the beta? – nik Jul 27 '09 at 18:39
@opierce: toward tht end, the "blog" link at the bottom of the page is helpful. Look back a bit... – dmckee Jul 27 '09 at 20:28
Joined SU, thanks! – opierce Jul 28 '09 at 20:54

17 Answers 17

up vote 31 down vote accepted

/sbin/ifconfig -a

share|improve this answer
Yup and if you're planning on remotely administering the server, vi /etc/network/interfaces and set the interface to static (see – gravyface Apr 28 '10 at 13:26

You can use:

/bin/ip addr
share|improve this answer
@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
@javano Thanks! – mpbloch Oct 15 '13 at 19:31
You can make that shorter by just running ip a. – Gerald Kaszuba Dec 31 '13 at 20:42

If you have an internal address in use, checking


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,

That will confirm connectivity through,

  • external IP address (showing it to you on that page)
  • NAT, Port Forwards
  • Firewalls
share|improve this answer
True, but if that is the case, there are probably no NAT/Port Forwarding rules in place to match ssh to whatever the internal IP is. – Kyle Brandt Jul 27 '09 at 18:25
@Kyle, Which can be added... – nik Jul 27 '09 at 18:30
I wish I knew this command before ! thanks – petrus Apr 6 '11 at 18:17
+1 for curl – JatSing Feb 16 '12 at 2:46
Similar to this, there is also curl – jwbensley Jun 19 '13 at 15:04

/bin/hostname -i

share|improve this answer
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
Shorter alternative to @lrkwz's comment /bin/hostname -I – hanxue Apr 9 '14 at 8:58
/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}'
share|improve this answer
+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 7 '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: – sorin Jun 19 '13 at 14:16

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

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

taken from:

share|improve this answer

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

dig +short @ @

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

dig +short

You could also use this command.

share|improve this answer

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:

for INTERFACE in wlan0 eth0; do
    if [ -z $CURRENT_IP ]; then
        CURRENT_IP=$(ip addr show dev $INTERFACE | grep "inet " | awk '{ print $2 }')
share|improve this answer

Just curl this page:

$ curl

share|improve this answer
And how exactly is this superior to the similar services already mentioned during these years? – Esa Jokinen Mar 29 '15 at 20:24

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.

share|improve this answer
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.

share|improve this answer

What I understand is you want to connect a remote ubuntu machine which has dynamic ip. Go 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


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

share|improve this answer
This is my preferred method for dealing with dynamic IPs. is a fantastic, free service. – John Barrett Aug 12 '09 at 8:40

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/;s/.*inet (addr:)?(([0-9]*\.){3}[0-9]*).*/\2/p'

Send credits to

share|improve this answer

Here is what I am using:

LC_ALL=C /sbin/ifconfig | awk '
    /inet addr/ {
        if(($2!="") && ($2!="") && ($2!=""))
            { print $2 };
share|improve this answer

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

ifconfig eth0 | grep "inet " | awk '{gsub("addr:","",$2);  print $2 }' 
share|improve this answer

I once golfed the extraction of IP address on Linux:

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.