• Uncategorized

About linux : How-to-get-just-the-bare-IP-4-address-from-terminal-in-linux-using-the-new-ip-command-vs-ipconfig

Question Detail

ipconfig does not exist anymore as a command available in Ubuntu 20.04 and later I assume. The new command is just ip. When I run the ip address command I get the entire list of all devices and ip addresses associated. I want just the eth0 device and public ip 4 address associated.

I want just the bare ip address octets only. I want this to work on both Linux and Mac OS.

Question Answer

I found this pipe of cut and sed to work fine to get what I want:

on linux:

ip a | grep eth0 | cut -d " " --fields=6 | sed '2q;d' | awk -F'/' '{print $1}'

on BSD / Darwin / Mac OS:

ip a | grep en0 | tr -s 'inet' ' ' | sed '2q;d' | tr -s '' | awk -F' *? *' '{print $2}' | awk -F '/' '{print $1}'

which results in just the bare ip address I needed. I had to do some trial and error on what field column I actual needed. This probably could be more generalized, but this works for my use case.

Added a public git to just curl and run from anywhere like:

curl -L https://cutt.ly/UUYcT1r | /bin/bash

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.