73

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

3
  • 2
    i think it's better to ask this question somewhere else like Superuser or Unix and Linux. Commented Jan 14, 2015 at 12:38
  • @Cryrus could you post the command as an answer ? Commented Jan 14, 2015 at 12:47
  • Is the output from ip addr show useful for you? You'll have to adjust the manner in which you parse it of course...
    – twalberg
    Commented Jan 14, 2015 at 21:59

11 Answers 11

124

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.

6
  • 15
    For multiple interfaces (docker, flocker, etc) use echo $(hostname -I | cut -d" " -f 1) The first one is the default one. Commented Jun 23, 2016 at 17:35
  • 1
    That -f 1 doesn't always work. On my VM that gives me 127.0.0.1. So I would have to use -f 2 to get the proper IP. If there is also an IP v6 address then it's questionable again if it gets the right IP.
    – fred
    Commented Nov 24, 2016 at 14:15
  • 1
    For use within docker, I use $(hostname -i) instead. It gives me only one IP Commented Aug 29, 2017 at 20:02
  • this also worked for me: dig +short myip.opendns.com @resolver1.opendns.com. ; Commented Mar 13, 2018 at 20:58
  • 1
    the first IP is: ipaddr=$(hostname -I|awk '{print $1}')
    – Dudi Boy
    Commented Dec 30, 2018 at 15:52
36

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

3
  • have you got the sense of it?
    – brainLoop
    Commented Oct 26, 2018 at 2:44
  • A fairy came in my dreams to tell me this.
    – Siraj Alam
    Commented Oct 27, 2018 at 6:34
  • 1
    What package contains ip?
    – phreed
    Commented May 3, 2022 at 20:52
35

Enter the command ip addr at the console

enter image description here

6

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

0
4

Bit late however I use

curl -4 icanhazip.com 

returns the server Primary IP address.

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

replace ens160 with your interface name

1
  • 1
    I don't think the cut -f2 does anything in this command. The awk should be sufficient.
    – datakid
    Commented Dec 8, 2017 at 4:44
3

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

ip  -f inet a show eth0| grep inet| awk '{ print $2}' | cut -d/ -f1
2
  • 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. Commented May 10, 2018 at 1: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
    Commented Feb 16, 2021 at 17:08
3

You can run simple commands like

curl ifconfig.co

curl ifconfig.me

wget -qO - icanhazip.com
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.

2
  • 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);}') Commented Oct 1, 2019 at 9: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
    Commented Feb 6, 2021 at 14:30
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/)

-2

Run this command to show ip4 and ip6:

ifconfig eth0 | grep inet | awk '{print $2}' | cut -d/ -f1
1
  • 1
    The question explicitly says that ifconfig is no longer installed. Commented Jan 23, 2019 at 12:18

Not the answer you're looking for? Browse other questions tagged or ask your own question.