# Hostname

**URL:** <https://www.ruby-forum.com/t/hostname/137180>\
**Category:** Rails\
**Created:** [May 9, 2008, 6:42pm UTC](https://www.ruby-forum.com/t/hostname/137180 "2008-05-09T18:42:05Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![nallamani](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/53a042/32.png) [@nallamani](https://www.ruby-forum.com/u/nallamani)\
**Post date:** [May 9, 2008, 6:42pm UTC](https://www.ruby-forum.com/t/hostname/137180/1 "2008-05-09T18:42:05Z")

</div>

Hi,

I have written an rake task where I need to access “Hostname” . I need  
this feature because I will be testing my application using the rake  
task both locally and on live server .

I found code in another language which does the same

hostname = os.environ.get(‘HOSTNAME’)

puts “hostname =” + hostname

Is there anything similar to the above code in rails which I can use  
in my application .

Thanks,  
Madhu Nallamani

---

<div class="post-metadata">

**Author:** ![nallamani](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/53a042/32.png) [@nallamani](https://www.ruby-forum.com/u/nallamani)\
**Post date:** [May 9, 2008, 6:51pm UTC](https://www.ruby-forum.com/t/hostname/137180/2 "2008-05-09T18:51:31Z")

</div>

Hi,

If you’re using unix, you could do:

hostname = `hostname`  
puts “hostname = #{hostname}”

The backquotes simply execute the text inside them at a unix shell.

Julian.

Learn Ruby on Rails! Check out the FREE VIDS (for a limited time)  
VIDEO #4 parts a and b now available!  
[http://sensei.zenunit.com/](http://sensei.zenunit.com/)

---

<div class="post-metadata">

**Author:** ![nallamani](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/53a042/32.png) [@nallamani](https://www.ruby-forum.com/u/nallamani)\
**Post date:** [May 9, 2008, 6:54pm UTC](https://www.ruby-forum.com/t/hostname/137180/3 "2008-05-09T18:54:27Z")

</div>

try:

Socket.gethostname

You may need to require the socket library first.

Jamey

---

<div class="post-metadata">

**Author:** ![nallamani](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/53a042/32.png) [@nallamani](https://www.ruby-forum.com/u/nallamani)\
**Post date:** [May 15, 2008, 8:05pm UTC](https://www.ruby-forum.com/t/hostname/137180/4 "2008-05-15T20:05:33Z")

</div>

Thanks Jamey and Julian .
