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.

1
  • May I ask what the goal is ? Spoof the MAC address ?
    – Kate
    Commented Jan 26, 2020 at 16:46

3 Answers 3

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.

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.

7
  • the OP asked for MAC ADDRESS you command is giving me the IP.
    – BANJOSA
    Commented Jan 26, 2020 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
    Commented Jan 27, 2020 at 19:11
  • not on my kali... ifconfig | awk '/^eth0/ {getline;print $2}' 10.0.2.17
    – BANJOSA
    Commented Jan 27, 2020 at 20:47
  • so what $ ifconfig eth0 shows? Give me full terminal dump - it's 7 lines.
    – tansy
    Commented Jan 27, 2020 at 21:37
  • 1
    lets head over to the chat so we are no ruining the comment section with our debug. chat.stackexchange.com/rooms/info/103775/…
    – BANJOSA
    Commented Jan 27, 2020 at 22:03
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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .