182

I'm trying to use just the IP address (inet) as a parameter in a script I wrote.

Is there an easy way in a unix terminal to get just the IP address, rather than looking through ifconfig?

Pro Backup
  • 729
  • 14
  • 34
Mason
  • 6,893
  • 15
  • 71
  • 115

36 Answers36

230

You can write a script that only return the IP like:

/sbin/ifconfig eth0 | grep 'inet addr' | cut -d: -f2 | awk '{print $1}'

For MAC:

ifconfig | grep "inet " | grep -v 127.0.0.1 | cut -d\  -f2

Or for linux system

hostname -i | awk '{print $3}' # Ubuntu 

hostname -i # Debian
Book Of Zeus
  • 49,509
  • 18
  • 174
  • 171
  • 6
    `hostname -i` on mine it returns: `::1 127.0.1.1 192.168.1.100` – Book Of Zeus Dec 16 '11 at 02:52
  • 1
    On Debian I got just one ip. So using `hostname` isn't portable. – Timofey Stolbov Dec 16 '11 at 03:00
  • 1
    It says that -i and -I are both illegal options – Mason Dec 16 '11 at 03:08
  • The `hostname` command doesn't work on Mountain Lion for this task. The `man` page says the only flags it accepts are `[-fs]` which frankly aren't helpful here... – Matt Aug 16 '13 at 19:32
  • 5
    In summary: the first method depends on the adapter, which is not always the same. The second one, for me, shows two IP addresses, and the last two don't work on Mac. – Matt Aug 16 '13 at 19:33
  • how do you store that output in a variable? – Pototo Jun 17 '14 at 22:34
  • @Pototo http://www.cyberciti.biz/faq/unix-linux-bsd-appleosx-bash-assign-variable-command-output/ – Book Of Zeus Jul 23 '14 at 23:42
  • `cut -d/` works for me(forward slash). not the back slash one – mr5 May 18 '17 at 08:37
  • 5
    ifconfig is deprecated and unreliable here, e.g. the grep fails on debian 9 (stretch) because it no longer outputs "inet addr: 172.17.0.4", it only outputs "inet 172.17.0.4". Use "ip" instead as described in https://stackoverflow.com/a/26694162/72717. – jamshid Jun 15 '19 at 23:33
  • I must use `adr` instead of `addr`. – Rohlik Jul 07 '20 at 09:19
  • @BookOfZeus I mean `/sbin/ifconfig eth0 | grep 'inet adr' | cut -d: -f2 | awk '{print $1}'` instead of `/sbin/ifconfig eth0 | grep 'inet addr' | cut -d: -f2 | awk '{print $1}'`. – Rohlik Jul 10 '20 at 08:07
  • /sbin/ifconfig eth0 | grep 'inet ' | cut -d: -f2 | awk '{print $2}' – Jack Liu Shurui Jul 27 '20 at 06:53
  • If you read `man hostname` you'll see that nowadays you're supposed to say `hostname --all-ip-addresses` on Linux. That will print all IP addresses you have separated by spaces. If you don't run multiple NICs or virtual network you will get only one IP number. – Mikko Rantalainen Dec 17 '20 at 19:13
  • @MikkoRantalainen ```$ hostname --all-ip-addresses | sed '1s/[^ \t]//g' |wc -c``` result: 10 – Book Of Zeus Jan 17 '21 at 19:53
  • @BookOfZeus Why would you want to count number of characters in the output of `hostname --all-ip-addresses`? – Mikko Rantalainen Jan 20 '21 at 11:05
  • @MikkoRantalainen lol that counts the number of IPs ... my point is if you have multiple interfaces ```hostname --all-ip-addresses``` is not enough. So this commands output : ```10.0.0.18 172.22.0.1 172.19.0.1 172.20.0.1 172.18.0.1 172.21.0.1 172.17.0.1 172.23.0.1 192.168.42.1 etc....``` – Book Of Zeus Jan 27 '21 at 05:05
  • @BookOfZeus: ok, I have one IP and your script had output `2` so it's counting off-by-one at least. Doing something like `hostname --all-ip-addresses | xargs -rn1 echo | wc -l` or `hostname --all-ip-addresses | grep -o ' ' | wc -l` should emit correct number. – Mikko Rantalainen Jan 27 '21 at 15:37
  • From the entry for the `-I`/`--all-ip-addresses` option in the `hostname(1)` manpage: `Do not make any assumptions about the order of the output.` – kevmitch Jun 11 '21 at 21:53
