Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

This question already has an answer here:

I am trying to write a bash script to configure an app (unattended install). All I need to do is this. I need to get the IP address for the server and set it as a variable. I am writing this script to simplify that. What I have noticed is that when I test this in a vps though eth0 is listed its not the default, its something along the lines of venet0:0. Is there any way of getting this ip set as a vaiable (I know how to if I know the interface name, i am trying to do this without knowing as its hard to plan for every possible device naming). if not then i will just add a prompt for the ip address for the user right as they run the script.

This is not a duplicate question as I am asking for the server's IP not just the public IP of the server. Even though this is aimed at building a public mail server I am building it to work on an internal one as well.

share|improve this question

marked as duplicate by Jay, Zoredache, Jenny D, Stefan Lasiewski, Michael Hampton Apr 29 '13 at 17:48

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

2 Answers 2

up vote 1 down vote accepted

'The' IP address... servers have so many, default management, application, DB listener?

Does this do it for you?

IP_ADDR=$(getent hosts `hostname` | awk '{print $1}')
share|improve this answer
    
This worked Perfectly for me! thanks! I am a better PHP programmer than a shell scripter i swear! :) –  John Stapleton Apr 29 '13 at 18:06
    
This approach is quite nice on a server that was properly setup. However, it won't work until system administrator adds external IP address in /etc/hosts –  Danila V. Sep 17 at 20:24
    
However, note that on IPV6 enabled system, it returns IP V6. –  Danila V. Sep 17 at 20:30

If you have one interface:

ip addr show | grep "inet " | grep -v "127.0.0.1/8" | awk '{print $2}'
10.10.1.254/16
share|improve this answer

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