407

I know you can do ifconfig | grep inet, but that shows you several IPv4 addresses. How do I get the specific one for SSHing et al?

bmike
  • 235,889
JShoe
  • 5,139
  • 3
    What you are looking for is not your Mac IP address but the public IP address your ISP attributed to the Internet interface of your router. – dan Feb 12 '19 at 11:26
  • Need to change title for public IP address. – alturium Jun 18 '20 at 17:29
  • 4
    This worked for me : > curl ipecho.net/plain; echo – alturium Jun 18 '20 at 17:37
  • 4
    You should either clarify your question, i.e. you want to know internal IP in your local network or change the accepted answer, because none of the answers give you an option to get an external IP, but a few comments do. – ruslaniv Dec 04 '20 at 07:15
  • @alturium you should make it an answer, because it IS the answer to the question. Just add a command to get internal IP as well. – ruslaniv Dec 04 '20 at 07:16
  • 1
    @JShoe, can you confirm whether you wanted the internal or external IP address? That would help make this all of the answers below clearer: some show you how to get internal addresses, while others show external. – Mike Williamson Mar 08 '21 at 17:01
  • 3
    osascript -e 'return IPv4 address of (get system info)' – Carsten Jun 09 '22 at 08:03
  • 2
    Building on other ansvers: ipconfig getifaddr $(route -n get default | awk '$1=="interface:" { print $2 }') - get the address of the interface handling the default route. I don't have enough reputation to add this to an answer. – sastorsl Jan 25 '24 at 10:45
  • 1
    Also, to list all IP's, and which one is the default route: for INF in $(ipconfig getiflist); do IPADDR=$(ipconfig getifaddr ${INF}) ; [ "${INF}" = "$(route -n get default | awk '$1 == "interface:" { print $2 }')" ] && DEF=" (*)" ; [ -n "${IPADDR}" ] && echo ${INF}: ${IPADDR}${DEF:-''} ; DEF="" ; done. Still missing rep to post this as an answer. – sastorsl Jan 25 '24 at 12:01

9 Answers9

478

Local IP address

For wireless: Use ipconfig getifaddr en1

For ethernet: Useipconfig getifaddr en0.

ipconfig getifaddr en0 is default for the Wi-Fi network adapter

Public IP address

dig -4 TXT +short o-o.myaddr.l.google.com @ns1.google.com

Output, e.g.:

172.79.136.120

Command explanation

The command you provided, dig -4 TXT +short o-o.myaddr.l.google.com @ns1.google.com, is a command-line instruction that uses the dig utility to perform a DNS (Domain Name System) lookup. Let's break down the components of the command:

  • dig: dig stands for "domain information groper," and it is a command-line tool commonly available on Unix-like systems for querying DNS servers.

  • -4: This option specifies that the command should use IPv4 protocol for the DNS lookup. It ensures that the query is sent using IPv4 instead of IPv6.

  • TXT: This indicates the type of DNS record being requested. In this case, the command is asking for the TXT record.

  • +short: This option is used to display only the relevant information from the DNS response, providing a concise output.

  • o-o.myaddr.l.google.com: This is the hostname used for the DNS lookup. It is a special hostname that Google uses to provide information about the IP address from which the DNS query originates.

  • @ns1.google.com: This specifies the DNS server to which the query is sent. In this case, it is set to ns1.google.com, which is one of Google's public DNS servers.

When you run this command, it queries the ns1.google.com DNS server for the TXT record associated with o-o.myaddr.l.google.com. The response will contain the TXT record, which typically includes information about the IP address of the client making the DNS query. The +short option ensures that only the relevant information is displayed, making it easier to read the output.

IconDaemon
  • 19,234
