Hide

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again.

Delete that bloated snippets file you've been using and share your personal repository with the world. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.


If you have a new feature suggestion or find a bug, please get in touch via http://commandlinefu.uservoice.com/

Get involved!

You can sign-in using OpenID credentials, or register a traditional username and password.

First-time OpenID users will be automatically assigned a username which can be changed after signing in.

Universal configuration monitoring and system of record for IT.
Hide

Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for:

Hide

News

May 19, 2015 - A Look At The New Commandlinefu
I've put together a short writeup on what kind of newness you can expect from the next iteration of clfu. Check it out here.
March 2, 2015 - New Management
I'm Jon, I'll be maintaining and improving clfu. Thanks to David for building such a great resource!
Hide

Top Tags

Hide

Functions

Hide

Credits

Get your local IP regardless of your network interface

Terminal - Get your local IP regardless of your network interface
ifconfig|sed '/inet/!d;/127.0/d;/dr:\s/d;s/^.*:\(.*\)B.*$/\1/'
2010-12-01 21:08:29
User: marssi
Functions: ifconfig sed
1
Get your local IP regardless of your network interface

Shows only IP-addresses of ifconfig except 127.0.0.0/8.

I fixed the script to work on more systems and configs

short info

/inet/!d; #grep inet

/127.0/d; # grep -v 127.0

/dr:\s/d; # grep -v dr:

s/^.*:\(.*\)B.*$/\1/ # remove everything exept between : and B

Alternatives

There is 1 alternative - vote for the best!

Terminal - Alternatives
{ if (/^[A-Za-z0-9]/) { interface=$1; next } else { if (/inet [Aa][d]*r/) { split($2,ip,":") } else { next } } print interface"\t: "ip[2] }
2010-12-14 15:38:25
User: gismo82
Tags: ifconfig awk
1

Shows the interface and the ip-address

ifconfig | sed -ne 's/^.*inet \(addr:\)*\([^ ]*\).*/\2/;te' -e 'd;:e' -e '/^127\./d;p'
2010-12-05 17:14:57
User: dramaturg
Functions: ifconfig sed
Tags: ifconfig sed
0

This also works on non-Linux machines. If you have GNU sed you can do it more elegantly:

ifconfig | sed -n 's/^\s*inet \(addr:\)\?\([^\s]*\) .*/\2/;T;/^127\./d;p'
ifconfig | grep -o "inet [^ ]*" | cut -d: -f2
2010-12-06 10:36:52
User: dooblem
Functions: cut grep ifconfig
Tags: ifconfig grep cut
-2

This is what we use.

You can grep -v 127.0.0.1 if you wish.

ifconfig | grep "inet\ " | grep -v "127.0" | sed -e 's/inet\ addr://g' | sed -e 's/Bcast:/\ \ \ \ \ \ \ /g' | cut -c 1-29 | sed -e 's/\ //g'

Know a better way?

If you can do better, submit your command here.

What others think

Nice, this is much better than my command, though a little cryptic to a sed newbie like myself.

Comment by LinuxMan 239 weeks ago

On my Debian PCs the marssi method doesn't report the local IP but the Bcast

Comment by rollopack 238 weeks and 6 days ago

I tested it on debian lenny and sid, both works for me.

If you wanna try this one on your system rollopack?

ifconfig|sed '/inet/!d;/127.0/d;/dr:\s/d;s/^.*:\(.*\)B.*$/\1/'

The problem with my suggestion is that if you use it in a script and put it in a variable, the variable might have extra whitespaces in the end.

Comment by marssi 238 weeks and 6 days ago

ifconfig|sed '/inet/!d;/127.0/d;/dr:\s/d;s/^.*:\(.*\)\s.*$/\1/'

192.168.0.255

Which is the Bcast address.

Comment by carlesso 238 weeks and 6 days ago

ifconfig|sed '/inet/!d;/127.0/d;/dr:\s/d;s/^.*:\(.*\)B.*$/\1/'

192.168.1.3 Ok - IP address

ifconfig|sed '/inet/!d;/127.0/d;/dr:\s/d;s/^.*:\(.*\)\s.*$/\1/'

192.168.1.255 Fail - Bcast address

Comment by rollopack 238 weeks and 2 days ago

thx

Comment by marssi 238 weeks ago

Your point of view

You must be signed in to comment.