Read UNIX / Linux System IP Address In a Shell Script

Reading an IP address in shell script required many time. However, different Linux distribution stores IP address in different files. If you are looking to run script under different UNIX like OSes such as Solaris or FreeBSD then you need to use the ifconfig command. The ifconfig command is not just used to configure a network interface, but it can be use to obtained information such as network IP, netmask and much more.

Linux ifconfig Example

Type the following command:

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

FreeBSD/OpenBSD ifconfig Example

Type the following command:

ifconfig  | grep -E 'inet.[0-9]' | grep -v '' | awk '{ print $2}'

Sun / Oracle Solaris Unix Example:

Type the following command:

ifconfig -a | grep inet | grep -v '' | awk '{ print $2}'

Note: use /sbin/ifconfig full path if you are the non-root user.

How Does It Works?

You are using the ifconfig command and sending its output to shell pipeline. A pipeline is a set of processes chained by their standard streams, so that the output of each process (“stdout”) feeds directly as input (“stdin”) of the next one.

  1. The ifconfig command list all network interfaces.
  2. From the output of ifconfig command, find out IPv4 IP address using the grep command (grep ‘inet addr:’).
  3. Next, you do not need loopback IP address ( so again with the help of grep รขโ‚ฌโ€œv you invert the sense of matching, to select all non-matching lines.
  4. Finally, awk command is used to select an IP address. (awk ‘{print $2}’)

See also: read-ip-address.bash script which reads/finds an IP address from different UNIX/Linux OSes.

๐Ÿง Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

๐Ÿง 32 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
32 comments… add one
  • Anonymous Jan 17, 2006 @ 21:43

    The process you listed would only give you the local address, if you are behind a firewall that uses NAT.

    To get your public IP address try out the following bash script that I wrote a while ago:


    read T1

  • ๐Ÿง nixcraft Jan 18, 2006 @ 0:42

    Script is ok, but you can do that with one line command:

    lynx –dump

    Or better create a alias:

    alias myip=’lynx –dump

  • Anonymous Jan 18, 2006 @ 0:48

    True, This script is actually a portion of another script that sends out an email when my public IP changes. I just copied the portion that actually gets the IP and pasted it here.

    That being said your version of the script is a lot cleaner. Mind if I ‘steal’ it for use in my scripts? ๐Ÿ˜‰


  • ๐Ÿง nixcraft Jan 18, 2006 @ 8:53

    Mind if I ‘steal’ it for use in my scripts? ๐Ÿ˜‰

    Heh no problem, use it in your script ๐Ÿ™‚

  • Anonymous Jan 19, 2006 @ 13:27

    @nixcraft. nice tip/hint ๐Ÿ˜€ I would like to see more shell scripting stuff here. keep it up good work

  • Michael Gauthier Jun 15, 2006 @ 3:24

    If you don’t have lynx installed and want to use wget without messy temporary files, you can use:

    wget -qO –

  • Clue Dec 18, 2007 @ 0:52

    Hi there,

    I’m new to all this and I was wondering, how do those websites get our IP and is there not a shell script that can be written to find out your public IP depending on your private IP without having to use these external websites?

  • TJB Jan 16, 2008 @ 18:41

    Dude, you just saved me 15 minutes of relearning awk and the like just to pull out the IP addy. Double thanks!

  • lex Jan 18, 2008 @ 22:36

    no. for as far as I know you’ll always need some external server to ‘see’ who you are.
    But if someone knows another way to do this, the n please let me know.

  • igor Jan 30, 2008 @ 18:18

    if you use a different locale, consider this version for linux:

    LC_ALL=C ifconfig | grep ‘inet addr:’| grep -v ‘’ | cut -d: -f2 | awk ‘{ print $1}’

  • Oscar Feb 1, 2008 @ 21:42

    @the suggestion of using whatsmyip.

    That is a great idea, but if you’re looking to find your ip address on a box that has no internet access then that would not work and the original script(s) should always work. If you’re on a private lan for example with no web access then you can’t see whatsmyip, or ipchicken, etc.

    This quick how-to is not so much on how to find your external IP address, but more on the actual address your system has been assigned either DHCP, or statically.

  • Mohan Feb 27, 2008 @ 14:33

    This should do the work

    ifconfig eth0| awk ‘NR==2 {print $2}’| awk -F: ‘{print $2}’

  • Alan Haggai Alavi Nov 26, 2008 @ 14:16

    curl does the same too. No need of extra parametres. ๐Ÿ™‚

  • Paul Colby Feb 10, 2009 @ 7:01

    My current favourite:
    ifconfig | sed -n -e 's/: //g' -e 's/ *inet addr:([0-9.]+).*/1/gp'


    • vio Apr 25, 2011 @ 11:33

      hostname -I

  • Aaron Kushner May 13, 2009 @ 21:37

    @mohan, you can have multiple separators and not have to rerun awk.
    @paul, do you like yours because it has a sed abomination in it? Make it simple!

    ifconfig eth0 | awk -F”[: ]+” ‘NR==2 {print $4}’

  • Mika Marttila Oct 8, 2009 @ 14:14

    Thanks! My virtual Ubuntu server is having problems keeping the same IP from DHCP. I need it to start bind my Django app for whole network so this tip came in handy.

  • Marko Rauhamaa Nov 24, 2009 @ 10:03

    On linux:

    hostname -i

  • foo bar Jan 18, 2010 @ 12:50

    OMFG, guys, please, you *do* realize anybody can hack into and INSTANTLY gain access to your boxes ?!!!! And you’re not even using HTTPS – so any monkey in the middle can 0wn your boxes too !!!!
    Are you dumb enough to run those lynx/curl scripts as root as well ???

    NEVER EVER put this much trust into 3rd party systems, or you WILL get screwed !

    it would be funny watching you after returns `rm -rf /`

    use ifconfig / ip show… and don’t be so freaking lazy

    • Chris Browne Jun 8, 2011 @ 9:24

      Yeah, because lynx/curl execute the returned website. That’s how they work. Didn’t you know? They take what respond with, pass it into a system() call and it gets executed. Yup. And when you’re executing lynx as your user (which nobody suggested to do otherwise) rm -rf / is still able to wipe out your hard drive, because well… linux just doesn’t have a permissions system.

      ifconfig on the other hand, an administrator’s tool which sends a lot of information about your network interfaces to your shell, potentially sending the result in an email if the script is run from cron; well, that’s just brilliant. So much more secure. I’d definitely recommend using ifconfig. Yup. Blast a load of intimate hardware details across SMTP, way more secure than fetching a HTML response from a web server that contains nothing but your IP address.

      Seriously, though, if you’re gonna get angry at people for lax security practices at least have a clue what you’re talking about first.

      • wag Nov 7, 2014 @ 19:13

        The result from curl is not interpreted by curl itself, but might be by the shell script if not carefully written. Shell script injection is a common attack vector. If it happens, it’s already a huge security breach, even if the script doesn’t run as root.

        “hostname -i” or even parsed ifconfig is much more secure, just don’t print the result if it contains sensitive data.

  • neha kamra Aug 17, 2010 @ 5:59


    Can anyone send me the simple script with explanation to find the ip address…as i am new to this


  • ulysses768 Feb 21, 2011 @ 15:59

    This can be done exclusively in bash, if you’re interested in shaving a ms or two.
    IP=${IP#*inet addr:}
    IP=${IP%% *}

  • Danny Walker May 13, 2011 @ 0:39

    You can also do it in cygwin (you have to use Microsoft’s ipconfig command):

    ipconfig | grep ‘IP Address’ | grep -v | awk ‘{ print $15}’
    ($15 signifies the 15th word – the dots are words too!)

    Therefore, to mimic cygwin’s default prompt, use:

    export PS1=”[e]0;wa]n[e[32m]u@`ipconfig | grep ‘IP Address’ | grep -v | awk ‘{ print $15}’` [e[33m]w[e[0m]n$”

    This work for me, but it’s a bit system specific. For example, if you have additional network interfaces, you may have to grep -v any others that might be returned.


  • Gaveen Jun 13, 2011 @ 11:59

    Or you could run:

    hostname -I

  • Marissa Mar 3, 2012 @ 5:54

    can any1 plzzzzz tell me wat’s the utility of this script (it gives the local address i know!! but y know my local address?!!!!!).

  • zlidka Mar 11, 2012 @ 18:09

    and learn your hostname if you dont know
    and ping your hostname

  • neha Sep 25, 2012 @ 5:33

    hey i want to know: the code for running a shell script to connect a Setup Box(STB) through Jenkins integration tool by fetching its IP or MAC address. Please help..

  • Dss Apr 16, 2013 @ 16:11

    Update for Fedora v17 as the examples above won’t work.

    ifconfig | grep ‘inet’| grep -v ‘’ | cut -d: -f2 | awk ‘{ print $2}’

  • Pavlichenko May 18, 2013 @ 21:31

    Keep only one grep.

    ifconfig wlan0 | grep -o -P ‘(?<=addr:)[0-9]+.[0-9]+.[0-9]+.[0-9]+s'

    Ubuntu 13.04

  • Devendra Jun 11, 2013 @ 18:45

    ipconfig | grep “IPv4” | awk ‘{print $14}’
    working fine in cygwin

    This is simple and self expainatory

  • amin ghadesi Nov 9, 2014 @ 22:06

    Hi, in CentOS el7 ….. this command changed to :
    ifconfig | grep ‘inet ‘| grep -v ‘’ | cut -d: -f2 | awk ‘{ print $2}’

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