daviesgeek
  • 38,901
  • 52
  • 159
  • 203
  • 22
    This shows my internal (to my router) IP address (e.g. 192.168.1.xxx), not the external IP address. (As do most of the other answers below.) – ShreevatsaR May 05 '15 at 22:38
  • 5
    Didn't work for me :( – Naveed Abbas May 13 '16 at 08:05
  • 7
    ifconfig shows all of the interfaces and their IPs – aralar Jul 07 '16 at 17:26
  • 18
    echo External IP: curl -s http://checkip.dyndns.org/ | sed 's/[a-zA-Z<>/ :]//g' – grigb Aug 16 '16 at 17:51
  • 52
    It has changed for years, 'ipconfig getifaddr en0' is default for wifi interface – Marek Szmalc Jan 31 '17 at 16:35
  • @JShoe mentioned ipconfig. So i guess he's not on windows. Linux or mac maybe? Did you mean ifconfig? Anyway, this didn't work for me. – ttt Jun 22 '17 at 07:11
  • This is totally wrong for all MacBooks connecting on anything other than built-in Ethernet (modern macbooks do not even have it built in). This ONLY tells you the addres at en1 or en2 and does not determine which is active and even that can be hard with modern multi-homing. – uchuugaka Sep 02 '17 at 11:19
  • 1
    ipconfig getifaddr en2 works for me – Liko Jan 05 '20 at 12:07
  • 7
    when you don't know which interface you're on: ifconfig -l | xargs -n1 ipconfig getifaddr – epylinkn Apr 06 '20 at 08:14
  • 1
    See the bash script answer below for a better solution. You don't always know if you're on wifi or ethernet, or you might be using an interface that's not en0 or en1. For example, I have a 10G thunderbolt NIC that for some reason is on en14. The solution below from @Brad Parks covers all this! – Chris Oct 19 '20 at 21:18
  • @grigb you should make it an answer, because it IS the answer to the question. Just add a command to get internal IP as well. – ruslaniv Dec 04 '20 at 07:17
  • ipconfig getifaddr en0 returns empty string – alper Mar 02 '21 at 09:47
  • 1
    @ShreevatsaR if you are behind a router, it is usually performing NAT, so your computer will not know "its" external IP, because technically it is assigned to the router, not the computer itself. therefore, you cannot get your external IP address without creating some connection to retrieve that data (e.g. using curl, dig, wget). every solution mentioning local-only tools (e.g. ifconfig, netstat, hostname) will only give you your machine's internal IP. – éclairevoyant Oct 30 '22 at 20:11
  • if dig is not installed install using (ubuntu) sudo apt install dnsutils – Sankha Jayalath Mar 02 '23 at 07:51
  • curl -s api.infoip.io/ip 2>/dev/null – Christopher Apr 24 '23 at 17:20
  • Now getifaddr en0 ipconfig works too – Thinkr May 08 '23 at 11:33
  • In my experience, wireless is en1 on my iMac probably since it has an ethernet port which must be en0, whereas on my Macbook Air, en0 is the wireless since it's the first (and only) interface – airstrike Aug 14 '23 at 18:03
182

The following works for me on 10.8 and on 10.10 Yosemite.

ifconfig | grep "inet " | grep -Fv 127.0.0.1 | awk '{print $2}' 

If you find the above gives you more than one answer, save the following to a script, and run it instead

ip_address.sh

#!/usr/bin/env bash

dumpIpForInterface() { IT=$(ifconfig "$1") if [[ "$IT" != "status: active" ]]; then return fi if [[ "$IT" != " broadcast " ]]; then return fi echo "$IT" | grep "inet " | grep -v 127.0.0.1 | awk '{print $2}' }

main() {

snagged from here: https://superuser.com/a/627581/38941

DEFAULT_ROUTE=$(route -n get 0.0.0.0 2>/dev/null | awk '/interface: / {print $2}') if [ -n "$DEFAULT_ROUTE" ]; then dumpIpForInterface "$DEFAULT_ROUTE" else for i in $(ifconfig -s | awk '{print $1}' | awk '{if(NR>1)print}') do if [[ $i != "vboxnet" ]]; then dumpIpForInterface "$i" fi done fi }

main

Brad Parks
  • 2,393
163

Typing curl ifconfig.me in Terminal will give your public IP address

Thinkr
  • 3,357
Kcidkcus
  • 1,647
  • 1
  • 10
  • 2
42

I've got this set up in an .aliases dotfile for frequent ip lookup:

alias ip="dig +short myip.opendns.com @resolver1.opendns.com"
alias localip="ipconfig getifaddr en0"
thibmaek
  • 907
  • For me, the localip option there doesn't work anymore, but this does: ifconfig | grep "inet " | grep -Fv 127.0.0.1 | awk '{print $2}' – i i Feb 10 '21 at 17:51
  • This is the best answer: While dig does reach outside of your machine, it is a domain name resolver, which means its main function is precisely to match domain name to IP address. In this case, it is your own IP address, of course. – Mike Williamson Mar 08 '21 at 16:58
  • the ip alias doesn't seem to be working (I'm on OSX Catalina). This one does: curl -s http://ipecho.net/plain; echo – jnaklaas Jul 12 '21 at 14:45
  • Running the dig command on macOS Monterey produces no output. Running the curl -s command does seem to work. – Craig S. Anderson Mar 13 '23 at 23:20
29

You can do the following:

Type ifconfig or ifconfig -a. This command shows you the list of interfaces along with their IP and MAC addresses (the latter one only if applicable). You can also type ifconfig en0 or ifconfig en1 for the configuration of a particular interface only (as someone said in their answers, en0 is typically the wired Ethernet while en1 is the WiFi interface).

As an alternative, netstat -i will list all interfaces and will show you the IP addresses you have assigned to each of them.

Typically, when you have SSH daemon running on a box, it will listen on all available interfaces, ie. you can use any IP address that's configured on your machine to connect to that machine via SSH (this, obviously, subject to Firewall rules). If you're after what the OS calls a Primary interface and primary IP address, you can use the scutil command like this:

MacBook:~ scutil
> show State:/Network/Global/IPv4
<dictionary> {
  PrimaryInterface : en0
  PrimaryService : C0550F84-5C07-484F-8D62-C8B90DC977D8
  Router : 10.103.4.1
}
> show State:/Network/Interface/en0/IPv4
<dictionary> {
  Addresses : <array> {
    0 : 10.103.4.234
  }
  BroadcastAddresses : <array> {
    0 : 10.103.4.255
  }
  SubnetMasks : <array> {
    0 : 255.255.255.0
  }
}

Please note, that the above, even though is a command-line command, is also interactive (so you run scutil and then enter its own commands into it). The first show command tells you the name of the primary interface for the OS (i.e. this will be the one on top of the list in your System Preferences / Network Preferences window), as well as the IP address of your default router. The second show command takes State:/Network/Interface/<ifname>/IPv4 argument (in this case, en0) and gives you the IP addresses assigned to it. You're looking for the address in the Addresses array, the other two entries are broadcast addresses and the netmasks.

Hope that helps, but if anything is not clear, let me know.

mike
  • 1,921
  • 3
    Thanks for this answer. I wrote a quick script to get the IP your primary interface: echo "show State:/Network/Interface/$(echo 'show State:/Network/Global/IPv4' | scutil | grep 'PrimaryInterface ' | sed 's/ PrimaryInterface : //')/IPv4" | scutil | pcregrep -Mo1 " Addresses : <array> {\n 0 : ([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})" – HellaMad Jan 14 '15 at 19:09
22

To find your Mac's current internal IP address, run:

ifconfig -l | xargs -n1 ipconfig getifaddr

This is basically equivalent to ipconfig getifaddr en0, but more reliable! en0 is not always the current network interface!

Thank you to @epylinkn in the comments of another answer for this hint. I'm posting it here so it's more visible; I initially missed it myself.

Wowfunhappy
  • 7,383
5

To get the IP address of your computer facing the Internet, here is a working receipe:

if=`netstat -nr | awk '{ if ($1 ~/default/) { print $6} }'`
ifconfig ${if} | awk '{ if ($1 ~/inet/) { print $2} }'

It should work even when you have multiple interfaces active, even when you have interfaces you don't know which one is actually the default gateway.

dan
  • 12,177
  • 8
  • 58
  • 136
  • 1
    This gets the internal network IP address (eg: 192.168.0.*), not the external one exposed to the internet. – SimplGy Apr 30 '15 at 05:30
  • Thank you for this feedback. Could you tell me on which version of the OS you are? Could you provide me the output of netstat -nr | grep default ? – dan Apr 30 '15 at 06:39
  • This gets the IP of the interface that serves in/outbound traffic. If you're behind NAT, that will give your LAN address, not WAN address. But it can be useful for a lot of purposes, and it doesn't reach out for network. – folex Jan 11 '19 at 11:25
3

Just for the record, you can make a bash script with the following content which gives you your external IP address

#!/bin/bash    
wget -qO - http://ipecho.net/plain; echo
1

This answer work on both mac and linux:

ifconfig | grep -E "([0-9]{1,3}\.){3}[0-9]{1,3}" | grep -v 127.0.0.1 | head -1 | awk '{ print $2 }'
Gerd
  • 191