How to get ip address of a server on Centos 7 in bash

BashIp AddressCentos7

Bash Problem Overview


Previously I used the following command in bash to find the main ip of my server

ipaddr=$(/sbin/ifconfig|grep inet|head -1|sed 's/\:/ /'|awk '{print $3}' | grep -v '127.0.0.1')

But in centos7 it no longer works since ifconfig isn't available and the command no longer works even if I install ifconfig using yum install net-tools

What is the equivalent command for centos 7

Thanks a lot

Bash Solutions


Solution 1 - Bash

You can use hostname command :

ipaddr=$(hostname -I)

> -i, --ip-address: Display the IP address(es) of the host. Note that this works only if the host name can be resolved.

> -I, --all-ip-addresses: Display all network addresses of the host. This option enumerates all configured addresses on all network interfaces. The loopback interface and IPv6 link-local addresses are omitted. Contrary to option -i, this option does not depend on name resolution. Do not make any assumptions about the order of the output.

Solution 2 - Bash

Enter the command ip addr at the console

enter image description here

Solution 3 - Bash

Ref: https://garbagevalue.com/blog/4-simle-ways-to-check-ip-adress-in-centos-7


I'm using CentOS 7 and command

ip a

is enough to do the job.

enter image description here

Edit

Just slice out the IP address part from that test.

ip a | grep 192

Solution 4 - Bash

hostname -I | awk ' {print $1}'

Solution 5 - Bash

Something like this - a riff on @maarten-vanlinthout's answer

ip  -f inet a show eth0| grep inet| awk '{ print $2}' | cut -d/ -f1

Solution 6 - Bash

SERVER_IP="$(ip addr show ens160 | grep 'inet ' | cut -f2 | awk '{ print $2}')"

replace ens160 with your interface name

Solution 7 - Bash

You can run simple commands like

curl ifconfig.co

curl ifconfig.me

wget -qO - icanhazip.com

Solution 8 - Bash

Actually, when you do not want to use external sources (or cannot), I would recommend:

DEVICE=$(ls -l /sys/class/net | awk '$NF~/pci0/ { print $(NF-2); exit }')
IPADDR=$(ip -br address show dev $DEVICE | awk '{print substr($3,1,index($3,"/")-1);}')

The first line gets the name of the first network device on the PCI bus, the second one gives you its IP address.

BTW ps ... | grep ... | awk ... stinks. awk does not need grep.

Solution 9 - Bash

Bit late however I use

curl -4 icanhazip.com 

returns the server Primary IP address.

Solution 10 - Bash

I believe that the most reliable way to get the external server ip address would be to use an external service.

ipaddr=$(curl -s http://whatismyip.akamai.com/)

Solution 11 - Bash

Run this command to show ip4 and ip6:

ifconfig eth0 | grep inet | awk '{print $2}' | cut -d/ -f1

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser2650277View Question on Stackoverflow
Solution 1 - BashAlexanderView Answer on Stackoverflow
Solution 2 - BashRijul SudhirView Answer on Stackoverflow
Solution 3 - BashSiraj AlamView Answer on Stackoverflow
Solution 4 - BashjperretView Answer on Stackoverflow
Solution 5 - BashdatakidView Answer on Stackoverflow
Solution 6 - BashMaarten VanlinthoutView Answer on Stackoverflow
Solution 7 - BashKernelView Answer on Stackoverflow
Solution 8 - BashTuxinoseView Answer on Stackoverflow
Solution 9 - BashWill TView Answer on Stackoverflow
Solution 10 - Bashuser2650277View Answer on Stackoverflow
Solution 11 - Bashmohammad hasan daneshvarView Answer on Stackoverflow