110

This will give you all IPv4 interfaces, including the loopback 127.0.0.1:

ip -4 addr | grep -oP '(?<=inet\s)\d+(\.\d+){3}'

This will only show eth0:

ip -4 addr show eth0 | grep -oP '(?<=inet\s)\d+(\.\d+){3}'

And this way you can get IPv6 addresses:

ip -6 addr | grep -oP '(?<=inet6\s)[\da-f:]+'

Only eth0 IPv6:

ip -6 addr show eth0 | grep -oP '(?<=inet6\s)[\da-f:]+'
SAM
  • 1,431
  • 1
  • 10
  • 6
  • The `grep -oP` fails on busybox v1.24.1: invalid option -- P – Pro Backup Aug 18 '16 at 11:30
  • 1
    grep is not installed on just about every docker conatiner for prod. So although these are all good commands, they won't be useful on containers :-( – AndyGee Feb 27 '19 at 08:07
  • 2
    Best answer for my particular case. You need `-P` switch for Perl regular expressions or otherwise we can't use the lookbehind `(?<=inet\s)` token. You can get a similar result by running `grep -oe 'inet [0-9\.]\+'` or `grep -oe 'inet6 [0-9a-f:]\+'` but this way I can't get rid of the first word. In SuSE `man grep` reports that `-P` flag is experimental. – Jorge Bellon Mar 08 '19 at 08:17
  • This is the best answer. ifconfig is deprecated several years now on most os's. – pozcircuitboy Jun 19 '19 at 20:39
  • 4
    A shorter version of the second command: `ip -4 a show eth0 | grep -Po 'inet \K[0-9.]*'` – tukusejssirs Jan 19 '20 at 10:57
  • You can get rid of regexp when using -br (brief) switch for ip command: `ip -4 -br addr show eth0 | awk '{print $3}'` – isp_developer Oct 21 '21 at 10:48
  • grep with -P and ip with -br dont work everywhere (alpine) this will work everywhere though `ip -4 a show eth0 | awk '/inet/ {gsub(/\/\S+$/,"",$2) ; print $2}'` – krad Nov 23 '22 at 11:27
61

Generally, it is never guaranteed that a system will only have one IP address, for example, you can have both an ethernet and wlan connections, and if you have an active VPN connection then you'll have yet another IP address.

Linux

On Linux, hostname -I will list the current IP address(es). Relying on it always returning just one IP address will most likely not work as expected under some scenarios (i.e. a VPN link is up, multiple ethernet adapters, etc), so a more reliable way would be converting the result to an array and then loop over the elements:

ips=($(hostname -I))

for ip in "${ips[@]}"
do
    echo $ip
done

Note: If hostname -I returns the IP both in IPv4 and IPv6 formats then you can use instead hostname -I | cut -f1 -d' ' to only show the IPv4 IP.

OSX

On OSX, if you know the interface, you could use:

~$ ipconfig getifaddr en0
# OUTPUT: 192.168.1.123

which will return just the IP address.

To detect dynamically the (first) active network interface on MacOS:

network_device=$(scutil --dns |awk -F'[()]' '$1~/if_index/ {print $2;exit;}')
ip=$(ipconfig getifaddr "$network_device")
echo $ip
### OUTPUT: 192.168.1.123 

