26

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
Paul D. Waite
  • 96,640
  • 56
  • 199
  • 270

18 Answers18

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'
Community
  • 1
  • 1
Valentin Rocher
  • 11,667
  • 45
  • 59
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?

Joey Robert
  • 7,336
  • 7
  • 34
  • 31
  • That sounds great, although it doesn’t seem to work on my network here (other machines don’t seem to be able to see 0.0.0.0). – Paul D. Waite May 10 '09 at 21:16
  • 2
    No, you still connect to your regular IP address. So for example if you run that command on your machine, and your IP is 192.168.0.100, the machines on your network still access your machine through 192.168.0.100. Putting in 0.0.0.0 just automates the task rather than looking for the IP. It is what you want. – Joey Robert May 11 '09 at 17:48
  • 2
    huh, just saw I never replied to this. I now use your method all the time, it works just fine. Slightly ashamed I didn’t understand that I didn’t actually need the other machines to connect to 0.0.0.0. – Paul D. Waite Mar 14 '11 at 10:29
  • 1
    Maybe you should change this to be your avcepted answer then? – tripleee Apr 30 '12 at 10:30
  • 1
    This seems to be the best answer. awking ifconfig seems brittle at best. +1 Would suggest this answer be improved with _why_ `0.0.0.0` works. It's a bit of a special ip, after all – willscripted May 31 '15 at 20:49
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̶ ̶

ederollora
  • 1,165
  • 2
  • 11
  • 29
5

The best solution would be to:

ifconfig | sed -n 's/.*inet addr:\([0-9.]\+\)\s.*/\1/p'
Stoic
  • 10,536
  • 6
  • 41
  • 60
  • Works for me. If you don't want the 127.0.0.1 IP, you can use 'head -n1' to get the top result. Might not be portable, not sure. But it got the job done. – wrangler Sep 27 '12 at 15:26
  • I wouldn't say "best". I copied directly into mac osx terminal. I got no results returned. – Mike S. Mar 15 '15 at 15:39
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
Matt Crampton
  • 380
  • 4
  • 4
  • 2
    This is one of the cleaner answers that also [almost] works on OS X, although I happen to need `en0` instead of `en1` (or just drop the network name to get all). – natevw Sep 05 '13 at 06:02
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.

stjernstrom
  • 99
  • 3
  • 7
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/

Heath Borders
  • 30,998
  • 16
  • 147
  • 256
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
  • 31
  • 1
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

M.S. Arun
  • 1,053
  • 13
  • 22
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.

James Gregory
  • 14,173
  • 2
  • 42
  • 60
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}'
Eric Lubow
  • 763
  • 2
  • 12
  • 30
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.

oblalex
  • 5,366
  • 2
  • 24
  • 25
  • I don't appear to have `resolveip` on my Mac OS X machine (10.7.3). – Paul D. Waite Aug 29 '12 at 12:27
  • I have no `$HOSTNAME` and if I do `resolveip -s $(hostname)` it gives 127.0.0.1 which is expected in many cases since /etc/hosts usually contains such kind of recprd – ony Aug 24 '13 at 09:54
  • Pardon me, I was using Arch Linux at that moment and I did not mention that. The post is updated. – oblalex Aug 24 '13 at 10:54
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

Community
  • 1
  • 1
C0D3LIC1OU5
  • 8,600
  • 2
  • 37
  • 47
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
  • 9
  • 1
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.

tangxinfa
  • 1,410
  • 13
  • 12
0

On Android Shell, I'm tried:

ifconfig eth0 | grep 'inet addr' | tr -d '^[A-Za-z]+$' | tr -s ':' | cut -d ':' -f 2
拉拉姬
  • 140
  • 1
  • 8
-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
Farshad
  • 337
  • 2
  • 9