71

After I've booted up, what's the easiest way to obtain and display the IP address that the device is currently using?
I'm using Raspbian, and ifconfig doesn't appear to be installed.

Are there any widgets that display this information in LXDE?

Zoot
  • 2,986
  • 3
  • 25
  • 36

18 Answers18

77

ifconfig (part of the package net-tools) is being deprecated and replaced by the newer ip command. You can use one of the following from the command line to determine your IP address:

ip addr show

Or a shortened version of this:

ip a s

This will typically show every ip address the system has, including 127.0.0.1 or ::1 - the localhost address. The addresses remaining that are not the localhost address (or an IPv6 link local address starting with fe80::) will usually be network accessible addresses.

The command

hostname --ip-address

will also return the network IP address if your computer has been assigned a domain name by the DHCP server or a domain name is otherwise configured, but may return the localhost address if this is not the case.

Although being depreciated, ifconfig is often installed by default still. One reason ifconfig may not work is because it usually resides in /sbin which may not be in your path. You may be able to run ifconfig as a normal user by running:

/sbin/ifconfig

If this doesn't work, it means ifconfig is not installed. You can install it with:

sudo apt-get install net-tools
Fred
  • 4,552
  • 18
  • 29
Steve Robillard
  • 34,687
  • 17
  • 103
  • 109
13

You can use this little python script as well.

import socket

def get_local_ip_address(target):
  ipaddr = ''
  try:
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    s.connect((target, 8000))
    ipaddr = s.getsockname()[0]
    s.close()
  except:
    pass

  return ipaddr
print "Raspberry Pi - Local IP Address"
print(get_local_ip_address('10.0.1.1'))
print(get_local_ip_address('google.com'))
Bryan
  • 131
  • 2
8

As an alternative to finding the DHCP assigned IP address, I've added a reserved IP address in my router/DHCP server. It matches the MAC address of the Raspi and always assigns the same IP address - even after a fresh install of the OS.

With Wheezy now having SSH enabled by default, it means I can login to a freshly installed Raspberry Pi without ever needing to connect a keyboard or monitor.

Apologies for not answering the question directly, but it seemed closely related enough to suggest.

Greg Woods
  • 181
  • 4
7

You may be able to check the DHCP status/logs on your DHCP server. Especially if it's on your home network. On all the routers I have owned this has been fairly easy to find.

This is helpful if you are running headless and just want to know the address to ssh to.

John La Rooy
  • 11,947
  • 9
  • 47
  • 75
  • @gnibbler +1 for addressing the issue for those running headless. One think to note, identifying which device is the Pi can be tricky since it does not always identify itself over the network, and so, may show up without a name in the attached devices list. This thread http://www.raspberrypi.org/phpBB3/viewtopic.php?f=36&t=6998 has more on the problem and solution – Steve Robillard Aug 03 '12 at 11:07
  • @SteveRobillard, mine does identify itself, but it also may help someone if they see a MAC address starting like b8:27:eb:xx:xx:xx, it's probably a RPi :) – John La Rooy Aug 03 '12 at 11:14
5

If you want to see your external ip address use this on your command line

curl http://ipecho.net/plain; echo;

You could create a function to make it easier.

Edit your .bashrc and add the following function at the end of the file.

Function to display the external ip address

Function to display the external ip address

Calling your function from cli

enter image description here

You may find more interesting ways to obtain your ip address in this link

http://www.if-not-true-then-false.com/2010/linux-get-ip-address/

andreskwan
  • 171
  • 1
  • 3
5
$ host raspberrypi
raspberrypi has address 192.168.1.20

$ host raspberrypi | grep ‘address’ | cut -d’ ‘ -f4
192.168.1.20

$ nslookup 192.168.1.20
Server:  192.168.1.1
Address:    192.168.1.1#53
20.1.168.192.in-addr.arpa   name = raspberrypi.

$ nslookup 192.168.1.20 | grep ‘=’ | cut -d’ ‘ -f3
raspberrypi
devpsp
  • 59
  • 1
  • 1
5

What worked for me :

sudo ifconfig

since ifconfig was at sbin/ifconfig

  • While I appreciate your response, Alex Chamberlain's comment and Steve Robillard's response already contain this information. – Zoot Jul 09 '13 at 12:54
2

Baby script to return the ip address, works from a prompt:

