Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 
like image 428
Paul D. Waite Avatar asked May 10 '09 17:05

Paul D. Waite


People also ask

How do I find my IP address in bash?

To find out the IP address of Linux/UNIX/*BSD/macOS and Unixish system, you need to use the command called ifconfig on Unix and the ip command or hostname command on Linux. These commands used to configure the kernel-resident network interfaces and display IP address such as 10.8. 0.1 or 192.168. 2.254.


2 Answers

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' 
like image 126
Valentin Rocher Avatar answered Sep 27 '22 22:09

Valentin Rocher


You might already be aware, but running

python manage.py runserver 0.0.0.0:8000 

makes your machine visible to everyone on the network.

Is there a reason you'd need to specify your IP?

like image 32
Joey Robert Avatar answered Sep 27 '22 20:09

Joey Robert