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

Itai Ganot
  • 10,644
  • 29
  • 93
  • 146

2 Answers2

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

Itai Ganot
  • 10,644
  • 29
  • 93
  • 146
  • That was quick! thank you! I think I'll use the `hostname -i` one. –  Feb 09 '15 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. – andrekeller Feb 09 '15 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 09 '15 at 16:07
0

I'd use facter for that.

yum install facter
facter ipaddress
facter ipaddress_eth0
andrekeller
  • 499
  • 2
  • 5