1

I'm using Linux CentOS 6.4 .

I'm writing a shell script and I need to extract a machine's IP in order to use it in the script. How can I find the machine's IP?

I'm looking for the "best" way because I know there are a few ways to get IP addresses.

Thanks

0

2 Answers 2

7

Here are ways to get your Linux machine IP address:

/sbin/ifconfig 

If you know the name of your Network Interface card, you can specify it in the command and get the results for that specific NIC:

/sbin/ifconfig eth0

Another command is:

/sbin/ip addr

If you know the name of your Network Interface card, you can specify it in the command and get the results for that specific NIC:

/sbin/ip addr show eth0

I think this is the best way:

hostname -i

Bare in mind that in some servers you might have more than one Network Interfacr Cards...

3
  • That was quick! thank you! I think I'll use the hostname -i one.
    – user256033
    Feb 9, 2015 at 14:31
  • use of hostname -i is discouraged in the manpage: Display the network address(es) of the host name. Note that this works only if the host name can be resolved. Avoid using this option; use hostname --all-ip-addresses instead. Feb 9, 2015 at 14:34
  • In case your machine is behind a router and you need your public IP you can use this service: curl ifconfig.me
    – r_3
    Feb 9, 2015 at 16:07
0

I'd use facter for that.

yum install facter
facter ipaddress
facter ipaddress_eth0
3
  • Isn't facter related to Puppet or something like that?
    – user256033
    Feb 9, 2015 at 14:32
  • yes it comes from the puppet ecosystem, but works perfectly fine as standalone application. Feb 9, 2015 at 14:34
  • Facter is related to Puppet, yes, but you can use Facter without Puppet (though not the other way around). Feb 9, 2015 at 14:34