Bash Shell Command to Find or Get IP address

How do I find out my Linux / UNIX system ip address, subnet, and related networking information from a bash shell command prompt? How can I determine my private and public IP addresses from the command line?

To find out the IP address of Linux/UNIX/*BSD/macOS and Unixish system, you need to use the command called ifconfig on Unix and the ip command or hostname command on Linux. These commands used to configure the kernel-resident network interfaces and display IP address such as or It is used at boot time to set up interfaces as necessary. After that, it is usually only needed when debugging or when system tuning is needed.

Bash Shell Command to Find or Get IP address

If no arguments are given to ifconfig command, it displays the status of the currently active interfaces. It shows an Ethernet IP address, Mac address, subnet mask, and other information. Type the following /sbin/ipconfig command to display IP address and releated networking information:
$ /sbin/ifconfig
OR type the following command:
$ /sbin/ifconfig | less
Under Solaris and other Unixish oses you may need to type ifconfig command with -a option as follows:
$ /sbin/ifconfig -a
Sample outputs:

eth0     Link encap:Ethernet  HWaddr 00:0F:EA:91:04:07
  inet addr:  Bcast:  Mask:
  inet6 addr: fe80::20f:eaff:fe91:407/64 Scope:Link
  RX packets:31167 errors:0 dropped:0 overruns:0 frame:0
  TX packets:26404 errors:0 dropped:0 overruns:0 carrier:0
  collisions:0 txqueuelen:1000
  RX bytes:38338591 (36.5 MiB)  TX bytes:3538152 (3.3 MiB)
  Interrupt:18 Base address:0xc000

lo        Link encap:Local Loopback
  inet addr:  Mask:
  inet6 addr: ::1/128 Scope:Host
  UP LOOPBACK RUNNING  MTU:16436  Metric:1
  RX packets:1994 errors:0 dropped:0 overruns:0 frame:0
  TX packets:1994 errors:0 dropped:0 overruns:0 carrier:0
  collisions:0 txqueuelen:0
  RX bytes:188041 (183.6 KiB)  TX bytes:188041 (183.6 KiB)

In the above example, is the IP address of the eth0 Ethernet interface. Of course, NIC (network interface card) name will be different as per your version of Unix system and network card driver. Here is an output from my macOS Unix based desktop:
/sbin/ifconfig en0

Determine Your Private and Public IP Addresses from the Bash Command Line

Displaying private IP addresses of FreeBSD Unix server using the ifconfig

Simply run:
ifconfig -a
ifconfig em0

FreeBSD Unix Bash Shell Command To Find IP address of server

Finding default routing information on Unix

We need to type the following command:
# netstat -rn

FreeBSD Unix Default Route

FreeBSD Unix Default Route

Find the IP address of the local machine on Linux using hostname

We can determine the IP address or addresses of the Linux server by using the hostname command too. Open the Terminal application and execute the hostname command as follows:
# hostname -I
Sample outputs:

Linux ip Command

It is recommend that you use the ip command under Linux based systems. The ip command display information about ip address, manipulate routing, network devices, interfaces, tunnels and much more. The following ip command will show all ip address assigned to your system:
# ip addr show
To see information about NIC named eth0 ip address, enter:
# ip addr show eth0
Sample outputs:

2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000
    link/ether b8:ac:6f:65:31:e5 brd ff:ff:ff:ff:ff:ff
    inet brd scope global eth0
    inet6 fe80::baac:6fff:fe65:31e5/64 scope link 
       valid_lft forever preferred_lft forever

Enter the following command to list all network interfaces from bash shell:
# ip link show
Bash command to know ip address of Linux and Unix server

Linux command to display default routes

The syntax is as follows:
ip route show
ip r s
route -n

default via dev tun0 proto static metric 50 
default via dev enp0s31f6 proto static metric 100 dev tun0 proto kernel scope link src metric 50 dev lxdbr0 proto kernel scope link src via dev enp0s31f6 proto static metric 100 dev enp0s31f6 proto kernel scope link src metric 100 dev enp0s31f6 proto static scope link metric 100

Bash command for showing the public IP address of Linux and Unix systems

We can use the host command or dig command as follows to display the public IP address of your Linux or Unix system:
dig +short

How To Find My Public IP Address From Command Line On a Linux

How To Find My Public IP Address From Command Line On a Linux


You learned various bash commands to find both private and public IP addresses, netmask, default route, and other information. For more information please see howto read UNIX/Linux system IP address in a shell script.

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 53 comments so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersncdu pydf
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
53 comments… add one
  • Anonymous Mar 1, 2006 @ 7:24

    ipconfig usually doesn’t work outside of windows

  • 🐧 nixcraft Mar 1, 2006 @ 13:07

    Opps! Just corrected typo since I work on both UNIX and Windows Server and sometime I get confused :(.

    Thanks, I appreciate your post :)

  • saeed Jul 23, 2007 @ 14:17

    i worked in a company and the I.T man close the face book site what can i do

    • Criss Nov 21, 2011 @ 23:03

      Work saeed and don`t be a facebook adict

  • Jiten Mistry Apr 25, 2008 @ 12:28

    hostname -i
    hostname -I

    also find other parameters using man hostname.

  • Oscar Jun 29, 2008 @ 6:32

    hostname -i doen’t get the internet ip address:

    ?????:~ # hostname -i

    it gives me the lo ip address.

    I want to get the internet ip address from a command.


  • Oscar Jun 29, 2008 @ 9:27

    I have found this way to get my ip public address (I have two IPs in my eth0 card for NAT):
    xxxx:~ # ifconfig|sed -n “/inet addr:.*{s/.*inet addr://; s/ .*//; p}”


  • Oscar Jul 2, 2008 @ 16:32

    I’m trying OpenSuSE LiveCD 64Bits (Linux linux #1 SMP 2008-06-07 01:55:22 +0200 x86_64 x86_64 x86_64 GNU/Linux)

    and the previous command doesn’t work. I have different broadcast address, so I get it with the mask (that is the same):

    linux:/home/linux # ifconfig eth0 | sed -n “/inet addr:.*{s/.*inet addr://; s/.*//; p}”
    linux:/home/linux #

  • Alejandro Smith Jul 8, 2008 @ 20:27

    Oscar, try this.

    root@azrael:~# ifconfig eth0 | grep inet | awk '{print $2}' | sed 's/addr://'

    Where eth0 is the interface you want to know the ip address. In my case was eth0. This will retrieve the Ip Address from the specified interface. If you have ipv6 active and you only use ipv4 Address, then add a grep . to the end of the sentence.
    it would be like this:
    root@azrael:~# ifconfig eth0 | grep inet | awk '{print $2}' | sed 's/addr://' | grep .

  • Alejandro Smith Jul 8, 2008 @ 20:30

    I use the above to retrieve the Ip address (dynamic one) from my ISP in order to forward the HTTP petitions to the web server (behind a firewall). Hope it works for you.

  • Oscar Jul 25, 2008 @ 10:25

    Is great!

    Thanks a lot, Alejandro

  • Bastiaan Nov 18, 2008 @ 12:33

    almost good:
    ifconfig eth0 | grep 'inet ' | awk '{print $2}' | sed 's/addr://'

    will work ->inet otherwise you will get an empty line from the inet6 line you grepped

  • Bastiaan Nov 18, 2008 @ 12:34


    • shweta Dec 29, 2011 @ 11:58

      is there any other option available to get ip address on linux

      • helloworldbr Aug 3, 2012 @ 4:12

        I simply issue a bash command:root@debian:~ echo $IP

  • Alejandro Smith Nov 18, 2008 @ 13:53

    that’s why i typed two lines, the first one if you don’t have inet6 active on your server, and the second one if you do have it on.

    root@azrael:~# ifconfig eth0 | grep inet | awk ‘{print $2}’ | sed ’s/addr://’ | grep . <– is not the best way to do it, but the “grep .” stuff works for that matter (at least it does for me) as i said it depends on the flavor of Linux you have and some other stuff.

    Thanks for the tip on the “space after inet” i’ll try it out..

    • award Sep 24, 2010 @ 16:07

      in case someone still interested, i think easiest way to get local ip address is:
      hostname -I (capital i)
      this will return just the ip address with nothing else.

      To get my external ip address (without use dyndns, noip & c) i use in a script:

      wget -O /tmp/ip.txt -o /dev/null

      This will write to /tmp/ip.txt my external ip and then i upload the text file with curl
      to a directory on my web page

      wget -O /tmp/ip.txt -o /dev/null
      extip=`cat /tmp/ip.txt`
      echo "$extip" > /tmp/ip.txt
      hostname -I > /tmp/intip.txt
      exit 0

      C u in /dev/null

  • rahul Dec 11, 2008 @ 7:25


  • Aaron Jan 9, 2009 @ 14:07

    Hi, I’m using RedHat Workstation 4 and would like to write a script to set the IP address, subnet mask and hostname of my machine. The same as Rahul.

    How can i do this?
    Thanks in advance.

  • Bill Gates Jan 19, 2009 @ 7:10

    A lot of the above example scripts/commands dont work corectly due to missformatting of the various Quote characters. Single quotes, double quotes and backticks are messed up.

    ` is a backtick – top left of the keyboard. Its used to go around a command thats ran.
    export FOO=`ls`
    puts the output of the ls command in the variable FOO

    single Quote ‘ and double quotes ” are used in different places to go around arguments to commands. and to keep special chaacters from gettting parsed by the shell.

  • Bill Gates Jan 19, 2009 @ 7:12

    oops.. Watch out for the way this thread/forum alters the matching Quote to the single and double qupte if you cut and paste the examples. ie:

    single Quotes ‘hello’

    double quotes “hello”

    back ticks `hello`

    I had to manually alter the lines after i pasted them to use proper quoteing.

  • kishore Feb 12, 2009 @ 4:18

    how to find the ip address of an website in linux

  • vyagh Mar 2, 2009 @ 10:47

    we have server configured on Linux.
    Is there any way to get the IP Address of the client which is conncetd to that server.

    • mrG Jun 29, 2010 @ 22:15

      IP=`who -m --ips | awk '{print $6}'`
      Another syntax
      IP=$(who -m --ips | awk '{print $6}')

  • Bijoy Meethal May 28, 2009 @ 12:13

    Kishore , go to Terminal, and say ‘ping’

  • Dave Nov 28, 2009 @ 9:55

    ifconfig eth0 | grep inet | sed -n "s/^\s*inet addr://p" | awk '{ print $1 }'

    Works on my Linux system

  • wuhaa Dec 31, 2009 @ 16:44

    This one I have used on my Ubunut 9.04 system

    # ifconfig eth0|grep "inet addr:"|awk '{print $2}'|awk -F : '{print $2}'

  • Mike Godin Jun 8, 2010 @ 16:09

    I like to use the simple and concise:
    host $(hostname) | sed 's/.*has address //g'

    It simply returns the IP address that the rest of the world sees for your machine.

  • /dev/movebo Sep 12, 2010 @ 22:09

    @wuhaa @Alejandro\ Smith

    Personally I prefer this version:
    $ /sbin/ifconfig eth0 | sed '/inet\ /!d;s/.*r://g;s/\ .*//g'

    Why, you may ask. Because of this:

    $ time { /sbin/ifconfig eth0|grep "inet addr:"|awk '{print $2}'|awk -F : '{print $2}' ; }

    real 0m0.027s
    user 0m0.013s
    sys 0m0.003s

    $ time { /sbin/ifconfig eth0 | grep inet | awk '{print $2}' | sed 's/addr://' | grep . ; }

    real 0m0.009s
    user 0m0.011s
    sys 0m0.010s

    $ time { /sbin/ifconfig eth0 | sed '/inet\ /!d;s/.*r://g;s/\ .*//g' ; }

    real 0m0.005s
    user 0m0.005s
    sys 0m0.004s

    The fewer processes, the faster the execution.

    • mrG Sep 13, 2010 @ 14:03

      brill score! well done.

    • Alejandro Smith Sep 13, 2010 @ 14:51

      Excellent info on that.. thanks for updating this. That’s true, fewer processes translates into a faster execution.

      Two years ago when i first posted on this faq with a “solution”, i was a new linux user, and i did not had any experience on this. I read some good information on bash scripting and came out with this solution for a problem i was having at that time.

      I didn’t care back then if it was faster than other solutions, i just needed to solve the issue.. ;-) but of course, YOU are completely right with your point of view.

      Thank you all for sharing your knowledge on this.

      God bless you all.

    • Keith Kyzivat Mar 27, 2011 @ 17:51

      Similar approach, but with Awk instead of sed:

      time { ifconfig vmnet8 | awk ‘/inet addr:/ {print substr($2,6)}’ ; }

      Do some *nix implementations put a space after ‘addr:’ ? If so, this will break in those cases.

  • Sandeep Sep 30, 2010 @ 10:59

    Hi Award

    Excellent command to find the originating IP addresss
    hostname -i


    • Peter Raeth Jan 3, 2017 @ 21:04

      On my Ubuntu box, “hostname -i” does not give the same result as “host $(hostname)”.

  • Henrique Nov 24, 2010 @ 19:44

    Work For me (Debian Lenny 5.0)
    # ifconfig eth0 | grep inet | grep -v "inet6" | awk {'print $3'}

  • Sérgio Luiz Araújo Silva Dec 6, 2010 @ 20:03


    Run it as
    source ~/.bashrc

  • radha Dec 22, 2010 @ 7:19

    Now this works for Solaris 10
    ifconfig -a

  • Adi Jan 4, 2011 @ 12:17

    To find the ip address of Unix box from Unix prompt:


  • Dan Aug 3, 2011 @ 15:25

    Here’s one that works (quickly) for me using cut:

    time { ifconfig eth0|grep 192|cut -d: -f2|cut -d' ' -f1 ; }
    Look for summarize system resource usage
    real	0m0.007s
    user	0m0.000s
    sys	0m0.002s
  • Thomas Arend Jan 28, 2012 @ 21:43

    A computer may have multiple network interfaces and therefore more than one ip-address you should try:

    /sbin/ifconfig [dev] | \
    sed -n '/inet [Aa]d\{1,2\}resse\{0,1\}:/ { s# *inet [Aa]d\{1,2\}resse\{0,1\}:##; s# .*##p }'

    If you omit [dev] this command will give you a list of all IP-address and should work with English and German localization.

    hostname -i is unpredictable in its result and nslookup $(hostname) will give you only what DNS knows about your machine.

  • Paritoshik Oct 26, 2012 @ 6:15

    its realy grtt i found my way thankss..

  • zak Sep 28, 2013 @ 14:23

    i have always the same result : inet iddr :

    what could be the problem ) thank you

  • Abdoulaye Siby Nov 23, 2013 @ 19:16

    Just running the following

    # ip -f inet -o addr show eth0

    The result will be (on one line)

    2: eth0   inet brd scope global eth0

    Then you can apply a simple filter on it to extract the IP

  • Abdoulaye Siby Nov 23, 2013 @ 19:17

    Sorry for the typos. Unable to re-edit my post.

  • stoffel010170 Mar 18, 2014 @ 10:45
    ip -4 -o addr show dev eth0 |awk '{split($4,a,"/") ;print a[1]}'
  • s Mar 27, 2014 @ 18:16

    I’m using cygwin on windows and a VPN. None of the above worked for me. I had to use the following command and some grep and sed commands

    route print | egrep "*" | sed 's/\s*\(.*\)*/\1/g'
    ### I’ve used as a fictional router address.##

    The vertical line, the pipe symbol, | , takes the output of everything on the left and feeds it to the command on the right. So the solution that works for me (the rest do not) involves essentially three commands – each of which that does something and parse things to the next step.

    • route print : the main command of many things – over many lines
    • egrep "* : this selects a line that has the netmask at then any characters as shown by the .* after the 255’s and then looks for my router’s address of
    • sed 's/\s*\(.*\)*/\1/g' : sed command replaces/extracts text. basic use: ‘s/replace/withThis/g’ does a global search and replaces all references of replace with the string withThis. This sed command extracts the part of the line before the 255 part. It ignores leading spaces indicated by the \s and also anything after the 255 part shown by .* which is a shorthand for any character or number (the . part) that is found zero of more times (the * part). By placing part of the replace string in parentheses \(.*\) I am saying keep this part. So it’s the part after leading spaces and before the 255s. The \1 part is the replacement part of the sed command throws away all the other stuff and just keeps that part in parentheses and is displayed. This is the IP address on my machine.
  • D Jul 23, 2015 @ 2:13

    Excelent post
    i’m learning i’m learning !!!

  • likehowdoi Aug 13, 2015 @ 21:18

    how about this: curl

  • Vijay Kanta Aug 16, 2015 @ 8:11

    Fantastic, as usual.

  • Anto Rij Oct 12, 2015 @ 7:36

    I want to show ip address using the command line.

  • Ravilal May 28, 2016 @ 5:55

    How to find IP Address in Linux Operating System
    i’m using this command
    ifconfig ,/sbin/ifconfig and /sbin/ifconfig eth0 and
    ifconfig eth0 | grep inet | sed -n “s/^\s*inet addr://p” | awk ‘{ print $1 }’ and
    more command i’m using but i’m not geting a ip address pls help any one

  • Yuri Schukow Jun 16, 2016 @ 13:59

    As a sample: I’m using IPSec/StrongSWAN and ifconfig doesn’t show the ip’s used by strongswan, ip does.

  • Pranav Bhavsar Jan 11, 2017 @ 12:57

    Any idea about the how to other ip address to find the server and cpu usage & pid find.

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum