0

Is there a way to get the MAC address from ifconfig and append after HWADDR in ifconfig-eth0 file? I have used awk command to show MAC address but couldn't figure it out to hold that information and paste it to ifconfig-eth0 file.

fpmurphy
  • 4,636
Parajd
  • 1

3 Answers3

4

Rather than parsing the output of ifconfig (which might not even be installed by default) you should just set the variable from the contents of the /sys data:

MACADDR=$(cat /sys/class/net/eth0/address)

Then you can just

echo “HWADDR=$MACADDR” >> /etc/sysconfig/network-scripts/ifcfg-eth0

This is assuming you don’t already have a line with HWADDR, otherwise you’ll need to use sed.

jsbillings
  • 24,406
0

You can get MAC address of eth0 with:

$ ifconfig eth0 | awk '/ether/ {print $2}'

where: ifconfig eth0 displays only given network device

awk '/ether/ {print $2}' seeks 'ether' and prints second column in matching line

--

What is ifconfig-eth0 file?

ed: former $ ifconfig | awk '/^eth0/ {getline;print $2}' was actually right only for not connected eth0, so proper one is now above.

tansy
  • 741
  • the OP asked for MAC ADDRESS you command is giving me the IP. – BANJOSA Jan 26 '20 at 20:29
  • It's giving mac address. My result is 00:1c:XX:XX:XX:XX. It doesn look like IP. Ipv4 would be something like 192.168.1.15 and IPv6 fe80::21c:XXX:XXXX:XXXX. (these are my areal addresses so I "obfuscated" them a bit). It gives exactly the same result as $ cat /sys/class/net/eth0/address. Check it yourself. – tansy Jan 27 '20 at 19:11
  • not on my kali... ifconfig | awk '/^eth0/ {getline;print $2}' 10.0.2.17 – BANJOSA Jan 27 '20 at 20:47
  • so what $ ifconfig eth0 shows? Give me full terminal dump - it's 7 lines. – tansy Jan 27 '20 at 21:37
  • 1
    lets head over to the chat so we are no ruining the comment section with our debug. https://chat.stackexchange.com/rooms/info/103775/room-for-banjosa-and-tansy – BANJOSA Jan 27 '20 at 22:03
  • Thanks to @BANJOSA it's been corrected and should work with every device now. – tansy Jan 27 '20 at 23:45
  • collaboration is always nice :) thanks for you time in troubleshooting – BANJOSA Jan 27 '20 at 23:49
0

Tried with below command and it worked fine

k=`ifconfig -a |awk '/HWaddr/{print $NF}'`
sed -i "/HWADDR/s/=.*/=$k/g" /etc/sysconfig/network-scripts/ifcfg-eth0