Also, getting the IP address becomes non-deterministic in case both a cable and wifi connections are established, when a machine has more than one ethernet interfaces, or when VPN tunnels are up.

Getting the external IP

If you need the external IP, then you can query a text-mode service, for example curl https://ipecho.net/plain would return a plain text external IP.

A possibly faster alternative is to query a known DNS server, e.g.:

dig @ns1-1.akamaitech.net ANY whoami.akamai.net +short
ccpizza
  • 28,968
  • 18
  • 162
  • 169
27
hostname -I  

This command will give you the exact IP address as you want in Ubuntu. Note the flag is uppercase.

ᄂ ᄀ
  • 5,669
  • 6
  • 43
  • 57
Vignajeth
  • 658
  • 1
  • 7
  • 10
15

To get only the IP address on Mac OS X you can type the following command:

ipconfig getifaddr en0
MBT
  • 21,733
  • 19
  • 84
  • 102
Tukan3
  • 278
  • 3
  • 6
15

On latest Ubuntu versions (14.04 - 16.04), this command did the trick for me.

hostname -I | awk '{print $1}'
Da CodeKid
  • 723
  • 10
  • 12
  • 5
    From the manual page: "[...] This option enumerates all configured addresses on all network interfaces. [...] Do not make any assumptions about the order of the output". The last sentence tells you that `print $1` may or may not give you the correct result. – 9769953 Jul 22 '19 at 08:41
  • Bad solution as the hostname is often bound to the loopback interface, and how does this work in multihomed environments? – krad Nov 23 '22 at 11:30
10

If you have limited environment, you may use this command:

ip -4 addr show dev eth0 | grep inet | tr -s " " | cut -d" " -f3 | head -n 1
caglar
  • 380
  • 3
  • 8
  • 5
    Or even: `ip -o -4 addr show dev eth0 | cut -d' ' -f7 | cut -d'/' -f1` – Jose Alban Apr 10 '18 at 10:11
  • Yes, you are right. But be careful, if eth0 has more than one IP address, this shows all of them. – caglar May 09 '18 at 08:10
  • bash-4.4# ip -4 addr show dev eth0 | grep inet | tr -s " " | cut -d" " -f3 | head -n 1 172.17.0.3/16 So not good with the /16 – AndyGee Feb 27 '19 at 08:28
  • Adding one more to the loop: `ip -4 addr show eth0 | grep inet | awk '{print $2}' | cut -d'/' -f1`. For both v4 and v6: `ip addr show eth0 | grep inet | awk '{print $2}' | cut -d'/' -f1` – Sumit Murari Jun 01 '19 at 08:09
  • If the interface name is not known (or differs between platforms) but you search for the **private** ip address then instead of `ip -4 addr show eth0 | grep inet` do a: `ip -4 addr show | grep 192` – cyberbird Feb 05 '20 at 11:04
  • This also works `ip -4 address show eth0 | awk '/inet/{split($2,a,"/");print a[1];exit}'` – sparks Jul 13 '21 at 10:06
10

I prefer not to use awk and such in scripts.. ip has the option to output in JSON.

If you leave out $interface then you get all of the ip addresses:

ip -json addr show $interface | \
  jq -r '.[] | .addr_info[] | select(.family == "inet") | .local'
