IP address

From Freephile Wiki
Jump to navigation Jump to search

Oftentimes people want to know what their Internet Protocol address is.

On a linux host, you can type hostname -i to get the IP, but it won't show all network interfaces, so really it's just a quick answer to "What's my primary IP?"

Something a little more elaborate will give you all the IPv4 addresses assigned to all NICs (note the space after inet in the grep). What's silly about the internet sites that list these grep variants is that the ip command itself is the tool you want to use to show this information. Still grep is useful as a means to filter output if you want the numbers exclusively (in order to sort) as in ip -4 addr show | grep inet | awk '{print $2}' | sort -n. Removing the grep and sort, you get not only the IP addresses, but the interfaces they are assigned to. Or, you can use the -o option to list one record per line which also makes grepping easier.

Protocol using ip options "dumb" grep (no options to ip)
IPv4
ip -4 addr show | awk '{ print $2 }'
ip addr show | grep 'inet ' | awk '{ print $2 }'
IPv4 and IPv6
ip addr show | grep 'inet' | awk '{ print $2 }'
IPv6
ip -6 addr show | awk '{ print $2 }'
ip addr show | grep 'inet6' | awk '{ print $2 }'

External[edit | edit source]

If you're looking for your external IP address instead, simply google "what is my ip?"

Older[edit | edit source]

On older systems you might need to use ifconfig, and specify the /sbin path because it's not in the standard path for regular users on RedHat.

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