46

I'm trying to find a short and robust way to put my IP address into a bash variable and was curious if there was an easier way to do this. This is how I am currently doing it:

ip=`ifconfig|xargs|awk '{print $7}'|sed -e 's/[a-z]*:/''/'`
Dessa Simpson
  • 1,232
  • 1
  • 15
  • 30

20 Answers20

76

I've been struggling with this too until I've found there's a simple command for that purpose

hostname -i

Is that simple!

TuxSax
  • 1,045
  • 2
  • 8
  • 12
64

man hostname recommends using the --all-ip-addresses flag (shorthand -I ), instead of -i, because -i works only if the host name can be resolved. So here it is:

hostname  -I

And if you are interested only in the primary one, cut it:

hostname  -I | cut -f1 -d' '
user000001
  • 32,226
  • 12
  • 81
  • 108
  • 1
    One one server I get from `hostname -I`: `192.168.1.30`, only the single IP. On my VPS server I get this output `127.0.0.2 143.127.52.130 2a00:dee0:ed3:83:245:70:fc12:d196`, so then I need to add some filters. On my `linux tuner `-I` does not exist, so best over all is `ip route get 8.8.8.8 | awk 'NR==1 {print $NF}'` – Jotne Nov 10 '14 at 11:53
  • 1
    Only Linux and no Mac but it's the best answer. – Ostati Jan 30 '17 at 19:13
  • 3
    Note that the man page for `-I` says _Do not make any assumptions about the order of the out-put._ – wbyoung Jun 22 '17 at 17:15
42

ip is the right tool to use as ifconfig has been deprecated for some time now. Here's an awk/sed/grep-free command that's significantly faster than any of the others posted here!:

ip=$(ip -f inet -o addr show eth0|cut -d\  -f 7 | cut -d/ -f 1)

(yes that is an escaped space after the first -d)

Synchro
  • 35,538
  • 15
  • 81
  • 104
24

You can take a look at this site for alternatives.

One way would be:

ifconfig  | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $1}'

A bit smaller one, although it is not at all robust, and can return the wrong value depending on your system:

$ /sbin/ifconfig | sed -n '2 p' | awk '{print $3}'

(from http://www.htmlstaff.org/ver.php?id=22346)

Dessa Simpson
  • 1,232
  • 1
  • 15
  • 30
woliveirajr
  • 9,433
  • 1
  • 39
  • 49
  • I'm kind of looking for a shorter or simpler solution. But thank you for the link I'm going to check it out. –  Jul 26 '11 at 12:11
  • sorry, but I don't think that there's a shorter (or using less programs through pipe) alternatives... – woliveirajr Jul 26 '11 at 12:21
  • The shorter version does not work for me on Ubuntu 12.04, it yields the broadcast address instead of the IP, e.g. `Bcast:XXX.XXX.XXX.XXX` – LexLythius May 15 '14 at 18:41
  • @LexLythius I don't have any 12.04 installation right now, so I can't test it. Please remove the last command (awk) and see where the ip is printed, perhaps it's not the 3rd position anymore – woliveirajr May 15 '14 at 18:43
  • 1
    @woliveirajr In Ubuntu 12.04 it would be `awk | '{print $2}'`. But your longer solution is more interoperable than this. – LexLythius May 15 '14 at 21:33
  • 3
    `ifconfig` is long deprecated on Linux (but not on BSD) and has not been updated since 2001! All should use iproute2 commands to get network info, such as `ip a` – Synchro Jun 16 '16 at 15:02
  • http://serverfault.com/questions/633087/where-is-the-statement-of-deprecation-of-ifconfig-on-linux – woliveirajr Jun 16 '16 at 16:50
  • 1
    You can use `ifconfig eth0 | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $1}'` to get IP address of a specific interface, for instance in this example, `eth0`. – zxcmehran Nov 21 '17 at 07:16
13

The ifdata command (found in the moreutils package) provides an interface to easily retrieve ifconfig data without needing to parse the output from ifconfig manually. It's achieved with a single command:

ifdata -pa eth1

Where eth1 is the name of your network interface.

I don't know how this package behaves when ifconfig is not installed. As Syncrho stated in his answer, ifconfig has been deprecated for sometime, and is no longer found on a lot of modern distributions.

Matthew G
  • 1,090
  • 2
  • 14
  • 23
  • 4
    YES! This should be the correct answer. Thank you so much for showing this excellent and badly needed utility. – suprjami May 10 '15 at 05:46
11

Here is the best way to get IP address of an device into an variable:

