Reputation: 98816
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
Upvotes: 26
Views: 55069
Reputation: 420
On Windows you can use:
ipconfig
or
ipconfig /all
or this bash script:
ipconfig | findstr "IPv4" | findstr "Address" | awk "{print $2}"
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
Upvotes: 0
Reputation:
On Android Shell, I'm tried:
ifconfig eth0 | grep 'inet addr' | tr -d '^[A-Za-z]+$' | tr -s ':' | cut -d ':' -f 2
Upvotes: 0
Reputation: 1510
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.
Upvotes: 0
Reputation: 1187
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
Upvotes: 3
Reputation: 32117
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/
Upvotes: 4
Reputation: 8680
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
Upvotes: 0
Reputation: 1181
Thi should work as well as other commands I've already seen:
ifconfig eth0 | grep inet | awk '{print $2}' | cut -d':' -f2
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̶ ̶
Upvotes: 7
Reputation: 306
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]}'
Upvotes: 0
Reputation: 5666
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.
Upvotes: 1
Reputation: 803
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}'
Upvotes: 2
Reputation: 31
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'
Upvotes: 3
Reputation: 11669
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'
Upvotes: 53
Reputation: 119
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.
Upvotes: 4
Reputation: 518
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
Upvotes: 5
Reputation: 10754
The best solution would be to:
ifconfig | sed -n 's/.*inet addr:\([0-9.]\+\)\s.*/\1/p'
Upvotes: 5
Reputation: 9
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.
Upvotes: 0
Reputation: 7574
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?
Upvotes: 15
Reputation: 14223
ifconfig
is probably what you're after. You'll need to either run it through grep
to filter out some of the noise though.
Upvotes: 3