# How do I get the host or Ip address where my rails app is running

**URL:** <https://www.ruby-forum.com/t/how-do-i-get-the-host-or-ip-address-where-my-rails-app-is-running/155425>\
**Category:** Rails\
**Created:** [December 11, 2008, 6:04pm UTC](https://www.ruby-forum.com/t/how-do-i-get-the-host-or-ip-address-where-my-rails-app-is-running/155425 "2008-12-11T18:04:03Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![kadoudal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f0a364/32.png) [@kadoudal](https://www.ruby-forum.com/u/kadoudal)\
**Post date:** [December 11, 2008, 6:04pm UTC](https://www.ruby-forum.com/t/how-do-i-get-the-host-or-ip-address-where-my-rails-app-is-running/155425/1 "2008-12-11T18:04:03Z")

</div>

Hi

I would like to get in a variable the ip address of the host where my  
app is running, to change some parameters if it’s running locally or  
remotely … is it possible ?

I cannot use request.host, since there is no request yet

I suppose I should test it in application.rb, not in environment.rb  
during initialization …

thanks for your suggestions …

erwin

---

<div class="post-metadata">

**Author:** ![kadoudal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f0a364/32.png) [@kadoudal](https://www.ruby-forum.com/u/kadoudal)\
**Post date:** [December 12, 2008, 12:22am UTC](https://www.ruby-forum.com/t/how-do-i-get-the-host-or-ip-address-where-my-rails-app-is-running/155425/2 "2008-12-12T00:22:54Z")

</div>

Erwin wrote:

> I would like to get in a variable the ip address of the host where my  
> app is running, to change some parameters if it’s running locally or  
> remotely .. is it possible ?

You can try

require ‘socket’  
REAL\_PRODUCTION = Socket.gethostname == ‘[mysite.com](http://mysite.com)’

or

hostinfo = Socket.gethostbyname(Socket.gethostname)  
hostnames = hostinfo[1] \<\< hostinfo[0]  
REAL\_PRODUCTION = hostnames.include?(‘[mysite.com](http://mysite.com)’)

–  
Rails Wheels - Find Plugins, List & Sell Plugins -  
[http://railswheels.com](http://railswheels.com)

---

<div class="post-metadata">

**Author:** ![kadoudal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f0a364/32.png) [@kadoudal](https://www.ruby-forum.com/u/kadoudal)\
**Post date:** [January 8, 2010, 5:16pm UTC](https://www.ruby-forum.com/t/how-do-i-get-the-host-or-ip-address-where-my-rails-app-is-running/155425/3 "2010-01-08T17:16:16Z")

</div>

Mark Reginald J. wrote:

> You can try
> 
> require ‘socket’  
> REAL\_PRODUCTION = Socket.gethostname == ‘[mysite.com](http://mysite.com)’
> 
> or
> 
> hostinfo = Socket.gethostbyname(Socket.gethostname)  
> hostnames = hostinfo[1] \<\< hostinfo[0]  
> REAL\_PRODUCTION = hostnames.include?(‘[mysite.com](http://mysite.com)’)
> 
> –  
> Rails Wheels - Find Plugins, List & Sell Plugins -  
> [http://railswheels.com](http://railswheels.com)

This returns the hostname from the box where the site is running which  
can help as well but is there a way to get the ip of the box?

I have tried to mirror two different servers this way i can test on one  
and deploy on the other and also keep the other as a backup should i  
ever need to redeploy to the other so the hostname and all are the same.  
(this maybe wrong way to do things so you may correct me)…

So my issue comes that the hostname is same on both machines and only  
the ip will do.

---

<div class="post-metadata">

**Author:** ![kadoudal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f0a364/32.png) [@kadoudal](https://www.ruby-forum.com/u/kadoudal)\
**Post date:** [January 8, 2010, 7:21pm UTC](https://www.ruby-forum.com/t/how-do-i-get-the-host-or-ip-address-where-my-rails-app-is-running/155425/4 "2010-01-08T19:21:33Z")

</div>

Hi Kad K.

require ‘ipaddr’  
require ‘net/http’

def get\_ip  
con = Net::HTTP.new(‘[checkip.dyndns.org](http://checkip.dyndns.org)’, 80)  
resp,body = con.get(“/”, nil)  
ip = body.match(/\d+.\d+.\d+.\d+/)

ip[0]  
end

my\_ip = IPAddr.new(get\_ip)

Sijo

---

<div class="post-metadata">

**Author:** ![kadoudal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f0a364/32.png) [@kadoudal](https://www.ruby-forum.com/u/kadoudal)\
**Post date:** [January 8, 2010, 7:43pm UTC](https://www.ruby-forum.com/t/how-do-i-get-the-host-or-ip-address-where-my-rails-app-is-running/155425/5 "2010-01-08T19:43:13Z")

</div>

Thanks Sijo. This will help a lot.

Sijo k g wrote:

> Hi Kad K.  
> require ‘ipaddr’  
> require ‘net/http’  
> def get\_ip  
> con = Net::HTTP.new(‘[checkip.dyndns.org](http://checkip.dyndns.org)’, 80)  
> resp,body = con.get(“/”, nil)  
> ip = body.match(/\d+.\d+.\d+.\d+/)
> 
> ip[0]  
> end  
> my\_ip = IPAddr.new(get\_ip)