ip=$(ip route get 8.8.8.8 | awk 'NR==1 {print $NF}')

NB Update to support new Linux version. (works also with older)

ip=$(ip route get 8.8.8.8 | awk -F"src " 'NR==1{split($2,a," ");print a[1]}')

Why is it the best?

  1. Hostname -I some times get only the IP or as on my VPS it gets 127.0.0.2 143.127.52.130 2a00:dee0:ed3:83:245:70:fc12:d196
  2. Hostnmae -I does not work on all system.
  3. Using ifconfig may not always give the IP you like.
    a. It will fail you have multiple interface (wifi/etcernet) etc.
    b. Main IP may not be on the first found interface
  4. Searching of eth0 may fail if interface have other name as in VPS server or wifi

    ip route get 8.8.8.8

Tries to get route and interface to Googles DNS server (does not open any session)
Then its easy to get the ip or interface name if you like.

This can also be used to get a ip address of an interface to a host on a multiruted net

Jotne
  • 40,548
  • 12
  • 51
  • 55
8

my short version. Useful when you have multiple interface and just want the main ip.

host `hostname` | awk '{print $4}'
skatkatt
  • 81
  • 1
  • 1
5

You can get just awk to do all the parsing of ifconfig:

ip=$(ifconfig | gawk '
    /^[a-z]/ {interface = $1}
    interface == "eth0" && match($0, /^.*inet addr:([.0-9]+)/, a) {
        print a[1]
        exit
    }
')
glenn jackman
  • 238,783
  • 38
  • 220
  • 352
4

Not really shorter or simpler, but it works for me:

ip=$(ip addr show eth0 | grep -o 'inet [0-9]\+\.[0-9]\+\.[0-9]\+\.[0-9]\+' | grep -o [0-9].*)
gpojd
  • 22,558
  • 8
  • 42
  • 71
3
ifconfig | grep -oP "(?<=inet addr:).*?(?=  Bcast)"

When using grep to extract a portion of a line (as some other answers do), perl look-ahead and look-behind assertions are your friends.

The quick explanation is that the first (?<=inet addr:) and last (?= Bcast) parenthesis contain patterns that must be matched, but the characters that match those patters won't be returned by grep, only the characters that are between the two patterns and match the pattern .*? that is found between the sets of parenthesis, are returned.

Sample ifconfig output:

eth0      Link encap:Ethernet  HWaddr d0:67:e5:3f:b7:d3  
          inet addr:10.0.0.114  Bcast:10.0.0.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:1392392 errors:0 dropped:0 overruns:0 frame:0
          TX packets:1197193 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:1294730881 (1.2 GB)  TX bytes:167208753 (167.2 MB)
          Interrupt:18

This will extract your IP address from the ifconfig output:

ifconfig | grep -oP "(?<=inet addr:).*?(?=Bcast)"

To assign that to a variable, use this:

ip=$(ifconfig | grep -oP "(?<=inet addr:).*?(?=Bcast)")

A slightly more in depth explanation:

From man grep:

-o Print only the matched (non-empty) parts of matching lines, with each such part on a separate output line.

-P Interpret the pattern as a Perl regular expression. This is highly experimental and ‘grep -P’ may warn of unimplemented features.

From permonks.org:

(?<=pattern) is a positive look-behind assertion

(?=pattern) is a positive look-ahead assertion

-o Tells grep to only return the portion of the line that matches the pattern. The look-behinds/aheads are not considered by grep to be part of the pattern that is returned. The ? after .* is important since we want it to look for the very next look-ahead after the .* pattern is matched, and not look for the last look-ahead match. (This is not needed if we added a regex for the IP address instead of .*, but, readability).

Jake
  • 625
  • 6
  • 16
3

The following works on Mac OS (where there is no ip commnand or hostname options):

#!/bin/bash

#get interface used for defalt route (usually en0)
IF=$(route get default |grep 'interface' |awk -F: '{print $2}');

#get the IP address for inteface IF
#does ifconfig, greps for interface plus 5 lines, greps for line with 'inet '
IP=$(ifconfig |grep -A5 $IF | grep 'inet ' | cut -d: -f2 |awk '{print $2}');
#get the gateway for the default route
GW=$(route get default | awk '/gateway:/ {print $2}');
2

I am using

IFACE='eth0'
IP=$(ip -4 address show $IFACE | grep 'inet' | sed 's/.*inet \([0-9\.]\+\).*/\1/')

The advantage of this way is to specify the interface (variable IFACE in the example) in case you are using several interfaces on your host.

Moreover, you could modify ip command in order to adapt this snippet at your convenience (IPv6 address, etc).

caligari
  • 2,110
  • 20
  • 25
2

I think the most reliable answer is :

ifconfig  | grep 'inet addr:' | grep -v '127.0.0.1' | awk -F: '{print $2}' | awk '{print $1}' | head -1

AND

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

because when you don't use head -1 it shows all ips....

MLSC
  • 5,872
  • 8
  • 55
  • 89
2

In my script i did need only the network part of the IP, so I did it like that

local=$(hostname -I | awk '{print $2}' | cut -f1,2,3 -d".")

Where the cut -f1,2,3 -d"." can be read as "get first 3 parts separated by commas" To change interfaces just change $2 to your interface number, to get whole IP remove cut.

Demon
  • 21
  • 1
1

In trying to avoid too many pipes, work on various linuxes, set an exit code, and avoiding ifconfig or other packages, I tried the whole thing in awk:

 ip addr show | awk '
     BEGIN {FS="/"}
     /^[0-9]+: eth[0-9]+.*UP*/ {ss=1}
     ss==1 && /^ +inet / {print substr($1,10); exit 0}
     END {exit 1}'

and note that a particular interface can be specified after "ip addr show" if you don't want just the first eth interface. And adapting to ipv6 is a matter of looking for "inet6" instead of "inet"...

  • This now fails since many systems don't use the `eth*` names for interfaces any more. (Mine is `enp0s25` on one laptop.) It also completely ignores `ppp*` and other types of interfaces. – Ian D. Allen Feb 02 '23 at 03:44
1

On mac osx, you can use ipconfig getifaddr [interface] to get the local ip:

$ ipconfig getifaddr en0
192.168.1.30
$ man ipconfig 

DESCRIPTION
     ipconfig is a utility that communicates with the IPConfiguration agent to
     retrieve and set IP configuration parameters.  It should only be used in
     a test and debug context.  Using it for any other purpose is strongly
     discouraged.  Public API's in the SystemConfiguration framework are cur-
     rently the only supported way to access and control the state of IPCon-
     figuration.

     ...

     getifaddr interface-name
                 Prints to standard output the IP address for the first net-
                 work service associated with the given interface.  The output
                 will be empty if no service is currently configured or active
                 on the interface.
Mac Cowell
  • 256
  • 2
  • 5
0

In my case I had some more interfaces in list before eth0. By this command you can get ip4 address for any interface. For that you need to change eth0 to interface that you need.

/sbin/ifconfig eth0 | grep 'inet addr:' | cut -d: -f2 | awk '{print $1}'
Victor
  • 2,321
  • 2
  • 13
  • 12
0

The "eth3" is optional (useful for multiple NIC's)

ipaddress=`ip addr show eth3 | grep 'inet ' | awk '{ print $2}' | cut -d'/' -f1`
Sachin
  • 176
  • 1
  • 6
0

If by "my ip address" you mean "the IP address my machine will use to get to the public Internet to which I am connected", then this very tidy JSON answer may work for you, depending on what O/S your machine runs:

ip=$( ip -j route get 8.8.8.8 | jq -r '.[].prefsrc' )

It does require an ip command that produces JSON output (some say the BusyBox ip command cannot do this), the CLI jq parser that can extract fields from JSON input, and your machine has to know how to get to the public IP at 8.8.8.8.

If you want the IP address your machine would use to get to some other place, such as a local network, put that other IP in place of the public IP 8.8.8.8, e.g.

ip=$( ip -j route get 192.168.1.1 | jq -r '.[].prefsrc' )
ip=$( ip -j route get 10.1.2.3    | jq -r '.[].prefsrc' )

If you only have one interface, then most any non-localhost IP address should work to get your IP address.

Parsing the JSON output with jq is so much simpler than all those complex examples with sed, awk, and grep, but the more complex examples do use tools that are present by default on almost all Unix/Linux/BSD systems.

Ian D. Allen
  • 201
  • 2
  • 7
0

assuming you have something like curl or wget, then one easy way to get external IP addresses with no downstream parsing necessary would be :

(wildcard syntax for wget might differ)

curl -w '\n\n%{url_effective}\n\n' -s 'http://api{,6}.ipify.org' 
98.xxx.132.255

http://api.ipify.org/

2603:7000:xxxx:xxxx:xxxx:c80f:1351:390d

http://api6.ipify.org/

Yielding both IPv4 and IPv6 addresses in one shot

RARE Kpop Manifesto
  • 2,453
  • 3
  • 11