Tal Zion
  • 1,331
  • 2
  • 14
  • 23
  • I like the way the result of command in json structural format like ip command. here is another way to get the IP address similar as above ```bash ip -j addr show eth0 | jp "[0].addr_info[?family== 'inet'].local | [0]" ``` – Jack Liu Shurui Jul 27 '20 at 07:20
  • Note that this only works on newish releases. For instance, it doesn't work on CentOS 7.5 – Jean Spector Dec 16 '20 at 19:58
  • 1
    Debian 11 here. Thanks for the -j pointer (the more you know). My take is: `ip -json -pretty -family inet addr show scope global | sed -n '/local/{s/[^0-9.]//g;p;q}'`. This will print the IPv4 address of the first interface with global scope (that excludes the loopback). Another take witout sed: `ip -json -pretty -family inet addr show scope global | grep -m 1 local | tr -d -c 0-9.`. And finally if you can, you may want to give jq a shot, which is the non-hacky way of processing ip's output (FINALLY a tokenizer): `ip -json -family inet addr show scope global | jq -r '.[0].addr_info[0].local'` – pupitetris Jul 24 '22 at 15:00
  • @pupitetris good hint about `scope global`. Probably in many cases it is desirable to omit the `0` indexes in `jq`. Then you will get one IP address per line if there is more than one: `ip -json -family inet addr show scope global | jq -r '.[].addr_info[].local'` – pabouk - Ukraine stay strong Oct 09 '22 at 17:04
6

Command ifconfig is deprected and you should use ip command on Linux.

Also ip a will give you scope on the same line as IP so it's easier to use.

This command will show you your global (external) IP:

ip a | grep "scope global" | grep -Po '(?<=inet )[\d.]+'

All IPv4 (also 127.0.0.1):

ip a | grep "scope" | grep -Po '(?<=inet )[\d.]+'

All IPv6 (also ::1):

ip a | grep "scope" | grep -Po '(?<=inet6 )[\da-z:]+'
Nux
  • 9,276
  • 5
  • 59
  • 72
6

ip -4 addr show eth0 doesn't work on some machines. For example, I get this error: ip: symbol lookup error: ip: undefined symbol: bpf_program__section_name, version LIBBPF_0.2.0

This works for me:

/sbin/ifconfig eth0 | grep 'inet ' | awk '{ print $2}'

This has one less pipe than the accepted answer. In addition, my ifconfig output does not have inet addr.

To get the IPv6 address, use this:

/sbin/ifconfig eth0 | grep 'inet6 ' | awk '{ print $2}'
user2233706
  • 6,148
  • 5
  • 44
  • 86
5

I wanted something simple that worked as a Bash alias. I found that hostname -I works best for me (hostname v3.15). hostname -i returns the loopback IP, for some reason, but hostname -I gives me the correct IP for wlan0, and without having to pipe output through grep or awk. A drawback is that hostname -I will output all IPs, if you have more than one.

Sophie
  • 304
  • 10
  • 12
5

We can simply use only 2 commands ( ifconfig + awk ) to get just the IP (v4) we want like so:

On Linux, assuming to get IP address from eth0 interface, run the following command:

/sbin/ifconfig eth0 | awk '/inet addr/{print substr($2,6)}'

On OSX, assumming to get IP adddress from en0 interface, run the following command:

/sbin/ifconfig en0 | awk '/inet /{print $2}'

To know our public/external IP, add this function in ~/.bashrc

whatismyip () {
    curl -s "http://api.duckduckgo.com/?q=ip&format=json" | jq '.Answer' | grep --color=auto -oE "\b([0-9]{1,3}\.){3}[0-9]{1,3}\b"
}

Then run, whatismyip

zdk
  • 1,528
  • 11
  • 17
5

To print only the IP address of eth0, without other text:

ifconfig eth0 | grep -Po '(?<=inet addr:)[\d.]+'

To determine your primary interface (because it might not be "eth0"), use:

route | grep ^default | sed "s/.* //"

The above two lines can be combined into a single command like this:

ifconfig `route | grep ^default | sed "s/.* //"` \
  | grep -Po '(?<=inet addr:)[\d.]+'
personal_cloud
  • 3,943
  • 3
  • 28
  • 38
  • Note that `ifconfig` output is not stable over multiple versions. Some versions require `inet addr:` but some other versions only want `inet `. – Mikko Rantalainen Sep 27 '21 at 09:45
5

Few answers appear to be using the newer ip command (replacement for ifconfig) so here is one that uses ip addr, grep, and awk to simply print the IPv4 address associated with the wlan0 interface:

