74

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... 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.

Sign up to request clarification or add additional context in comments.

6 Comments

For multiple interfaces (docker, flocker, etc) use echo $(hostname -I | cut -d" " -f 1) The first one is the default one.
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.
For use within docker, I use $(hostname -i) instead. It gives me only one IP
this also worked for me: dig +short myip.opendns.com @resolver1.opendns.com. ;
the first IP is: ipaddr=$(hostname -I|awk '{print $1}')
|
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 Comments

have you got the sense of it?
A fairy came in my dreams to tell me this.
What package contains ip?
35

Enter the command ip addr at the console

enter image description here

Comments

6

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

Comments

4

Bit late however I use

curl -4 icanhazip.com 

returns the server Primary IP address.

Comments

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

replace ens160 with your interface name

1 Comment

I don't think the cut -f2 does anything in this command. The awk should be sufficient.
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 Comments

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.
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, ...
3

You can run simple commands like

curl ifconfig.co curl ifconfig.me wget -qO - icanhazip.com 

Comments

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 Comments

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);}')
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.
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/)

Comments

-2

Run this command to show ip4 and ip6:

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

1 Comment

The question explicitly says that ifconfig is no longer installed.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.