[Django]-How do I find my computer's IP address using the bash shell?

54👍

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'

15👍

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?

7👍

Thi should work as well as other commands I’ve already seen:

ifconfig eth0 | grep inet | awk '{print $2}' | cut -d':' -f2
  • Replace eth0 with the desired interface (eth0, eth1, wlan0…)

I̶ ̶t̶h̶i̶n̶k̶ ̶y̶o̶u̶ ̶c̶a̶n̶ ̶a̶l̶s̶o̶ ̶w̶r̶i̶t̶e̶:̶

̶ ̶ ̶ ̶h̶o̶s̶t̶n̶a̶m̶e̶ ̶-̶I̶ ̶|̶ ̶c̶u̶t̶ ̶-̶d̶’̶ ̶’̶ ̶-̶f̶1̶ ̶

5👍

The best solution would be to:

ifconfig | sed -n 's/.*inet addr:\([0-9.]\+\)\s.*/\1/p'
👤Stoic

5👍

Folks are using character counts to pull the right columns from the ip address line, but using spaces as a delim makes this more scalable to different length ip addresses…

ifconfig en1 | grep inet | grep -v inet6 | cut -d" " -f2

4👍

This is a quick and dirty way, that works under OSX

/sbin/ifconfig | grep 'inet ' | grep -v '127.0.0.1' | head -n1 | awk '{print $2}'

Basically get all interfaces with an IPV4 address, skip localhost and then get the first interface.

Also, use path to ifconfig. I have seen to many shell script brake when used from ex. cron because of PATH failure.

4👍

On Mac OS X 10.11.6 El Capitan (and probably older releases), you can print your IP with

ipconfig getifaddr <device>

where <device> is en0, en1, etc.

http://osxdaily.com/2010/11/21/find-ip-address-mac/

3👍

ifconfig is probably what you’re after. You’ll need to either run it through grep to filter out some of the noise though.

3👍

The following command works perfectly for me on RHEL 6.3:

 ifconfig | grep -v '127.0.0.1' | sed -n 's/.*inet addr:\([0-9.]\+\)\s.*/\1/p'
👤SMQ

3👍

Simple Command to find IP Address with default interface.

ip -o route get "8.8.8.8" 2>/dev/null | sed -e 's/^.* src \([^ ]*\) .*$/\1/'  

or

ip route | grep src | awk -F 'src' '{print $NF; exit}' | awk '{print $1}'

or

ip route | sed -n 's/.* src \(.*\) metric .*/\1/p' | uniq

Tested on All Unix OS

2👍

This may not be as elegant as some of the other solutions, but it works on Linux systems and is more comforting to look at than a regex:

ifconfig eth0 | grep 'inet addr:' | awk '{print $2}' | awk -F ':' '{print $2}'

1👍

Try this (if you are an Arch user)

resolveip -s $HOSTNAME

Alternative

For getting IPv4 adress you can use:

host $(uname -n) | grep "address" | grep -v "IPv6" | head -n 1 | awk '{print $4}'

For getting IPv6 one:

host $(uname -n) | grep "IPv6 address" | head -n 1 | awk '{print $5}'

You can replace $(uname -n) with $(hostname) if you’d like.

0👍

Here a solution to find the current IP address:

route -n get default|grep interface|awk ‘{ print $2 }’|xargs ipconfig getifaddr

tested on Mac only.

👤Mat

0👍

This checks your default interface, and use that to retrieve your primary ip.
Helpful when you have many interfaces, and tons of virtual ip addresses:

netstat -rn | gawk '/UG/ {print $NF}' | xargs ifconfig | gawk 'match($0,/inet addr:(.*) B/,a) {print a[1]}'

0👍

Recap: if you’d like to copy/paste to command line and just get the network IP address, here’s what works on Mac (on WiFi):

echo $(ifconfig en0 | grep inet | grep -v inet6 | cut -d" " -f2)

Here is assigning it to bash script variable:

LOCAL_IP=$(ifconfig en0 | grep inet | grep -v inet6 | cut -d" " -f2)

This is based on answers by Valentin Rocher and Matt Kropmton

0👍

Tested on Archlinux only:

ifconfig $(route | awk '{if($1=="default") print $NF}') | awk '{if($1=="inet") print $2}'

Get the default interface with route, get the ip address of interface with ifconfig.

Because the command syntax or output may vary, you may need to change for works on your system.

0👍

On Android Shell, I’m tried:

ifconfig eth0 | grep 'inet addr' | tr -d '^[A-Za-z]+$' | tr -s ':' | cut -d ':' -f 2

-1👍

On Ubuntu( and Debian like distro)

to see your local IP address:

hostname -I | awk '{print $1}' 

to see your Global IP address:

curl -4 icanhazip.com
curl  ifconfig.co //this responds faster

to see all information about your(or any)IP address:

 whois $(curl ifconfig.co)     

assumed you have installed whois on your machine, if it’s not:

sudo apt-get install whois

Leave a comment