ip addr show wlan0|grep inet|grep -v inet6|awk '{print $2}'|awk '{split($0,a,"/"); print a[1]}'

While not the most compact or fancy solution, it is (arguably) easy to understand (see explanation below) and modify for other purposes, such as getting the last 3 octets of the MAC address like this:

ip addr show wlan0|grep link/ether|awk '{print $2}'|awk '{split($0,mac,":"); print mac[4] mac[5] mac[6]}'

Explanation: ip addr show wlan0 outputs information associated with the network interface named wlan0, which should be similar to this:

4: wlan0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether dc:a6:32:04:06:ab brd ff:ff:ff:ff:ff:ff
    inet 172.18.18.1/24 brd 172.18.18.255 scope global noprefixroute wlan0
       valid_lft forever preferred_lft forever
    inet6 fe80::d340:5e4b:78e0:90f/64 scope link 
       valid_lft forever preferred_lft forever

Next grep inet filters out the lines that don't contain "inet" (IPv4 and IPv6 configuration) and grep -v inet6 filters out the remaining lines that do contain "inet6", which should result in a single line like this one:

    inet 172.18.18.1/24 brd 172.18.18.255 scope global noprefixroute wlan0

Finally, the first awk extract the "172.18.18.1/24" field and the second removes the network mask shorthand, leaving just the IPv4 address.

Also, I think it's worth mentioning that if you are scripting then there are often many richer and/or more robust tools for obtaining this information, which you might want to use instead. For example, if using Node.js there is ipaddr-linux, if using Ruby there is linux-ip-parser, etc.

See also https://unix.stackexchange.com/questions/119269/how-to-get-ip-address-using-shell-script

jacobq
  • 11,209
  • 4
  • 40
  • 71
4

That would do the trick in a Mac :

ping $(ifconfig en0 | awk '$1 == "inet" {print $2}')

That resolved to ping 192.168.1.2 in my machine.

Pro tip: $(...) means run whatever is inside the parentheses in a subshell and return that as the value.

4

ip adddr, the short way

ip -4 -br addr show enp1s0 | awk -F" " '{print $3}'|cut -d'/' -f1

or shorten

ip -4 -br a s enp1s0 | awk -F" " '{print $3}'|cut -d'/' -f1

must work in most modern Linux distribution

blackgreen
  • 34,072
  • 23
  • 111
  • 129
vpolaris
  • 41
  • 1
4

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
3

I always wind up needing this at the most unexpected times and, without fail, wind up searching for threads like this on SO. So I wrote a simple script to get IPv4 addresses via netstat, called echoip - you can find it here. The bash for network addresses looks like this, it also gets your public address from ipecho.net:

IPV4='\d+(\.\d+){3}'
INTERFACES=`netstat -i | grep -E "$IPV4" | cut -d ' ' -f 1`
INTERFACE_IPS=`netstat -i | grep -oE "$IPV4"`

for i in "${!INTERFACES[@]}"; do
  printf "%s:\t%s\n" "${INTERFACES[$i]}" "${INTERFACE_IPS[$i]}"
done

The echoip script yields an output like this:

$ echoip
public: 26.106.59.169
en0:    10.1.10.2
stites
  • 4,903
  • 5
  • 32
  • 43
3

In man hostname there is even more easier way which automatically excluding loopback IP and showing only space separated list of all assigned to host ip addresses:

root@srv:~# hostname --all-ip-addresses
11.12.13.14 192.168.15.19 

root@srv:~# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
   link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
   inet 127.0.0.1/8 scope host lo
   inet6 ::1/128 scope host 
   valid_lft forever preferred_lft forever
2: venet0: <BROADCAST,POINTOPOINT,NOARP,UP,LOWER_UP> mtu 1500 qdisc noqueue state UNKNOWN 
  link/void 
  inet 11.12.13.14/32 scope global venet0:0
  inet 192.168.15.19/32 scope global venet0:1
