Mason
Mason

Reputation: 7103

Which terminal command to get just IP address and nothing else?

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?

Upvotes: 197

Views: 353975

Answers (30)

ccpizza
ccpizza

Reputation: 31746

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 active VPN connections then you'll have yet more IP addresses.

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, 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 

Getting the external IP

To get the external IP you could query a text-mode service, for example curl https://ipecho.net/plain would return a plain text external IP.

A supposedly faster approach could be to query a known DNS server, e.g.:

dig @ns1-1.akamaitech.net ANY whoami.akamai.net +short

Upvotes: 70

This is universal command which is worked for most of the linux flavors.

This command shows details about your IPv4 address which is configured on Linux system.

 ip -4 addr show scope global | grep inet | tr -s " " | awk '{print $NF,$2}'


 ip -4 addr show scope global | grep inet | tr -s " " | awk '{print $NF,$2}' | paste -sd ','

Upvotes: 0

Bonifazy
Bonifazy

Reputation: 11

result only IPv4 address exclude local specific addresses: 10.x.x.x, 127.x.x.x, 172.x.x.x, 192.x.x.x:

ip -4 addr | grep -oP '(?<=inet\s)\d+(\.\d+){3}' | grep -vE '^10|^127|^172|^192'

Upvotes: 0

Farshad
Farshad

Reputation: 420

On Ubuntu( and Debian like distro)

To obtain just the local address digits, execute the following command:

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

For a comprehensive picture of your network interface configuration and global IP address use:

curl -4 icanhazip.com
curl  ifconfig.co //this responds faster

To dive deeper into the mysteries of any IP address (including your own):

 whois $(curl ifconfig.co)   

Ensure you have the whois tool installed. If not, simply execute:

sudo apt-get install whois

Upvotes: 5

Vignajeth
Vignajeth

Reputation: 708

hostname -I  

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

Upvotes: 29

Chris S
Chris S

Reputation: 437

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

Upvotes: 2

user2233706
user2233706

Reputation: 7227

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}'

Upvotes: 8

Scott Prive
Scott Prive

Reputation: 888

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.

Upvotes: 0

vpolaris
vpolaris

Reputation: 51

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

Upvotes: 5

Yehouda
Yehouda

Reputation: 122

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

Upvotes: 3

Mikko Rantalainen
Mikko Rantalainen

Reputation: 16015

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

Upvotes: 3

Miae Kim
Miae Kim

Reputation: 1771

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

ip addr | grep eth0 | grep inet | awk '{print $2}' | cut -d"/" -f1

Upvotes: 0

user13353532
user13353532

Reputation:

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

Upvotes: 1

personal_cloud
personal_cloud

Reputation: 4514

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.]+'

Upvotes: 5

Milind
Milind

Reputation: 1

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

Upvotes: 0

Tal Zion
Tal Zion

Reputation: 1441

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'

Upvotes: 15

jacobq
jacobq

Reputation: 11577

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

Upvotes: 5

htaccess
htaccess

Reputation: 3120

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'

Upvotes: 0

user11621528
user11621528

Reputation: 1

#!/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

Upvotes: 0

zdk
zdk

Reputation: 1576

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

Upvotes: 4

Tukan3
Tukan3

Reputation: 288

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

ipconfig getifaddr en0

Upvotes: 16

Prudhvi Raj Mudunuri
Prudhvi Raj Mudunuri

Reputation: 87

curl ifconfig.co 

This returns only the ip address of your system.

Upvotes: -2

Search Info
Search Info

Reputation: 21

You can also use the following command:

ip route | grep src

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

Upvotes: 2

user10101010
user10101010

Reputation: 31

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

Upvotes: 3

Nux
Nux

Reputation: 10022

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:]+'

Upvotes: 6

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.

Upvotes: 4

caglar
caglar

Reputation: 380

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

Upvotes: 10

Statham
Statham

Reputation: 4118

On Redhat 64bit, this solved problem for me.

ifconfig $1|sed -n 2p|awk '{ print $2 }'|awk -F : '{ print $2 }'

Upvotes: 0

Huan
Huan

Reputation: 297

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.

Upvotes: 2

philosophie
philosophie

Reputation: 304

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.

Upvotes: 5

Related Questions