How do I find my computer's IP address using the bash shell?

Paul D. Waite picture Paul D. Waite · May 10, 2009 · Viewed 46k times · Source

Every now and again, I need to start the Django development server, and have it viewable by other machines on my network, as described here:

http://docs.djangoproject.com/en/dev/ref/django-admin/#runserver

My machine’s IP address tends to change every now and again, so I’d like to have a little shell alias or something that spits out the manage.py command with my machine’s current IP address, maybe like this:

python manage.py runserver $(COMMAND TO FIND MY MACHINE’S IP ADDRESS GOES HERE):8000

Answer

Valentin Rocher picture Valentin Rocher · May 10, 2009
ifconfig en0 | grep inet | grep -v inet6

Output of above is expected to be in the following form:

inet 192.168.111.1 netmask 0xffffff00 broadcast 192.168.111.255

Add an awk statement to print the second column to avoid using cut (awk is a pretty standard unix tool):

ifconfig en0 | grep inet | grep -v inet6 | awk '{print $2}'

I use the following to get the current IP when on a LAN where the first few numbers of the IP are always the same (replace 192.168.111 with your own numbers):

ifconfig | grep 192.168.111 | awk '{print $2}'

To get the ip of another machine that you know the name of, try (replace hostname and 192.168.111 with your own values):

ping -c 1 hostname | grep 192.168.11 | grep 'bytes from' | awk '{print $4}' | sed 's/://g'