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'

Webinar
One Click, Universal Protection: Implementing Centralized Security Policies on Linux Systems

As Linux continues to play an ever increasing role in corporate data centers and institutions, ensuring the integrity and protection of these systems must be a priority. With 60% of the world's websites and an increasing share of organization's mission-critical workloads running on Linux, failing to stop malware and other advanced threats on Linux can increasingly impact an organization's reputation and bottom line.

Learn More

Sponsored by Bit9

Webinar
Linux Backup and Recovery Webinar

Most companies incorporate backup procedures for critical data, which can be restored quickly if a loss occurs. However, fewer companies are prepared for catastrophic system failures, in which they lose all data, the entire operating system, applications, settings, patches and more, reducing their system(s) to “bare metal.” After all, before data can be restored to a system, there must be a system to restore it to.

In this one hour webinar, learn how to enhance your existing backup strategies for better disaster recovery preparedness using Storix System Backup Administrator (SBAdmin), a highly flexible bare-metal recovery solution for UNIX and Linux systems.

Learn More

Sponsored by Storix