72

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

Vini.g.fer
  • 11,639
  • 16
  • 61
  • 90
user2650277
  • 6,289
  • 17
  • 63
  • 132

11 Answers11

121

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.

Alexander
  • 12,424
  • 5
  • 59
  • 76
37

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

Siraj Alam
  • 9,217
  • 9
  • 53
  • 65
36

Enter the command ip addr at the console

enter image description here

Rijul Sudhir
  • 2,064
  • 1
  • 15
  • 19
5

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

jperret
  • 51
  • 1
  • 2
4

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

ip  -f inet a show eth0| grep inet| awk '{ print $2}' | cut -d/ -f1
datakid
  • 2,293
  • 5
  • 23
  • 35
  • 2
    There is nothing worse than down-voting an answer without giving reasons why. Please, provide a reason and down-vote, or simply provide just the reason you have a problem with the answer. – demented hedgehog May 10 '18 at 01:46
  • 1
    This version does not work anymore one modern Linux distros since the device simply has a different name. eth0, eth1 etc is not used anymore - at least on Red Hat 8, newer Fedoras, Opensuse, ... – Tuxinose Feb 16 '21 at 17:08
3
SERVER_IP="$(ip addr show ens160 | grep 'inet ' | cut -f2 | awk '{ print $2}')"

replace ens160 with your interface name

3

You can run simple commands like

curl ifconfig.co

curl ifconfig.me

wget -qO - icanhazip.com
Machavity
  • 30,841
  • 27
  • 92
  • 100
Kernel
  • 31
  • 1
3

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.

Tuxinose
  • 184
  • 4
  • That's the best one so far. Also, works on CentOS 8. But using more than one PCI card, it won't show IP address. Here's my version using only awk, and showing IP of all up interfaces `IPADDR=$(ip -br address show | awk '$2 == "UP" {print substr($3,1,index($3,"/")-1);}')` – Orsiris de Jong Oct 01 '19 at 09:50
  • My version was intended to yield the *first* card, that is what the first line takes care of. If that device is not up, you might end without IP address. – Tuxinose Feb 06 '21 at 14:30
3

Bit late however I use

curl -4 icanhazip.com 

returns the server Primary IP address.

Will T
  • 647
  • 5
  • 16
0

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/)

user2650277
  • 6,289
  • 17
  • 63
  • 132
-2

Run this command to show ip4 and ip6:

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