3

the easiest way is as Mikko said

hostname --all-ip-addresses

the output enter image description here

you can also do that for little more details :

ip route

the output enter image description here

Yehouda
  • 112
  • 1
  • 6
2

Use the following command:

/sbin/ifconfig $(netstat -nr | tail -1 | awk '{print $NF}') | awk -F: '/inet /{print $2}' | cut -f1 -d ' '
mhlester
  • 22,781
  • 10
  • 52
  • 75
2

Here is my version, in which you can pass a list of interfaces, ordered by priority:

getIpFromInterface()
{
    interface=$1
    ifconfig ${interface}  > /dev/null 2>&1 && ifconfig ${interface} | awk -F'inet ' '{ print $2 }' | awk '{ print $1 }' | grep .
}

getCurrentIpAddress(){
    IFLIST=(${@:-${IFLIST[@]}})
    for currentInterface in ${IFLIST[@]}
    do
        IP=$(getIpFromInterface  $currentInterface)
        [[ -z "$IP" ]] && continue
    echo ${IP/*:}
    return
    done
}

IFLIST=(tap0 en1 en0)
getCurrentIpAddress $@

So if I'm connected with VPN, Wifi and ethernet, my VPN address (on interface tap0) will be returned. The script works on both linux and osx, and can take arguments if you want to override IFLIST

Note that if you want to use IPV6, you'll have to replace 'inet ' by 'inet6'.

MatthieuP
  • 1,116
  • 5
  • 12
2

use this one line script: ifconfig | grep "inet " | grep -v 127.0.0.1|awk 'match($0, /([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)/) {print substr($0,RSTART,RLENGTH)}' mac & linux (tested in ubuntu) both works.

Huan
  • 287
  • 5
  • 12
  • Nice that it works on both. Plus you can call it with `ifconfig en0` or `ifconfig eth1` etc if you know the interface you want :) – jaygooby Nov 10 '17 at 12:10
2

You can also use the following command:

ip route | grep src

NOTE: This will only work if you have connectivity to the internet.

MBT
  • 21,733
  • 19
  • 84
  • 102
  • If you have multiple interfaces, this emits multiple IPs. You need to figure out default route if you want IP of outgoing internet connection. – Mikko Rantalainen Dec 17 '20 at 19:25
2

The IPv4 address for the default route:

ip address show $(ip route | grep "^default " | head -n1 | grep -Po "(?<=dev )[^ ]+") | grep -Po "(?<=inet )[^ /]+"

The IPv6 address for the default route:

ip address show $(ip route | grep "^default " | head -n1 | grep -Po "(?<=dev )[^ ]+") | grep -Po "(?<=inet6 )[^ /]+"

These only require commands ip and grep with support for -P and -o. The head -1 is required because ip route may show multiple default routes when system has complex enough network setup.

If you don't mind which IP is which, you can just do

ip route | grep -Po '(?<=src )[^ ]+'

or

hostname --all-ip-addresses
Mikko Rantalainen
  • 14,132
  • 10
  • 74
  • 112
  • It's possible to replace `grep -Po` with `sed` or `awk` if some system exists that supports `ip` command but not `grep -Po`. The `grep -Po` is not defined by POSIX... but POSIX doesn't define any networking commands so you cannot do this with POSIX only tools. – Mikko Rantalainen Dec 17 '20 at 19:50
1

I don't see any answer with nmcli yet which is a command-line tool for controlling NetworkManager.

So here you go :)

wolf@linux:~$ nmcli device 
DEVICE  TYPE      STATE        CONNECTION 
eth1    ethernet  unavailable  --         
eth0    ethernet  unmanaged    --         
lo      loopback  unmanaged    --         
wolf@linux:~$ 

If you want to get the information from specific network interface (let say lo for this example)

wolf@linux:~$ nmcli device show lo
GENERAL.DEVICE:                         lo
GENERAL.TYPE:                           loopback
GENERAL.HWADDR:                         00:00:00:00:00:00
GENERAL.MTU:                            65536
GENERAL.STATE:                          10 (unmanaged)
GENERAL.CONNECTION:                     --
GENERAL.CON-PATH:                       --
IP4.ADDRESS[1]:                         127.0.0.1/8
IP4.GATEWAY:                            --
IP4.ROUTE[1]:                           dst = 127.0.0.0/8, nh = 0.0.0.0,>
IP4.ROUTE[2]:                           dst = 127.0.0.1/32, nh = 0.0.0.0>
IP6.ADDRESS[1]:                         ::1/128
IP6.GATEWAY:                            --
IP6.ROUTE[1]:                           dst = ::1/128, nh = ::, mt = 256
IP6.ROUTE[2]:                           dst = ::1/128, nh = ::, mt = 0, >
wolf@linux:~$ 

But since you just want to get the IP address, just send the output to grep, cut or awk.

Let's do it step by step. (Not sure what's wrong, the code sample format just didn't work for these 3 example.)

  1. Get the IPv4 line

    wolf@linux:~$ nmcli device show lo | grep 4.A IP4.ADDRESS[1]: 127.0.0.1/8 wolf@linux:~$

  2. Use awk to get the IP

    wolf@linux:~$ nmcli device show lo | awk '/4.A/ {print $2}' 127.0.0.1/8 wolf@linux:~$

  3. Use cut to remove the CIDR notation (/8)

    wolf@linux:~$ nmcli device show lo | awk '/4.A/ {print $2}' | cut -d / -f1 127.0.0.1 wolf@linux:~$

There your answer.

Please take note that there are tons of ways to do it using the tools that I demonstrated just now.

Let's recap the commands that I used.

nmcli device show lo | grep 4.A
nmcli device show lo | awk '/4.A/ {print $2}'
nmcli device show lo | awk '/4.A/ {print $2}' | cut -d / -f1

Sample output for these 3 commands

Command 1 output

IP4.ADDRESS[1]:                         127.0.0.1/8

Command 2 output

127.0.0.1/8

Command 3 output

127.0.0.1
  • nmcli is a good one. It can *almost* be used without auxiliary tools. Please consider updating the answer with this a bit more native/elegant method: `nmcli -g IP4.ADDRESS device show lo | cut -d / -f 1` – Jean Spector Dec 16 '20 at 20:09
1

yet another way to do it.

I often need to check the IP of various interfaces (e.g eth0, tunnel, etc.)

So this is how i do it:

ip -4 -br a | awk '{print $1,$3}'

output:

$ ip -4 -br a | awk '{print $1,$3}'
lo 127.0.0.1/8
eth0 192.168.0.248/24
tun0 192.168.119.1

If you do not care about the network adapter, just remove the $1 from awk command.

ip -4 -br a | awk '{print $3}'   
127.0.0.1/8
192.168.0.248/24
192.168.119.124/24

to make it easy, i just add it as an alias (remember to escape the $'s!)

$ alias me="ip -4 -br a | awk '{print \$1,\$3}'" 
                                                                                                                              
$ me                                            
lo 127.0.0.1/8
eth0 192.168.0.248/24
tun0 192.168.119.124/24
Chris S
  • 422
  • 1
  • 4
  • 13
0
ip addr|awk '/eth0/ && /inet/ {gsub(/\/[0-9][0-9]/,""); print $2}'

shows all your ips

John Slegers
  • 45,213
  • 22
  • 199
  • 169
MaXiM
  • 39
  • 3
0

On Redhat 64bit, this solved problem for me.

ifconfig $1|sed -n 2p|awk '{ print $2 }'|awk -F : '{ print $2 }'
Statham
  • 4,000
  • 2
  • 32
  • 45
0
#!/bin/sh
# Tested on Ubuntu 18.04 and Alpine Linux 
# List IPS of following network interfaces:
# virtual host interfaces
# PCI interfaces
# USB interfaces
# ACPI interfaces
# ETH interfaces
for NETWORK_INTERFACE in $(ls /sys/class/net -al | grep -iE "(/eth[0-9]+$|vif|pci|acpi|usb)" | sed -E "s@.* ([^ ]*) ->.*@\1@"); do 
    IPV4_ADDRESSES=$(ifconfig $NETWORK_INTERFACE | grep -iE '(inet addr[: ]+|inet[: ]+)' | sed -E "s@\s*(inet addr[: ]+|inet[: ]+)([^ ]*) .*@\2@")
    IPV6_ADDRESSES=$(ifconfig $NETWORK_INTERFACE | grep -iE '(inet6 addr[: ]+|inet6[: ]+)' | sed -E "s@\s*(inet6 addr[: ]+|inet6[: ]+)([^ ]*) .*@\2@")
    if [ -n "$IPV4_ADDRESSES" ] || [ -n "$IPV6_ADDRESSES" ]; then
        echo "NETWORK INTERFACE=$NETWORK_INTERFACE"
        for IPV4_ADDRESS in $IPV4_ADDRESSES; do 
            echo "IPV4=$IPV4_ADDRESS"
        done
        for IPV6_ADDRESS in $IPV6_ADDRESSES; do 
            echo "IPV6=$IPV6_ADDRESS"
        done
    fi
done
0

When looking up your external IP address on a NATed host, quite a few answers suggest using HTTP based methods like ifconfig.me eg:

$ curl ifconfig.me/ip

Over the years I have seen many of these sites come and go, I find this DNS based method more robust:

$ dig +short myip.opendns.com @resolver1.opendns.com

I have this handy alias in my ~/.bashrc:

alias wip='dig +short myip.opendns.com @resolver1.opendns.com'
htaccess
  • 2,800
  • 26
  • 31
0

These two ways worked for me:

To get IP address of your interface eth0. Replace eth0 in the below example with your interface name. ifconfig eth0 | grep -w "inet" | tr -s " " | cut -f3 -d" "

Using hostname: This will give you the inet i.e. IPAddress of your etho. using -I will give you inet value of all the interfaces whereever this value is present.

hostname -i

Milind
  • 1
  • 2
0

If you don't want to use ifconfig nor regex...

ip addr | grep eth0 | grep inet | awk '{print $2}' | cut -d"/" -f1
Miae Kim
  • 1,713
  • 19
  • 21
0

If you mean "Linux" and portable to Embedded Linux OS:

ip route get 8.8.8.8 | head -n 1 | tr -s ' ' | cut -d ' ' -f 7

This eliminates the need to call awk, which is not installed on all embedded Linux. Awk is a bit heavy of a process just to get a field from a string.

Every Linux environment will provide tr, head, and cut. If the embedded environment is really old it may have 'ifconfig' only, but you get the picture here: get the line, remove padding, cut what you need.

Scott Prive
  • 801
  • 11
  • 16
-2
curl ifconfig.co 

This returns only the ip address of your system.

MBT
  • 21,733
  • 19
  • 84
  • 102
  • 1
    This requires a working internet connection and will return your publicly visible IP address, which might or might not be what you want. – jlh May 24 '18 at 14:39
  • If the system is connected to the internet over NAT it will only return the public address an not the address of the system's network interface. – SePeF Apr 08 '21 at 08:26
-3

I would Use Hostname -L to get just the IP to use as a variable in a script.

Arman H
  • 5,488
  • 10
  • 51
  • 76
  • Perhaps you were trying to say `hostname -i` or `hostname -I`? – jlh May 24 '18 at 14:48
  • `user@linux:~$ Hostname -L -bash: Hostname: command not found user@linux:~$ ` 1. Capital `H` is wrong 2. No such `-L` –  Jan 17 '19 at 05:51