Tech Tip: Getting Your MAC and IP Address In a Script

 in

Ever wanted to get the MAC or IP address of your computer in a Linux shell script? The following two commands should work on most flavours of Linux/Unix.

To get your IP address:

/sbin/ifconfig \
   | grep '\<inet\>' \
   | sed -n '1p' \
   | tr -s ' ' \
   | cut -d ' ' -f3 \
   | cut -d ':' -f2

To get your MAC address (Hardware address):

/sbin/ifconfig \
   | grep 'eth0' \
   | tr -s ' ' \
   | cut -d ' ' -f5

Note that this retrieves the address of the eth0 interface by default.

______________________

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

How about this: hostname -i

Anonymous's picture

How about this:

hostname -i

this one-liner shows all IP addresses for all interfaces

freephile's picture

/sbin/ifconfig | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2 | sort | awk '{print "# "$1}'

get your IP

kaloyan's picture

ifconfig eth0
| sed -n '/inet addr:/p'
| cut -d: -f2
| cut -d" " -f1

all in one

Anonymous's picture

ifconfig ath0 | perl -ne 'm/.*HWaddr (\S+).*/ && print "$1 ";m/.*inet addr:(\S+)/ && print "$1\n"'

Perl could be used for extracting both IPv4 and IPv6:

Roland Müller's picture
# ifconfig | perl -ne '(s|^\s+inet6\s+addr:\s*([0-9a-f:/]+)\s+.*|${1}| or s|^\s+inet\s+addr:\s*([0-9.]+)\s+.*|${1}|) and print;'
******************************
******************************
127.0.0.1
::1/128

showing mac address

Joe Eykholt's picture

To show MAC:

cat /sys/class/net/eth0/address

egrep

Kwirkie's picture

How about...

IP:
ifconfig eth1 | egrep -o '([0-9]{1,3}\.){3}[0-9]{1,3}' | sed -n '1p'

MAC:
ifconfig eth1 | egrep -o '([[:xdigit:]]{2}[:]){5}[[:xdigit:]]{2}'

This provides validation and also the regexp can be used elsewhere to find IPs and MACs

IP: ip addr show dev eth0 |

zsteva's picture

IP:
ip addr show dev eth0 | sed -e's/^.*inet \([^ ]*\)\/.*$/\1/;t;d'

MAC:
ip addr show dev eth0 | sed -e's/^.*link[^ ]* \([^ ]*\) .*$/\1/;t;d'

or with /sbin/ifconfig

IP:
/sbin/ifconfig wlan0 | sed -e's/^.*inet addr:\([^ ]*\) .*$/\1/;t;d'

MAC:
/sbin/ifconfig eth0 | sed -e's/^.*HWaddr \([^ ]*\) .*$/\1/;t;d'

White Paper
Fabric-Based Computing Enables Optimized Hyperscale Data Centers

Today’s modular x86 servers are compute-centric, designed as a least common denominator to support a wide range of IT workloads. Those generic, virtualized IT workloads have much different resource optimization requirements than hyperscale and cloud applications. They have resulted in a “one size fits all” enterprise IT architecture that is not optimized for a specific set of IT workloads, and especially not emerging hyperscale workloads, such as web applications, big data, and object storage. In this report, you will learn how shifting the focus from traditional compute-centric IT architectures to an innovative disaggregated fabric-based architecture can optimize and scale your data center.

Learn More

Sponsored by AMD

White Paper
Red Hat White Paper: Using an Open Source Framework to Catch the Bad Guy

Built-in forensics, incident response, and security with Red Hat Enterprise Linux 6

Every security policy provides guidance and requirements for ensuring adequate protection of information and data, as well as high-level technical and administrative security requirements for a system in a given environment. Traditionally, providing security for a system focuses on the confidentiality of the information on it. However, protecting the data integrity and system and data availability is just as important. For example, when processing United States intelligence information, there are three attributes that require protection: confidentiality, integrity, and availability.

Learn more about catching the bad guy in this free white paper.

Learn More

Sponsored by DLT Solutions