ip address list | grep inet | grep -v 127.0.0 | cut -d " " -f 6 | cut -d "/" -f 1

2

Shea Silverman and Jacob Bates have recently created a tool called PIP that allows you to obtain the IP of your raspberry pi without even attaching it to a screen, as it installs a script that send your IP address to a server that you can visit with your main PC. It may not be the best option in every situation, but it is a very clever hack.

Zoot
  • 2,986
  • 3
  • 25
  • 36
1

ip a is a shortcut for ip address

So: ip a

should be sufficient

There is no need to use sudo if all you are interested in doing is viewing the IP address.

For more information, the man page for the ip utility is available by running: man ip

moo
  • 298
  • 2
  • 10
1

I suggest that you use Bonjour instead of having to find the IP address. Let it be assigned via DHCP and as long as your PC is on the same network you can access it by name.

For example.. raspberrypi.local

Once this is setup you can run completely headless, connect to the Pi with SSH, or VNC etc..

Here is an article explaining how to setup for Bonjour. http://www.raspberrypi.org/forums/viewtopic.php?f=66&t=18207

Note that for a windows machine you will need to install Apply Bonjour printer services driver, a tiny thing...

For Mac and Ubuntu, Bonjour is already there.

dfowler7437
  • 286
  • 1
  • 4
1

I saw a variety of answers. some I knew and some i did not know. There is also one I always use that has not been listed yet. If it has and I missed it oops sorry.

sudo hostname -I

Pismurf
  • 313
  • 1
  • 15
0

For some inexplicable reason the answers to this keep getting added/modified. Some are overly complex.

In fact, "Easiest/Best" is off topic, but it is hard to beat

hostname -I

(as in the original answer) which shows IP for multiple interfaces.

Milliways
  • 59,890
  • 31
  • 101
  • 209
0

Just to add some supplement here, the reason you might not have ifconfig is because your system is probably using the iproute2 suite instead. iproute2 includes updated equivalents of the old ifconfig and route suites.

I'm bringing this up because if you start to try commands you're used to with ifconfig or route, your first instinct might be to install those old packages instead of just using the newer equivalent. For instance, if you need to use netstat and realize it's missing, just do a search for "iproute2 netstat equivalent" and you should find the "ss" command. For further reading and documentation, refer to the Linux Foundation's article on it: http://www.linuxfoundation.org/collaborate/workgroups/networking/iproute2

coxley
  • 198
  • 5
  • 1
    I appreciate your answer, but I did have ifconfig installed. I just forgot to sudo before the command. Check out the comments below the answer. – Zoot Apr 16 '13 at 18:20
  • 1
    Doh! I only use Arch and it hasn't used ifconfig for awhile. – coxley Apr 18 '13 at 03:02
  • No problem. It threw me for a loop, too. – Zoot Apr 19 '13 at 20:00
  • Arugh, you don't need to sudo for ifconfig, you just need to call the entire path as it's not in your $PATH currently.

    whereis ifconfig will give you the path if required; then you can run it from there. sudo is not the answer!.

    – djsmiley2kStaysInside Apr 24 '15 at 07:16
0

One liner with ifconfig and sed:

sudo ifconfig wlan0 | sed -En -e 's/.*inet ([0-9.]+).*/\1/p'

Replace wlan0 with the desired interface.

bleater
  • 101
  • 1
-1
curl ipinfo.io 

works for me on both Ubuntu and Raspbian

techraf
  • 4,319
  • 10
  • 31
  • 42
adonet
  • 1
  • 1
  • Just to clarify for those that might not be aware: This particular answer will give the external (internet-facing) IP address of the LAN router or firewall that the Raspberry Pi is on. It won't give the specific LAN IP address of the Pi itself. This could be useful for internet troubleshooting. To obtain the local LAN IP address as the question asked for, look to the other answers here in this question. – MrChips Feb 04 '18 at 16:16
-2

I believe arp -a on either windows or linux would be the simplest once your pi is connected to the network.

goldilocks
  • 58,859
  • 17
  • 112
  • 227
  • 2
    Seems to me this gives addresses of other systems, not your own. Also note man arp currently begins "This program is obsolete." – goldilocks Apr 01 '16 at 14:31
-3

I don't know about LXDE. To know your IP address of your device visit the site Ip-Details.com . Here they will also provide IP location, ISP address, country etc...