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 ifconfig command. ifconfig not just use to configure a network interface but it can be use to obtained information such as network IP, netmask etc. Here are different command for different UNIX/Linux/BSD OSes in shell scripts to read IP address:
Linux:
ifconfig | grep 'inet addr:'| grep -v '127.0.0.1' |
cut -d: -f2 | awk '{ print $1}'
FreeBSD/OpenBSD:
ifconfig | grep -E 'inet.[0-9]' | grep -v '127.0.0.1' |
awk '{ print $2}'
Solaris:
ifconfig -a | grep inet | grep -v '127.0.0.1' |
awk '{ print $2}'
See read-ip-address.bash script which reads/finds an IP address from diffrent UNIX/Linux OSes.
How it works?
Here you used something called 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 (see pipe at wikipedia for more information).
- ifconfig command list all network interfaces.
- From the output of ifcomfing command, find out IPv4 IP address using grep command (grep 'inet addr:').
- Next, you do not need loopback IP address (127.0.0.1) so again with the help of grep –v you invert the sense of matching, to select all non-matching lines.
- Finally, awk command selects an IP address. (awk '{print $2}')
Want to stay up to date with the latest Linux tips, news and announcements? Subscribe to our free e-mail newsletter or RSS feed to get all updates.
You can Email this page to a friend.
You may also be interested in other helpful articles:
- Linux Iptables block incoming access to selected or specific ip address
- Security: Shell script optimization and security tips
- How to: Linux Iptables avoid spoofing and bad addresses attacks
- Iptables mac address filtering
- How to debug a Shell Script under Linux or UNIX
Discussion on This Article:
Leave a Reply
We encourage your comments, and suggestions. But please stay on topic, be polite, and avoid spam. Thank you very much for stopping by our site!


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:
#!/bin/bash
wget whatismyip.org
read T1 http://www.suramya.com
Suramya,
Script is ok, but you can do that with one line command:
lynx –dump http://whatismyip.org/
Or better create a alias:
alias myip=’lynx –dump http://whatismyip.org/‘
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?
Thanks,
Suramya
Mind if I ’steal’ it for use in my scripts?
Heh no problem, use it in your script
@nixcraft. nice tip/hint
I would like to see more shell scripting stuff here. keep it up good work
If you don’t have lynx installed and want to use wget without messy temporary files, you can use:
wget -qO - http://whatismyip.org/
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?
Dude, you just saved me 15 minutes of relearning awk and the like just to pull out the IP addy. Double thanks!
@Clue
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.
if you use a different locale, consider this version for linux:
LC_ALL=C ifconfig | grep ‘inet addr:’| grep -v ‘127.0.0.1′ | cut -d: -f2 | awk ‘{ print $1}’
@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.
This should do the work
ifconfig eth0| awk ‘NR==2 {print $2}’| awk -F: ‘{print $2}’